Code Monkey home page Code Monkey logo

Comments (3)

rakeshk1987 avatar rakeshk1987 commented on June 14, 2024 2

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

from google-takeout-location-parser.

DovarFalcone avatar DovarFalcone commented on June 14, 2024

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.

from google-takeout-location-parser.

rakeshk1987 avatar rakeshk1987 commented on June 14, 2024

Closing the issue

from google-takeout-location-parser.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.