made a collection of twitter api solutions
[twitter-api-cdsw-solutions] / solution-geo-4.py
1 # Do "static" (i.e., not using the streaming API) geolocation search
2 # using code like this: d = api.search(geocode='37.781157,-122.398720,1mi')
3
4 import encoding_fix
5 import tweepy
6 from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
7 import time
8
9 auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
10 auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
11
12 api = tweepy.API(auth)
13
14 # 100 is the maximum number taht can be returned according to:
15 # https://dev.twitter.com/rest/reference/get/search/tweets
16
17 counter = 0
18 for page in tweepy.Cursor(api.search, "party", geocode='37.781157,-122.398720,1mi',  count=100).pages():
19     counter = counter + len(page)
20     for tweet in page:
21         print(tweet.user.screen_name + "\t" + str(tweet.created_at) + "\t" + tweet.text)
22     # end this loop if we've gotten 1000
23     if counter == 1000:
24         break
25
26     # This page suggests we can do one request every 5 seconds:
27     # https://dev.twitter.com/rest/reference/get/search/tweets
28     time.sleep(5)

Benjamin Mako Hill || Want to submit a patch?