Geolocated tweets with TwitterAPI


#1

I’m trying to extract tweets around a certain location using the 30day or full archive data from Twitter. For that I’m using the TwitterAPI library, but I keep on getting an error when constraining to location.

from TwitterAPI import TwitterAPI

api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)

PRODUCT = '30day'
LABEL = 'development'
SEARCH_TERM = 'JFK'


r = api.request('tweets/search/%s/:%s' % (PRODUCT, LABEL),
                {'query':SEARCH_TERM, 'point_radius':'[-73.7781 40.6413 2km]'})

for item in r:
    print(item['text'] if 'text' in item else item)

I always get the same error but I can’t figure out what am I doing wrong.

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/Fil/PycharmProjects/Twitter/importtweets.py
Traceback (most recent call last):
  File "/Users/Fil/PycharmProjects/Twitter/importtweets.py", line 18, in <module>
    for item in r:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/TwitterAPI/TwitterAPI.py", line 219, in __iter__
    return self.get_iterator()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/TwitterAPI/TwitterAPI.py", line 206, in get_iterator
    raise TwitterRequestError(self.response.status_code)
TwitterAPI.TwitterError.TwitterRequestError: Twitter request failed (422)

Process finished with exit code 1

It would even be better to this with R as I’m a complete newbie in Python, but I’ll use what I have.


#2

422 error means the parameters are incorrect, https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search.html#HTTPCodes

Also point_radius coordinates are lat long last time I checked, so if you want JFK the airport, that should that be 40.6413 -73.7781 (please double check that though because I have a feeling i’m wrong, also I can’t remember if you need commas like [-73.7781,40.6413,2km]) point_radius is [long lat radius]


#3

Unfortunately that’s not fully correct. The point_radius uses lon lat radius without commas.
https://developer.twitter.com/en/docs/tweets/search/guides/premium-operators.
I tried to use the geocode parameter but it only works for the standard queries.


#4

Ah, that’s the docs page - thanks.

I can’t try the query myself to test, but looks like you’re using this library? https://github.com/geduldig/TwitterAPI

I think it might be that point_radius parameter has to be included together with query - does this work?

SEARCH_TERM = 'JFK point_radius:[-73.7781 40.6413 2km]'

the other parameters that don’t go in the query, but might go exactly where you’ve put point_radius are toDate, fromDate, maxResults, next from https://developer.twitter.com/en/docs/tweets/search/api-reference/premium-search


#5

Thank you so much. That was indeed the problem. I wasn’t including it with the query :). That actually solved the issue both for python and for R which is awesome.


#6

Great! Incidentally, if you’ve got a working example of the premium Search API working with R and it’s public, please post it somewhere! Quite a few people have been looking for that.


#7

I’ll add a new topic on this but here’s the code https://github.com/FilipeamTeixeira/TweetAPI-premium/blob/master/TweetAPI_premium.R.