Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Brandon-Clark-00/1673b4d9c5d8575e3569650f6d74cad3 to your computer and use it in GitHub Desktop.
Save Brandon-Clark-00/1673b4d9c5d8575e3569650f6d74cad3 to your computer and use it in GitHub Desktop.
import json
import csv
import sys
import datetime
import os
def make_reader(in_json):
# Open location history data
json_data = json.loads(open(in_json).read())
# Get the easy fields
for item in json_data['locations']:
print(item['timestamp'])
try:
dt = datetime.datetime.strptime(item['timestamp'], '%Y-%m-%dT%H:%M:%S.%fZ')
except ValueError:
dt = datetime.datetime.strptime(item['timestamp'], '%Y-%m-%dT%H:%M:%SZ')
date = dt.strftime('%Y-%m-%d')
timestamp = dt.strftime('%H:%M:%S')
#date = datetime.datetime.fromtimestamp(
# item['timestamp']).strftime('%Y-%m-%d')
#timestamp = datetime.datetime.fromtimestamp(
# item['timestamp']).strftime('%H:%M:%S')
longitude = item['longitudeE7']/10000000.0
latitude = item['latitudeE7']/10000000.0
accuracy = item['accuracy']
yield [dt, date, timestamp, longitude, latitude, accuracy]
def getFullPath(inPath):
if(not os.path.isabs(inPath)):
# we need to set up the absolute path
script_path = os.path.abspath(__file__)
path, file = os.path.split(script_path)
inPath = os.path.join(path, inPath)
return inPath
# Read the Parameters
in_file = sys.argv[1]
out_file = sys.argv[2]
in_file = getFullPath(in_file)
out_file = getFullPath(out_file)
features = []
# add the Headers
features.append(['Datetime', 'Date', 'Time', 'Longitude', 'Latitude', 'Accuracy', 'Activity'])
print("Reading {0}".format(in_file))
reader = make_reader(in_file)
for r in reader:
features.append(r)
print('Read {0} Records'.format(len(features)-1))
# write this data
with open(out_file, 'w', newline='')as f:
writer = csv.writer(f)
writer.writerows(features)
@ljhicks87
Copy link

@Brandon-Clark-00 Hello, I just tried to use your fork of this script, but it failed, with:

2017-01-26T02:00:25.428Z
Traceback (most recent call last):
  File "<snip>/./ConvertGoogleHistory.py", line 57, in <module>
    for r in reader:
  File "<snip>/./ConvertGoogleHistory.py", line 29, in make_reader
    accuracy = item['accuracy']
KeyError: 'accuracy'

Upon investigation, it seems that the Records-003.json file I got from Google Takeout contains some entries with latitudeE7 and longitudeE7 values of 0, and no accuracy field.

Would you be able to fix the script to address this? Perhaps it could just discard any rows of data which aren't as expected (missing fields, 0 location, etc.).

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment