Thank you to @Alfe and @Hellsgate for their help and suggestions.
The following code works (Currently Hellsgate's code above returns geotagged tweets but doesn't filter by coordinates.)
Above the CustomStreamListener, you just need to add the import statements and the OAuth method. The code below will only return tweets from a box around the UK for the search terms "Rarity Diamonds,Applejack,Discord"
class CustomStreamListener(tweepy.StreamListener):
def check_coords(self, status):
if status.coordinates is not None:
lat = status.coordinates['coordinates'][1]
lng = status.coordinates['coordinates'][0]
return 49.7 < lat < 58.5 and -6 < lng < 2.1
def on_status(self, status):
if self.check_coords(status):
# if check_coords returns true, the following will run
try:
print "'tweet text'%s\n\ 'User Name't%s\n\ 'Created at't%s\n\ 'Created with't%s\n\ 'geo't%s\ 'coordinates't%s" % (status.text,
status.author.screen_name,
status.created_at,
status.source,
status.geo,
status.coordinates)
except Exception, e:
print >> sys.stderr, 'Encountered Exception:', e
pass
def on_error(self, status_code):
print >> sys.stderr, 'Encountered error with status code:', status_code
return True # Don't kill the stream
def on_timeout(self):
print >> sys.stderr, 'Timeout...'
return True # Don't kill the stream
streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)
"""For track=[""], put in words to search for. Commas separate individual terms IE"Applejack, Discord",
to search for tweets containing more than one term, separate keywords with a space. IE "Rarity Diamonds" """
streaming_api.filter(follow=None, track=["Rarity Diamonds,Applejack,Discord"])