Improve error reporting.
This commit is contained in:
parent
cf310894b5
commit
6a82235cf0
|
@ -89,7 +89,13 @@ class BotInstance:
|
||||||
image_request = requests.get(image_url)
|
image_request = requests.get(image_url)
|
||||||
# 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("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
|
return False
|
||||||
#Save image_request.content, based on image_url extension
|
#Save image_request.content, based on image_url extension
|
||||||
with open("image_original" + os.path.splitext(image_url)[-1], "wb") as image_file:
|
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})
|
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("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
|
return False
|
||||||
else:
|
else:
|
||||||
# Submit a /drive/files/create request to Misskey
|
# 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": 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 error, print error and exit
|
||||||
if create_file_request.status_code != 200:
|
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
|
return False
|
||||||
|
|
||||||
os.remove("image.jpg")
|
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})
|
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("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
|
return False
|
||||||
# Wait for the image to be uploaded
|
# Wait for the image to be uploaded
|
||||||
time.sleep(1)
|
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})
|
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 error, print error and exit
|
||||||
if file_id_request.status_code != 200:
|
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
|
return False
|
||||||
file_id_json = file_id_request.json()
|
file_id_json = file_id_request.json()
|
||||||
if len(file_id_json) > 0:
|
if len(file_id_json) > 0:
|
||||||
|
@ -144,7 +166,11 @@ class BotInstance:
|
||||||
break
|
break
|
||||||
|
|
||||||
if attempts > 10:
|
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
|
return False
|
||||||
|
|
||||||
# If the image hasn't been uploaded after 10 attempts, exit
|
# 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})
|
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 error, print error and exit
|
||||||
if create_note_request.status_code != 200:
|
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
|
return True
|
||||||
|
|
||||||
# Submit a /notes/create request to Misskey
|
# 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})
|
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 error, print error and exit
|
||||||
if create_note_request.status_code != 200:
|
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
|
return True
|
||||||
|
|
||||||
def bot_process(self, log_file):
|
def bot_process(self, log_file):
|
||||||
|
|
Loading…
Reference in New Issue