simple python script and fun with GeoAPI

I had my first opportunity yesterday to play around with the GeoAPI geo-referencing service. Although not quite as intuitive as Google Map’s forward and reverse geocoding services, GoeAPI gives you immediate access to geo-located feeds from Twitter, Flickr, and YouTube, plus the ability to create custom neighborhoods (what they called “guids”) for you to query.

Sounds nerdy – so here’s the English translation:

You now can, with a few lines of code, get Tweets happening real-time in your neighborhood.

Fun? I guess it depends on where you live. Mostly what I’ve been finding is that my neighbors are kind of pervs.

Here’s a quick and dirty Python code sample for you to get started. You’ll need your own API key first.

Also, you’ll need to know the latitude and longitude of a point in the neighborhood you’re interested in. Try (37.766 -122.417) for the Mission. Also, because sometimes you get garbage for location data (like Where: !!!!Frisco U noe!!!!), I’ve add a quick parser to find correctly formatted latitude and longitude pairs. Plenty more you can do with this with a few minutes of time.

– Nerdy Part –

import sys, os, json, urllib, re

#usage: python get_tweets.py [lat] [lng]

lat = sys.argv[1]
lng = sys.argv[2]

# Regex to find properly formatted lat-long pairs in the where response.

lat_lng_re = re.compile('^[\D]*[^-^\d](?P[\d\.-]+)\s*\,\s*(?P[\d\.-]+).*')
api = '[YOUR API KEY]'

parent_request = 'http://api.geoapi.com/v1/parents?lat=%s&lon=%s&apikey=%s' % (lat,lng,api)
response = urllib.urlopen(parent_request).read()

json1 = json.loads(response)

# Find your neighborhood 

guid = json1['result']['parents'][0]['guid']
print "\n\nYour neighborhood: %s\n\n" % json1['result']['parents'][0]['meta']['name']
print "--TWEETS--\n\n"

# Get Tweets in your neighborhood

tweets_request = 'http://api.geoapi.com/v1/e/%s/view/twitter?apikey=%s' % (guid,api)

response = urllib.urlopen(tweets_request).read()

json2 = json.loads(response)

for tweet in json2['result']:
	print "Tweet: %s" % tweet['text']
	print "When: %s" % tweet['created_at']
	loc = lat_lng_re.match(tweet['location'])
	if loc:
		print "Lat: %s Lng: %s" % (loc.group('lat'),loc.group('lng'))
	else:
		print "Unusable location: %s" % tweet['location']
	print "User: %s" % tweet['from_user']
	print "Profile image url: %s\n" % tweet['profile_image_url']

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

One Response to “simple python script and fun with GeoAPI”

  1. Thanks, very good example

Leave a Comment