Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Twitter Stream by Location in Python

Tags:

python

twitter

I'm using the Python wrapper to the Twitter API (not tweepy, but maybe that is my problem). Also, I'm using the Spyder IDE. I can easily filter by track, by I am having trouble determining the exact syntax to filter by location. When I try:

auth = twitter.oauth.OAuth(OAUTH_TOKEN,OAUTH_TOKEN_SECRET,
                           CONSUMER_KEY,CONSUMER_SECRET)                         
twitter_api = twitter.Twitter(auth=auth)
twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)
stream = twitter_stream.statuses.filter(locations=[-6.38,49.87,1.77,55.81])

I get the error:

details: Location track items must be given as pairs of comma separated lat/longs:
[Ljava.lang.String;@1f1dbc92

I have tried several variations and nothing seems to work, such passing strings vs. floats. Can someone please explain how to do this correctly?

like image 974
airpierre Avatar asked Apr 19 '26 03:04

airpierre


1 Answers

Actually, after a bit of guesswork, apparently the locations parameter ought to be a string that contains comma-separated longitude/latitude pairs, with the first pair being the south-west corner and the second pair the north-east corner of the bounding box of interest. So in your example, you should write :

stream = twitter_stream.statuses.filter(locations="-6.38,49.87,1.77,55.81")

like image 181
Chiraz BenAbdelkader Avatar answered Apr 21 '26 01:04

Chiraz BenAbdelkader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!