Generate proper gelbooru url for the image.

This commit is contained in:
dotnet 2023-10-13 19:31:15 -04:00
parent 41d8a52d36
commit 41cc4c8132
1 changed files with 10 additions and 6 deletions

View File

@ -82,7 +82,7 @@ class BotInstance:
max_pages = 200 max_pages = 200
# Make sure there are images on the page # Make sure there are images on the page
if 'post' not in gelbooru_json: 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 # Choose a random image from the page
image_number = random.randint(0, len(gelbooru_json['post'])) image_number = random.randint(0, len(gelbooru_json['post']))
@ -91,6 +91,10 @@ class BotInstance:
#with open("gelbooru.json", "w") as gelbooru_json_file: #with open("gelbooru.json", "w") as gelbooru_json_file:
# gelbooru_json_file.write(str(gelbooru_json)) # 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 # Get the image URL
image_url = gelbooru_json['post'][image_number]["file_url"] image_url = gelbooru_json['post'][image_number]["file_url"]
# Get the image source if exists # Get the image source if exists
@ -101,10 +105,10 @@ class BotInstance:
# Get the image rating # Get the image rating
image_rating = gelbooru_json['post'][image_number]["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 # 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 image_found = False
#Extract image filename, replace extension with .jpg #Extract image filename, replace extension with .jpg
@ -219,7 +223,7 @@ class BotInstance:
time.sleep(min(30, (attempts ** 2) / 2)) time.sleep(min(30, (attempts ** 2) / 2))
# Submit a /notes/create request to Misskey # 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}) 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 error, print error and exit
if create_note_request.status_code != 200: if create_note_request.status_code != 200:
@ -234,14 +238,14 @@ class BotInstance:
while True: while True:
if attempts <= 0: if attempts <= 0:
return False 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 self.max_page_number = cur_page_number
if image_url is None: if image_url is None:
attempts -= 1 attempts -= 1
continue continue
break break
# Download and post the image to Misskey # 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): def generate_config(defaults):
if os.path.exists("config.json"): if os.path.exists("config.json"):