-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport_bookmarks.py
executable file
·78 lines (62 loc) · 2.13 KB
/
import_bookmarks.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
#!/usr/bin/env python
import sys
import json
import sqlite3
import argparse
import conf
parser = argparse.ArgumentParser(
description="Import feeds from firefox json backup into sqlite database")
parser.add_argument("json_file", help="json file to import")
parser.add_argument("--import_folder", help="bookmark folder to import, e.g. /feed", default = "/")
parser.add_argument("--list-folders", help="list folders in backup and exit", action="store_true")
args = parser.parse_args()
bms = json.load(open(args.json_file))
def get_folder(bms, folder):
if len(folder) == 0:
return bms
find = folder.pop()
for child in bms['children']:
if child['title'] == find:
return get_folder(child, folder)
raise RuntimError("Could not find folder {}".format(find))
def list_folders(bms, path=""):
for child in bms['children']:
if 'children' in child:
cpath = path + "/" + child['title']
print(cpath)
list_folders(child, cpath)
if args.list_folders:
list_folders(bms)
sys.exit(0)
folder = get_folder(bms, [x for x in reversed(args.import_folder.split('/')) if len(x)])
def get_feeds(bms):
feeds = []
for child in bms['children']:
if 'children' in child:
feeds.extend(get_feeds(child))
else:
site_uri = None
feed_uri = None
for anno in child['annos']:
if anno['name'] == "livemark/siteURI":
site_uri = anno['value']
if anno['name'] == "livemark/feedURI":
feed_uri = anno['value']
if not feed_uri:
continue
feeds.append((child['title'], feed_uri, site_uri))
return feeds
feeds = get_feeds(folder)
print feeds
db = sqlite3.connect(conf.database)
c = db.cursor()
c.execute("""CREATE TABLE IF NOT EXISTS
feeds (id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
feed_uri TEXT NOT NULL,
site_uri TEXT,
active BOOL)
""")
c.executemany("INSERT INTO feeds VALUES (NULL,?,?,?,1)", feeds)
c.close()
db.commit()