]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_htmlparser.py
Style: Fix flake8 E261 at least two spaces before inline comment
[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
7 __author__ = "Oleg Broytman <phd@phdru.name>"
8 __copyright__ = "Copyright (C) 1997-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 __all__ = ['parse_html']
12
13
14 from HTMLParser import HTMLParseError
15 from m_lib.net.www.html import HTMLParser as _HTMLParser
16
17
18 class HTMLHeadDone(Exception): pass
19
20
21 class HTMLParser(_HTMLParser):
22     def __init__(self, charset=None):
23         _HTMLParser.__init__(self)
24         self.charset = charset
25         self.meta_charset = 0
26         self.title = None
27         self.refresh = None
28         self.icon = None
29
30     def end_head(self):
31         raise HTMLHeadDone()
32
33     def do_meta(self, attrs):
34         http_equiv = ""
35         content = ""
36
37         for attrname, value in attrs:
38             if value:
39                 value = value.strip()
40                 if attrname == 'http-equiv':
41                     http_equiv = value.lower()
42                 elif attrname == 'content':
43                     content = value
44                 elif (attrname == 'charset') and (not self.charset):
45                     self.charset = value.lower()
46                     self.meta_charset = 1
47
48         if (not self.charset) and (http_equiv == "content-type"):
49             try:
50                 # extract charset from "text/html; foo; charset=UTF-8, bar; baz;"
51                 self.charset = content.lower().split('charset=')[1].split(';')[0].split(',')[0]
52                 # Remember that the charset was retrieved from
53                 # META tag, not from the Content-Type header
54                 self.meta_charset = 1
55             except IndexError:
56                 pass
57
58         if http_equiv == "refresh":
59             self.refresh = content
60
61     def start_title(self, attrs):
62         self.accumulator = ''
63
64     def end_title(self):
65         if not self.title:  # use only the first title
66             self.title = self.accumulator
67
68     def do_link(self, attrs):
69         has_icon = False
70         href = None
71
72         for attrname, value in attrs:
73             if value:
74                 value = value.strip()
75                 if (attrname == 'rel') and (value.lower() in ('icon', 'shortcut icon')):
76                     has_icon = True
77                 elif attrname == 'href':
78                     href = value
79
80         if has_icon:
81             self.icon = href
82
83
84 def parse_html(html_text, charset=None, log=None):
85     parser = HTMLParser(charset)
86
87     try:
88         parser.feed(html_text)
89     except (HTMLParseError, HTMLHeadDone):
90         pass
91
92     try:
93         parser.close()
94     except (HTMLParseError, HTMLHeadDone):
95         pass
96
97     if (parser.title is None) and (parser.refresh is None) and (parser.icon is None):
98         return None
99     return parser