Created
January 6, 2010 21:34
-
-
Save edwardgeorge/270692 to your computer and use it in GitHub Desktop.
show changes in twitter followers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Script to inform you of changes to your twitter followers. | |
ie: who just unfollowed you. | |
""" | |
from __future__ import with_statement | |
import bsddb | |
import contextlib | |
import functools | |
import pickle | |
import httplib2 | |
#import lxml | |
import simplejson | |
USERNAME = 'edwardgeorge' | |
DBFILE = '.twitterdb' | |
def getfollowers(username): | |
httpc = httplib2.Http() | |
resp, content = httpc.request('http://twitter.com/followers/ids/%s.json' % username, 'GET') | |
assert resp.status == 200 | |
data = simplejson.loads(content) | |
return data | |
def getdiffandstore(db, key, username, data): | |
dbkey = '%s:%s' % (key, username) | |
existed = False | |
additions, removals = [], [] | |
if dbkey in db: | |
existed = True | |
olddata = pickle.loads(db[dbkey]) | |
olddata = set(olddata) | |
newdata = set(data) | |
additions = newdata - olddata | |
removals = olddata - newdata | |
else: | |
print 'no previous data found (%s)' % dbkey | |
db[dbkey] = pickle.dumps(data) | |
return additions, removals, existed | |
def getuserinfo(userid, cache): | |
dbkey = 'user:%d' % userid | |
if dbkey in cache: | |
return cache[dbkey] | |
try: | |
httpc = httplib2.Http() | |
resp, content = httpc.request('http://twitter.com/users/show/%s.json' % userid, 'GET') | |
assert resp.status == 200 | |
data = simplejson.loads(content) | |
screen_name = data['screen_name'] | |
cache[dbkey] = screen_name | |
return screen_name | |
except Exception, e: | |
return 'user_id: %d' % userid | |
if __name__ == '__main__': | |
with contextlib.closing(bsddb.hashopen(DBFILE, 'c')) as db: | |
data = getfollowers(USERNAME) | |
additions, removals, existed = getdiffandstore(db, 'followers', USERNAME, data) | |
if existed: | |
getuserinfo = functools.partial(getuserinfo, cache=db) | |
if removals: | |
print '\n'.join(map(lambda i: '-%s' % i, map(getuserinfo, removals))) | |
if additions: | |
print '\n'.join(map(lambda i: '+%s' % i, map(getuserinfo, additions))) | |
db.sync() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment