#!/usr/bin/python import sys, urllib, urllib2, simplejson, time, logging, base64, BeautifulSoup TWITTER_EMAIL = "" TWITTER_PASSWORD = "" if TWITTER_EMAIL == "": print "Fill in your Twitter email address" sys.exit(0) if TWITTER_PASSWORD == "": print "Fill in your Twitter password" sys.exit(0) logging.basicConfig(level=logging.DEBUG) friends_url = 'http://twitter.com/statuses/friends.json?page=%s' page = 0 def whoisi(endpoint, **kw): base = 'http://whoisi.com/api/' arg = None if kw and len(kw): arg = '?' + urllib.urlencode(kw) u = urllib.urlopen(base + endpoint + arg) return simplejson.loads(u.read()) # get the person for a url def checkWhoisi(twitterfriend): name = twitterfriend["name"] logging.debug("Looking for Twitter person %s on whoisi" % name) twitterurl = "http://twitter.com/%s" % twitterfriend["screen_name"] data = whoisi("getPersonForURL", app="sample", url=twitterurl) if data["person"] is None: print "Go add %s to whoisi at http://whoisi.com/addform?name=%s&url=%s" % (name, urllib.quote_plus(name), urllib.quote_plus(twitterurl)) else: whoisiurl = "http://whoisi.com/p/%s" % data["person_id"] print "Found %s on whoisi; follow them at %s" % (data["person"]["name"], whoisiurl) time.sleep(1) def grabTwitterFriendsAndParse(): page = 0 while 1: page += 1 logging.debug("Fetching page %s of Twitter friends" % page) req = urllib2.Request(friends_url % page) req.add_header("Authorization", "Basic %s" % base64.encodestring('%s:%s' % (TWITTER_EMAIL, TWITTER_PASSWORD))[:-1]) try: fp = urllib2.urlopen(req) except: print "Error from Twitter" sys.exit() thesefriends = simplejson.loads(fp.read()) fp.close() if len(thesefriends) == 0: break logging.debug("Only checking your first five friends: remove [:5] to check them all") for friend in thesefriends[:5]: checkWhoisi(friend) logging.debug("Only checking the first page of friends: remove this line to run for real"); return time.sleep(1) if __name__ == "__main__": grabTwitterFriendsAndParse()