3

How could I make the following call in Python? Pseudocode version:

jsonTwitterResponse = twitter.get(up to max of 3 
                      tweets within 3km of longitude: 7, latitude: 5)
print jsonTwitterResponse

It looks like the geocode API is what I need. I have no idea how to actually code this up though. How would I do the above in actual code?

Casey Patton
  • 4,021
  • 9
  • 41
  • 54

3 Answers3

4

Here is a sample geocode request:

import urllib, json, pprint

params = urllib.urlencode(dict(q='obama', rpp=10, geocode='37.781157,-122.398720,1mi'))
u = urllib.urlopen('http://search.twitter.com/search.json?' + params)
j = json.load(u)
pprint.pprint(j)

The full Twitter REST API is described here: https://dev.twitter.com/docs/api Also, Twitter has a location search FAQ that may be of interest.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
3

In addition to Raymond Hettinger's answer, I'd like to mention that you can also use a query like "near:Amsterdam within:5km" if you don't want to work with actual coordinates.

Example: http://search.twitter.com/search?q=near:Amsterdam%20within:5km

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
0

I think this method might've been added more recently:

import urllib, json, pprint

params = urllib.urlencode(dict(lat=37.76893497, long=-122.42284884))
u = urllib.urlopen('https://api.twitter.com/1/geo/reverse_geocode.json?' + params)
j = json.load(u)
pprint.pprint(j)

Documentation: https://dev.twitter.com/docs/api/1/get/geo/reverse_geocode

A T
  • 13,008
  • 21
  • 97
  • 158