IrcMiRCARTBot.py:_{dispatchPrivmsg,uploadToImgur}(): gracefully handle invalid responses from Imgur.com.

This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-12-03 11:51:20 +01:00
parent fbf415816d
commit ffdc3cb6e5

View File

@ -139,7 +139,10 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
canvasStore.outMap.append([[1, 1, 0, " "]] * len(canvasStore.outMap[0])) canvasStore.outMap.append([[1, 1, 0, " "]] * len(canvasStore.outMap[0]))
MiRCARTToPngFile(canvasStore.outMap, "DejaVuSansMono.ttf", 11).export(imgTmpFilePath) MiRCARTToPngFile(canvasStore.outMap, "DejaVuSansMono.ttf", 11).export(imgTmpFilePath)
imgurResponse = self._uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", self.imgurApiKey) imgurResponse = self._uploadToImgur(imgTmpFilePath, "MiRCART image", "MiRCART image", self.imgurApiKey)
if imgurResponse[0] == 200: if imgurResponse[0] == None:
self._log("Upload failed with exception `{}'".format(imgurResponse[1]))
self.queue("PRIVMSG", message[2], "4/!\\ Upload failed with exception `{}'!".format(imgurResponse[1]))
elif imgurResponse[0] == 200:
self._log("Uploaded as: {}".format(imgurResponse[1])) self._log("Uploaded as: {}".format(imgurResponse[1]))
self.queue("PRIVMSG", message[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1])) self.queue("PRIVMSG", message[2], "8/!\\ Uploaded as: {}".format(imgurResponse[1]))
self.clientChannelLastMessage = int(time.time()) self.clientChannelLastMessage = int(time.time())
@ -177,7 +180,10 @@ class IrcMiRCARTBot(IrcClient.IrcClient):
requestHeaders = { \ requestHeaders = { \
"Authorization": "Client-ID " + apiKey} "Authorization": "Client-ID " + apiKey}
responseHttp = requests.post("https://api.imgur.com/3/upload.json", data=requestData, headers=requestHeaders) responseHttp = requests.post("https://api.imgur.com/3/upload.json", data=requestData, headers=requestHeaders)
try:
responseDict = json.loads(responseHttp.text) responseDict = json.loads(responseHttp.text)
except json.decoder.JSONDecodeError as err:
return [None, err]
if responseHttp.status_code == 200: if responseHttp.status_code == 200:
return [200, responseDict.get("data").get("link")] return [200, responseDict.get("data").get("link")]
else: else: