From 55bfe256fb77dd083ab7283d795f8932938e73f1 Mon Sep 17 00:00:00 2001 From: jwansek Date: Sun, 4 Oct 2020 01:49:36 +0100 Subject: fixed bug where pixiv links 403'd --- bot.py | 5 ++++- get_images.py | 15 ++++++++++----- utils.py | 25 +------------------------ 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/bot.py b/bot.py index 7e3c6f7..3c8f6e0 100644 --- a/bot.py +++ b/bot.py @@ -7,7 +7,10 @@ import get_images twitter = Twython(*get_images.CONFIG["twitterapi"].values()) def post(): - impath, source, text = get_images.main() + images = get_images.main() + while images is None: + images = get_images.main() + impath, source, text = images with open(impath, "rb") as img: response = twitter.upload_media(media = img) message = f"{text} ({source})" diff --git a/get_images.py b/get_images.py index 37becdd..bd1efd3 100644 --- a/get_images.py +++ b/get_images.py @@ -85,6 +85,13 @@ def get_num_pages(tags): else: return int(int(urllib.parse.parse_qs(page_element.get("href"))["pid"][0]) / (5*8)) +def fix_source_url(url): + if "pixiv.net" in url or "pximg.net" in url: + if requests.get(url).status_code == 403: + return "https://www.pixiv.net/en/artworks/%s" % url.split("/")[-1][:8] + + return url + def append_blacklisted(id_): with open(CONFIG["blacklist"], "a") as f: f.write(str(id_) + "\n") @@ -116,13 +123,11 @@ def main(draw_faces = False): simg = get_image(get_random_searchtag()) except ConnectionError: logging.warning("Retried since couldn't get source...") - main() - return + return main() if id_is_blacklisted(simg.id): logging.info("Retried, already posted image...") - main() - return + return main() append_blacklisted(simg.id) @@ -159,7 +164,7 @@ def main(draw_faces = False): y = y + height pilimg.save("img.png") - return "img.png", simg.source, text + return "img.png", fix_source_url(simg.source), text if __name__ == "__main__": diff --git a/utils.py b/utils.py index bdb9eb2..ce3f3b9 100644 --- a/utils.py +++ b/utils.py @@ -26,30 +26,6 @@ def draw_with_border(x, y, message, color_fill, color_border, font, draw): draw.text((x, y), message, fill=color_fill, font=font) - -def detect(filename): - result = subprocess.run( - [ - "conda", - "activate", - "detection", - "&&", - "python", - "anime-face-detector/main.py ", - "-i", - filename, - "-o", - "output.json", - ], - shell=True, - stdout=subprocess.DEVNULL, - ) - with open("output.json", "r") as f: - output = json.load(f) - print(output) - return [f["bbox"] for f in output[filename]] - - def set_font(image, message, font_name="fonts/Caveat-Bold.ttf"): # keep increasing font size until the font area is 1/5th the size of image font_size = 10 @@ -137,3 +113,4 @@ def get_quote(p): with open(p, "r", encoding="utf-8") as f: lines = f.read().splitlines() return random.choice(lines) + -- cgit v1.2.3