Fix image optimization and adjust error logging.
This commit is contained in:
parent
df4001644c
commit
a6c9af11d2
|
@ -75,9 +75,11 @@ class BotInstance:
|
||||||
# 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_rating, log_file):
|
||||||
image_found = False
|
image_found = False
|
||||||
image_fname = os.path.split(image_url)[-1]
|
|
||||||
|
|
||||||
#Check if the image is already uploaded with original extension
|
#Extract image filename, replace extension with .jpg
|
||||||
|
image_fname = os.path.splitext(os.path.split(image_url)[-1])[0] + ".jpg"
|
||||||
|
|
||||||
|
#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:
|
||||||
image_found = False
|
image_found = False
|
||||||
|
@ -85,17 +87,6 @@ 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
|
||||||
|
|
||||||
#Check if the image is already uploaded with .jpg extension
|
|
||||||
if not image_found:
|
|
||||||
file_presence_check = requests.post(self.misskey_url + "drive/files/find", json = {"name": image_fname + ".jpg", "i": self.misskey_token})
|
|
||||||
if file_presence_check.status_code != 200:
|
|
||||||
image_found = False
|
|
||||||
else:
|
|
||||||
file_presence_json = file_presence_check.json()
|
|
||||||
image_found = len(file_presence_json) > 0
|
|
||||||
if image_found:
|
|
||||||
image_fname += ".jpg"
|
|
||||||
|
|
||||||
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"):
|
||||||
|
@ -104,20 +95,18 @@ class BotInstance:
|
||||||
# If error, print error and exit
|
# If error, print error and exit
|
||||||
if image_request.status_code != 200:
|
if image_request.status_code != 200:
|
||||||
print(self.cfg_name + ": Error: ", file=log_file)
|
print(self.cfg_name + ": Error: ", file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(image_request.json()["error"]["message"], file=log_file)
|
print(image_request.json()["error"]["message"], file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(image_url, file=log_file)
|
print(image_url, file=log_file)
|
||||||
return False
|
return False
|
||||||
#Save image_request.content, based on image_url extension
|
|
||||||
with open("image_original" + os.path.splitext(image_url)[-1], "wb") as image_file:
|
|
||||||
image_file.write(image_request.content)
|
|
||||||
|
|
||||||
# Optimise the image by reducing it to max width of 2048px
|
# Optimise the image by reducing it to max width of 2048px
|
||||||
image = Image.open(BytesIO(image_request.content))
|
image = Image.open(BytesIO(image_request.content))
|
||||||
|
image_original_exists = False
|
||||||
|
if os.path.splitext(image_url)[-1] == ".jpg" or os.path.splitext(image_url)[-1] == ".jpeg":
|
||||||
|
image_original_exists = True
|
||||||
|
with open("image_original.jpg", "wb") as image_file:
|
||||||
|
image_file.write(image_request.content)
|
||||||
if image.width > 2048:
|
if image.width > 2048:
|
||||||
image = image.resize((2048, int(image.height * (2048 / image.width))), Image.LANCZOS)
|
image = image.resize((2048, int(image.height * (2048 / image.width))), Image.LANCZOS)
|
||||||
# Apply JPEG compression
|
# Apply JPEG compression
|
||||||
|
@ -125,38 +114,28 @@ class BotInstance:
|
||||||
image.save("image.jpg", optimize=True, quality=90)
|
image.save("image.jpg", optimize=True, quality=90)
|
||||||
|
|
||||||
# If the image is larger than the original, use the original
|
# If the image is larger than the original, use the original
|
||||||
if os.path.getsize("image.jpg") > os.path.getsize("image_original" + os.path.splitext(image_url)[-1]):
|
if image_original_exists and os.path.getsize("image.jpg") > os.path.getsize("image_original.jpg"):
|
||||||
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})
|
image_upld_src = "image_original.jpg"
|
||||||
# If error, print error and exit
|
|
||||||
if upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200:
|
|
||||||
print(self.cfg_name + ": Error: ", file=log_file)
|
|
||||||
print("\n", file=log_file)
|
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
|
||||||
print("\n", file=log_file)
|
|
||||||
print(upload_from_url_request.json()["error"]["message"], file=log_file)
|
|
||||||
return False
|
|
||||||
else:
|
else:
|
||||||
# Submit a /drive/files/create request to Misskey
|
image_upld_src = "image.jpg"
|
||||||
create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": image_fname, "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open("image.jpg", "rb")})
|
|
||||||
# If error, print error and exit
|
# Submit a /drive/files/create request to Misskey
|
||||||
if create_file_request.status_code != 200:
|
create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": image_fname, "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open(image_upld_src, "rb")})
|
||||||
print(self.cfg_name + ": Error: ", file=log_file)
|
# If error, print error and exit
|
||||||
print("\n", file=log_file)
|
if create_file_request.status_code != 200:
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(self.cfg_name + ": Error: ", file=log_file)
|
||||||
print("\n", file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print(create_file_request.json()["error"]["message"], file=log_file)
|
print(create_file_request.json()["error"]["message"], file=log_file)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
os.remove("image.jpg")
|
os.remove("image.jpg")
|
||||||
os.remove("image_original" + os.path.splitext(image_url)[-1])
|
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})
|
||||||
# If error, print error and exit
|
# If error, print error and exit
|
||||||
if upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200:
|
if upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200:
|
||||||
print(self.cfg_name + ": Error: ", file=log_file)
|
print(self.cfg_name + ": Error: ", file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(upload_from_url_request.json()["error"]["message"], file=log_file)
|
print(upload_from_url_request.json()["error"]["message"], file=log_file)
|
||||||
return False
|
return False
|
||||||
# Wait for the image to be uploaded
|
# Wait for the image to be uploaded
|
||||||
|
@ -169,9 +148,7 @@ class BotInstance:
|
||||||
# If error, print error and exit
|
# If error, print error and exit
|
||||||
if file_id_request.status_code != 200:
|
if file_id_request.status_code != 200:
|
||||||
print(self.cfg_name + ": Error: ", file=log_file)
|
print(self.cfg_name + ": Error: ", file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(file_id_request.json()["error"]["message"], file=log_file)
|
print(file_id_request.json()["error"]["message"], file=log_file)
|
||||||
return False
|
return False
|
||||||
file_id_json = file_id_request.json()
|
file_id_json = file_id_request.json()
|
||||||
|
@ -181,15 +158,14 @@ class BotInstance:
|
||||||
|
|
||||||
if attempts > 10:
|
if attempts > 10:
|
||||||
print(self.cfg_name + ": Error: ", file=log_file)
|
print(self.cfg_name + ": Error: ", file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print("Image not uploaded", file=log_file)
|
print("Image not uploaded", file=log_file)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# If the image hasn't been uploaded after 10 attempts, exit
|
# If the image hasn't been uploaded after 10 attempts, exit
|
||||||
attempts += 1
|
attempts += 1
|
||||||
# Wait and try again
|
# Wait and try again
|
||||||
|
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
|
# Try to determine if the image_src is a fediverse link, if so renote it instead of posting a new note
|
||||||
|
@ -208,9 +184,7 @@ class BotInstance:
|
||||||
# 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(self.cfg_name + ": Error: ", file=log_file)
|
print(self.cfg_name + ": Error: ", file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(create_note_request.json()["error"]["message"], file=log_file)
|
print(create_note_request.json()["error"]["message"], file=log_file)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -225,9 +199,7 @@ class BotInstance:
|
||||||
# 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(self.cfg_name + ": Error: ", file=log_file)
|
print(self.cfg_name + ": Error: ", file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print("\n", file=log_file)
|
|
||||||
print(create_note_request.json()["error"]["message"], file=log_file)
|
print(create_note_request.json()["error"]["message"], file=log_file)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -332,9 +304,8 @@ def main():
|
||||||
# If error, print error and continue
|
# If error, print error and continue
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#Print time
|
#Print time
|
||||||
print("\n\n" + cfg_name + ": Error: ", file=log_file)
|
print("\n" + cfg_name + ": Error: ", file=log_file)
|
||||||
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), file=log_file)
|
||||||
print("\n\n", file=log_file)
|
|
||||||
traceback.print_exc(file=log_file)
|
traceback.print_exc(file=log_file)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue