Make bots pull original source of the image.

This commit is contained in:
Himanshu Goel 2023-10-09 14:00:02 -04:00
parent 627ddd4582
commit 18cc1a7408

View file

@ -13,7 +13,7 @@ class BotInstance:
# Gelbooru API URL # Gelbooru API URL
gelbooru_url = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit=100&tags=" gelbooru_url = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&limit=100&tags="
# Misskey API URL # Misskey API URL
misskey_url = "https://fediverse.dotnet00.dev/api/" misskey_url = "https://misskey.io/api/"
# Misskey API token # Misskey API token
misskey_token = "NONE" misskey_token = "NONE"
# Bot message # Bot message
@ -59,13 +59,18 @@ class BotInstance:
# 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
if 'source' not in gelbooru_json['post'][image_number] or gelbooru_json['post'][image_number]["source"] == "":
image_src = image_url
else:
image_src = gelbooru_json['post'][image_number]["source"]
# 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_rating, max_pages return image_url, image_src, 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_rating, log_file): def post_image(self, image_url, image_src, image_rating, log_file):
image_found = False 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}) file_presence_check = requests.post(self.misskey_url + "drive/files/find", json = {"name": os.path.split(image_url)[-1], "i": self.misskey_token})
if file_presence_check.status_code != 200: if file_presence_check.status_code != 200:
@ -110,7 +115,7 @@ class BotInstance:
msg = self.bot_message msg = self.bot_message
if random.randint(0, 100) < 5: if random.randint(0, 100) < 5:
msg += " " + self.bot_hashtags msg += " " + self.bot_hashtags
create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_id], "text": "%s\nURL: %s\n" % (msg, image_url), "i": self.misskey_token}) create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_id], "text": "%s\nURL: %s\n" % (msg, image_src), "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:
print("Error: " + create_note_request.json()["error"]["message"], file=log_file) print("Error: " + create_note_request.json()["error"]["message"], file=log_file)
@ -119,14 +124,14 @@ class BotInstance:
def bot_process(self, log_file): def bot_process(self, log_file):
# Get a random image making sure it's not in the saved image list # Get a random image making sure it's not in the saved image list
while True: while True:
image_url, image_rating, cur_page_number = self.get_random_image(max_page_number=self.max_page_number) image_url, image_src, image_rating, cur_page_number = self.get_random_image(max_page_number=self.max_page_number)
if cur_page_number < self.max_page_number: if cur_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:
continue continue
break break
# Download and post the image to Misskey # Download and post the image to Misskey
self.post_image(image_url, image_rating, log_file) self.post_image(image_url, image_src, image_rating, log_file)
def generate_config(defaults): def generate_config(defaults):
if os.path.exists("config.json"): if os.path.exists("config.json"):