made a collection of twitter api solutions
[twitter-api-cdsw-solutions] / solution-geo-3.py
1 # Set up a bounding box around Times Square and around NYC as a whole.
2
3 # Alter the streaming code to include a "locations" filter. You need
4 # to use the order sw_lng, sw_lat, ne_lng, ne_lat for the four
5 # coordinates.
6
7 # Note: to answer this, I used this website to find a good box:
8 # http://boundingbox.klokantech.com/
9
10 import encoding_fix
11 import tweepy
12 from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
13
14 auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
15 auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
16
17 api = tweepy.API(auth)
18
19 class StreamListener(tweepy.StreamListener):
20     def on_status(self, tweet):
21         print(tweet.author.screen_name + "\t" + tweet.text)
22
23     def on_error(self, status_code):
24         print('Error: ' + repr(status_code))
25         return False
26
27 l = StreamListener()
28 streamer = tweepy.Stream(auth=auth, listener=l)
29
30 # This should grab tweets in Times Square /and/ NYC as a whole
31 streamer.filter(locations=[-73.9864799803,40.7575460197,-73.9837820197,40.7602439803,
32                           -74.25909,40.477399,-73.700171,40.917577])
33

Benjamin Mako Hill || Want to submit a patch?