]> git.phdru.name Git - bookmarks_db.git/blobdiff - parse_html/bkmk_ph_htmlparser.py
Fix(parse_html): Do not parse empty strings
[bookmarks_db.git] / parse_html / bkmk_ph_htmlparser.py
index 45e89f5119817787b0b926c7f43627a7dc22e7dd..c0f89b411d688bc99ecaab1a377c80abb36b6891 100644 (file)
@@ -5,13 +5,16 @@ This file is a part of Bookmarks database and Internet robot.
 """
 
 __author__ = "Oleg Broytman <phd@phdru.name>"
-__copyright__ = "Copyright (C) 1997-2017 PhiloSoft Design"
+__copyright__ = "Copyright (C) 1997-2023 PhiloSoft Design"
 __license__ = "GNU GPL"
 
 __all__ = ['parse_html']
 
 
-from HTMLParser import HTMLParseError
+try:
+    from HTMLParser import HTMLParseError
+except ImportError:
+    class HTMLParseError(Exception): pass
 from m_lib.net.www.html import HTMLParser as _HTMLParser
 
 
@@ -47,10 +50,13 @@ class HTMLParser(_HTMLParser):
 
         if (not self.charset) and (http_equiv == "content-type"):
             try:
-                # extract charset from "text/html; foo; charset=UTF-8, bar; baz;"
-                self.charset = content.lower().split('charset=')[1].split(';')[0].split(',')[0]
-                self.meta_charset = 1 # Remember that the charset was retrieved from
-                                      # META tag, not from the Content-Type header
+                # extract charset from
+                # "text/html; foo; charset=UTF-8, bar; baz;"
+                self.charset = content.lower().split('charset=')[1].\
+                    split(';')[0].split(',')[0]
+                # Remember that the charset was retrieved from
+                # META tag, not from the Content-Type header
+                self.meta_charset = 1
             except IndexError:
                 pass
 
@@ -61,7 +67,7 @@ class HTMLParser(_HTMLParser):
         self.accumulator = ''
 
     def end_title(self):
-        if not self.title: # use only the first title
+        if not self.title:  # use only the first title
             self.title = self.accumulator
 
     def do_link(self, attrs):
@@ -71,7 +77,9 @@ class HTMLParser(_HTMLParser):
         for attrname, value in attrs:
             if value:
                 value = value.strip()
-                if (attrname == 'rel') and (value.lower() in ('icon', 'shortcut icon')):
+                if (attrname == 'rel') and (
+                        value.lower() in ('icon', 'shortcut icon')
+                ):
                     has_icon = True
                 elif attrname == 'href':
                     href = value
@@ -81,6 +89,8 @@ class HTMLParser(_HTMLParser):
 
 
 def parse_html(html_text, charset=None, log=None):
+    if not html_text:
+        return None
     parser = HTMLParser(charset)
 
     try:
@@ -93,6 +103,7 @@ def parse_html(html_text, charset=None, log=None):
     except (HTMLParseError, HTMLHeadDone):
         pass
 
-    if (parser.title is None) and (parser.refresh is None) and (parser.icon is None):
+    if (parser.title is None) and (parser.refresh is None) \
+            and (parser.icon is None):
         return None
     return parser