Skip to content

Instantly share code, notes, and snippets.

@jayrambhia
Created February 1, 2012 22:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayrambhia/1719957 to your computer and use it in GitHub Desktop.
Save jayrambhia/1719957 to your computer and use it in GitHub Desktop.
A python script to back up all the bookmarks(firefox compatible). It returns a dictionary with url as key and (title, tag, add_date, modified_date) tuple as the value. It also stores it in gdbm. Another script is provided to read all the bookmarks.
'''
Author: Jay Rambhia
email : jayrambhia777@gmail.com
A new script supporting both Mozilla Firefox and Google Chrome bookmarks:
https://github.com/jayrambhia/Bookmark-Manager
'''
import os
import json
import pickle
import gdbm
import time
def __getPath():
for path, dirs, files in os.walk(os.environ['HOME']):
if 'bookmarkbackups' in dirs:
break
dir_path = os.path.join(path,'bookmarkbackups')
if os.path.isdir(dir_path):
return dir_path
else:
return None
def __getFile():
path = __getPath()
filename = None
if path:
files = os.listdir(path)
files.sort()
filename = os.path.join(path, files[-1])
return filename
def getBookmarks():
filename = __getFile()
if not filename:
print 'No bookmark backup found!'
return
bookmark_dict = getBookmarkDict(filename)
return bookmark_dict
def getBookmarkDict(filename):
f = gdbm.open('bookmarkDB','c')
bookmark_dict = fetchBookmarks(filename)
if bookmark_dict:
for key in bookmark_dict:
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 f.has_key('@author@'):
name, email, add_date, modified_date = pickle.loads(f['@author@'])
modified_date = time.time()
f['@author@'] = pickle.dumps((name, email, add_date, modified_date))
else:
name = 'Jay Rambhia'
email = 'jayrambhia777@gmail.com'
add_date = modified_date = time.time()
f['@author@'] = pickle.dumps((name, email, add_date, modified_date))
print 'bookmarks saved'
f.close()
return bookmark_dict
def fetchBookmarks(filename):
f = open(filename)
con = json.load(f)
f.close()
bookmark_dict = {}
# Get Bookmarks Menu / Bookmarks toolbar / Tags / Unsorted Bookmarks
con_list = con['children'] # this list will have all of the above mentioned things
for i in range(len(con_list)):
con_sub_list = con_list[i]['children'] # Access them individually
for tags in con_sub_list:
if tags.has_key('children'): # Accessing Tags # get tag list
bookmarks = tags['children'] # get all the bookmarks corresponding to the tag
if bookmarks:
for bookmark in bookmarks: # Access each bookmark
Tag = tags['title']
uri = bookmark['uri']
title = bookmark['title']
dateAdded = bookmark['dateAdded'] # it gives a long int eg. 1326378576503359L
add_date = dateAdded/1000000 # The output of time.time() would be 1326378576.503359
lastModified = bookmark['lastModified']
modified_date = lastModified/1000000
bookmark_dict[uri] = (repr(title), Tag, add_date, modified_date)
else:
if (tags['title'] != 'Recently Bookmarked'
and tags['title'] != 'Recent Tags'
and tags['title'] != 'Most Visited'
and con_list[i]['title'] != 'Bookmarks Menu'):
# Accessing Unsorted Bookmarks
Tag = con_list[i]['title']
title = tags['title']
uri = tags['uri']
dateAdded = tags['dateAdded']
add_date = dateAdded/1000000
lastModified = tags['lastModified']
modified_date = lastModified/1000000
bookmark_dict[uri] = (repr(title), Tag, add_date, modified_date)
return bookmark_dict
if __name__ == '__main__':
getBookmarks()
'''
Author : Jay Rambhia
email : jayramhbia777@gmail.com
A new script supporting both Mozilla Firefox and Google Chrome bookmarks:
https://github.com/jayrambhia/Bookmark-Manager
'''
import gdbm
import pickle
import os
def __getDB():
filename = __getFilename()
if not filename:
return
if os.path.isfile(filename):
db = gdbm.open(filename,'c')
if db.has_key('@author@'):
name, email, add_date, modified_date = pickle.loads(db['@author@'])
if name == 'Jay Rambhia'and email == 'jayrambhia777@gmail.com':
return db
print 'Bookmark database not found'
return None
def __getFilename():
dirs = os.listdir('.')
if 'bookmarkDB' in dirs:
filename = 'bookmarkDB'
file_path = os.path.join(os.path.abspath('.'),filename)
print file_path
return file_path
else:
print 'bookmark database not found'
print '1. Search for bookmark database'
print '2. Enter bookmark database name'
print '3. Exit'
n = int(raw_input())
if n == 1 :
for path, dirs, filenames in os.walk(os.environ['HOME']):
if 'bookmarkDB' in filenames:
break
file_path = os.path.join(path,'bookmarkDB')
return file_path
elif n == 2:
filename = str(raw_input('File Name: '))
print filename
if os.path.isfile(filename):
path, file_name = os.path.split(filename)
print path
if path:
return filename
file_path = os.path.join(os.path.abspath('.'),filename)
return file_path
else:
print 'Bookamrk database not found'
return None
elif n==3:
print 'Exit'
return None
else:
__getFilename()
def bookmarkReader(db):
if not db:
return
keys = db.keys()
author_tuple = pickle.loads(db['@author@'])
name, email, add_date, modified_date = author_tuple
print 'Author:',name
print 'email:',email
keys.remove('@author@')
bookmark_list = []
for key in keys:
bookmark_tuple = (key,pickle.loads(db[key]))
bookmark_list.append(bookmark_tuple)
bookmark_list.sort(key = lambda b:b[1][1])
for bookmark in bookmark_list:
print bookmark[1][0],'tag:',bookmark[1][1]
print bookmark[0]
print ''
return
if __name__ == '__main__':
db = __getDB()
bookmarkReader(db)
@jayrambhia
Copy link
Author

A python script supporting both Firefox and Mozilla created.
https://github.com/jayrambhia/Bookmark-Manager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment