Skip to content

Instantly share code, notes, and snippets.

@holgr
Forked from mdornseif/latitudedumper.py
Created July 13, 2011 21:46
Show Gist options
  • Save holgr/1081412 to your computer and use it in GitHub Desktop.
Save holgr/1081412 to your computer and use it in GitHub Desktop.
Moving Google Latitude Data from one user to an other
# Google latitude backup
#
# This is a little helper to backup your location from Google Latitude to local disk.
# It saves data as JSON and as GPX.
# Usage is somewhat annoying:
#
# 1. get OACurl http://code.google.com/p/oacurl/
# 2. follow the steps in http://code.google.com/apis/latitude/oacurl.html to get strarted
# You will have to register a domain with Google Apps and jump through hoops
# 3. Renerate a Key and a Secret for "installed applications" on the google API console,
# add them below as `consumerkey` and `consumersecret`.
# 4. Run this programm, - it will open a browser. Output will look like this:
# mooncrusher:Downloads md$ python lattitudemover.py
# 1000 : 2011-07-03 12:16:38 bis 2011-06-10 18:16:10
# 2000 : 2011-07-03 12:16:38 bis 2011-05-23 17:50:29
# 3000 : 2011-07-03 12:16:38 bis 2011-05-05 08:39:43
# {'data': {'kind': 'latitude#locationFeed'}}
# mooncrusher:Downloads md$
# 5. `latitude.json` now contains your location history.
# `latitude.gpx` contains a subset of that data easily importable in other systems
import os
import simplejson
import subprocess
import datetime
# needs to be filled in
consumerkey='xxx'
consumersecret='xxx'
# Login
cmd = 'java -cp oacurl-1.2.0.jar com.google.oacurl.Login --latitude --consumer-key=%s --consumer-secret=%s -P granularity=best -P location=all' % (consumerkey, consumersecret)
os.system(cmd)
locdata = {}
maxtime = 9000000000000
while True:
ret = subprocess.Popen(['java', '-cp', 'oacurl-1.2.0.jar', 'com.google.oacurl.Fetch',
'https://www.googleapis.com/latitude/v1/location' +
'?prettyprint=true' + '&max-results=1000' + '&granularity=best' + '&max-time=%s' % maxtime],
stdout=subprocess.PIPE).communicate()[0]
ret = simplejson.loads(ret)
if not ret.get('data').get('items'):
print ret
break
for loc in ret.get('data').get('items'):
locdata[loc['timestampMs']] = loc
print len(locdata), ":",
print datetime.datetime.fromtimestamp(int(max(locdata.keys()))/1000), "to",
print datetime.datetime.fromtimestamp(int(min(locdata.keys()))/1000)
maxtime = int(min(locdata.keys())) - 1
simplejson.dump(locdata, open('latitude.json', 'w'))
fd = open('latitude.gpx', 'w')
fd.write("""# <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><name>Google Latitude Export</name><trkseg>""")
lastloc = None
for key in sorted(locdata.keys()):
if lastloc:
timedelta = (int(locdata[key]['timestampMs']) - int(lastloc['timestampMs'])) / 1000 / 60
if timedelta > 10:
# no points for 10 Minutes, Start new Track
fd.write( "</trkseg><trkseg>")
fd.write( '<trkpt lat="%s" lon="%s">' % (locdata[key]['latitude'], locdata[key]['longitude']))
fd.write( '<time>%s</time></trkpt>' % str(datetime.datetime.fromtimestamp(int(locdata[key]['timestampMs'])/1000)).replace(' ', 'T') + 'Z')
lastloc = locdata[key]
fd.write( "</trkseg></trk></gpx>")
# Google latitude upload.
#
# Meant to push data read from latitudedumper.py into an other account.
# Usage is somewhat annoying:
#
# 1. use latitudedumper.py
# 2. log out of your google account, log into the other account, where you want the data to be uploaded to.
# 3. update `consumerkey` and `consumersecret` below.
# 3. call lattitudeloader.py
import os
import simplejson
import subprocess
import datetime
# needs to be filled in
consumerkey='xxx'
consumersecret='xxx'
# Login
cmd = 'java -cp oacurl-1.2.0.jar com.google.oacurl.Login --latitude --consumer-key=%s --consumer-secret=%s -P granularity=best -P location=all' % (consumerkey, consumersecret)
#os.system(cmd)
lastloc = None
locdata = simplejson.load(open('latitude.json', 'r'))
written = 0
for key in sorted(locdata.keys()):
data = locdata[key]
upload = simplejson.dumps(dict(data=data))
print datetime.datetime.fromtimestamp(int(data['timestampMs'])/1000)
if lastloc and lastloc['latitude'] == data['latitude']:
timedelta = (int(lastloc['timestampMs']) - int(data['timestampMs'])) / 1000 / 60
if timedelta < 8:
# we assume nothing changed
print "ignoring"
continue
# else: write to latitude
cmd = "echo %r | " % upload
cmd += ' '.join(['java', '-cp', 'oacurl-1.2.0.jar', 'com.google.oacurl.Fetch',
'-t', 'JSON', '-X POST',
'https://www.googleapis.com/latitude/v1/location'])
os.system(cmd)
written += 1
print len(locdata) - written
lastloc = data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment