]> git.phdru.name Git - bookmarks_db.git/blob - Robots/parse_html.py
Try BeautifulSoup; if it fails - fall back to HTML Parser.
[bookmarks_db.git] / Robots / parse_html.py
1 #! /usr/bin/env python
2 """
3    HTML Parsers wrapper
4
5    Written by BroytMann. Copyright (C) 1997-2007 PhiloSoft Design
6 """
7
8 import codecs
9
10 from m_lib.defenc import default_encoding
11 current_charset = default_encoding.replace("windows-", "cp")
12 DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
13
14 parsers = []
15 try:
16    from parse_html_beautifulsoup import parse_html
17 except ImportError:
18    pass
19 else:
20    parsers.append(parse_html)
21
22 from parse_html_htmlparser import parse_html
23 parsers.append(parse_html)
24
25
26 import re
27 entity_re = re.compile("(&#[0-9]+;)")
28
29 def recode_entities(title, charset):
30    output = []
31    for part in entity_re.split(title):
32       if entity_re.match(part):
33          part = unichr(int(part[2:-1])).encode(charset, "replace")
34       output.append(part)
35
36    return ''.join(output)
37
38
39 def parse_html(filename, charset=None, log=None):
40    if charset:
41       try:
42          codecs.lookup(charset) # In case of unknown charset...
43       except (ValueError, LookupError):
44          charset = None         # ...try charset from HTML
45
46    for p in parsers:
47       parser = p(filename, charset)
48       if parser:
49          break
50       else:
51          if log: log("Parser %s failed, trying next one." % p)
52
53    title = parser.title
54    if not parser.charset:
55       try:
56          unicode(title, "ascii")
57       except UnicodeDecodeError:
58          parser.charset = DEFAULT_CHARSET
59
60    if parser.charset:
61       parser.charset = parser.charset.replace("windows-", "cp").lower()
62
63    if parser.charset and (parser.charset <> current_charset):
64       try:
65          if parser.meta_charset:
66             if log: log("   META charset   : %s" % parser.charset)
67          else:
68             if log: log("   HTTP charset   : %s" % parser.charset)
69          if log: log("   current charset: %s" % current_charset)
70          if log: log("   title          : %s" % title)
71          save_title = title
72          try:
73             title = unicode(title, parser.charset).encode(current_charset)
74          except UnicodeError:
75             if parser.meta_charset and parser.charset.endswith("1252") and \
76                   not DEFAULT_CHARSET.endswith("1252") and (DEFAULT_CHARSET <> current_charset):
77                parser.charset = DEFAULT_CHARSET
78                if log: log("   incorrect conversion from cp1252, converting from %s" % DEFAULT_CHARSET)
79                title = unicode(save_title, DEFAULT_CHARSET, "replace").encode(current_charset, "replace")
80             else:
81                title = unicode(title, parser.charset, "replace").encode(current_charset, "replace")
82          if log: log("   converted title: %s" % title)
83       except LookupError:
84          if log: log("   unknown charset: `%s' or `%s'" % (parser.charset, current_charset))
85
86    parser.title = recode_entities(title, current_charset)
87    return parser
88
89
90 if __name__ == '__main__':
91    import sys
92    parser = parse_html(sys.argv[1], current_charset)
93    print parser.charset
94    print parser.title
95    print parser.refresh
96    print parser.icon