]> git.phdru.name Git - bookmarks_db.git/blobdiff - Robots/bkmk_rurllib_py3.py
Style(Robots/bkmk_rurllib_py3): Remove unused variable
[bookmarks_db.git] / Robots / bkmk_rurllib_py3.py
index 796a6fca3425bfb8d91d035968d02be246605bb3..ae88b1956f54e5f894f9ae2c584258408e5cfa9b 100644 (file)
@@ -11,6 +11,8 @@ __license__ = "GNU GPL"
 __all__ = ['robot_urllib_py3']
 
 
+import http.client
+import socket
 import sys
 import urllib.request
 
@@ -41,6 +43,8 @@ class MyURLopener(urllib.request.URLopener):
     http_error_303 = http_error_301
     # Error 307 -- relocated (temporarily)
     http_error_307 = http_error_301
+    # Error 308 -- relocated (permanently)
+    http_error_308 = http_error_301
 
     # Error 401 -- authentication required
     def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
@@ -53,6 +57,9 @@ class MyURLopener(urllib.request.URLopener):
             fp.close()
         raise IOError(('http error', errcode, errmsg, headers))
 
+    def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
+        return urllib.request.URLopener.open(self, fullurl, data)
+
 
 urllib.request._opener = opener = MyURLopener()
 
@@ -102,15 +109,39 @@ class robot_urllib_py3(robot_base):
                     # Remove Accept-Charset
                     del opener.addheaders[-1]
 
-            infile = open(fname, 'rt')
-            content = infile.read()
-            infile.close()
-
+            possible_encodings = []
+            for encoding in (
+                    bookmark.charset,
+                    sys.getfilesystemencoding(),
+                    'utf-8',
+            ):
+                if encoding and encoding not in possible_encodings:
+                    possible_encodings.append(encoding)
+            content = e = None
+            infile = open(fname, 'rb')
+            try:
+                content = infile.read()
+            except Exception:
+                content = None
+            finally:
+                infile.close()
+
+            if content is None:
+                e = str(e)
+                return (
+                    'ERROR: ' + e,
+                    None, None, None, None
+                )
             return None, None, None, headers, content
 
         except RedirectException as e:
             return None, e.errcode, e.newurl, None, None
 
+        except (OSError, http.client.IncompleteRead) as e:
+            error = str(e)
+            self.log('   Error: %s' % error)
+            return error, None, None, None, None
+
         except IOError as e:
             if (e[0] == "http error") and (e[1] == -1):
                 error = None