X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=sidebyside;f=Robots%2Fbkmk_rurllib_py3.py;h=9493a589556a2aaeba7a88925a8b6a27344cfb3b;hb=38f9ac83edd748caa0d262ddaa6af5df9139bda9;hp=2b73347568813505a270d9308e1595172aa98819;hpb=30b81f6ecef5e78c70e2700a9b2837fa8f081095;p=bookmarks_db.git diff --git a/Robots/bkmk_rurllib_py3.py b/Robots/bkmk_rurllib_py3.py index 2b73347..9493a58 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 @@ -106,16 +107,37 @@ 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 = None + for encoding in possible_encodings: + infile = open(fname, 'rt', encoding=encoding) + try: + content = infile.read() + except UnicodeDecodeError: + infile.close() + continue + else: + break + infile.close() + + if content is None: + return ( + 'ERROR: File encoding was not recognized', + None, None, None, None + ) return None, None, None, headers, content except RedirectException as e: return None, e.errcode, e.newurl, None, None - except OSError as e: + except (OSError, http.client.IncompleteRead) as e: error = str(e) self.log(' Error: %s' % error) return error, None, None, None, None