X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=Robots%2Fbkmk_rurllib_py3.py;h=73f9b77ca3ef19f0106cc3bd3dc3e16902b5a818;hb=a67f7facca14260d7d0557a9542bf469b2e5b8e9;hp=aca07cd86a09d21845597a3d84f76f10c98a2a48;hpb=6d04cae4210534002c2e6ac94d08c48f3d667646;p=bookmarks_db.git diff --git a/Robots/bkmk_rurllib_py3.py b/Robots/bkmk_rurllib_py3.py index aca07cd..73f9b77 100644 --- a/Robots/bkmk_rurllib_py3.py +++ b/Robots/bkmk_rurllib_py3.py @@ -11,6 +11,7 @@ __license__ = "GNU GPL" __all__ = ['robot_urllib_py3'] +import http.client import socket import sys import urllib.request @@ -42,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): @@ -106,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 as e: + 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