Skip to content

Instantly share code, notes, and snippets.

@devdattaT
Last active April 4, 2025 18:47
Show Gist options
  • Save devdattaT/c9dcae2107622215ff2e798dd185087e to your computer and use it in GitHub Desktop.
Save devdattaT/c9dcae2107622215ff2e798dd185087e to your computer and use it in GitHub Desktop.
import json
import csv
import sys
from datetime import datetime
import os
def has_keys(dictionary, keys):
return all(key in dictionary for key in keys)
def make_reader(in_json):
# Open location history data
json_data = json.loads(open(in_json).read())
#Will read the following keys
keys_to_check = ['timestamp', 'longitudeE7', 'latitudeE7', 'accuracy']
# Get the easy fields
for item in json_data['locations']:
if has_keys(item, keys_to_check):
timestamp = item['timestamp']
if ('.' in timestamp):
date = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ').date()
else:
date = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ').date()
tm = timestamp.split('T')[1].split('Z')[0]
longitude = item['longitudeE7']/10000000.0
latitude = item['latitudeE7']/10000000.0
accuracy = item['accuracy']
yield [date, tm, 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(['Date', 'Time', 'Longitude', 'Latitude', 'Accuracy'])
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)
@epk
Copy link

epk commented Dec 30, 2024

@sitz
Copy link

sitz commented Jan 1, 2025

As of Dec 31, 2024 location timeline export on Android - the JSON data format has changed and needs a chance in make_reader() function, which I added to my fork. Please free to pull it to sync if needed. Thanks for bootstrapping this script.

@fab343
Copy link

fab343 commented Feb 6, 2025

@sitz I tried to run your code but I get a KeyError: 'semanticSegments'.

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