X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=sidebyside;f=Robots%2Fparse_html.py;h=a83585df318afb1602a916a1a17768485a099138;hb=8ce74f839238093d7278aa041ff55dbcb7abd3a0;hp=f50b01b415b5c6fd775fa41cb20325127ebf0469;hpb=928e59567b0ba5e11efe915ae28d0e89f52bcc4a;p=bookmarks_db.git diff --git a/Robots/parse_html.py b/Robots/parse_html.py index f50b01b..a83585d 100755 --- a/Robots/parse_html.py +++ b/Robots/parse_html.py @@ -13,22 +13,33 @@ DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic parsers = [] try: - import parse_html_html5 + import parse_html_beautifulsoup + parse_html_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET except ImportError: pass else: - parsers.append(parse_html_html5.parse_html) + parsers.append(parse_html_beautifulsoup.parse_html) try: - import parse_html_beautifulsoup - parse_html_beautifulsoup.DEFAULT_CHARSET = DEFAULT_CHARSET + from parse_html_lxml import parse_html except ImportError: pass else: - parsers.append(parse_html_beautifulsoup.parse_html) + parsers.append(parse_html) + +try: + from parse_html_htmlparser import parse_html +except ImportError: + pass +else: + parsers.append(parse_html) -from parse_html_htmlparser import parse_html -parsers.append(parse_html) +try: + import parse_html_html5 +except ImportError: + pass +else: + parsers.append(parse_html_html5.parse_html) import re @@ -61,6 +72,9 @@ def recode_entities(title, charset): def parse_html(filename, charset=None, log=None): + if not parsers: + return None + if charset: try: codecs.lookup(charset) # In case of unknown charset... @@ -70,8 +84,9 @@ def parse_html(filename, charset=None, log=None): charsets = [universal_charset, DEFAULT_CHARSET] if charset: charset = charset.lower().replace("windows-", "cp") - if charset not in charsets: - charsets.insert(0, charset) + if charset in charsets: + charsets.remove(charset) + charsets.insert(0, charset) for p in parsers: parser = None @@ -86,6 +101,9 @@ def parse_html(filename, charset=None, log=None): else: if log: log("Parser %s.%s failed, trying next one." % (p.__module__, p.__name__)) + if not parser: + return None + converted_title = title = parser.title if title and (not parser.charset): try: @@ -125,6 +143,14 @@ def parse_html(filename, charset=None, log=None): final_title = ' '.join([s for s in parts if s]) if log and (final_title <> converted_title): log(" final title : %s" % final_title) parser.title = final_title + + icon = parser.icon + if isinstance(icon, unicode): + try: + parser.icon = icon.encode('ascii') + except UnicodeEncodeError: + if parser.charset: + parser.icon = icon.encode(parser.charset) return parser