index : logbot | |
Archlinux32 log bot | gitolite user |
summaryrefslogtreecommitdiff |
-rwxr-xr-x | logbot.py | 38 |
@@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python # coding: utf-8 """ @@ -143,17 +143,17 @@ html_header = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ### Helper functions def append_line(filename, line): - data = open(filename, "rb").readlines()[:-2] + data = open(filename, "r").readlines()[:-2] data += [line, "\n<br />", "\n</body>", "\n</html>"] write_lines(filename, data) def write_lines(filename, lines): - f = open(filename, "wb") + f = open(filename, "w") f.writelines(lines) f.close() def write_string(filename, string): - f = open(filename, "wb") + f = open(filename, "w") f.write(string) f.close() @@ -223,15 +223,15 @@ class Logbot(SingleServerIRCBot): self.nick_pass = nick_pass self.load_channel_locations() - print "Logbot %s" % __version__ - print "Connecting to %s:%i..." % (server, port) - print "Press Ctrl-C to quit" + print("Logbot %s" % __version__) + print("Connecting to %s:%i..." % (server, port)) + print("Press Ctrl-C to quit") def quit(self): self.connection.disconnect("Quitting...") def color(self, user): - hash = md5(user).hexdigest() + hash = md5(user.encode()).hexdigest() r = int(hash[0:2],16) g = int(hash[2:4],16) b = int(hash[4:6],16) @@ -249,7 +249,7 @@ class Logbot(SingleServerIRCBot): def format_event(self, name, event, params): msg = self.format[name] - for key, val in params.iteritems(): + for key, val in params.items(): msg = msg.replace(key, val) # Always replace %user% with e.source() @@ -288,7 +288,7 @@ class Logbot(SingleServerIRCBot): if self.ftp and self.count > FTP_WAIT: self.count = 0 - print "Uploading to FTP..." + print("Uploading to FTP...") for root, dirs, files in os.walk("logs"): #TODO: Create folders @@ -299,25 +299,25 @@ class Logbot(SingleServerIRCBot): remote_fname = "/".join(full_fname.split("\\")[1:]) else: remote_fname = "/".join(full_fname.split("/")[1:]) - if DEBUG: print repr(remote_fname) + if DEBUG: print(repr(remote_fname)) # Upload! try: self.ftp.storbinary("STOR %s" % remote_fname, open(full_fname, "rb")) # Folder doesn't exist, try creating it and storing again - except ftplib.error_perm, e: #code, error = str(e).split(" ", 1) + except ftplib.error_perm as e: #code, error = str(e).split(" ", 1) if str(e).split(" ", 1)[0] == "553": self.ftp.mkd(os.path.dirname(remote_fname)) self.ftp.storbinary("STOR %s" % remote_fname, open(full_fname, "rb")) else: raise e # Reconnect on timeout - except ftplib.error_temp, e: self.set_ftp(connect_ftp()) + except ftplib.error_temp as e: self.set_ftp(connect_ftp()) # Unsure of error, try reconnecting except: self.set_ftp(connect_ftp()) - print "Finished uploading" + print("Finished uploading") def append_log_msg(self, channel, msg): - print "%s >>> %s" % (channel, msg) + print("%s >>> %s" % (channel, msg)) #Make sure the channel is always lowercase to prevent logs with other capitalisations to be created channel_title = channel channel = channel.lower() @@ -365,7 +365,7 @@ class Logbot(SingleServerIRCBot): def on_all_raw_messages(self, c, e): """Display all IRC connections in terminal""" - if DEBUG: print e.arguments()[0] + if DEBUG: print(e.arguments()[0]) def on_welcome(self, c, e): """Join channels after successful connection""" @@ -432,7 +432,7 @@ class Logbot(SingleServerIRCBot): self.write_event("pubnotice", e) def on_privmsg(self, c, e): - print nm_to_n(e.source()), e.arguments() + print(nm_to_n(e.source()), e.arguments()) c.privmsg(nm_to_n(e.source()), self.format["help"]) def on_quit(self, c, e): @@ -451,10 +451,10 @@ class Logbot(SingleServerIRCBot): self.channel_locations = {} if os.path.exists(CHANNEL_LOCATIONS_FILE): f = open(CHANNEL_LOCATIONS_FILE, 'r') - self.channel_locations = dict((k.lower(), v) for k, v in dict([line.strip().split(None,1) for line in f.readlines()]).iteritems()) + self.channel_locations = dict((k.lower(), v) for k, v in dict([line.strip().split(None,1) for line in f.readlines()]).items()) def connect_ftp(): - print "Using FTP %s..." % (FTP_SERVER) + print("Using FTP %s..." % (FTP_SERVER)) f = ftplib.FTP(FTP_SERVER, FTP_USER, FTP_PASS) f.cwd(FTP_FOLDER) return f |