Skip to content

Instantly share code, notes, and snippets.

@Kagee
Created October 25, 2013 19:44
Show Gist options
  • Save Kagee/7160718 to your computer and use it in GitHub Desktop.
Save Kagee/7160718 to your computer and use it in GitHub Desktop.
import sys # stdin
import re # regexp
import os
import json
qsize = 20;
filename = "chat.json"
smsg = re.compile(r"\[(\d{2}:\d{2}:\d{2})\] \[Server thread/INFO\]: (.*)")
chat = re.compile(r"<(.*)> (.*)")
achi = re.compile(r"(.*) has just earned the achievement \[(.*)\]")
died = re.compile(r"([^ ]*) ((blew|burned|died|drowned|fell|got|hit|tried|starved|suffocated|walked|was|went|withered).*)")
storage = list()
def put(a,t,n,m):
if(len(storage) > qsize):
storage.pop(0)
storage.append({'action':a,'timestamp':t,'nick':n,'message':m})
def writeJSON():
f = open(filename+".new", 'w+')
js = json.dumps(storage)
f.write(js)
f.flush()
f.close()
# This way the webserver will always
# serve "complete" json
os.rename(filename+".new", filename)
while True:
line = sys.stdin.readline()
line=line.strip()
sm = re.match(smsg, line)
if (sm):
time = sm.group(1)
data = sm.group(2)
cl = re.match(chat, data);
if(cl):
# action, timestamp, nick, message
put("said", time, cl.group(1), cl.group(2))
writeJSON()
continue;
ac = re.match(achi, data);
if(ac):
put("gained", time, ac.group(1), ac.group(2))
writeJSON()
continue;
di = re.match(died, data)
if(di):
put("died", time, di.group(1), di.group(2));
writeJSON()
continue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment