-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsetimelinedata.py
63 lines (49 loc) · 1.99 KB
/
parsetimelinedata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sys
import json
import datetime
import closestgeoname.closestgeoname as closestgeoname
# couldn't figure out how to get closestgeoname.py to read constants.py
# when operating as a submodule, so specifying DBFILENAME directly instead
DBFILENAME = './closestgeoname/geonames.sqlite'
# take command line input as a file (assumed to be json), open it,
# read it, load it, and then close the file
with open(sys.argv[1], "r") as file:
data = json.load(file)
timeline_objects = data["timelineObjects"]
places = {}
for i in timeline_objects:
lat = ""
long = ""
# parse the object, depending on its type
if "activitySegment" in i:
entry = i["activitySegment"]
lat = entry["endLocation"]["latitudeE7"]/(10**7)
long = entry["endLocation"]["longitudeE7"]/(10**7)
elif "placeVisit" in i:
entry = i["placeVisit"]
lat = entry["location"]["latitudeE7"]/(10**7)
long = entry["location"]["longitudeE7"]/(10**7)
# get the location
localdb_response = closestgeoname.query_closest_city(DBFILENAME, lat, long)
if localdb_response is not None:
place = str(localdb_response[0]) + ", " + str(localdb_response[2])
# find the time spent
start_time = datetime.datetime.fromisoformat(entry["duration"]["startTimestamp"])
end_time = datetime.datetime.fromisoformat(entry["duration"]["endTimestamp"])
duration = end_time - start_time
# add the entry
if place in places:
places[place] += duration
else:
places[place] = duration
else:
print(f"could not find the location for {lat}, {long}; skipping entry")
for place in places:
print(place, ":", places[place])
def years_months_weeks_hours_days_minutes(td):
# check conversion code
years, remainder = divmod(td.days, 365)
months, remainder = divmod(remainder, 30.4)
weeks, days = divmod(remainder, 7)
hours, remainder = divmod(td.seconds, 3600)
minutes, seconds = divmod(remainder, 60)