Created
November 11, 2019 00:13
-
-
Save daragao/b215557abc65fa1cb158d91a4b4605d7 to your computer and use it in GitHub Desktop.
Parses history location from https://takeout.google.com/ (useful to check where you have been in the last few years)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from datetime import datetime | |
import reverse_geocoder as rg | |
location_history = None | |
json_filename = 'timeline_history/Location History/Location History.json' | |
print('Loading: %s' % json_filename) | |
with open(json_filename, 'r') as f: | |
location_history = json.load(f) | |
location = location_history["locations"] | |
total_locations = len(location) | |
print('Parsing Locations(%s)' % f'{total_locations:,}') | |
# {'timestampMs': '1367498078066', 'latitudeE7': 515236996, 'longitudeE7': -826260, 'accuracy': 39} | |
def parse_location_object(location_obj): | |
# print(location_obj) | |
latitudeE7 = location_obj['latitudeE7'] | |
longitudeE7 = location_obj['longitudeE7'] | |
lat = int(latitudeE7) / (10**7) | |
lng = int(longitudeE7) / (10**7) | |
# print('%s\tlat: %f\tlng: %f' % (datetime_str, lat, lng)) | |
return (lat, lng) | |
# location_value = rg.search(((lat, lng))) | |
# print(location_value) | |
#date_str, geo_coord = parse_location_object(location[0]) | |
#print(date_str, geo_coord) | |
#date_str, geo_coord = parse_location_object(location[-1]) | |
#print(date_str, geo_coord) | |
print('ETL of coordinates and dates') | |
geo_coords = [] | |
for loc in location: | |
coordinates = parse_location_object(loc) | |
geo_coords.append(coordinates) | |
print('GeoReverse coordinates') | |
location_value = rg.search(geo_coords) | |
print('Generate list with days and countries') | |
results = [] | |
previous_cc = None | |
for i in range(len(location_value)): | |
if(previous_cc != location_value[i]['cc']): | |
loc = location[i] | |
timestampMs = int(loc['timestampMs']) | |
date_obj = datetime.fromtimestamp(int(timestampMs / 1000)) | |
cc = location_value[i]['cc'] | |
previous_cc = cc | |
if(len(results) > 0): | |
days = (date_obj - results[-1][2]).days | |
results[-1][1] = days | |
results.append([timestampMs, -1, date_obj, cc]) | |
# filter place with 0 days spent | |
results = list(filter(lambda v: v[1] != 0, results)) | |
# join adjacent same country rows | |
tmp_results = [] | |
for r in results: | |
if(len(tmp_results) > 0 and r[3] == tmp_results[-1][3]): | |
tmp_results[-1][1] = tmp_results[-1][1] + r[1] | |
else: | |
tmp_results.append(r) | |
for v in tmp_results: | |
print('%d\t%d\t%s\t%s' % tuple(v)) | |
''' | |
print('Print dates and country code') | |
previous_cc = None | |
for i in range(len(location_value)): | |
if(previous_cc != location_value[i]['cc']): | |
loc = location[i] | |
timestampMs = loc['timestampMs'] | |
date_str = datetime.fromtimestamp(int(int(timestampMs) / 1000)) | |
cc = location_value[i]['cc'] | |
admin1 = location_value[i]['admin1'] | |
admin2 = location_value[i]['admin2'] | |
name = location_value[i]['name'] | |
print('%s\t%s\t%s\t%s\t%s\t%s' % (timestampMs, date_str, cc, admin1, admin2, name)) | |
previous_cc = cc | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment