1 """Bookmarks storage manager - json
3 This file is a part of Bookmarks database and Internet robot.
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 2010-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
11 __all__ = ['storage_json']
17 import simplejson as json
19 from bkmk_objects import Folder, Bookmark, Ruler, Walker
22 class storage_json(Walker):
23 filename = "bookmarks_db.json"
25 def root_folder(self, f):
27 dict["children"] = children = []
28 self.folder_stack = [children]
30 def start_folder(self, f, level):
32 comment = getattr(f, 'comment')
33 if comment: dict["annos"] = make_annos(comment)
34 dict["children"] = children = []
35 dict["dateAdded"] = convert_date_to_json(f.add_date)
36 guid = getattr(f, 'guid')
37 if guid: dict["guid"] = guid
39 index = getattr(f, 'index')
40 if index is not None: dict["index"] = index
41 dict["lastModified"] = convert_date_to_json(f.last_modified)
42 root = getattr(f, 'root')
43 if root: dict["root"] = root
44 dict["title"] = f.name
45 dict["type"] = "text/x-moz-place-container"
47 self.dict["children"].append(dict)
49 self.folder_stack[-1].append(dict)
50 self.folder_stack.append(children)
52 def end_folder(self, f, level):
53 del self.folder_stack[-1]
55 def bookmark(self, b, level):
57 comment = getattr(b, 'comment')
58 if comment: dict["annos"] = make_annos(comment)
59 charset = getattr(b, 'charset')
60 if charset: dict["charset"] = charset
61 dict["dateAdded"] = convert_date_to_json(b.add_date)
62 guid = getattr(b, 'guid')
63 if guid: dict["guid"] = guid
65 iconuri = getattr(b, 'icon_href')
66 if iconuri: dict["iconuri"] = iconuri
67 index = getattr(b, 'index')
68 if index is not None: dict["index"] = index
69 keyword = getattr(b, 'keyword')
70 if keyword: dict["keyword"] = keyword
71 dict["lastModified"] = convert_date_to_json(b.last_modified)
72 dict["title"] = b.name
73 dict["type"] = "text/x-moz-place"
75 self.folder_stack[-1].append(dict)
77 def ruler(self, r, level):
79 comment = getattr(r, 'comment')
80 if comment: dict["annos"] = make_annos(comment)
81 dict["dateAdded"] = convert_date_to_json(r.add_date)
83 guid = getattr(r, 'guid')
84 if guid: dict["guid"] = guid
85 dict["index"] = r.index
86 dict["lastModified"] = convert_date_to_json(r.last_modified)
87 if r.name: dict["title"] = r.name
88 dict["type"] = "text/x-moz-place-separator"
89 self.folder_stack[-1].append(dict)
91 def store(self, root_folder):
92 root_folder.walk_depth(self)
94 outfile = open(self.filename, 'wt')
95 json.dump(self.dict, outfile)
100 infile = open(self.filename, 'rt')
101 bkmk_s = infile.read()
104 # Work around a bug in Mozilla - remove the trailing comma
105 bkmk_s = bkmk_s.strip().replace(',]', ']')
106 bookmarks_dict = json.loads(bkmk_s)
109 root_folder = Folder()
110 root_folder.header = ''
111 root_folder.add_date = convert_date_from_json(
112 bookmarks_dict.get("dateAdded"))
113 root_folder.comment = ''
114 root_folder.last_modified = convert_date_from_json(
115 bookmarks_dict.get("lastModified"))
116 self.folder_stack = [root_folder]
117 self.current_folder = root_folder
119 if "type" not in bookmarks_dict:
120 bookmarks_dict["id"] = "0"
121 bookmarks_dict["title"] = ""
122 bookmarks_dict["type"] = "text/x-moz-place-container"
123 self.load_folder(root_folder, bookmarks_dict)
124 if self.folder_stack:
125 raise RuntimeError('Excessive folder stack: %s'
130 def load_folder(self, folder, fdict):
131 if fdict["type"] != "text/x-moz-place-container":
132 raise ValueError("The object is not a Mozilla container")
134 folder.id = fdict["id"]
135 folder.guid = fdict.get("guid")
136 folder.index = fdict.get("index")
137 folder.root = fdict.get("root")
138 folder.name = fdict["title"]
140 if "children" in fdict:
141 for record in fdict["children"]:
142 if record["type"] == "text/x-moz-place-container":
144 add_date=convert_date_from_json(
145 record.get("dateAdded")),
146 comment=get_comment(record.get("annos")),
147 last_modified=convert_date_from_json(
148 record.get("lastModified")))
149 folder.guid = record.get("guid")
150 self.current_folder.append(folder)
151 self.folder_stack.append(folder)
152 self.current_folder = folder
153 self.load_folder(folder, record)
155 elif record["type"] == "text/x-moz-place":
158 add_date=convert_date_from_json(
159 record.get("dateAdded")),
160 last_modified=convert_date_from_json(
161 record.get("lastModified")),
162 keyword=get_str(record, "keyword"),
163 comment=get_comment(record.get("annos")),
164 icon_href=record.get("iconuri"),
165 charset=get_str(record, "charset"))
166 bookmark.guid = record.get("guid")
167 bookmark.id = record["id"]
168 bookmark.index = record.get("index")
169 bookmark.name = record["title"]
170 self.current_folder.append(bookmark)
172 elif record["type"] == "text/x-moz-place-separator":
174 ruler.add_date = convert_date_from_json(
175 record.get("dateAdded"))
176 ruler.guid = record.get("guid")
177 ruler.id = record["id"]
178 ruler.index = record["index"]
179 ruler.last_modified = convert_date_from_json(
180 record.get("lastModified"))
181 ruler.name = record.get("title")
182 ruler.comment = get_comment(record.get("annos"))
183 self.current_folder.append(ruler)
186 raise ValueError('Unknown record type "%s"'
189 del self.folder_stack[-1]
190 if self.folder_stack:
191 self.current_folder = self.folder_stack[-1]
193 self.current_folder = None
196 def convert_date_to_json(date):
198 date = int(float(date) * 10**6)
202 def convert_date_from_json(date):
210 def get_str(record, name):
216 def get_comment(annos):
221 if a["name"] == "bookmarkProperties/description":
227 def make_annos(value, name="bookmarkProperties/description"):