From a6c9af11d25a3112f092e3ebd0abeba41c69c620 Mon Sep 17 00:00:00 2001 From: dotnet Date: Fri, 13 Oct 2023 15:31:42 -0400 Subject: [PATCH] Fix image optimization and adjust error logging. --- gelbooru_poster.py | 77 +++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 53 deletions(-) diff --git a/gelbooru_poster.py b/gelbooru_poster.py index 26e67bc..2d994cc 100644 --- a/gelbooru_poster.py +++ b/gelbooru_poster.py @@ -75,9 +75,11 @@ class BotInstance: # Download and post the image to Misskey def post_image(self, image_url, image_src, image_rating, log_file): image_found = False - image_fname = os.path.split(image_url)[-1] - #Check if the image is already uploaded with original extension + #Extract image filename, replace extension with .jpg + image_fname = os.path.splitext(os.path.split(image_url)[-1])[0] + ".jpg" + + #Check if the image is already uploaded file_presence_check = requests.post(self.misskey_url + "drive/files/find", json = {"name": image_fname, "i": self.misskey_token}) if file_presence_check.status_code != 200: image_found = False @@ -85,17 +87,6 @@ class BotInstance: file_presence_json = file_presence_check.json() image_found = len(file_presence_json) > 0 - #Check if the image is already uploaded with .jpg extension - if not image_found: - file_presence_check = requests.post(self.misskey_url + "drive/files/find", json = {"name": image_fname + ".jpg", "i": self.misskey_token}) - if file_presence_check.status_code != 200: - image_found = False - else: - file_presence_json = file_presence_check.json() - image_found = len(file_presence_json) > 0 - if image_found: - image_fname += ".jpg" - if not image_found: # If the file is a static image, download, optimize and post it to Misskey if image_url.endswith(".jpg") or image_url.endswith(".jpeg") or image_url.endswith(".png"): @@ -104,20 +95,18 @@ class BotInstance: # If error, print error and exit if image_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) print(image_request.json()["error"]["message"], file=log_file) - print("\n", file=log_file) print(image_url, file=log_file) return False - #Save image_request.content, based on image_url extension - with open("image_original" + os.path.splitext(image_url)[-1], "wb") as image_file: - image_file.write(image_request.content) # Optimise the image by reducing it to max width of 2048px image = Image.open(BytesIO(image_request.content)) - + image_original_exists = False + if os.path.splitext(image_url)[-1] == ".jpg" or os.path.splitext(image_url)[-1] == ".jpeg": + image_original_exists = True + with open("image_original.jpg", "wb") as image_file: + image_file.write(image_request.content) if image.width > 2048: image = image.resize((2048, int(image.height * (2048 / image.width))), Image.LANCZOS) # Apply JPEG compression @@ -125,38 +114,28 @@ class BotInstance: image.save("image.jpg", optimize=True, quality=90) # If the image is larger than the original, use the original - if os.path.getsize("image.jpg") > os.path.getsize("image_original" + os.path.splitext(image_url)[-1]): - upload_from_url_request = requests.post(self.misskey_url + "drive/files/upload-from-url", json = {"url": image_url, "isSensitive": image_rating != 'general', "i": self.misskey_token}) - # If error, print error and exit - if upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200: - print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) - print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) - print(upload_from_url_request.json()["error"]["message"], file=log_file) - return False + if image_original_exists and os.path.getsize("image.jpg") > os.path.getsize("image_original.jpg"): + image_upld_src = "image_original.jpg" else: - # Submit a /drive/files/create request to Misskey - create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": image_fname, "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open("image.jpg", "rb")}) - # If error, print error and exit - if create_file_request.status_code != 200: - print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) - print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) - print(create_file_request.json()["error"]["message"], file=log_file) - return False + image_upld_src = "image.jpg" + + # Submit a /drive/files/create request to Misskey + create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": image_fname, "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open(image_upld_src, "rb")}) + # If error, print error and exit + if create_file_request.status_code != 200: + print(self.cfg_name + ": Error: ", file=log_file) + print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) + print(create_file_request.json()["error"]["message"], file=log_file) + return False os.remove("image.jpg") - os.remove("image_original" + os.path.splitext(image_url)[-1]) + os.remove("image_original.jpg") else: upload_from_url_request = requests.post(self.misskey_url + "drive/files/upload-from-url", json = {"url": image_url, "isSensitive": image_rating != 'general', "i": self.misskey_token}) # If error, print error and exit if upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) print(upload_from_url_request.json()["error"]["message"], file=log_file) return False # Wait for the image to be uploaded @@ -169,9 +148,7 @@ class BotInstance: # If error, print error and exit if file_id_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) print(file_id_request.json()["error"]["message"], file=log_file) return False file_id_json = file_id_request.json() @@ -181,15 +158,14 @@ class BotInstance: if attempts > 10: print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) print("Image not uploaded", file=log_file) return False # If the image hasn't been uploaded after 10 attempts, exit attempts += 1 # Wait and try again + print("Waiting for image to be uploaded...\n", file=log_file) time.sleep(min(30, (attempts ** 2) / 2)) # Try to determine if the image_src is a fediverse link, if so renote it instead of posting a new note @@ -208,9 +184,7 @@ class BotInstance: # If error, print error and exit if create_note_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) print(create_note_request.json()["error"]["message"], file=log_file) return True @@ -225,9 +199,7 @@ class BotInstance: # If error, print error and exit if create_note_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file) - print("\n", file=log_file) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n", file=log_file) print(create_note_request.json()["error"]["message"], file=log_file) return True @@ -332,9 +304,8 @@ def main(): # If error, print error and continue except Exception as e: #Print time - print("\n\n" + cfg_name + ": Error: ", file=log_file) + print("\n" + cfg_name + ": Error: ", file=log_file) print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file) - print("\n\n", file=log_file) traceback.print_exc(file=log_file) continue