DovarFalcone / google-takeout-location-parser

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

Getting an issue while using the 'semantic_location_parser.py' #2

Closed rakeshk1987 closed 11 months ago

rakeshk1987 commented 12 months ago

File "C:\Users\rakes\OneDrive\Documents\Repos\google-takeout-location-parser\semantic_location_parser.py", line 15, in process_file lat = obj['placeVisit']['location']['latitudeE7'] / 10**7


KeyError: 'latitudeE7'
PS C:\Users\rakes\OneDrive\Documents\Repos\google-takeout-location-parser> python semantic_location_parser.py
Traceback (most recent call last):
  File "C:\Users\rakes\OneDrive\Documents\Repos\google-takeout-location-parser\semantic_location_parser.py", line 47, in <module>
    main()
  File "C:\Users\rakes\OneDrive\Documents\Repos\google-takeout-location-parser\semantic_location_parser.py", line 44, in main
    process_file(file_path, data_writer)
  File "C:\Users\rakes\OneDrive\Documents\Repos\google-takeout-location-parser\semantic_location_parser.py", line 15, in process_file
    lat = obj['placeVisit']['location']['latitudeE7'] / 10**7
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
KeyError: 'latitudeE7'
DovarFalcone commented 12 months ago

I haven't run an export in a while, it looks like the script is failing when looking for the key 'latitudeE7'.

Check to make sure the json object still has that key. Try commenting out line 15 and see if you get a new error.

Lastly make sure your are running the .py file from the correct location so it knows where to find the Takeout/Location History/Semantic Location History folder.

rakeshk1987 commented 11 months ago

I was able to fix the issue by myself. For some data, the lat and lon was missing. I have added a Try Catch block to skip these records

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:
                try:
                    name = obj['placeVisit']['location']['name']
                except KeyError:
                    if obj['placeVisit']['location'].get('address') is None:
                        name = 'Missing'
                    else:
                        name = obj['placeVisit']['location']['address']                    
                try:
                    lat = obj['placeVisit']['location']['latitudeE7'] / 10**7                
                except KeyError as e:
                    lat = 'Missing'                
                try:
                    lon = obj['placeVisit']['location']['longitudeE7'] / 10**7
                except KeyError as e:
                    lon = 'Missing'
                try:
                    timestamp = obj['placeVisit']['duration']['startTimestamp']
                except KeyError as e:
                    timestamp = 'Missing'
                try:
                    address = obj['placeVisit']['location']['address']
                except KeyError as e:
                    address = 'Missing'
                try:
                    placeid = obj['placeVisit']['location']['placeId']
                except KeyError as e:
                    placeid = 'Missing'

                # Convert the timestamp to a datetime object
                try:
                    datetime_obj = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ')
                except ValueError:
                    datetime_obj = datetime.datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
                # Convert the datetime object to Unix epoch time
                epoch_time = int(datetime_obj.timestamp())

                # Write the data to the CSV file
                try:
                    data_writer.writerow([timestamp, lat, lon, address, placeid, name, epoch_time])
                except UnicodeEncodeError as e:
                    data_writer.writerow([timestamp, lat, lon, 'missing',placeid,'missing', epoch_time])

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

    with open(output_file, 'w', newline='') as outfile:
        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('\n',end="semantic_location_history.csv created in current directory" )
rakeshk1987 commented 11 months ago

Closing the issue