]> git.phdru.name Git - bookmarks_db.git/blob - parse_html/bkmk_ph_beautifulsoup4.py
Style: Fix flake8 F401 module imported but unused
[bookmarks_db.git] / parse_html / bkmk_ph_beautifulsoup4.py
1 """HTML Parser using BeautifulSoup4
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) 2017-2023 PhiloSoft Design"
9 __license__ = "GNU GPL"
10
11 __all__ = ['parse_html']
12
13
14 from bs4 import BeautifulSoup
15 from .bkmk_ph_util import HTMLParser
16
17 universal_charset = "utf-8"
18 DEFAULT_CHARSET = "cp1251"  # Stupid default for Russian Cyrillic
19
20
21 def _parse_html(html_text, charset):
22     try:
23         return BeautifulSoup(html_text, from_encoding=charset)
24     except TypeError:
25         return None
26
27
28 def parse_html(html_text, charset=None, log=None):
29     root = _parse_html(html_text, charset)
30     if root is None:
31         return None
32
33     _charset = root.originalEncoding
34     html = root.html
35     if html is None:
36         html = root
37
38     head = html.head
39     if head is None:
40         head = html  # Some sites put TITLE in HTML without HEAD
41
42     title = head.title
43     if (title is None) and (html is not head):
44         # Some sites put TITLE in HTML outside of HEAD
45         title = html.title
46
47     if title is None:
48         # Lookup TITLE in the root
49         title = root.title
50
51     if title is not None:
52         if title.string:
53             title = title.string
54         else:
55             parts = []
56             for part in title:
57                 if not isinstance(part, basestring):
58                     part = unicode(part)
59                 parts.append(part.strip())
60             title = ''.join(parts)
61
62     meta = head.find(_find_contenttype, recursive=False)
63     if meta:
64         try:
65             meta_content = meta.get("content")
66             if meta_content:
67                 __charset = meta_content.lower().split('charset=')[1].\
68                     split(';')[0]
69             else:
70                 __charset = False
71         except IndexError:  # No charset in the META Content-Type
72             meta_charset = False
73         else:
74             meta_charset = _charset = __charset
75     else:
76         meta_charset = False
77
78     if not meta_charset:
79         meta = head.find(_find_charset, recursive=False)
80         if meta:
81             meta_content = meta.get("charset")
82             if meta_content:
83                 meta_charset = _charset = meta_content.lower()
84
85     if title and (_charset or meta_charset):
86         try:
87             title = title.encode(_charset or meta_charset)
88         except LookupError:
89             title = title.encode(universal_charset)
90             _charset = universal_charset
91
92     meta = head.find(_find_refresh, recursive=False)
93     if meta:
94         refresh = meta.get("content")
95     else:
96         refresh = None
97
98     meta = head.find(_find_icon, recursive=False)
99     if meta:
100         icon = meta.get("href")
101     else:
102         icon = None
103
104     if (title is None) and (refresh is None) and (icon is None):
105         return None
106     return HTMLParser(_charset, meta_charset, title, refresh, icon)
107
108
109 def _find_contenttype(Tag):
110     return (Tag.name == "meta") and \
111        (Tag.get_attribute_list("http-equiv", '')[0].lower() == "content-type")
112
113
114 def _find_charset(Tag):
115     return (Tag.name == "meta") and Tag.get("charset", '')
116
117
118 def _find_refresh(Tag):
119     return (Tag.name == "meta") and \
120        (Tag.get_attribute_list("http-equiv", '')[0].lower() == "refresh")
121
122
123 def _find_icon(Tag):
124     return (Tag.name == "link") and \
125        (Tag.get_attribute_list("rel", '')[0].lower()
126         in ('icon', 'shortcut icon'))