]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_htmlparser.py
d7020b03e933a9d822c541fe9c3936a5e0310ee1
[bookmarks_db.git] / parse_html / bkmk_ph_htmlparser.py
1 """HTML Parser using Pythons' HTMLParser
2
3 This file is a part of Bookmarks database and Internet robot.
4 """
5
6 __author__ = "Oleg Broytman <phd@phdru.name>"
7 __copyright__ = "Copyright (C) 1997-2013 PhiloSoft Design"
8 __license__ = "GNU GPL"
9
10 __all__ = ['parse_html']
11
12
13 from HTMLParser import HTMLParseError
14 from m_lib.net.www.html import HTMLParser as _HTMLParser
15
16
17 class HTMLHeadDone(Exception): pass
18
19
20 class HTMLParser(_HTMLParser):
21    def __init__(self, charset=None):
22       _HTMLParser.__init__(self)
23       self.charset = charset
24       self.meta_charset = 0
25       self.title = None
26       self.refresh = None
27       self.icon = None
28
29    def end_head(self):
30       raise HTMLHeadDone()
31
32    def do_meta(self, attrs):
33       http_equiv = ""
34       content = ""
35
36       for attrname, value in attrs:
37          if value:
38             value = value.strip()
39             if attrname == 'http-equiv':
40                http_equiv = value.lower()
41             elif attrname == 'content':
42                content = value
43             elif (attrname == 'charset') and (not self.charset):
44                self.charset = value.lower()
45                self.meta_charset = 1
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) and (parser.refresh is None) and (parser.icon is None):
100       return None
101    return parser