X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fparse_html.py;h=b30c66458eaa32960b5badce9bc849a2082213d7;hb=55ee943b3ef63d03826318d36fd964d182c3c322;hp=350ad789b5fa34592db37ce6e097de98e8b8dd6a;hpb=62977582003da9f434cd4fd377837935b6f0520c;p=bookmarks_db.git diff --git a/Robots/parse_html.py b/Robots/parse_html.py index 350ad78..b30c664 100755 --- a/Robots/parse_html.py +++ b/Robots/parse_html.py @@ -1,24 +1,16 @@ -#! /usr/local/bin/python -O +#! /usr/bin/env python """ HTML Parser - Written by BroytMann. Copyright (C) 1997-2005 PhiloSoft Design + Written by BroytMann. Copyright (C) 1997-2007 PhiloSoft Design """ +import codecs -import sys -current_charset = sys.getdefaultencoding() -if current_charset == "ascii": - try: - import locale - except ImportError: - pass - else: - current_charset = locale.getpreferredencoding() -current_charset = current_charset.replace("windows-", "cp").lower() +from m_lib.defenc import default_encoding +current_charset = default_encoding.replace("windows-", "cp") DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic - from HTMLParser import HTMLParseError from m_lib.net.www.html import HTMLParser as _HTMLParser @@ -33,6 +25,7 @@ class HTMLParser(_HTMLParser): self.meta_charset = 0 self.title = '' self.refresh = '' + self.icon = None def end_head(self): raise HTMLHeadDone() @@ -71,6 +64,22 @@ class HTMLParser(_HTMLParser): self.title = self.accumulator + def do_link(self, attrs): + has_icon = False + href = None + + for attrname, value in attrs: + if value: + value = value.strip().lower() + if (attrname == 'rel') and (value.lower() in ('icon', 'shortcut icon')): + has_icon = True + elif attrname == 'href': + href = value + + if has_icon: + self.icon = href + + import re entity_re = re.compile("(&#[0-9]+;)") @@ -85,6 +94,12 @@ def recode_entities(title, charset): def parse_html(filename, charset=None, log=None): + if charset: + try: + codecs.lookup(charset) # In case of unknown charset... + except (ValueError, LookupError): + charset = None # ...try charset from HTML + infile = open(filename, 'r') parser = HTMLParser(charset) @@ -135,3 +150,4 @@ if __name__ == '__main__': print parser.charset print parser.title print parser.refresh + print parser.icon