mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2025-08-19 12:59:36 -07:00
Use six to standardize imports between Python 2 and Python 3
This commit is contained in:
parent
382d108db2
commit
cf1ae938fc
4 changed files with 33 additions and 34 deletions
|
@ -1,4 +1,6 @@
|
||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
|
from six.moves import reload_module
|
||||||
import locale
|
import locale
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
@ -252,7 +254,7 @@ def initialize(section=None):
|
||||||
SYS_ENCODING = 'UTF-8'
|
SYS_ENCODING = 'UTF-8'
|
||||||
|
|
||||||
if not hasattr(sys, "setdefaultencoding"):
|
if not hasattr(sys, "setdefaultencoding"):
|
||||||
reload(sys)
|
reload_module(sys)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# pylint: disable=E1101
|
# pylint: disable=E1101
|
||||||
|
|
|
@ -18,12 +18,8 @@ from core.transmissionrpc.torrent import Torrent
|
||||||
from core.transmissionrpc.session import Session
|
from core.transmissionrpc.session import Session
|
||||||
from six import PY3, integer_types, string_types, iteritems
|
from six import PY3, integer_types, string_types, iteritems
|
||||||
|
|
||||||
if PY3:
|
from six.moves.urllib_parse import urlparse
|
||||||
from urllib.parse import urlparse
|
from six.moves.urllib_request import urlopen
|
||||||
from urllib.request import urlopen
|
|
||||||
else:
|
|
||||||
from urlparse import urlparse
|
|
||||||
from urllib2 import urlopen
|
|
||||||
|
|
||||||
|
|
||||||
def debug_httperror(error):
|
def debug_httperror(error):
|
||||||
|
|
|
@ -4,19 +4,15 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from core.transmissionrpc.error import HTTPHandlerError
|
from six.moves.urllib_request import (
|
||||||
from six import PY3
|
build_opener, install_opener,
|
||||||
|
HTTPBasicAuthHandler, HTTPDigestAuthHandler, HTTPPasswordMgrWithDefaultRealm,
|
||||||
|
Request,
|
||||||
|
)
|
||||||
|
from six.moves.urllib_error import HTTPError, URLError
|
||||||
|
from six.moves.http_client import BadStatusLine
|
||||||
|
|
||||||
if PY3:
|
from core.transmissionrpc.error import HTTPHandlerError
|
||||||
from urllib.request import Request, build_opener, \
|
|
||||||
HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, HTTPDigestAuthHandler
|
|
||||||
from urllib.error import HTTPError, URLError
|
|
||||||
from http.client import BadStatusLine
|
|
||||||
else:
|
|
||||||
from urllib2 import Request, build_opener, \
|
|
||||||
HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, HTTPDigestAuthHandler
|
|
||||||
from urllib2 import HTTPError, URLError
|
|
||||||
from httplib import BadStatusLine
|
|
||||||
|
|
||||||
|
|
||||||
class HTTPHandler(object):
|
class HTTPHandler(object):
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
# coding=utf8
|
# coding=utf8
|
||||||
|
|
||||||
import urllib
|
|
||||||
import urllib2
|
|
||||||
import urlparse
|
|
||||||
import cookielib
|
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
import StringIO
|
|
||||||
|
from six import StringIO
|
||||||
|
from six.moves.http_cookiejar import CookieJar
|
||||||
|
from six.moves.urllib_error import HTTPError
|
||||||
|
from six.moves.urllib_parse import urljoin, urlencode
|
||||||
|
from six.moves.urllib_request import (
|
||||||
|
build_opener, install_opener,
|
||||||
|
HTTPBasicAuthHandler, HTTPCookieProcessor,
|
||||||
|
Request,
|
||||||
|
)
|
||||||
|
|
||||||
from core.utorrent.upload import MultiPartForm
|
from core.utorrent.upload import MultiPartForm
|
||||||
|
|
||||||
|
@ -23,23 +28,23 @@ class UTorrentClient(object):
|
||||||
def _make_opener(self, realm, base_url, username, password):
|
def _make_opener(self, realm, base_url, username, password):
|
||||||
"""uTorrent API need HTTP Basic Auth and cookie support for token verify."""
|
"""uTorrent API need HTTP Basic Auth and cookie support for token verify."""
|
||||||
|
|
||||||
auth_handler = urllib2.HTTPBasicAuthHandler()
|
auth_handler = HTTPBasicAuthHandler()
|
||||||
auth_handler.add_password(realm=realm,
|
auth_handler.add_password(realm=realm,
|
||||||
uri=base_url,
|
uri=base_url,
|
||||||
user=username,
|
user=username,
|
||||||
passwd=password)
|
passwd=password)
|
||||||
opener = urllib2.build_opener(auth_handler)
|
opener = build_opener(auth_handler)
|
||||||
urllib2.install_opener(opener)
|
install_opener(opener)
|
||||||
|
|
||||||
cookie_jar = cookielib.CookieJar()
|
cookie_jar = CookieJar()
|
||||||
cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar)
|
cookie_handler = HTTPCookieProcessor(cookie_jar)
|
||||||
|
|
||||||
handlers = [auth_handler, cookie_handler]
|
handlers = [auth_handler, cookie_handler]
|
||||||
opener = urllib2.build_opener(*handlers)
|
opener = build_opener(*handlers)
|
||||||
return opener
|
return opener
|
||||||
|
|
||||||
def _get_token(self):
|
def _get_token(self):
|
||||||
url = urlparse.urljoin(self.base_url, 'token.html')
|
url = urljoin(self.base_url, 'token.html')
|
||||||
response = self.opener.open(url)
|
response = self.opener.open(url)
|
||||||
token_re = "<div id='token' style='display:none;'>([^<>]+)</div>"
|
token_re = "<div id='token' style='display:none;'>([^<>]+)</div>"
|
||||||
match = re.search(token_re, response.read())
|
match = re.search(token_re, response.read())
|
||||||
|
@ -122,8 +127,8 @@ class UTorrentClient(object):
|
||||||
|
|
||||||
def _action(self, params, body=None, content_type=None):
|
def _action(self, params, body=None, content_type=None):
|
||||||
# about token, see https://github.com/bittorrent/webui/wiki/TokenSystem
|
# about token, see https://github.com/bittorrent/webui/wiki/TokenSystem
|
||||||
url = self.base_url + '?token=' + self.token + '&' + urllib.urlencode(params)
|
url = self.base_url + '?token=' + self.token + '&' + urlencode(params)
|
||||||
request = urllib2.Request(url)
|
request = Request(url)
|
||||||
|
|
||||||
if body:
|
if body:
|
||||||
request.add_data(body)
|
request.add_data(body)
|
||||||
|
@ -134,5 +139,5 @@ class UTorrentClient(object):
|
||||||
try:
|
try:
|
||||||
response = self.opener.open(request)
|
response = self.opener.open(request)
|
||||||
return response.code, json.loads(response.read())
|
return response.code, json.loads(response.read())
|
||||||
except urllib2.HTTPError, e:
|
except HTTPError:
|
||||||
raise
|
raise
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue