forked from shangma/Bookmark-Manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookmarkChrome.py
143 lines (121 loc) · 3.47 KB
/
bookmarkChrome.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
'''
Author : Jay Rambhia
email : [email protected]
Git : https://github.com/jayrambhia
gist : https://gist.github.com/jayrambhia
=============================================
Name : bookmarkChrome
Repo : Bookmark-Manager
Git : https://github.com/jayrambhia/Bookmark-Manager
version : 0.2
'''
import os
import json
import pickle
import gdbm
import time
def __getPath():
'''
Returns directory path of browser bookmark backup if found
'''
for path, dirs, files in os.walk(os.environ['HOME']):
if 'Default' in dirs:
if os.path.split(path)[-1] == 'google-chrome':
list_dir = os.listdir(os.path.join(path,'Default'))
if 'Bookmarks' in list_dir:
break
dir_path = os.path.join(path,'Default')
if os.path.isdir(dir_path):
return dir_path
else:
return None
def __getFile():
'''
Returns filename with absoulte path of the browser bookmark backup if found
'''
path = __getPath()
if path:
#files = os.listdir(path)
#files.sort()
filename = os.path.join(path, 'Bookmarks')
return filename
def getBookmarks():
'''
If browser bookmark backup file found,
Returns a dictionary object with bookmark url as key and (title, tag, add_date) tuple as value.
'''
filename = __getFile()
print filename
if not filename:
print 'No bookmark backup found!'
return
bookmark_dict = getBookmarkDict(filename)
return bookmark_dict
def getBookmarkDict(filename):
'''
Input: Absolute path of browser bookmark backup file
Creates/Updates Bookamrk-Manager database
Returns a dictionary object with bookmark url as key and (title, tag, add_date) tuple as value.
'''
f = gdbm.open('bookmarkDB','c')
bookmark_dict = fetchBookmarks(filename)
if bookmark_dict:
for key in bookmark_dict:
if not f.has_key(key):
f[key] = pickle.dumps(bookmark_dict[key])
if f.keys():
bookmark_dict = {}
for key in f.keys():
bookmark_dict[key] = pickle.loads(f[key])
if not f.has_key('@author@'):
name = 'Jay Rambhia'
email = '[email protected]'
add_date = time.time()
f['@author@'] = pickle.dumps((name, email, add_date))
print 'bookmarks saved'
f.close()
return bookmark_dict
def fetchBookmarks(filename):
'''
Decodes browser bookmark backup files using json
Returns a dictionary with bookmark url as key and (title, tag, add_date) tuple as value
'''
f = open(filename,'r')
con = json.load(f)
f.close()
bookmark_dict = {}
# bookmark_bar
con_list = con['roots'] # Access the roots
bookmark_bar = con_list['bookmark_bar']
tags = bookmark_bar['children'] # Contains unsorted bookmarks and Tags
for tag in tags:
if tag.has_key('children'): # Tags / or stored in folder
Tag = tag['name']
bookmarks = tag['children']
for bookmark in bookmarks:
if bookmark['type'] == 'url':
uri = bookmark['url']
title = bookmark['name']
dateAdded = int(bookmark['date_added'])
add_date = dateAdded/10000000
#print Tag, title
#print 'saved on:',time.ctime(add_date)
#print uri
#print ''
bookmark_dict[uri] = (repr(title), Tag, add_date)
else: # Unsorted Bookmarks
if tag['type'] == 'url':
bookmark = tag
uri = bookmark['url']
title = bookmark['name']
dateAdded = int(bookmark['date_added'])
add_date = dateAdded/10000000
Tag = 'Unsorted Bookmarks'
#print Tag, title
#print 'saved on:',time.ctime(add_date)
#print uri
#print ''
bookmark_dict[uri] = (repr(title), Tag, add_date)
return bookmark_dict
if __name__ == '__main__':
getBookmarks()