#!/usr/bin/env python3 from datetime import datetime, date, timedelta import sys # this is optional, to autolink mentioned urls if format --html is chosen # if you don't have it, also comment out line 96 from bleach import linkify if __name__ == '__main__': #print(sys.argv) if len(sys.argv) < 2 or (len(sys.argv) > 1 and sys.argv[1] == '-h'): print("Usage: %s [offset-in-hours] [--html]" % sys.argv[0]) exit() elif len(sys.argv) >= 2: filename = sys.argv[1] offset = 0 elif len(sys.argv) >= 3: filename = sys.argv[1] offset = int(sys.argv[2]) if len(sys.argv) >= 4 and sys.argv[3] == '--html': formatting = 'html' else: formatting = 'plain' today = date.today() delta = timedelta(hours=offset) nickcolor = {} log = [] colors = ['red', 'blue', 'green', 'yellow', 'teal', 'purple'] max_nick = 0 with open(filename, 'r') as f: for line in f: when = line[:15] when_parsed = datetime.strptime(when, '%b %d %H:%M:%S').replace(year=today.year) when_est = when_parsed + delta what = line[15:] if what[1] == '<': nick_start = 2 nick_end = what.find('>') nick = what[nick_start:nick_end].strip() if not nick in nickcolor: nickcolor[nick] = colors[0] colors = colors[1:] + [colors[0]] if len(nick) > max_nick: max_nick = len(nick) what = what[nick_end + 2:].strip('\n') log.append(dict(when=when_est, nick=nick, what=what)) elif what[1] == '*': nick_start = 3 nick_end = what.find('\s') what = what[nick_start:nick_end].strip() nick_start = 0 nick_end = what.find(' ') nick = what[nick_start:nick_end].strip() if not nick in nickcolor: nickcolor[nick] = colors[0] colors = colors[1:] + [colors[0]] if len(nick) > max_nick: max_nick = len(nick) what = what[nick_end + 1:].strip('\n') log.append(dict(when=when_est, nick=nick, what=what, action=True)) if formatting == 'html': fmt1 = "%s%s%s" fmt2 = "%s* %s %s" print(""" %s """) thisnick = '' for line in log: line['what'] = linkify(line['what']) if not 'action' in line.keys(): if thisnick == line['nick']: displaynick = u'▲' else: displaynick = line['nick'] print(fmt1 % (line['when'], line['nick'], displaynick, line['nick'], line['what'])) else: print(fmt2 % (line['when'], line['nick'], line['nick'], line['nick'], line['what'])) thisnick = line['nick'] print("""
""") else: fmt = "%%s | %%%ds | %%s" % (max_nick) for line in log: if not 'action' in line.keys(): print(fmt % (line['when'], line['nick'], line['what'])) else: print("%s | * %s %s" % (line['when'], line['nick'], line['what']))