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-2017 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.decode('utf-8')
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.decode('utf-8')
73 dict["type"] = "text/x-moz-place"
74 dict["uri"] = uri = b.href
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.decode('utf-8')
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, 'wb')
95 json.dump(self.dict, outfile)
100 infile = open(self.filename, 'rb')
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(bookmarks_dict.get("dateAdded"))
112 root_folder.comment = ''
113 root_folder.last_modified = convert_date_from_json(bookmarks_dict.get("lastModified"))
114 self.folder_stack = [root_folder]
115 self.current_folder = root_folder
117 if "type" not in bookmarks_dict:
118 bookmarks_dict["id"] = "0"
119 bookmarks_dict["title"] = ""
120 bookmarks_dict["type"] = "text/x-moz-place-container"
121 self.load_folder(root_folder, bookmarks_dict)
122 if self.folder_stack:
123 raise RuntimeError('Excessive folder stack: %s' % self.folder_stack)
127 def load_folder(self, folder, fdict):
128 if fdict["type"] != "text/x-moz-place-container":
129 raise ValueError("The object is not a Mozilla container")
131 folder.id = fdict["id"]
132 folder.guid = fdict.get("guid")
133 folder.index = fdict.get("index")
134 folder.root = fdict.get("root")
135 folder.name = encode_title(fdict["title"])
137 if "children" in fdict:
138 for record in fdict["children"]:
139 if record["type"] == "text/x-moz-place-container":
141 add_date=convert_date_from_json(record.get("dateAdded")),
142 comment=get_comment(record.get("annos")),
143 last_modified=convert_date_from_json(record.get("lastModified")))
144 folder.guid = record.get("guid")
145 self.current_folder.append(folder)
146 self.folder_stack.append(folder)
147 self.current_folder = folder
148 self.load_folder(folder, record)
150 elif record["type"] == "text/x-moz-place":
152 href=record["uri"].encode('utf-8'),
153 add_date=convert_date_from_json(record.get("dateAdded")),
154 last_modified=convert_date_from_json(record.get("lastModified")),
155 keyword=get_str(record, "keyword"),
156 comment=get_comment(record.get("annos")),
157 icon_href=record.get("iconuri"),
158 charset=get_str(record, "charset"))
159 bookmark.guid = record.get("guid")
160 bookmark.id = record["id"]
161 bookmark.index = record.get("index")
162 bookmark.name = encode_title(record["title"])
163 self.current_folder.append(bookmark)
165 elif record["type"] == "text/x-moz-place-separator":
167 ruler.add_date = convert_date_from_json(record.get("dateAdded"))
168 ruler.guid = record.get("guid")
169 ruler.id = record["id"]
170 ruler.index = record["index"]
171 ruler.last_modified = convert_date_from_json(record.get("lastModified"))
172 ruler.name = encode_title(record.get("title"))
173 ruler.comment = get_comment(record.get("annos"))
174 self.current_folder.append(ruler)
177 raise ValueError('Unknown record type "%s"' % record["type"])
179 del self.folder_stack[-1]
180 if self.folder_stack:
181 self.current_folder = self.folder_stack[-1]
183 self.current_folder = None
186 def convert_date_to_json(date):
188 date = int(float(date) * 10**6)
191 def convert_date_from_json(date):
198 def encode_title(title):
200 return title.encode("UTF-8", "xmlcharrefreplace")
203 def get_str(record, name):
205 return record[name].encode('utf-8')
208 def get_comment(annos):
213 if a["name"] == "bookmarkProperties/description":
214 return a["value"].encode('utf-8')
218 def make_annos(value, name="bookmarkProperties/description"):
223 "value": value.decode('utf-8')}]