0

I'm trying to search tweets by geo-location. I'm trying to get tweets in New York using:

twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

geocode = "42.3482° N, 75.1890° W"

search_results = twitter.search_geo(count=10,geocode=geocode)

   try:
       for tweet in search["statuses"]:
       print(tweet ['text'])

it returns an error

Traceback (most recent call last):
  File "D:\Projects\M Tools\Twython\My works\new.py", line 18, in <module>
    search_results = twitter.search_geo(count=10,geocode=geocode)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\endpoints.py", line 818, in search_geo
    return self.get('geo/search', params=params)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 263, in get
    return self.request(endpoint, params=params, version=version)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 257, in request
    api_call=url)
  File "C:\Python34\lib\site-packages\twython-3.2.0-py3.4.egg\twython\api.py", line 198, in _request
    retry_after=response.headers.get('X-Rate-Limit-Reset'))
twython.exceptions.TwythonError: Twitter API returned a 400 (Bad Request), You must provide valid coordinates, IP address, query, or attributes.
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Nicole W.
  • 11
  • 4

2 Answers2

0

The geocode parameter should be similar to the following in the eventual Twitter search call:

41.8734,-70.6394,5mi

According to the call in the Twitter API console (https://dev.twitter.com/rest/tools/console)

Returns tweets by users located within a given radius of the given latitude/longitude. The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile. The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers). Note that you cannot use the near operator via the API to geocode arbitrary locations; however you can use this geocode parameter to search near geocodes directly. A maximum of 1,000 distinct "sub-regions" will be considered when using the radius modifier.

Phil
  • 2,797
  • 1
  • 24
  • 30
0

In Twython, the method search_geo is used to find place_id for 'places' that are within radius of the provided geolocation. You need to use the search method like so:

from twython import Twython, TwythonError


app_key = 'your_app_key'
app_secret = 'your_app_secret'
oauth_token = 'your_oauth_tokem'
oauth_token_secret = 'your_oauth_secret'

twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

geocode = '42.3482,75.1890,1mi' # latitude,longitude,distance(mi/km)

search_results = twitter.search(count=10, geocode=geocode)

try:
    for tweet in search_results['statuses']:
        print (tweet['text'])
except TwythonError as e:
    print(e)
mbeacom
  • 1,466
  • 15
  • 25