Files

70 lines
2.3 KiB
Python
Raw Permalink Normal View History

2018-06-13 14:34:23 +00:00
#!/usr/bin/python
import fileinput
import json
import time
import calendar
import shutil
logfile = "/home/archangelic/irc/log"
2018-10-05 16:02:38 -04:00
# logfile = "/home/jumblesale/Code/irc/log"
2018-06-13 14:34:23 +00:00
outfile = "/home/krowbar/logs/chatStack.json"
2018-10-05 16:02:38 -04:00
chatData = {
"hours": [],
"regions": {},
} # hash keyed by "region" and then hour that counts chat instances
# we only care about recent chats, let's say for the past couple weeks
2018-06-13 14:34:23 +00:00
oneHour = 60 * 60
oneWeek = 7 * 24 * 60 * 60
timeNow = calendar.timegm(time.gmtime())
timeCutoff = calendar.timegm(time.gmtime()) - (2 * oneWeek)
# this will eventually represent each region users are from
def getAllRegions():
2018-10-05 16:02:38 -04:00
return ["unknown"]
2018-06-13 14:34:23 +00:00
# this will provide a way to look up what region a user is from
def getRegion(user):
2018-10-05 16:02:38 -04:00
return "unknown"
2018-06-13 14:34:23 +00:00
# populate the hours array with time 1 hour away from each other
startTime = timeCutoff
while startTime < timeNow:
2018-10-05 16:02:38 -04:00
chatData["hours"].append(startTime)
2018-06-13 14:34:23 +00:00
startTime += oneHour
# populate the regions array with blank data for each region
for region in getAllRegions():
2018-10-05 16:02:38 -04:00
chatData["regions"][region] = {
"name": region,
"values": [0] * len(chatData["hours"]),
}
2018-06-13 14:34:23 +00:00
with open(logfile, "r") as log:
2018-10-05 16:02:38 -04:00
hourIdx = (
0
) # starting with the oldest time slot, we will count instances of user chatting
2018-06-13 14:34:23 +00:00
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
2018-06-13 14:34:23 +00:00
if time > timeCutoff:
region = getRegion(user)
2018-10-05 16:02:38 -04:00
while (
time > chatData["hours"][hourIdx] + oneHour
): # we are past the current hour idx, move ahead until we find the right idx
hourIdx += 1
if hourIdx >= len(chatData["hours"]):
break
# uh oh! somehow we are parsing a line from the future! we're in pretty bad shape!
2018-06-13 14:34:23 +00:00
# hourIdx should now be a good value
2018-10-05 16:02:38 -04:00
chatData["regions"][region]["values"][
hourIdx
] += 1 # increment the user region's count for the current hour
2018-06-13 14:34:23 +00:00
with open(outfile + ".tmp", "w") as tmpFile:
tmpFile.write(json.dumps(chatData))
shutil.move(outfile + ".tmp", outfile)