Skip to content

Instantly share code, notes, and snippets.

@shweshi
Forked from devdattaT/ConvertGoogleHistory.py
Created May 20, 2024 16:32
Show Gist options
  • Save shweshi/00963c0ffdc8ed4343e9235a8077274d to your computer and use it in GitHub Desktop.
Save shweshi/00963c0ffdc8ed4343e9235a8077274d to your computer and use it in GitHub Desktop.

Revisions

  1. @devdattaT devdattaT created this gist Feb 13, 2019.
    57 changes: 57 additions & 0 deletions ConvertGoogleHistory.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    import json
    import csv
    import sys
    import datetime
    import os


    def make_reader(in_json):

    # Open location history data
    json_data = json.loads(open(in_json).read())

    # Get the easy fields
    for item in json_data['locations']:
    date = datetime.datetime.fromtimestamp(
    int(item['timestampMs'])/1000).strftime('%Y-%m-%d')
    timestamp = datetime.datetime.fromtimestamp(
    int(item['timestampMs'])/1000).strftime('%H:%M:%S')
    longitude = item['longitudeE7']/10000000.0
    latitude = item['latitudeE7']/10000000.0
    accuracy = item['accuracy']

    yield [date, timestamp, longitude, latitude, accuracy]


    def getFullPath(inPath):
    if(not os.path.isabs(inPath)):
    # we need to set up the absolute path
    script_path = os.path.abspath(__file__)
    path, file = os.path.split(script_path)
    inPath = os.path.join(path, inPath)
    return inPath


    # Read the Parameters
    in_file = sys.argv[1]
    out_file = sys.argv[2]

    in_file = getFullPath(in_file)
    out_file = getFullPath(out_file)

    features = []
    # add the Headers
    features.append(['Date', 'Time', 'Longitude', 'Latitude', 'Accuracy'])
    print("Reading {0}".format(in_file))

    reader = make_reader(in_file)

    for r in reader:
    features.append(r)

    print('Read {0} Records'.format(len(features)-1))

    # write this data
    with open(out_file, 'w', newline='')as f:
    writer = csv.writer(f)
    writer.writerows(features)