Was throwing some errors for me with unicode and keys so fixed that.
import os
import csv
import json
import datetime
def process_file(file_path, data_writer):
with open(file_path, encoding='utf-8-sig') as file:
data = json.load(file)
for obj in data['timelineObjects']:
if 'placeVisit' in obj:
location = obj['placeVisit']['location']
name = location.get('name', '') # Use .get() to avoid KeyError and provide a default value
address = location.get('address', '') # Use .get() to avoid KeyError and provide a default value
lat = location.get('latitudeE7', 0) / 107
lon = location.get('longitudeE7', 0) / 107
timestamp = obj['placeVisit']['duration'].get('startTimestamp', '')
placeid = location.get('placeId', '')
# Convert the timestamp to a datetime object
try:
datetime_obj = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
except ValueError:
try:
datetime_obj = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
except ValueError:
datetime_obj = None
# Convert the datetime object to Unix epoch time
if datetime_obj:
epoch_time = int(datetime_obj.timestamp())
else:
epoch_time = ''
# Write the data to the CSV file
data_writer.writerow([timestamp, lat, lon, address, placeid, name, epoch_time])
Was throwing some errors for me with unicode and keys so fixed that.
import os import csv import json import datetime
def process_file(file_path, data_writer): with open(file_path, encoding='utf-8-sig') as file: data = json.load(file) for obj in data['timelineObjects']: if 'placeVisit' in obj: location = obj['placeVisit']['location'] name = location.get('name', '') # Use .get() to avoid KeyError and provide a default value address = location.get('address', '') # Use .get() to avoid KeyError and provide a default value lat = location.get('latitudeE7', 0) / 107 lon = location.get('longitudeE7', 0) / 107 timestamp = obj['placeVisit']['duration'].get('startTimestamp', '') placeid = location.get('placeId', '')
def main(): root_dir = "Takeout/Location History/Semantic Location History" output_file = "semantic_location_history.csv"
if name == "main": main() print('semantic_location_history.csv created in current directory')