From 41cc4c8132a79e8359e59c10875b774dc40ac509 Mon Sep 17 00:00:00 2001 From: dotnet Date: Fri, 13 Oct 2023 19:31:15 -0400 Subject: [PATCH] Generate proper gelbooru url for the image. --- gelbooru_poster.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/gelbooru_poster.py b/gelbooru_poster.py index 8f3d463..e8c3f3d 100644 --- a/gelbooru_poster.py +++ b/gelbooru_poster.py @@ -82,7 +82,7 @@ class BotInstance: max_pages = 200 # Make sure there are images on the page if 'post' not in gelbooru_json: - return None, None, None, max_pages + return None, None, None, None, max_pages # Choose a random image from the page image_number = random.randint(0, len(gelbooru_json['post'])) @@ -91,6 +91,10 @@ class BotInstance: #with open("gelbooru.json", "w") as gelbooru_json_file: # gelbooru_json_file.write(str(gelbooru_json)) + # Get the image ID + image_id = gelbooru_json['post'][image_number]["id"] + image_post_url = "https://gelbooru.com/index.php?page=post&s=view&id=" + str(image_id) + # Get the image URL image_url = gelbooru_json['post'][image_number]["file_url"] # Get the image source if exists @@ -101,10 +105,10 @@ class BotInstance: # Get the image rating image_rating = gelbooru_json['post'][image_number]["rating"] - return image_url, image_src, image_rating, max_pages + return image_url, image_src, image_post_url, image_rating, max_pages # Download and post the image to Misskey - def post_image(self, image_url, image_src, image_rating, log_file): + def post_image(self, image_url, image_src, image_post_url, image_rating, log_file): image_found = False #Extract image filename, replace extension with .jpg @@ -219,7 +223,7 @@ class BotInstance: time.sleep(min(30, (attempts ** 2) / 2)) # Submit a /notes/create request to Misskey - msg = self.format_message(image_src, image_url) + msg = self.format_message(image_src, image_post_url) create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_id], "text": msg, "i": self.misskey_token}) # If error, print error and exit if create_note_request.status_code != 200: @@ -234,14 +238,14 @@ class BotInstance: while True: if attempts <= 0: return False - image_url, image_src, image_rating, cur_page_number = self.get_random_image(max_page_number=self.max_page_number) + image_url, image_src, image_post_url, image_rating, cur_page_number = self.get_random_image(max_page_number=self.max_page_number) self.max_page_number = cur_page_number if image_url is None: attempts -= 1 continue break # Download and post the image to Misskey - return self.post_image(image_url, image_src, image_rating, log_file) + return self.post_image(image_url, image_src, image_post_url, image_rating, log_file) def generate_config(defaults): if os.path.exists("config.json"):