pngbot.py: splits main into class IrcMiRCARTBot.

pngbot.py:IrcBot.close(): only close() clientSocket if non-None.
pngbot.py:IrcMiRCARTBot.connect(): correctly zero-initialise clientLastMessage.
pngbot.py:IrcMiRCARTBot.dispatch(): fix imgurResponseHttp.status_code reference.
This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-01-02 22:20:20 +01:00
parent 4c5755d2fc
commit 4f56b9c783

77
pngbot.py Executable file → Normal file
View File

@ -46,6 +46,7 @@ class IrcBot:
# }}} # }}}
# {{{ close(): Close connection to server # {{{ close(): Close connection to server
def close(self): def close(self):
if self.clientSocket != None:
self.clientSocket.close() self.clientSocket.close()
self.clientSocket = self.clientSocketFile = None; self.clientSocket = self.clientSocketFile = None;
# }}} # }}}
@ -83,34 +84,39 @@ class IrcBot:
self.clientNick = clientNick; self.clientIdent = clientIdent; self.clientGecos = clientGecos; self.clientNick = clientNick; self.clientIdent = clientIdent; self.clientGecos = clientGecos;
# }}} # }}}
# class IrcMiRCARTBot(IrcBot):
# Entry point """IRC<->MiRCART bot"""
def main(argv0, ircServerHname, ircServerPort="6667", ircClientNick="pngbot", ircClientIdent="pngbot", ircClientGecos="pngbot", ircClientChannel="#MiRCART"): clientLastMessage = clientChannel = None
_IrcBot = IrcBot(ircServerHname, ircServerPort, ircClientNick, ircClientIdent, ircClientGecos)
print("Connecting to {}:{}...".format(ircServerHname, ircServerPort)) # {{{ connect(): Connect to server and (re)initialise
_IrcBot.connect() def connect(self):
print("Connected to {}:{}.".format(ircServerHname, ircServerPort)) print("Connecting to {}:{}...".format(self.serverHname, self.serverPort))
print("Registering on {}:{} as {}, {}, {}...".format(ircServerHname, ircServerPort, ircClientNick, ircClientIdent, ircClientGecos)) super().connect()
print("Connected to {}:{}.".format(self.serverHname, self.serverPort))
print("Registering on {}:{} as {}, {}, {}...".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
self.clientLastMessage = 0
# }}}
# {{{ dispatch(): Read, parse, and dispatch single line from server
def dispatch(self):
while True: while True:
ircClientLastMessage = int(time.time()) serverMessage = self.readline()
ircServerMessage = _IrcBot.readline() if serverMessage == None:
if ircServerMessage == None: print("Disconnected from {}:{}.".format(self.serverHname, self.serverPort))
print("Disconnected from {}:{}.".format(ircServerHname, ircServerPort)) self.close(); break;
_IrcBot.close(); break; elif serverMessage[1] == "001":
elif ircServerMessage[1] == "001": print("Registered on {}:{} as {}, {}, {}.".format(self.serverHname, self.serverPort, self.clientNick, self.clientIdent, self.clientGecos))
print("Registered on {}:{} as {}, {}, {}.".format(ircServerHname, ircServerPort, ircClientNick, ircClientIdent, ircClientGecos)) print("Joining {} on {}:{}...".format(self.clientChannel, self.serverHname, self.serverPort))
print("Joining {} on {}:{}...".format(ircClientChannel, ircServerHname, ircServerPort)) self.sendline("JOIN", self.clientChannel)
_IrcBot.sendline("JOIN", ircClientChannel) elif serverMessage[1] == "PING":
elif ircServerMessage[1] == "PING": self.sendline("PONG", serverMessage[2])
_IrcBot.sendline("PONG", ircServerMessage[2]) elif serverMessage[1] == "PRIVMSG" \
elif ircServerMessage[1] == "PRIVMSG" \ and serverMessage[2].lower() == self.clientChannel.lower() \
and ircServerMessage[2].lower() == ircClientChannel.lower() \ and serverMessage[3].startswith("!pngbot "):
and ircServerMessage[3].startswith("!pngbot "): if (int(time.time()) - self.clientLastMessage) < 45:
if (int(time.time()) - ircClientLastMessage) < 45:
continue continue
else: else:
ircClientLastMessage = int(time.time()) self.clientLastMessage = int(time.time())
asciiUrl = ircServerMessage[3].split(" ")[1] asciiUrl = serverMessage[3].split(" ")[1]
asciiTmpFilePath = "tmp.txt"; imgTmpFilePath = "tmp.png"; asciiTmpFilePath = "tmp.txt"; imgTmpFilePath = "tmp.png";
if os.path.isfile(asciiTmpFilePath): if os.path.isfile(asciiTmpFilePath):
os.remove(asciiTmpFilePath) os.remove(asciiTmpFilePath)
@ -120,12 +126,27 @@ def main(argv0, ircServerHname, ircServerPort="6667", ircClientNick="pngbot", ir
_MiRCART = mirc2png.MiRCART(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11) _MiRCART = mirc2png.MiRCART(asciiTmpFilePath, imgTmpFilePath, "DejaVuSansMono.ttf", 11)
imgurResponseHttp = requests.post("https://api.imgur.com/3/upload.json", data={"key":"c9a6efb3d7932fd", "image":base64.b64encode(open(imgTmpFilePath, "rb").read()), "type":"base64", "name":"tmp.png", "title":"tmp.png"}, headers={"Authorization": "Client-ID c9a6efb3d7932fd"}) imgurResponseHttp = requests.post("https://api.imgur.com/3/upload.json", data={"key":"c9a6efb3d7932fd", "image":base64.b64encode(open(imgTmpFilePath, "rb").read()), "type":"base64", "name":"tmp.png", "title":"tmp.png"}, headers={"Authorization": "Client-ID c9a6efb3d7932fd"})
imgurResponse = json.loads(imgurResponseHttp.text) imgurResponse = json.loads(imgurResponseHttp.text)
if imgurResponse.status_code == 200: if imgurResponseHttp.status_code == 200:
imgurResponseUrl = imgurResponse.get("data").get("link") imgurResponseUrl = imgurResponse.get("data").get("link")
_IrcBot.sendline("PRIVMSG", ircServerMessage[2], "8/!\\ Uploaded as: {}".format(imgurResponseUrl)) self.sendline("PRIVMSG", serverMessage[2], "8/!\\ Uploaded as: {}".format(imgurResponseUrl))
else: else:
_IrcBot.sendline("PRIVMSG", ircServerMessage[2], "4/!\\ Uploaded failed with HTTP status code {}!".format(imgurResponse.status_code)) self.sendline("PRIVMSG", serverMessage[2], "4/!\\ Uploaded failed with HTTP status code {}!".format(imgurResponseHttp.status_code))
os.remove(asciiTmpFilePath); os.remove(imgTmpFilePath); os.remove(asciiTmpFilePath); os.remove(imgTmpFilePath);
# }}}
# {{{ Initialisation method
def __init__(self, serverHname, serverPort="6667", clientNick="pngbot", clientIdent="pngbot", clientGecos="pngbot", clientChannel="#MiRCART"):
super().__init__(serverHname, serverPort, clientNick, clientIdent, clientGecos)
self.clientChannel = clientChannel
# }}}
#
# Entry point
def main(*argv):
_IrcMiRCARTBot = IrcMiRCARTBot(*argv[1:])
while True:
_IrcMiRCARTBot.connect()
_IrcMiRCARTBot.dispatch()
_IrcMiRCARTBot.close()
if __name__ == "__main__": if __name__ == "__main__":
if ((len(sys.argv) - 1) < 1)\ if ((len(sys.argv) - 1) < 1)\