I wanted to write an example of plotting geo data with maps but I didn't have any good example data. Having played around with Twitter's API before, I decided to use that. There are several Python libraries for interfacing with Twitter but to use the Twitter Search API you actually don't need any of them. Since the result is returned in JSON you just need something like simplejson (or the built in JSON modules in later versions of Python).
Here is a simple example (twitter_geo.py) that searches for tweets with a query string and/or a geographic location. Pretty simple.
Here is a simple example (twitter_geo.py) that searches for tweets with a query string and/or a geographic location. Pretty simple.
import urllib2
import simplejson as json
def search(q='',geo=''):
""" use search api to find tweets matching a query string and/or
location as string of "lat,lon,radius", see api documentation"""
query = 'http://search.twitter.com/search?q=%s&format=json&rpp=100&result_type=recent&geocode=%s' % (q,geo)
f = urllib2.urlopen(query)
r = json.loads(f.read())
return r["results"]
def parse(res):
""" take the relevant parts of the result"""
#list of tuples (user,time,geo)
return [(r['from_user'],r['created_at'],r['geo'])
for r in res if r['geo'] != None]
if __name__ == "__main__":
tag = '%23fail'
geo = '55.583333,13.033333,100km' #Malmo, Sweden
#query Search API
print parse(search(q=tag,geo=geo))