Fix filename handling.

This commit is contained in:
dotnet 2023-10-13 15:21:19 -04:00
parent 6a82235cf0
commit df4001644c
1 changed files with 17 additions and 3 deletions

View File

@ -75,13 +75,27 @@ class BotInstance:
# Download and post the image to Misskey
def post_image(self, image_url, image_src, image_rating, log_file):
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})
image_fname = os.path.split(image_url)[-1]
#Check if the image is already uploaded with original extension
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:
image_found = False
else:
file_presence_json = file_presence_check.json()
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 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"):
@ -123,7 +137,7 @@ class BotInstance:
return False
else:
# Submit a /drive/files/create request to Misskey
create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": os.path.split(image_url)[-1], "i": self.misskey_token, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open("image.jpg", "rb")})
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
if create_file_request.status_code != 200:
print(self.cfg_name + ": Error: ", file=log_file)
@ -151,7 +165,7 @@ class BotInstance:
attempts = 0
while True:
# Get the file ID using the /drive/files/find request
file_id_request = requests.post(self.misskey_url + "drive/files/find", json = {"name": os.path.split(image_url)[-1], "i": self.misskey_token})
file_id_request = requests.post(self.misskey_url + "drive/files/find", json = {"name": image_fname, "i": self.misskey_token})
# If error, print error and exit
if file_id_request.status_code != 200:
print(self.cfg_name + ": Error: ", file=log_file)