-
Notifications
You must be signed in to change notification settings - Fork 3
/
journey2dayonejson.py
103 lines (79 loc) · 2.88 KB
/
journey2dayonejson.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import uuid
import os, glob
from datetime import datetime
from pytz import timezone
from markdownify import markdownify
from zipfile import ZipFile
import shutil
def getuuid():
uu = str(uuid.uuid4())
return uu.replace("-", "").upper()
def convert_unixtime(unixtime, timezone_str):
date = datetime.fromtimestamp(unixtime / 1000)
date = timezone(timezone_str).localize(date)
return date.astimezone(timezone("UTC")).strftime("%Y-%m-%dT%H:%M:%SZ")
def convert_photo(name, i):
md5, type = os.path.splitext(name)
type = type[1:] if len(type) else type
return {
"orderInEntry": i,
"identifier": getuuid(),
"type": type,
"isSketch": False,
"creationDevice": "Joe's MacBook Pro",
"md5": md5,
}
def journeyjson2dayonejson(journey):
dayone = {
"creationOSName": "macOS",
"creationOSVersion": "10.15.7",
"creationDevice": "Joe's MacBook Pro",
"creationDeviceType": "MacBook Pro",
"creationDate": convert_unixtime(journey["date_journal"], journey["timezone"]),
"timeZone": journey["timezone"],
"starred": False,
"uuid": getuuid(),
"text": markdownify(journey["text"], strip=["div"]).strip().replace("\\n\\n", "\\n").replace("\\n \\n", "\\n"),
}
if abs(journey["lat"]) < 90:
dayone["location"] = {
"longitude": journey["lon"],
"latitude": journey["lat"],
"placeName": journey["address"].strip(),
}
if journey["weather"]["degree_c"] < 100:
dayone["weather"] = {
"temperatureCelsius": journey["weather"]["degree_c"],
"conditionsDescription": journey["weather"]["description"],
}
if journey["tags"]:
dayone["tags"] = journey["tags"]
if journey["photos"]:
i = 0
dayone["photos"] = []
for f in journey["photos"]:
dayone["photos"].append(convert_photo(f, i))
i += 1
return dayone
if os.path.exists("./dayone"):
shutil.rmtree("./dayone")
os.makedirs("dayone/photos")
entries = []
for f in glob.glob("./journey/*.json"):
with open(f, "r") as fh:
journey = json.load(fh)
dayone = journeyjson2dayonejson(journey)
if journey["photos"]:
for (p, j) in zip(dayone["photos"], journey["photos"]):
source = "./journey/" + str(j)
target = "./dayone/photos/" + str(j)
shutil.copyfile(source, target)
dayone["text"] = dayone["text"] + "\n![](dayone-moment://{})".format(p["identifier"])
entries.append(dayone)
dayone_json = {"metadata": {"version": "1.0"}, "entries": entries}
with open("./dayone/Journey.json", "w") as fh:
fh.write(json.dumps(dayone_json, indent=4, separators=(",", ": ")).replace("/", r"\/"))
if os.path.exists("dayone.zip"):
os.remove("dayone.zip")
shutil.make_archive("dayone", "zip", "./dayone")