]> git.phdru.name Git - bookmarks_db.git/blobdiff - Robots/parse_html.py
Fixed a bug: import codecs.
[bookmarks_db.git] / Robots / parse_html.py
index e95dcef24e0e2f7dc76c8e3f2908b466e17e83b9..90b777b4e97a2754c2bfc33d2e4f9ff966f6d0b5 100755 (executable)
@@ -1,18 +1,18 @@
-#! /usr/local/bin/python -O
+#! /usr/bin/env python
 """
    HTML Parser
 
-   Written by BroytMann, Jun 2002 - Aug 2002. Copyright (C) 1997-2002 PhiloSoft Design
+   Written by BroytMann. Copyright (C) 1997-2007 PhiloSoft Design
 """
 
+import codecs
 
-import sys
-current_charset = sys.getdefaultencoding()
-DEFAULT_CHARSET = "windows-1251"
-
+from m_lib.defenc import default_encoding
+current_charset = default_encoding.replace("windows-", "cp")
+DEFAULT_CHARSET = "cp1251" # Stupid default for Russian Cyrillic
 
 from HTMLParser import HTMLParseError
-from m_lib.www.html import HTMLParser as _HTMLParser
+from m_lib.net.www.html import HTMLParser as _HTMLParser
 
 
 class HTMLHeadDone(Exception): pass
@@ -25,6 +25,7 @@ class HTMLParser(_HTMLParser):
       self.meta_charset = 0
       self.title = ''
       self.refresh = ''
+      self.icon = None
 
    def end_head(self):
       raise HTMLHeadDone()
@@ -46,7 +47,8 @@ class HTMLParser(_HTMLParser):
          try:
             # extract charset from "text/html; foo; charset=UTF-8; bar;"
             self.charset = content.lower().split('charset=')[1].split(';')[0]
-            self.meta_charset = 1
+            self.meta_charset = 1 # Remember that the charset was retrieved from
+                                  # META tag, not from the Content-Type header
          except IndexError:
             pass
 
@@ -56,12 +58,50 @@ class HTMLParser(_HTMLParser):
 
    def start_title(self, attrs):
       self.accumulator = ''
+
    def end_title(self):
       if not self.title: # use only the first title
          self.title = self.accumulator
 
 
+   def do_link(self, attrs):
+      has_icon = False
+      href = None
+
+      for attrname, value in attrs:
+         if value:
+            value = value.strip().lower()
+            if (attrname == 'rel') and (value in ('icon', 'shortcut icon')):
+               has_icon = True
+            elif attrname == 'href':
+               href = value
+
+      if has_icon:
+         self.icon = href
+      else:
+         self.icon = None
+
+
+import re
+entity_re = re.compile("(&#[0-9]+;)")
+
+def recode_entities(title, charset):
+   output = []
+   for part in entity_re.split(title):
+      if entity_re.match(part):
+         part = unichr(int(part[2:-1])).encode(charset, "replace")
+      output.append(part)
+
+   return ''.join(output)
+
+
 def parse_html(filename, charset=None, log=None):
+   if charset:
+      try:
+         codecs.lookup(charset) # In case of unknown charset...
+      except (ValueError, LookupError):
+         charset = None         # ...try charset from HTML
+
    infile = open(filename, 'r')
    parser = HTMLParser(charset)
 
@@ -78,34 +118,37 @@ def parse_html(filename, charset=None, log=None):
    except (HTMLParseError, HTMLHeadDone):
       pass
 
+   title = parser.title
+
    if not parser.charset:
-      title = parser.title
-      ascii = 1
-      for c in title:
-         if not (32 <= ord(c) <= 127): # non-ASCII character
-            ascii = 0
-            break
-      if not ascii:
+      try:
+         unicode(title, "ascii")
+      except UnicodeDecodeError:
          parser.charset = DEFAULT_CHARSET
+
+   if parser.charset:
+      parser.charset = parser.charset.replace("windows-", "cp").lower()
+
    if parser.charset and (parser.charset <> current_charset):
       try:
          if parser.meta_charset:
             if log: log("   META charset   : %s" % parser.charset)
          else:
             if log: log("   charset        : %s" % parser.charset)
-         if log: log("   title          : %s" % parser.title)
-         parser.title = unicode(parser.title, parser.charset, "replace").encode(current_charset, "replace")
+         if log: log("   title          : %s" % title)
+         title = unicode(title, parser.charset, "replace").encode(current_charset, "replace")
          if log: log("   current charset: %s" % current_charset)
-         if log: log("   converted title: %s" % parser.title)
+         if log: log("   converted title: %s" % title)
       except LookupError:
          if log: log("   unknown charset: `%s' or `%s'" % (parser.charset, current_charset))
 
+   parser.title = recode_entities(title, current_charset)
    return parser
 
 
 if __name__ == '__main__':
-   import sys
    parser = parse_html(sys.argv[1])
    print parser.charset
    print parser.title
    print parser.refresh
+   print parser.icon