Instantly share code, notes, and snippets.

Embed
What would you like to do?
simple twitter search api client
import json
import urllib
import time
def search_twitter(query, callback, maxpages=10):
resp = urllib.urlopen('http://search.twitter.com/search.json%s' % query)
data = json.loads(resp.read())
for item in data['results']:
callback(item)
if 'next_page' in data and maxpages > 1:
time.sleep(0.5)
search_twitter(data['next_page'], callback, maxpages - 1)
if __name__ == '__main__':
import sys
query = '?' + urllib.urlencode({'q': ' '.join(sys.argv[1:]), 'rpp': 100})
def tester(item):
print item['text'].encode('ascii', 'replace')
search_twitter(query, tester)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment