Files

36 lines
1.4 KiB
Python
Raw Permalink Normal View History

2015-08-24 10:04:57 -05:00
#!/usr/bin/python
import fileinput
import json
import time
import calendar
import shutil
2018-01-24 20:51:54 +00:00
logfile = "/home/archangelic/irc/log"
2018-10-05 16:02:38 -04:00
# logfile = "/home/jumblesale/Code/irc/log"
2015-08-24 10:04:57 -05:00
outfile = "/home/krowbar/logs/userData.json"
2018-10-05 16:02:38 -04:00
userData = {} # hash keyed by "user" that contains an array of timestamps
# we only care about recent chats, let's say for the past couple weeks
2015-10-13 08:23:17 -05:00
oneWeek = 7 * 24 * 60 * 60
fiveMinutes = 5 * 60
timeCutoff = calendar.timegm(time.gmtime()) - (5 * oneWeek)
2015-08-24 10:04:57 -05:00
with open(logfile, "r") as log:
for line in log:
try:
time, user, message = line.split("\t", 3)
time = int(time)
except ValueError:
2018-10-05 16:02:38 -04:00
continue # There are some bad lines in the log file that we'll ignore if we can't parse
2015-08-24 10:04:57 -05:00
if time > timeCutoff:
2018-10-05 16:02:38 -04:00
# standardize all times into a single "week"
# time %= oneWeek
# if the user already exists in the list
2015-08-24 10:04:57 -05:00
if user in userData:
2015-10-13 08:23:17 -05:00
if time < userData[user][-1] + fiveMinutes:
2018-10-05 16:02:38 -04:00
continue # only record times that are at least five minutes apart
userData[user].append(time) # append the new time
else: # if they are new
userData[user] = [time] # create a new list
2015-08-24 10:04:57 -05:00
with open(outfile + ".tmp", "w") as tmpFile:
tmpFile.write(json.dumps(userData))
shutil.move(outfile + ".tmp", outfile)