X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fparse_html.py;h=57ad6a0cf69c89247281700915ced9854a3ef8ba;hb=fb98cfeffff4fb699b6d5ca3c8c151901002b32e;hp=ffacbee521ddecd0553008a158db163d07487503;hpb=387f77d110986aa12967c9cd788ab0e4f41f2be2;p=bookmarks_db.git diff --git a/Robots/parse_html.py b/Robots/parse_html.py index ffacbee..57ad6a0 100755 --- a/Robots/parse_html.py +++ b/Robots/parse_html.py @@ -1,14 +1,22 @@ -#! /usr/local/bin/python -O +#! /usr/bin/env python """ HTML Parser - Written by BroytMann, Jun 2002 - May 2003. Copyright (C) 1997-2003 PhiloSoft Design + Written by BroytMann. Copyright (C) 1997-2007 PhiloSoft Design """ import sys current_charset = sys.getdefaultencoding() -DEFAULT_CHARSET = "windows-1251" # Stupid default for Russian Cyrillic +if current_charset == "ascii": + try: + import locale + except ImportError: + pass + else: + current_charset = locale.getpreferredencoding() +current_charset = current_charset.replace("windows-", "cp").lower() +DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic from HTMLParser import HTMLParseError @@ -63,6 +71,24 @@ 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 in ('icon', 'shortcut icon')): + has_icon = True + elif attrname == 'href': + href = value + + if has_icon: + self.icon = href + else: + self.icon = None + + import re entity_re = re.compile("(&#[0-9]+;)") @@ -96,14 +122,14 @@ def parse_html(filename, charset=None, log=None): title = parser.title if not parser.charset: - ascii = 1 - for c in title: - if not (32 <= ord(c) <= 127): # non-ASCII character - ascii = 0 - break - if not ascii: + try: + unicode(title, "ascii") + except UnicodeDecodeError: parser.charset = DEFAULT_CHARSET + if parser.charset: + parser.charset = parser.charset.replace("windows-", "cp").lower() + if parser.charset and (parser.charset <> current_charset): try: if parser.meta_charset: @@ -127,3 +153,4 @@ if __name__ == '__main__': print parser.charset print parser.title print parser.refresh + print parser.icon