Using Tweepy to Sort Tweets by Location

Using tweepy, we can search for tweets by location and for multiple locations, for example:

stream.filter(locations=[-122.75,36.8,-121.75,37.8,-74,40,-73,41], track=terms) 

However, when I try to put all tweets from NY on one list and tweets from SF on another list, I cannot assign them to any of the lists. This is my piece of code:

 NY = 74,40,-73,41 NewYorkCNN = [] if status.coordinates == NY or status.place == 'New York': for term in terms: if term in status.text: NewYorkCNN.append(term) 

How can they be correctly placed on the correct list?

+4
source share
1 answer

You probably already figured out the answer, but I also struggled with this, and after a while I found out a fairly simple solution.

This is how I check the coordinates and find out whether to assign tweet data to a UK or US database.

The "coordinates" keyword, followed by [1] or [2], gets the lat / long metadata from the tweet and compares it with the border assigned in the line below.

Hope this helps :)

 class CustomStreamListener(tweepy.StreamListener): def check_ukcoords(self, status): if status.coordinates is not None: lat = status.coordinates['coordinates'][1] lng = status.coordinates['coordinates'][0] return 49.7 < lat < 58.8 and -6.1 < lng < 1.7 def check_uscoords(self, status): if status.coordinates is not None: lat = status.coordinates['coordinates'][1] lng = status.coordinates['coordinates'][0] return 25.1 < lat < 49.1 and -125 < lng < -60.5 
+3
source

All Articles