How to add a location filter to tweepy module
Date : March 29 2020, 07:55 AM
Hope this helps The streaming API doesn't allow to filter by location AND keyword simultaneously. import sys
import tweepy
consumer_key=""
consumer_secret=""
access_key=""
access_secret=""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
if 'manchester united' in status.text.lower():
print status.text
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
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
sapi.filter(locations=[-6.38,49.87,1.77,55.81])
|
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The issue here was the order of the coordinates. Correct format is: SouthWest Corner(Long, Lat), NorthEast Corner(Long, Lat). I had them transposed. :(
|
Date : March 29 2020, 07:55 AM
help you fix your problem Core Problem: I think you're misunderstanding what the Stream is here. Tl;dr: Your code is working, you're just not doing anything with the data that gets back. __author__ = 'iamwithnail'
from django.core.management.base import BaseCommand, CommandError
from django.db.utils import DataError
from harvester.tools import read_both
import django_rq
class Command(BaseCommand):
args = '<search_string search_string>'
help = "Opens a listener to the Twitter stream, and tracks the given string or list" \
"of strings, saving them down to the DB as they are received."
def handle(self, *args, **options):
try:
import urllib3.contrib.pyopenssl
urllib3.contrib.pyopenssl.inject_into_urllib3()
except ImportError:
pass
consumer_key = '***'
consumer_secret = '****'
access_token='****'
access_token_secret_var='****'
import tweepy
import json
# This is the listener, responsible for receiving data
class StdOutListener(tweepy.StreamListener):
def on_data(self, data):
decoded = json.loads(data)
try:
if decoded['lang'] == 'en':
django_rq.enqueue(read_both, decoded)
else:
pass
except KeyError,e:
print "Error on Key", e
except DataError, e:
print "DataError", e
return True
def on_error(self, status):
print status
l = StdOutListener()
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret_var)
stream = tweepy.Stream(auth, l)
stream.filter(track=args)
myStreamListener = MyStreamListener() #creates an instance of your class
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener())
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
myStream = tweepy.Stream(api.auth,myStreamListener)
|
Tweepy Location Filter Does Not Work
Tag : python , By : Matt Leacock
Date : March 29 2020, 07:55 AM
Any of those help PROBLEM SOLVED, SEE SOLUTION IN THE ACCEPTED POST , From the docs:
|
Location filter in tweepy stream
Tag : python , By : user182203
Date : March 29 2020, 07:55 AM
this will help Try with the following GEOBOX_GERMANY = [5.0770049095, 47.2982950435, 15.0403900146, 54.9039819757]
|