Tweepy location on Twitter API filter always throws 406 error

2.9k views Asked by At

I'm using the following code (from django management commands) to listen to the Twitter stream - I've used the same code on a seperate command to track keywords successfully - I've branched this out to use location, and (apparently rightly) wanted to test this out without disrupting my existing analysis that's running.

I've followed the docs and have made sure the box is in Long/Lat format (in fact, I'm using the example long/lat from the Twitter docs now). It looks broadly the same as the question here, and I tried using their version of the code from the answer - same error. If I switch back to using 'track=...', the same code works, so it's a problem with the location filter.

Adding a print debug inside streaming.py in tweepy so I can see what's happening, I print out the self.parameters self.url and self.headers from _run, and get:

{'track': 't,w,i,t,t,e,r', 'delimited': 'length', 'locations': '-121.7500,36.8000,-122.7500,37.8000'} 

/1.1/statuses/filter.json?delimited=length and {'Content-type': 'application/x-www-form-urlencoded'} respectively - seems to me to be missing the search for location in some way shape or form. I don't believe I'm/I'm obviously not the only one using tweepy location search, so think it's more likely a problem in my use of it than a bug in tweepy (I'm on 2.3.0), but my implementation looks right afaict.

My stream handling code is here:

    consumer_key = 'stuff'
    consumer_secret = 'stuff'
    access_token='stuff'
    access_token_secret_var='stuff'
    import tweepy
    import json

    # This is the listener, resposible for receiving data
    class StdOutListener(tweepy.StreamListener):
        def on_data(self, data):
            # Twitter returns data in JSON format - we need to decode it first
            decoded = json.loads(data)
            #print type(decoded), decoded
            # Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users
            try:
                user, created = read_user(decoded)
                print "DEBUG USER", user, created
                if decoded['lang'] == 'en':
                    tweet, created = read_tweet(decoded, user)
                    print "DEBUG TWEET", tweet, created
                else:
                    pass
            except KeyError,e:
                print "Error on Key", e
                pass
            except DataError, e:
                print "DataError", e
                pass

            #print user, created

            print ''
            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)
    #locations must be long, lat
    stream.filter(locations=[-121.75,36.8,-122.75,37.8], track='twitter')
2

There are 2 answers

0
Withnail On BEST ANSWER

The issue here was the order of the coordinates.

Correct format is: SouthWest Corner(Long, Lat), NorthEast Corner(Long, Lat). I had them transposed. :(

1
Praveen Mulchandani On

The streaming API doesn't allow to filter by location AND keyword simultaneously. you must refer to this answer i had the same problem earlier https://stackoverflow.com/a/22889470/4432830