Try renoting before uploading.
This commit is contained in:
parent
a6c9af11d2
commit
53a09f91cd
|
@ -79,6 +79,26 @@ class BotInstance:
|
||||||
#Extract image filename, replace extension with .jpg
|
#Extract image filename, replace extension with .jpg
|
||||||
image_fname = os.path.splitext(os.path.split(image_url)[-1])[0] + ".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
|
#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})
|
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:
|
if file_presence_check.status_code != 200:
|
||||||
|
@ -87,6 +107,7 @@ class BotInstance:
|
||||||
file_presence_json = file_presence_check.json()
|
file_presence_json = file_presence_check.json()
|
||||||
image_found = len(file_presence_json) > 0
|
image_found = len(file_presence_json) > 0
|
||||||
|
|
||||||
|
# If the image is not uploaded, download, optimize and upload it
|
||||||
if not image_found:
|
if not image_found:
|
||||||
# If the file is a static image, download, optimize and post it to Misskey
|
# 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"):
|
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)
|
print(create_file_request.json()["error"]["message"], file=log_file)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
# Delete the temporary image files
|
||||||
|
if os.path.exists("image.jpg"):
|
||||||
os.remove("image.jpg")
|
os.remove("image.jpg")
|
||||||
|
if os.path.exists("image_original.jpg"):
|
||||||
os.remove("image_original.jpg")
|
os.remove("image_original.jpg")
|
||||||
else:
|
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})
|
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
|
# Wait for the image to be uploaded
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
|
# Wait for the image to be uploaded and get the file ID
|
||||||
attempts = 0
|
attempts = 0
|
||||||
while True:
|
while True:
|
||||||
# Get the file ID using the /drive/files/find request
|
# 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)
|
print("Waiting for image to be uploaded...\n", file=log_file)
|
||||||
time.sleep(min(30, (attempts ** 2) / 2))
|
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
|
# Submit a /notes/create request to Misskey
|
||||||
if isinstance(self.bot_message, list):
|
if isinstance(self.bot_message, list):
|
||||||
msg = random.choice(self.bot_message)
|
msg = random.choice(self.bot_message)
|
||||||
|
|
Loading…
Reference in New Issue