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