5
Original file much thanks to
6
http://www.kylo.net/deli.py.txt
8
Modifications released under GPL v2 (or any later)
9
Ulrik Sverdrup <ulrik.sverdrup@gmail.com>
12
from ConfigParser import RawConfigParser
13
from HTMLParser import HTMLParser
14
from os.path import join, expanduser, exists, basename
16
def get_firefox_home_file(needed_file):
17
firefox_dir = expanduser("~/.mozilla/firefox/")
18
config = RawConfigParser({"Default" : 0})
19
config.read(expanduser(join(firefox_dir, "profiles.ini")))
22
for section in config.sections():
23
if config.has_option(section, "Default") and config.get(section, "Default") == "1":
24
path = config.get (section, "Path")
26
elif path == None and config.has_option(section, "Path"):
27
path = config.get (section, "Path")
32
if path.startswith("/"):
33
return join(path, needed_file)
35
return join(firefox_dir, path, needed_file)
38
class BookmarksParser(HTMLParser):
41
# this is python: explicitly invoke base class constructor
42
HTMLParser.__init__(self)
55
def setBaseTag(self, baseTag):
56
self.tags.append(baseTag)
58
def setIgnoreUrls(self, ignore):
62
# remove apostrophes, quote, double-quotes, colons, commas
63
def normalizeText(self, text):
64
text = text.replace('\'', '')
65
text = text.replace('"', '')
66
text = text.replace('`', '')
67
text = text.replace(':', '')
68
text = text.replace(',', '')
69
text = text.replace(' ', '')
70
text = text.replace(' ', '')
73
def handle_starttag(self, tag, attrs):
87
#print "Entering folder list; tags are", self.tags
89
def handle_endtag(self, tag):
91
self.tags.append(self.currentTag)
96
if self.debug == True:
98
print "href =", self.href
99
print "description =", self.description
100
print "tags =", self.tags
104
if len(self.href) == 0:
106
if not self.href.split(":")[0] in ["http", "https", "news", "ftp"]:
108
if self.href in self.ignore:
111
# actually post here, make sure there's a url to post
115
"title": self.description,
118
self.all_items.append(bookmark)
121
self.description = ""
124
# exiting a dl means end of a bookmarks folder, pop the last tag off
126
self.tags = self.tags[:-1]
128
# handle any data: note that this will miss the "escaped" stuff
129
# fix this by adding handle_charref, etc methods
130
def handle_data(self, data):
132
self.currentTag += self.normalizeText(data)
135
self.description += data
137
def get_bookmarks(bookmarks_file):
139
Return a list of bookmarks (dictionaries)
141
each bookmark has the keys:
144
tags: list of tags/the folder
146
# construct and configure the parser
147
parser = BookmarksParser()
149
# initiate the parse; this will submit requests to delicious
150
parser.feed(open(bookmarks_file).read())
155
return parser.all_items
159
fileloc = get_firefox_home_file("bookmarks.html")
161
print get_bookmarks(fileloc)
163
if __name__ == "__main__":