Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created April 3, 2011 15:58
Show Gist options
  • Save aparrish/900513 to your computer and use it in GitHub Desktop.
Save aparrish/900513 to your computer and use it in GitHub Desktop.
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