gitlab_snippets/Kelimion/twitchlog.py

119 lines
4.1 KiB
Python
Raw Normal View History

#!/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 <logname> [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 = "<tr><td class=\"dateline\">%s</td><td class=\"nick %s\">%s</td><td class=\"chat %s\">%s</td>"
fmt2 = "<tr><td class=\"dateline\">%s</td><td colspan=\"2\">* <span class=\"nick %s\">%s</span> <span class=\"chat %s\">%s</span></td>"
print("""<html>
<head>
<title>%s</title>
<meta charset="utf-8">
<style>
.nick { font-weight: bold; text-align: right; }
table { width: 100%%; }
tr > td { width: 10%%; }
tr > td+td { width: 10%%; }
tr > td+td+td { width: 80%%; }""" % filename)
for nick, color in nickcolor.items():
print(".%s { color: %s; }" % (nick, color))
print(".%s a { color: %s; font-weight: bold; }" % (nick, color))
print("""
</style>
</head>
<body>
<table>
""")
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("""
</table>
</body>
</html>""")
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']))