Using Google Latitude to print current location - UPDATED

May 23, 2013
Here goes the updated version of my old script.

Biggest change is Google Maps v3 don't require login anymore and there's no need for the geopy module. Feel free to add exceptions and other stuff.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# latitude.py
#
# Copyright (c) 2013 Ruslan Valiyev
# ruslan@valiyev.com
#
import simplejson as json
import time
import urllib2

#
# Get your Latitude user id
# Go to: http://www.google.com/latitude/apps/badge
# Look for the id in the badge html code eg.
# <iframe src="http://latitude.google.com/latitude/apps/badge/api?user=<YOUR_ID_WILL_BE_HERE>
#
GOOGLE_LATITUDE_KEY = '<YOUR_ID_WILL_BE_HERE>'
GOOGLE_LATITUDE_URL = 'http://www.google.com/latitude/apps/badge/api?user=%s&type=json'
GOOGLE_MAPS_URL = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=%s&sensor=false'

class GLatitude:
  def __init__(self, uid):
    # http://www.google.com/latitude/apps/badge/api?user=<YOUR_ID_WILL_BE_HERE>&type=json
    self.glatitude = json.load(
      urllib2.urlopen(GOOGLE_LATITUDE_URL % uid)
    )

  def coords(self):
    return self.glatitude['features'][0]['geometry']['coordinates']

  def timestamp(self):
    epoch = self.glatitude['features'][0]['properties']['timeStamp']
    human = time.strftime('%d %b %Y %H:%M', time.localtime(epoch))
    return human

class Gmaps:
  def __init__(self, i):
    self.gmaps = json.load(                                                                                                                                            
      urllib2.urlopen(GOOGLE_MAPS_URL % i)
    )
  def geoinfo(self):
    return self.gmaps['results'][0]['formatted_address']


def main():
  gl = GLatitude(GOOGLE_LATITUDE_KEY)
  location = gl.coords()[::-1]
  last_location_coords = ','.join(str(i) for i in location)
  last_time = gl.timestamp()
  gm = Gmaps(last_location_coords)

  return last_time, last_location_coords, gm.geoinfo()

if __name__ == '__main__':
  print main()
Sample output:

>>> import latitude
>>> latitude.main()
('23 May 2013 10:19', '46.2384963,6.1222947', 'Chemin du Pavillon 2, 1218 Grand-Saconnex, Switzerland')