#!/usr/bin/env python # # mycrochat.py # # tells mycrocosm (via twitter) how much chatting you've done # in pidgin & irssi # # (c) 2008 Ryan Forsythe . all rights reserved. # distributed under the BSD license. see LICENSE for details. # # based on mycromail, by tim smith # run with a date on the cmd line to generate that day's stats. # you'll need to fix the date by hand on the mycrocosm web site, # unfortunately. use international date format, DD/MM/YYYY # irssi setup: # autolog = "yes"; # autolog_level = "ALL -MSGS -CRAP -CLIENTCRAP -CTCPS"; # autolog_path = "~/irclogs/$tag/$0.%y.%m.%d.log"; ############################# # MYCROCHAT CONFIG -- modify this to match what you want generated. # your twitter username and password twitter = {'username': '', 'password': ''} # which mycrocosm dataset are we updating? mycro_dataset = '' # what logs should be searched? do_pidgin = True do_irssi = True do_adium = False # enable sending: debug = False debug = False # your nicknames # for pidgin, these should be your display names, which aren't # neccessarily the same as your usernames. enter all the accounts you # want to track. pidgin_names = ["me", "x@gmail.com"] irssi_names = ["fweez", "rmf"] adium_names = ["myaimname", "y@gmail.com"] # finally, if you've got a fancy irssi theme you'll want to modify the # regex on line 113 to match what goes around your nick. ############################# # constants twitter_api_url = 'http://twitter.com/direct_messages/new.xml' cmdline_date_fmt = "%d/%m/%Y" pidgin_log_location = "~/.purple/logs" pidgin_log_date_fmt = "%Y-%m-%d" # we only care about the beginning irssi_log_location = "~/irclogs" irssi_log_date_fmt = "%y.%m.%d" # we only care about the end adium_log_location = "~/Library/Application Support/Adium 2.0/Users/Default/Logs" adium_log_date_fmt = "%Y-%m-%d" ############################# import glob import imaplib, re, urllib, urllib2 import os import os.path import re import sys import time from urllib import urlencode mycro_cmds = [] chatters = {} def process(logfiles, chatterre, mynameres): """Loop over the logfiles, pulling the chat partner's name out of each logfile name using chatterre. Open the logfiles, searching for lines matching each mynamre, updating the chatters global.""" global chatters for log in logfiles: m = chatterre.match(log) chatter = m.group(1) chatters[chatter] = chatters.get(chatter, 0) logfile = open(log, 'r') for line in logfile: for mynamere in mynameres: if (mynamere.search(line)): chatters[chatter] += 1 # generate the date tuple if len(sys.argv) == 1: # no date specified on cmd line, search today's gendate = time.localtime() else: gendate = time.strptime(sys.argv[1], cmdline_date_fmt) if do_pidgin: # check pidgin logs for the specified date pidgindate = time.strftime(pidgin_log_date_fmt, gendate) # /home/x/.purple/logs/protocol/myid/theirid/2008-11-14.094449-0800PST.txt chatterre = re.compile(os.path.expanduser(pidgin_log_location) + '/.*?/.*?/(.*?)/.*') pidginlogs = glob.glob(os.path.expanduser('~/.purple/logs') + '/*/*/*/' + pidgindate + '*txt') mynameres = [ re.compile(name + ':.*') for name in pidgin_names ] process(pidginlogs, chatterre, mynameres) if do_irssi: # check irssi logs for the specified date irssidate = time.strftime(irssi_log_date_fmt, gendate) # /home/ryan/irclogs/server/#channel.08.12.05.log # "chatter" is the channel name chatterre = re.compile(os.path.expanduser(irssi_log_location) + '/.*?/(#.*?)\..*.*log'); irssilogs = glob.glob(os.path.expanduser(irssi_log_location) + '/*/#*' + irssidate + '.log') mynameres = [ re.compile('<.' + name + '>') for name in irssi_names ] process(irssilogs, chatterre, mynameres) if do_adium: # check adium logs for the specified datea adiumdate = time.strftime(adium_log_date_fmt, gendate) # /Users/x/Library/Application Support/Adium 2.0/Users/Default/Logs/ # myid/theirid/theirid (2008-12-05T14.55.52-0800).chatlog/ # theirid (2008-12-05T14.55.52-0800).xml chatterre = re.compile(os.path.expanduser(adium_log_location) + '/.*?/(.*?)/.*') adiumlogs = glob.glob(os.path.expanduser(adium_log_location) + '/*/*/*/*' + adiumdate + '*.xml') mynameres = [ re.compile('sender="' + name + '"') for name in adium_names ] process(adiumlogs, chatterre, mynameres) #process chatters for chatter,count in chatters.iteritems(): if count == 0: continue mycro_cmds.append(mycro_dataset + ' . ' + chatter + ' + ' + str(count)) if debug: print "I would send the following commands:\n", "\n".join(mycro_cmds) print "Set debug = False in the config area of the script to enable sending" else: #post via twitter direct message pmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() pmgr.add_password(None, 'http://twitter.com', twitter['username'], twitter['password']) handler = urllib2.HTTPBasicAuthHandler(pmgr) opener = urllib2.build_opener(handler) for cmd in mycro_cmds: values = {'user': 'mycro', 'text': cmd} payload = urllib.urlencode(values) req = urllib2.Request(twitter_api_url, payload) response = opener.open(req) time.sleep(1) # the above line is a mycro bug workaround. it looks like the # only time dmsg's are double-processed is when they are sent # at the same time (including the second). sleeping a bit # seems to maybe fix it. they really should be examining dmsg # ids...