diff --git a/gelbooru_poster.py b/gelbooru_poster.py index 577cf71..26e67bc 100644 --- a/gelbooru_poster.py +++ b/gelbooru_poster.py @@ -75,13 +75,27 @@ class BotInstance: # Download and post the image to Misskey def post_image(self, image_url, image_src, image_rating, log_file): image_found = False - file_presence_check = requests.post(self.misskey_url + "drive/files/find", json = {"name": os.path.split(image_url)[-1], "i": self.misskey_token}) + image_fname = os.path.split(image_url)[-1] + + #Check if the image is already uploaded with original extension + 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 else: 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"): @@ -123,7 +137,7 @@ class BotInstance: return False else: # Submit a /drive/files/create request to Misskey - create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": os.path.split(image_url)[-1], "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open("image.jpg", "rb")}) + 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) @@ -151,7 +165,7 @@ class BotInstance: attempts = 0 while True: # Get the file ID using the /drive/files/find request - file_id_request = requests.post(self.misskey_url + "drive/files/find", json = {"name": os.path.split(image_url)[-1], "i": self.misskey_token}) + file_id_request = requests.post(self.misskey_url + "drive/files/find", json = {"name": image_fname, "i": self.misskey_token}) # If error, print error and exit if file_id_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file)