Created
April 25, 2016 01:19
-
-
Save aont/02cca1a444c1781e842b9f4810118d55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # -*- coding: utf-8 -*- | |
| import sys | |
| from HTMLParser import HTMLParser, HTMLParseError | |
| preface='''<!DOCTYPE NETSCAPE-Bookmark-file-1> | |
| <!-- This is an automatically generated file. | |
| It will be read and overwritten. | |
| DO NOT EDIT! --> | |
| <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> | |
| <TITLE>Bookmarks</TITLE> | |
| <H1>Bookmarks</H1> | |
| <DL><p>''' | |
| postface='''</DL><P>''' | |
| class bookmark: | |
| def __init__(self): | |
| self.name = None | |
| self.url = None | |
| self.attrs = None | |
| # self.parent = None | |
| self.children = None | |
| self.index = -1 | |
| def print_attrs(self): | |
| if not isinstance(self.attrs, dict): | |
| return | |
| attrs = self.attrs.items() | |
| for attr in attrs: | |
| if attr[0]=="icon": | |
| continue | |
| print "%s=\"%s\"" % (attr[0].upper(), attr[1]), | |
| def is_folder(self): | |
| return isinstance(self.children, list) | |
| def hprint(self, depth=0): | |
| if depth==0: | |
| print preface | |
| for i in self.children: | |
| i.hprint(depth+1) | |
| print postface | |
| elif self.is_folder(): | |
| # print "%s%s" % (" "*depth, self.name) | |
| print "%s<DT><H3" % (" "*depth), | |
| self.print_attrs() | |
| print ">%s</H3>" % (self.name) | |
| print "%s<DL><P>" % (" "*depth) | |
| for i in self.children: | |
| i.hprint(depth+1) | |
| print "%s</DL><P>" % (" "*depth) | |
| else: | |
| # print "%s%s %s" % (" "*depth, self.name, self.url) | |
| print "%s<DT><A" % (" "*depth), | |
| self.print_attrs() | |
| print ">%s</A>" % (self.name) | |
| def hprint1(self, depth=0): | |
| if self.is_folder(): | |
| print "%s%s" % (" "*depth, self.name) | |
| else: | |
| print "%s%s %s" % (" "*depth, self.name, self.url) | |
| def __cmp__(self, other): | |
| c1 = cmp(self.name, other.name) | |
| f1 = self.is_folder() | |
| f2 = other.is_folder() | |
| if c1!=0: | |
| return c1 | |
| elif f1 or f2: | |
| return cmp(f1, f2) | |
| else: | |
| return cmp(self.url, other.url) | |
| def merge0(self, other): | |
| s1 = sorted(self.children) | |
| s2 = sorted(other.children) | |
| len_s1 = len(s1) | |
| len_s2 = len(s2) | |
| for j in xrange(len_s2): | |
| print s1[0].name, s1[0].url, s2[j].name, s2[j].url, cmp(s1[0], s2[j]) | |
| def merge(self, other): | |
| s1 = sorted(self.children) | |
| s2 = sorted(other.children) | |
| len_s1 = len(s1) | |
| len_s2 = len(s2) | |
| i = 0 | |
| for j in xrange(len_s2): | |
| c1 = -1 | |
| while True: | |
| c1 = cmp(s1[i], s2[j]) | |
| if c1>=0: | |
| break | |
| if i==len_s1-1: | |
| break | |
| i += 1 | |
| if c1==0 and isinstance(s2[j].children, list): | |
| s1[i].merge(s2[j]) | |
| elif c1 != 0: | |
| self.children.append(s2[j]) | |
| #print "----------------------------" | |
| #s1[i].hprint(1) | |
| #s2[j].hprint(1) | |
| #print "----------------------------" | |
| self.children.sort(key=lambda x:x.index) | |
| class BookmarkParser(HTMLParser): | |
| def __init__(self): | |
| HTMLParser.__init__(self) | |
| self.index_cur = 0 | |
| self.init_bookmark() | |
| def init_bookmark(self): | |
| self.depth_cur = 0 | |
| self.name_cur = "" | |
| self.attrs_cur = None | |
| root = bookmark() | |
| root.name = "root" | |
| root.children = [] | |
| self.root = root | |
| self.folder_path = [root] | |
| self.has_item = False | |
| self.is_folder = False | |
| self.tag_cur = "" | |
| def append_item(self): | |
| item = bookmark() | |
| item.name = self.name_cur | |
| item.attrs = self.attrs_cur | |
| item.index = self.index_cur | |
| if item.attrs.has_key("personal_toolbar_folder"): | |
| item.name = 'Bookmark Toolbar' | |
| self.index_cur += 1 | |
| self.folder_path[-1].children.append(item) | |
| # print " "*self.depth_cur, "h3", item.name, self.folder_path[-1].name | |
| if self.is_folder: | |
| item.children = [] | |
| self.folder_path.append(item) | |
| else: | |
| item.url = item.attrs['href'] | |
| def handle_starttag(self, tag, attrs): | |
| attrs = dict(attrs) | |
| # print 'starttag', tag, attrs | |
| self.tag_cur = tag | |
| if 'dl'==tag: | |
| # print "<dl>", self.depth_cur | |
| self.depth_cur += 1 | |
| self.is_folder = True | |
| elif 'dt'==tag: | |
| if self.has_item: | |
| self.append_item() | |
| self.is_folder = False | |
| self.has_item = True | |
| elif tag in ['a', 'h3']: | |
| self.attrs_cur = attrs | |
| self.name_cur = '' | |
| def handle_endtag(self, tag): | |
| # print 'endtag', tag | |
| self.tag_cur = '' | |
| if 'dl'==tag: | |
| if self.has_item: | |
| self.append_item() | |
| self.has_item = False | |
| self.depth_cur -= 1 | |
| self.folder_path.pop() | |
| #elif 'a'==tag: | |
| # self.tag_cur = '' | |
| #elif 'h3'==tag: | |
| # self.tag_cur = '' | |
| #elif 'h1'==tag: | |
| # self.tag_cur = '' | |
| def handle_data(self, data): | |
| # print 'data', data | |
| if self.tag_cur in ['a', 'h3']: | |
| self.name_cur += data | |
| def handle_entityref(self, data): | |
| # print 'data', data | |
| if self.tag_cur in ['a', 'h3']: | |
| # self.name_cur += self.unescape("&%s;" % data).encode('utf-8') | |
| self.name_cur += "&%s;" % data | |
| def handle_charref(self, data): | |
| # print 'data', data | |
| if self.tag_cur in ['a', 'h3']: | |
| # self.name_cur += self.unescape("&#%s;" % data).encode('utf-8') | |
| self.name_cur += "&#%s;" % data | |
| if __name__ == '__main__': | |
| parser = BookmarkParser() | |
| f1 = open(sys.argv[1], 'r') | |
| parser.feed(f1.read()) | |
| parser.close() | |
| b1 = parser.root | |
| f1.close() | |
| parser.init_bookmark() | |
| f2 = open(sys.argv[2], 'r') | |
| parser.feed(f2.read()) | |
| parser.close() | |
| b2 = parser.root | |
| f2.close() | |
| b1.merge(b2) | |
| b1.hprint() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment