Created
April 5, 2022 09:05
-
-
Save skrobul/bd1f64309123c34bf5cfcad144e66572 to your computer and use it in GitHub Desktop.
convert google timeline location history into owntracks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
from datetime import datetime | |
print("Parsing input data...") | |
records = json.load(open("./Takeout/Location History/Records.json"))['locations'] | |
print("Done.") | |
def convert_conn(record): | |
MAPPING = { | |
'WIFI': 'w', | |
'wifi': 'w', | |
'CELL': 'm', | |
'cell': 'm', | |
'network': 'm' | |
} | |
source = record['source'] | |
if source in MAPPING: | |
return MAPPING[source] | |
else: | |
return None | |
def convert_velocity(record): | |
if 'velocity' not in record: | |
return 0 | |
return record['velocity'] * 3.6 | |
def parse_timestamp(record): | |
try: | |
ts = datetime.strptime(record['timestamp'], "%Y-%m-%dT%H:%M:%S.%f%z") | |
except ValueError: | |
ts = datetime.strptime(record['timestamp'], "%Y-%m-%dT%H:%M:%S%z") | |
return ts | |
def convert_timestamp(record): | |
ts = parse_timestamp(record) | |
return int(ts.timestamp()) | |
def short_timestamp(record): | |
ts = parse_timestamp(record) | |
return ts.strftime("%Y-%m-%dT%H:%M:%SZ") | |
def year_month(record): | |
ts = parse_timestamp(record) | |
return ts.strftime("%Y-%m.rec") | |
output = [] | |
months = {} | |
for record in records: | |
# print(record) | |
timestamp = convert_timestamp(record) | |
payload = { | |
"_type": "location", | |
"lat": record['latitudeE7'] / 10**7, | |
"lon": record['longitudeE7'] / 10**7, | |
"acc": record['accuracy'], | |
"alt": record.get('altitude', None), | |
"tst": timestamp, | |
"created_at": timestamp, | |
"conn": convert_conn(record), | |
"vac": record.get('verticalAccuracy', None), | |
"vel": convert_velocity(record), | |
"tid": "l6" | |
} | |
payload = { k:v for k,v in payload.items() if v != None } | |
entry = "{}\t* \t{}\n".format(short_timestamp(record), json.dumps(payload, separators=(',', ':'))) | |
filename = year_month(record) | |
if filename not in months: | |
months[filename] = [] | |
months[filename].append(entry) | |
print("Creating output files") | |
for filename, data in months.items(): | |
print("Processing {}".format(filename)) | |
with open("output/{}".format(filename), "w") as outfile: | |
for entry in data: | |
outfile.write(entry) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment