-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate-bookmarks-file
executable file
·217 lines (159 loc) · 5.58 KB
/
generate-bookmarks-file
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
#
# Extract my Bookmarks from Firefox and generate bookmarks.md file.
#
# https://developer.mozilla.org/en-US/docs/Mozilla/Tech/Places/Database
# https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsINavBookmarksService
import html
import os
import shutil
import sqlite3
import sys
from enum import Enum
DB_PATH = os.environ.get("TMPDIR", "/tmp") + "/places.sqlite"
FIREFOX_DB = os.environ["HOME"] + "/.mozilla/firefox/wje8tal1.default/places.sqlite"
class Bookmark:
def __init__(
self, mb_parent=None, mp_url=None, mp_title=None, mp_description=None, **kwargs
):
self.url = mp_url
self.title = mp_title
self.description = mp_description
if mb_parent in FOLDERS:
FOLDERS[mb_parent].append_bookmark(self)
def to_markdown(self):
return "[{}]({})".format(self.title or self.url, self.url)
def to_html(self):
return '<DT><A HREF="{}">{}</A></DT>'.format(
html.escape(self.url), html.escape(self.title, quote=False)
)
class Folder:
def __init__(self, mb_id=None, mb_parent=None, mb_title=None, **kwargs):
self.id = mb_id
self.title = mb_title
self.parent = mb_parent
self.folders = []
self.bookmarks = []
@property
def id(self):
return self._id
@id.setter
def id(self, mb_id):
self._id = mb_id
FOLDERS[mb_id] = self
@property
def parent(self):
return self._parent
@parent.setter
def parent(self, mb_parent):
self._parent = FOLDERS.get(mb_parent, None)
if self._parent:
FOLDERS[mb_parent].append_folder(self)
def append_bookmark(self, bookmark):
self.bookmarks.append(bookmark)
def append_folder(self, dir):
self.folders.append(dir)
def to_html(self):
return "<DT><H3>{}</H3></DT>".format(html.escape(self.title, quote=False))
def to_markdown(self):
return self.title
class FolderTitle(Enum):
ROOT = 1
MENU = 2
TOOLBAR = 3
TAGS = 4
UNFILED = 5
MOBILE = 6
class Separator:
def __init__(self, **kwargs):
...
class DynamicContainer:
def __init__(self, **kwargs):
...
FOLDERS = {}
TYPE2CLASS = [Bookmark, Folder, Separator, DynamicContainer]
def get_and_map_sql_data():
con = sqlite3.connect(DB_PATH, detect_types=sqlite3.PARSE_DECLTYPES)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(
"""
select
mb.id as mb_id,
mb.type as mb_type,
mb.parent as mb_parent,
mb.title as mb_title,
mp.url as mp_url,
mp.title as mp_title,
mp.description as mp_description
from moz_bookmarks as mb
left outer join moz_places as mp on mb.fk = mp.id
order by mb.type desc, mb.parent asc, mb.title asc
"""
)
for row in cur:
TYPE2CLASS[row["mb_type"] - 1](**row)
con.close()
def read_markdown(reader, parent_id=0, id=0):
lvls = [None] * 8
while line := reader.readline():
if line := line.strip().removeprefix("---"):
parts = line.partition(" ")
if parts[0].startswith("#"):
lvl = len(parts[0])
if 0 <= lvl - 1 < len(lvls):
parent_id = lvls[lvl - 1] or 0
Folder(mb_id=id, mb_parent=parent_id, mb_title=parts[2])
lvls[lvl] = id
id += 1
elif parts[0].startswith("-"):
url = parts[2]
title = ""
if url.startswith("["):
url = url[1:]
title = url[: url.find("]")]
url = url[url.rfind("(") + 1 :]
url = url.replace(")", "")
Bookmark(mb_parent=id - 1, mp_url=url, mp_title=title)
def generate_markdown(writer, dir, headline=1):
if headline == 7:
sys.exit("\nerror: max header number reached!")
elif headline == 2:
writer.write(f"\n## {dir.to_markdown()}\n\n---\n\n")
else:
writer.write("\n{} {}\n\n".format("#" * headline, dir.to_markdown()))
for bm in dir.bookmarks:
writer.write(f"- {bm.to_markdown()}\n")
for folder in dir.folders:
generate_markdown(writer, folder, headline + 1)
def _generate_netscape_bookmark_file_1(writer, dir):
writer.write(f"{dir.to_html()}\n<DL><p>\n")
for bm in dir.bookmarks:
writer.write(f"{bm.to_html()}\n")
for folder in dir.folders:
_generate_netscape_bookmark_file_1(writer, folder)
writer.write("</DL><p>\n")
def generate_netscape_bookmark_file_1(writer, dir):
head = """\
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks Menu</H1>
"""
writer.write(head)
_generate_netscape_bookmark_file_1(writer, dir)
writer.write("</DL>\n")
if __name__ == "__main__":
if len(sys.argv) == 2:
Folder(mb_id=FolderTitle.ROOT.value, mb_title="root")
with open(sys.argv[1], "r", encoding="utf-8") as reader:
read_markdown(
reader, id=FolderTitle.MENU.value, parent_id=FolderTitle.ROOT.value
)
with open("/dev/stdout", "w", encoding="utf-8") as writer:
generate_netscape_bookmark_file_1(writer, FOLDERS[FolderTitle.MENU.value])
else:
shutil.copy(FIREFOX_DB, DB_PATH)
get_and_map_sql_data()
with open("/dev/stdout", "w", encoding="utf-8") as writer:
generate_markdown(writer, FOLDERS[FolderTitle.MENU.value])
# vim: set ft=python :