Created
December 28, 2010 14:03
-
-
Save huitseeker/757230 to your computer and use it in GitHub Desktop.
{Chrome bookmarks} \ {Delicious bookmarks}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
####################################################################### | |
# This program is free software; you can redistribute it and/or # | |
# modify it under the terms of the GNU General Public License as # | |
# published by the Free Software Foundation; either version 2 of the # | |
# License, or (at your option) any later version. # | |
# # | |
# This program is distributed in the hope that it will be useful, but # | |
# WITHOUT ANY WARRANTY; without even the implied warranty of # | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # | |
# General Public License for more details. # | |
# # | |
# Written and Copyright by Francois Garillot # | |
# Contact <[email protected]> for comment & bug reports # | |
####################################################################### | |
r"""Compares your Google chrome bookmarks with your delicious bookmarks, | |
and writes a file containing those Chrome bookmarks not in your | |
delicious bookmarks, removing duplicates along the way""" | |
import datetime | |
import decimal | |
import sys, os | |
import types, urllib | |
import unicodedata | |
import json | |
import jsonical | |
google_bookmarks = '~/.config/google-chrome/Default/Bookmarks' | |
delicious_user = 'huitseeker' | |
# actually, I currently have > 2030 ! | |
delicious_limit = 2050 | |
outmarks = "Bookmarks" | |
debug = "true" | |
def goodelement(elem,badlist): | |
"""Checks a dict for a url key, returns False iff it occurs in badlist""" | |
if 'url' in elem: | |
val = elem['url'] not in badlist | |
if debug and not val: | |
print "duplicate found!", elem['url'] | |
if val and remove_duplicates: | |
badlist.append(elem['url']) | |
return val | |
else: | |
return True | |
def childrenelem(elem, badlist): | |
"Checks a dict for children key, filters on that value" | |
if "children" in elem: | |
elem['children'] = childrenfilter(elem.get("children"), badlist) | |
return elem | |
def childrenfilter(uncleanlist,badlist): | |
"Checks a list for 1st-level badelements, then checks in depth" | |
cleanerlist = filter(lambda x :goodelement(x,badlist),uncleanlist) | |
return [childrenelem(y,badlist) for y in cleanerlist] | |
def load(bmks): | |
"""Loads delicious and google bookmarks, filters the second by the | |
first, and returns a JSON result""" | |
bmk_in = open(os.path.expanduser(bmks),"r") | |
delibmks = ("http://feeds.delicious.com/v2/json/" | |
+delicious_user+"/?count="+str(delicious_limit)) | |
deli_in = urllib.urlopen(delibmks) | |
try: | |
bmkobj = jsonical.load(bmk_in) | |
deliobj = jsonical.load(deli_in) | |
except ValueError, e: | |
raise SystemExit(e) | |
tofilter = [dm["u"] for dm in deliobj] | |
for bmk_root in bmkobj['roots']: | |
bmkobj['roots'][bmk_root] = childrenelem(bmkobj['roots'][bmk_root], | |
tofilter) | |
return bmkobj | |
if __name__ == '__main__': | |
filteredmarks = load(google_bookmarks) | |
outfile = open(os.path.expanduser(outmarks),'w') | |
jsonical.dump(filteredmarks,outfile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment