]> git.phdru.name Git - bookmarks_db.git/blobdiff - bkmk_objects.py
Fix(robots): Do not parse empty strings
[bookmarks_db.git] / bkmk_objects.py
index 89647aa52eb57ecced2d4681834bba4b79f4ffaf..741567039d7763077e1255ef162e6f09bb498e28 100644 (file)
@@ -15,7 +15,14 @@ __all__ = ['Folder', 'Bookmark', 'Ruler', 'Walker', 'Writer', 'Robot',
 
 
 import os
-import urllib
+try:
+    from urllib.parse import quote, \
+        splittype, splithost, splituser, splitpasswd, \
+        splitport
+except ImportError:
+    from urllib import quote, \
+        splittype, splithost, splituser, splitpasswd, \
+        splitport
 
 BKMK_FORMAT = os.environ.get("BKMK_FORMAT", "MOZILLA")
 
@@ -58,14 +65,14 @@ class Bookmark(object):
     def __init__(self, href, add_date, last_visit=None, last_modified=None,
                  keyword=None, comment='', icon_href=None, icon=None,
                  charset=None, parser_charset=None):
-        protocol, request = urllib.splittype(href)
+        protocol, request = splittype(href)
         user, password, port = None, None, None
-        host, path = urllib.splithost(request)
+        host, path = splithost(request)
         if host:
-            user, host = urllib.splituser(host)
+            user, host = splituser(host)
             if user:
-                user, password = urllib.splitpasswd(user)
-            host, port = urllib.splitport(host)
+                user, password = splitpasswd(user)
+            host, port = splitport(host)
             if port: port = int(port)
 
         if protocol == 'place':
@@ -73,12 +80,12 @@ class Bookmark(object):
         else:
             href = protocol + "://"
         if user:
-            href += urllib.quote(user)
+            href += quote(user)
             if password:
-                href += ':' + urllib.quote(password)
+                href += ':' + quote(password)
             href += '@'
         if host:
-            href += host.decode(parser_charset or 'utf-8').encode('idna')
+            href += host.encode('idna').decode('ascii')
             if port:
                 href += ':%d' % port
         if path:
@@ -221,10 +228,15 @@ def quote_title(title):
 
 def unquote_title(title):
     if BKMK_FORMAT == "MOZILLA":
-        from HTMLParser import HTMLParser
-        title = HTMLParser().unescape(
-            title.replace("&", '&').decode('utf-8'))
-        title = title.encode('utf-8').replace("'", "'")
+        try:
+            from HTMLParser import HTMLParser
+        except ImportError:
+            from html import unescape
+        else:
+            unescape = HTMLParser().unescape
+        title = unescape(
+            title.replace("&", '&'))
+        title = title.replace("'", "'")
     return title