diff --git a/gelbooru_poster.py b/gelbooru_poster.py index c0ae3bd..f6ff325 100644 --- a/gelbooru_poster.py +++ b/gelbooru_poster.py @@ -121,7 +121,11 @@ class BotInstance: post_json = post_request.json() if 'type' in post_json and post_json['type'] == 'Note' and 'object' in post_json and 'id' in post_json['object']: # Submit a /notes/create request to Misskey - create_note_request = requests.post(self.misskey_url + "notes/create", json = {"renoteId": post_json['object']['id'], "i": self.misskey_token}) + headers = { + "Authorization": "Bearer " + self.misskey_token, + "Content-Type": "application/json" + } + create_note_request = requests.post(self.misskey_url + "notes/create", json = {"renoteId": post_json['object']['id']}, headers=headers) # If error, print error and exit if create_note_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file) @@ -130,7 +134,11 @@ class BotInstance: 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}) + headers = { + "Content-Type": "application/json", + "Authorization": "Bearer " + self.misskey_token + } + file_presence_check = requests.post(self.misskey_url + "drive/files/find", json = {"name": image_fname}, headers=headers) if file_presence_check.status_code != 200: image_found = False else: @@ -171,11 +179,16 @@ class BotInstance: image_upld_src = "image.jpg" # Submit a /drive/files/create request to Misskey - 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")}) + headers = { + "Authorization": "Bearer " + self.misskey_token, + "Content-Type": "multipart/form-data" + } + create_file_request = requests.post(self.misskey_url + "drive/files/create", data = {"name": image_fname, "isSensitive": str(image_rating != 'general').lower()}, files = {"file": open(image_upld_src, "rb")}, headers=headers) # If error, print error and exit if create_file_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_file_request) print(create_file_request.json()["error"]["message"], file=log_file) return False @@ -185,7 +198,11 @@ class BotInstance: 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}) + headers = { + "Content-Type": "application/json", + "Authorization": "Bearer " + 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'}, headers=headers) # 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) @@ -199,7 +216,11 @@ 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": image_fname, "i": self.misskey_token}) + headers = { + "Content-Type": "application/json", + "Authorization": "Bearer " + self.misskey_token + } + file_id_request = requests.post(self.misskey_url + "drive/files/find", json = {"name": image_fname}, headers=headers) # If error, print error and exit if file_id_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file) @@ -224,8 +245,12 @@ class BotInstance: time.sleep(min(30, (attempts ** 2) / 2)) # Submit a /notes/create request to Misskey + headers = { + "Authorization": "Bearer " + self.misskey_token, + "Content-Type": "application/json" + } msg = self.format_message(image_src, image_post_url) - create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_id], "text": msg, "i": self.misskey_token}) + create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_id], "text": msg}, headers=headers) # If error, print error and exit if create_note_request.status_code != 200: print(self.cfg_name + ": Error: ", file=log_file)