DovarFalcone / google-takeout-location-parser

Easily parse location .json files provided by the Google Takeout service
MIT License
22 stars 3 forks source link

Fixed it. #3

Closed kimasplund closed 1 month ago

kimasplund commented 3 months ago

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])

def main(): root_dir = "Takeout/Location History/Semantic Location History" output_file = "semantic_location_history.csv"

with open(output_file, 'w', newline='', encoding='utf-8') as outfile:  # Specify encoding='utf-8'
    data_writer = csv.writer(outfile)
    data_writer.writerow(["timestamp", "latitude", "longitude", "address", "placeid", "name", "epoch_time"])

    for subdir, dirs, files in os.walk(root_dir):
        for file in files:
            if file.endswith(".json"):
                file_path = os.path.join(subdir, file)
                process_file(file_path, data_writer)

if name == "main": main() print('semantic_location_history.csv created in current directory')

DovarFalcone commented 3 months ago

Feel free to raise a PR so i can review changes and merge

DovarFalcone commented 1 month ago

closing