]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/htmlparser.py
Added __all__.
[bookmarks_db.git] / parse_html / htmlparser.py
1 """HTML Parser using Pythons' HTMLParser
2
3 This file is a part of Bookmarks database and Internet robot.
4 """
5
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 1997-2011 PhiloSoft Design"
11 __license__ = "GNU GPL"
12
13 __all__ = ['parse_html']
14
15
16 from HTMLParser import HTMLParseError
17 from m_lib.net.www.html import HTMLParser as _HTMLParser
18
19
20 class HTMLHeadDone(Exception): pass
21
22
23 class HTMLParser(_HTMLParser):
24    def __init__(self, charset=None):
25       _HTMLParser.__init__(self)
26       self.charset = charset
27       self.meta_charset = 0
28       self.title = None
29       self.refresh = None
30       self.icon = None
31
32    def end_head(self):
33       raise HTMLHeadDone()
34
35    def do_meta(self, attrs):
36       http_equiv = ""
37       content = ""
38
39       for attrname, value in attrs:
40          if value:
41             value = value.strip()
42             if attrname == 'http-equiv':
43                http_equiv = value.lower()
44             elif attrname == 'content':
45                content = value
46
47       if (not self.charset) and (http_equiv == "content-type"):
48          try:
49             # extract charset from "text/html; foo; charset=UTF-8, bar; baz;"
50             self.charset = content.lower().split('charset=')[1].split(';')[0].split(',')[0]
51             self.meta_charset = 1 # Remember that the charset was retrieved from
52                                   # META tag, not from the Content-Type header
53          except IndexError:
54             pass
55
56       if http_equiv == "refresh":
57          self.refresh = content
58
59    def start_title(self, attrs):
60       self.accumulator = ''
61
62    def end_title(self):
63       if not self.title: # use only the first title
64          self.title = self.accumulator
65
66    def do_link(self, attrs):
67       has_icon = False
68       href = None
69
70       for attrname, value in attrs:
71          if value:
72             value = value.strip()
73             if (attrname == 'rel') and (value.lower() in ('icon', 'shortcut icon')):
74                has_icon = True
75             elif attrname == 'href':
76                href = value
77
78       if has_icon:
79          self.icon = href
80
81
82 def parse_html(filename, charset=None, log=None):
83    infile = open(filename, 'r')
84    parser = HTMLParser(charset)
85
86    for line in infile:
87       try:
88          parser.feed(line)
89       except (HTMLParseError, HTMLHeadDone):
90          break
91
92    infile.close()
93
94    try:
95       parser.close()
96    except (HTMLParseError, HTMLHeadDone):
97       pass
98
99    if parser.title is None:
100       return None
101
102    return parser