Created
April 3, 2011 15:58
-
-
Save aparrish/900513 to your computer and use it in GitHub Desktop.
simple twitter search api client
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
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