Casey Patton
Casey Patton

Reputation: 4091

Using Twitter API with Python (geocode)

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?

Upvotes: 3

Views: 4160

Answers (3)

A T
A T

Reputation: 13826

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

Upvotes: 0

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29965

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

Upvotes: 3

Raymond Hettinger
Raymond Hettinger

Reputation: 226171

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.

Upvotes: 4

Related Questions