diff --git a/gelbooru_poster.py b/gelbooru_poster.py index 1a2c87e..577cf71 100644 --- a/gelbooru_poster.py +++ b/gelbooru_poster.py @@ -89,7 +89,13 @@ class BotInstance: image_request = requests.get(image_url) # If error, print error and exit if image_request.status_code != 200: - print("Error: " + image_request.json()["error"]["message"], 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("\n", file=log_file) + print(image_request.json()["error"]["message"], file=log_file) + print("\n", file=log_file) + print(image_url, file=log_file) 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: @@ -109,14 +115,22 @@ class BotInstance: 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 upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200: - print("Error: " + upload_from_url_request.json()["error"]["message"], 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("\n", file=log_file) + print(upload_from_url_request.json()["error"]["message"], file=log_file) 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")}) # If error, print error and exit if create_file_request.status_code != 200: - print("Error: " + create_file_request.json()["error"]["message"], 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("\n", file=log_file) + print(create_file_request.json()["error"]["message"], file=log_file) return False os.remove("image.jpg") @@ -125,7 +139,11 @@ class BotInstance: 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 upload_from_url_request.status_code != 204 and upload_from_url_request.status_code != 200: - print("Error: " + upload_from_url_request.json()["error"]["message"], 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("\n", file=log_file) + print(upload_from_url_request.json()["error"]["message"], file=log_file) return False # Wait for the image to be uploaded time.sleep(1) @@ -136,7 +154,11 @@ class BotInstance: file_id_request = requests.post(self.misskey_url + "drive/files/find", json = {"name": os.path.split(image_url)[-1], "i": self.misskey_token}) # If error, print error and exit if file_id_request.status_code != 200: - print("Error: " + file_id_request.json()["error"]["message"], 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("\n", file=log_file) + print(file_id_request.json()["error"]["message"], file=log_file) return False file_id_json = file_id_request.json() if len(file_id_json) > 0: @@ -144,7 +166,11 @@ class BotInstance: break if attempts > 10: - print("Error: Image not uploaded", 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("\n", file=log_file) + print("Image not uploaded", file=log_file) return False # If the image hasn't been uploaded after 10 attempts, exit @@ -167,7 +193,11 @@ class BotInstance: 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("Error: " + create_note_request.json()["error"]["message"], 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("\n", file=log_file) + print(create_note_request.json()["error"]["message"], file=log_file) return True # Submit a /notes/create request to Misskey @@ -180,7 +210,11 @@ class BotInstance: create_note_request = requests.post(self.misskey_url + "notes/create", json = {"fileIds": [file_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("Error: " + create_note_request.json()["error"]["message"], 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("\n", file=log_file) + print(create_note_request.json()["error"]["message"], file=log_file) return True def bot_process(self, log_file):