Try renoting before uploading.

This commit is contained in:
dotnet 2023-10-13 15:41:01 -04:00
parent a6c9af11d2
commit 53a09f91cd
1 changed files with 27 additions and 22 deletions

View File

@ -79,6 +79,26 @@ class BotInstance:
#Extract image filename, replace extension with .jpg
image_fname = os.path.splitext(os.path.split(image_url)[-1])[0] + ".jpg"
# Try to determine if the image_src is a fediverse link, if so renote it instead of posting a new note
post_request = requests.post(self.misskey_url + "ap/show", json = {"uri": image_src, "i": self.misskey_token})
if post_request.status_code == 200:
post_json = post_request.json()
if 'id' in post_json:
# Submit a /notes/create request to Misskey
if isinstance(self.bot_message, list):
msg = random.choice(self.bot_message)
else:
msg = self.bot_message
if random.randint(0, 100) < 5:
msg += " " + self.bot_hashtags
create_note_request = requests.post(self.misskey_url + "notes/create", json = {"renoteId": post_json['id'], "text":"", "i": self.misskey_token})
# If error, print error and exit
if create_note_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_note_request.json()["error"]["message"], file=log_file)
return True
#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:
@ -87,6 +107,7 @@ class BotInstance:
file_presence_json = file_presence_check.json()
image_found = len(file_presence_json) > 0
# If the image is not uploaded, download, optimize and upload it
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"):
@ -128,7 +149,10 @@ class BotInstance:
print(create_file_request.json()["error"]["message"], file=log_file)
return False
# Delete the temporary image files
if os.path.exists("image.jpg"):
os.remove("image.jpg")
if os.path.exists("image_original.jpg"):
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})
@ -141,6 +165,7 @@ class BotInstance:
# Wait for the image to be uploaded
time.sleep(1)
# Wait for the image to be uploaded and get the file ID
attempts = 0
while True:
# Get the file ID using the /drive/files/find request
@ -168,26 +193,6 @@ class BotInstance:
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
post_request = requests.post(self.misskey_url + "ap/show", json = {"uri": image_src, "i": self.misskey_token})
if post_request.status_code == 200:
post_json = post_request.json()
if 'id' in post_json:
# Submit a /notes/create request to Misskey
if isinstance(self.bot_message, list):
msg = random.choice(self.bot_message)
else:
msg = self.bot_message
if random.randint(0, 100) < 5:
msg += " " + self.bot_hashtags
create_note_request = requests.post(self.misskey_url + "notes/create", json = {"renoteId": post_json['id'], "text": "%s\n[Source](%s)\n" % (msg, image_src), "i": self.misskey_token})
# If error, print error and exit
if create_note_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_note_request.json()["error"]["message"], file=log_file)
return True
# Submit a /notes/create request to Misskey
if isinstance(self.bot_message, list):
msg = random.choice(self.bot_message)