forked from ivnvxd/arc-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (130 loc) · 5.39 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import datetime
import json
import os
def main() -> None:
data: dict = read_json()
html: str = convert_json_to_html(data)
write_html(html)
print("Done!")
def read_json() -> dict:
print("Reading JSON...")
filename: str = "StorableSidebar.json"
library_path: str = os.path.join(
os.path.expanduser("~/Library/Application Support/Arc/"), filename
)
try:
with open(filename, "r") as f:
print(f"> Found {filename} in current directory.")
data: dict = json.load(f)
except FileNotFoundError:
try:
with open(library_path, "r") as f:
print(f"> Found {filename} in Library directory.")
data: dict = json.load(f)
except FileNotFoundError:
print(
'> File not found. Look for the "StorableSidebar.json" '
'file within the "~/Library/Application Support/Arc/" folder.'
)
return data
def convert_json_to_html(json_data: dict) -> str:
spaces: dict = get_spaces(json_data["sidebar"]["containers"][1]["spaces"])
items: list = json_data["sidebar"]["containers"][1]["items"]
bookmarks: dict = convert_to_bookmarks(spaces, items)
html_content: str = convert_bookmarks_to_html(bookmarks)
return html_content
def get_spaces(spaces: list) -> dict:
print("Getting spaces...")
spaces_names: dict = {"pinned": {}, "unpinned": {}}
spaces_count: int = 0
n: int = 1
for space in spaces:
if "title" in space:
title: str = space["title"]
else:
title: str = "Space " + str(n)
n += 1
# Really the only way to tell if a space is pinned or not??
if isinstance(space, dict):
containers: list = space["newContainerIDs"]
for i in range(len(containers)):
if "pinned" in containers[i]:
spaces_names["pinned"][containers[i + 1]]: str = title
elif "unpinned" in containers[i]:
spaces_names["unpinned"][containers[i + 1]]: str = title
# containers: list = space["containerIDs"]
# for i in range(len(containers)):
# if containers[i] == "pinned":
# spaces_names["pinned"][containers[i + 1]]: str = title
# elif containers[i] == "unpinned":
# spaces_names["unpinned"][containers[i + 1]]: str = title
spaces_count += 1
print(f"> Found {spaces_count} spaces.")
return spaces_names
def convert_to_bookmarks(spaces: dict, items: list) -> dict:
print("Converting to bookmarks...")
bookmarks: dict = {"bookmarks": []}
bookmarks_count: int = 0
item_dict: dict = {item["id"]: item for item in items if isinstance(item, dict)}
def recurse_into_children(parent_id: str) -> list:
nonlocal bookmarks_count
children: list = []
for item_id, item in item_dict.items():
if item.get("parentID") == parent_id:
if "data" in item and "tab" in item["data"]:
children.append(
{
"title": item["data"]["tab"].get("savedTitle", ""),
"type": "bookmark",
"url": item["data"]["tab"].get("savedURL", ""),
}
)
bookmarks_count += 1
elif "title" in item:
child_folder: dict = {
"title": item["title"],
"type": "folder",
"children": recurse_into_children(item_id),
}
children.append(child_folder)
return children
for space_id, space_name in spaces["pinned"].items():
space_folder: dict = {
"title": space_name,
"type": "folder",
"children": recurse_into_children(space_id),
}
bookmarks["bookmarks"].append(space_folder)
print(f"> Found {bookmarks_count} bookmarks.")
return bookmarks
def convert_bookmarks_to_html(bookmarks: dict) -> str:
print("Converting bookmarks to HTML...")
html_str: str = """<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>"""
def traverse_dict(d: dict, html_str: str, level: int) -> str:
indent: str = "\t" * level
for item in d:
if item["type"] == "folder":
html_str += f'\n{indent}<DT><H3>{item["title"]}</H3>'
html_str += f"\n{indent}<DL><p>"
html_str = traverse_dict(item["children"], html_str, level + 1)
html_str += f"\n{indent}</DL><p>"
elif item["type"] == "bookmark":
html_str += f'\n{indent}<DT><A HREF="{item["url"]}">{item["title"]}</A>'
return html_str
html_str = traverse_dict(bookmarks["bookmarks"], html_str, 1)
html_str += "\n</DL><p>"
print("> HTML converted.")
return html_str
def write_html(html_content: str) -> None:
print("Writing HTML...")
current_date: str = datetime.datetime.now().strftime("%Y_%m_%d")
output_file: str = "arc_bookmarks_" + current_date + ".html"
with open(output_file, "w") as f:
f.write(html_content)
print(f"> HTML written to {output_file}.")
if __name__ == "__main__":
main()