Skip to content

Instantly share code, notes, and snippets.

@mplewis
Created July 14, 2013 03:54
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mplewis/5993157 to your computer and use it in GitHub Desktop.
Save mplewis/5993157 to your computer and use it in GitHub Desktop.
A script that downloads your Google Latitude data and saves it into separate JSON files. Check out my blog post on this: http://www.mplewis.com/octopress/blog/2013/07/13/downloading-your-google-location-history-with-the-google-api-python-client-library/
#!/usr/bin/python
import httplib2
import json
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run
from datetime import datetime
CLIENT_ID = 'YOUR_CLIENT_ID_HERE'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET_HERE'
REQUESTED_SCOPE = 'https://www.googleapis.com/auth/latitude.all.best'
if __name__ == '__main__':
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, REQUESTED_SCOPE)
storage = Storage('credentials.dat')
credentials = storage.get()
if credentials == None or credentials.invalid:
credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
latitude = build('latitude', 'v1', http=http)
last_time = None
num_reqs = 0
try:
while True:
response = latitude.location().list(granularity='best', max_time=last_time).execute()
try:
locations = response['items']
except KeyError:
break # done with list
num_reqs += 1
with open('req_%s.json' % num_reqs, 'wb') as f:
json.dump(locations, f)
last_time = int(locations[len(locations) - 1]['timestampMs']) - 1
print 'Request %s' % num_reqs
print datetime.fromtimestamp(last_time/1000.0)
except AccessTokenRefreshError:
print 'Access token refresh error. Please restart the application to reauthorize.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment