#! /usr/bin/env python # twitref: show recent refs to a search term. # Helpful for checking whether anyone's referred to your @handle recently. # No twitter authentication needed. # Copyright 2011 by Akkana Peck: # Please share, modify and enjoy under the GPLv2 or higher. import sys import simplejson, urllib, urllib2 # Set socket connection timeout shorter, since the app blocks # waiting for Twitter's sometimes very long timeouts: import socket socket.setdefaulttimeout(60) def get_search_data(query): s = simplejson.loads(urllib2.urlopen( urllib2.Request("http://search.twitter.com/search.json", urllib.urlencode({"q": query}))).read()) return s def json_search(query): for data in get_search_data(query)["results"]: yield data # optional main if __name__ == "__main__" : for searchterm in sys.argv[1:] : print "**** Tweets containing", searchterm statuses = json_search(searchterm) for st in statuses : print st['created_at'] print "<%s> %s" % (st['from_user'].encode('utf-8', 'backslashreplace'), st['text'].encode('utf-8', 'backslashreplace')) # st['id'] and st['id_str'] can be compared to see if # a tweet is new since we last checked. print ""