]> git.phdru.name Git - bookmarks_db.git/blobdiff - parse_html/bkmk_ph_beautifulsoup4.py
Fix(Py3): Remove forgotten `.decode()`/`.encode()`
[bookmarks_db.git] / parse_html / bkmk_ph_beautifulsoup4.py
index 1ce543e828936284bc469f6037bf4f0a77ca14c4..d76c05acd3da7f1bc6d09168562adfc1c05fa8c0 100644 (file)
@@ -11,9 +11,18 @@ __license__ = "GNU GPL"
 __all__ = ['parse_html']
 
 
-import re
+import warnings
+
 from bs4 import BeautifulSoup
+
 from .bkmk_ph_util import HTMLParser
+from compat import string_type
+
+warnings.filterwarnings(
+    'ignore', 'No parser was explicitly specified')
+warnings.filterwarnings(
+    'ignore',
+    "It looks like you're parsing an XML document using an HTML parser.")
 
 universal_charset = "utf-8"
 DEFAULT_CHARSET = "cp1251"  # Stupid default for Russian Cyrillic
@@ -21,12 +30,17 @@ DEFAULT_CHARSET = "cp1251"  # Stupid default for Russian Cyrillic
 
 def _parse_html(html_text, charset):
     try:
-        return BeautifulSoup(html_text, from_encoding=charset)
+        if isinstance(html_text, bytes):
+            return BeautifulSoup(html_text, from_encoding=charset)
+        else:
+            return BeautifulSoup(html_text)
     except TypeError:
         return None
 
 
 def parse_html(html_text, charset=None, log=None):
+    if not html_text:
+        return None
     root = _parse_html(html_text, charset)
     if root is None:
         return None
@@ -55,8 +69,8 @@ def parse_html(html_text, charset=None, log=None):
         else:
             parts = []
             for part in title:
-                if not isinstance(part, basestring):
-                    part = unicode(part)
+                #if not isinstance(part, string_type):
+                #    part = part.decode()
                 parts.append(part.strip())
             title = ''.join(parts)
 
@@ -83,12 +97,12 @@ def parse_html(html_text, charset=None, log=None):
             if meta_content:
                 meta_charset = _charset = meta_content.lower()
 
-    if title and (_charset or meta_charset):
-        try:
-            title = title.encode(_charset or meta_charset)
-        except LookupError:
-            title = title.encode(universal_charset)
-            _charset = universal_charset
+    #if title and (_charset or meta_charset):
+    #    try:
+    #        title = title.encode(_charset or meta_charset)
+    #    except LookupError:
+    #        title = title.encode(universal_charset)
+    #        _charset = universal_charset
 
     meta = head.find(_find_refresh, recursive=False)
     if meta: