mirror of
https://github.com/jellyfin/jellyfin-kodi.git
synced 2024-11-10 04:06:11 +00:00
Update downloadUtils requests
Code reduction, fix connections not being released.
This commit is contained in:
parent
a6995c9b8f
commit
213f42f4a5
1 changed files with 43 additions and 74 deletions
|
@ -193,8 +193,6 @@ class DownloadUtils():
|
||||||
'Accept-Charset': 'UTF-8,*',
|
'Accept-Charset': 'UTF-8,*',
|
||||||
'Authorization': auth
|
'Authorization': auth
|
||||||
}
|
}
|
||||||
log("Header: %s" % header, 2)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
userId = self.userId
|
userId = self.userId
|
||||||
token = self.token
|
token = self.token
|
||||||
|
@ -210,44 +208,36 @@ class DownloadUtils():
|
||||||
'Authorization': auth,
|
'Authorization': auth,
|
||||||
'X-MediaBrowser-Token': token
|
'X-MediaBrowser-Token': token
|
||||||
}
|
}
|
||||||
log("Header: %s" % header, 2)
|
|
||||||
|
|
||||||
return header
|
return header
|
||||||
|
|
||||||
def downloadUrl(self, url, postBody=None, action_type="GET", parameters=None,
|
def downloadUrl(self, url, postBody=None, action_type="GET", parameters=None,
|
||||||
authenticate=True):
|
authenticate=True):
|
||||||
|
|
||||||
log("=== ENTER downloadUrl ===", 2)
|
log("===== ENTER downloadUrl =====", 2)
|
||||||
|
|
||||||
|
session = requests
|
||||||
default_link = ""
|
default_link = ""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# If user is authenticated
|
|
||||||
|
kwargs = {
|
||||||
|
'timeout': self.timeout,
|
||||||
|
'json': postBody,
|
||||||
|
'params': parameters
|
||||||
|
}
|
||||||
|
|
||||||
if authenticate:
|
if authenticate:
|
||||||
# Get requests session
|
|
||||||
try:
|
|
||||||
s = self.s
|
|
||||||
# Replace for the real values
|
|
||||||
url = url.replace("{server}", self.server)
|
|
||||||
url = url.replace("{UserId}", self.userId)
|
|
||||||
|
|
||||||
# Prepare request
|
if self.s is not None:
|
||||||
if action_type == "GET":
|
session = self.s
|
||||||
r = s.get(url, json=postBody, params=parameters, timeout=self.timeout)
|
else:
|
||||||
elif action_type == "POST":
|
|
||||||
r = s.post(url, json=postBody, timeout=self.timeout)
|
|
||||||
elif action_type == "DELETE":
|
|
||||||
r = s.delete(url, json=postBody, timeout=self.timeout)
|
|
||||||
|
|
||||||
except AttributeError:
|
|
||||||
# request session does not exists
|
# request session does not exists
|
||||||
# Get user information
|
# Get user information
|
||||||
self.userId = window('emby_currUser')
|
self.userId = window('emby_currUser')
|
||||||
self.server = window('emby_server%s' % self.userId)
|
self.server = window('emby_server%s' % self.userId)
|
||||||
self.token = window('emby_accessToken%s' % self.userId)
|
self.token = window('emby_accessToken%s' % self.userId)
|
||||||
header = self.getHeader()
|
|
||||||
verifyssl = False
|
verifyssl = False
|
||||||
cert = None
|
|
||||||
|
|
||||||
# IF user enables ssl verification
|
# IF user enables ssl verification
|
||||||
if settings('sslverify') == "true":
|
if settings('sslverify') == "true":
|
||||||
|
@ -255,38 +245,16 @@ class DownloadUtils():
|
||||||
if settings('sslcert') != "None":
|
if settings('sslcert') != "None":
|
||||||
verifyssl = settings('sslcert')
|
verifyssl = settings('sslcert')
|
||||||
|
|
||||||
|
kwargs['headers'] = self.getHeader()
|
||||||
|
|
||||||
# Replace for the real values
|
# Replace for the real values
|
||||||
url = url.replace("{server}", self.server)
|
url = url.replace("{server}", self.server)
|
||||||
url = url.replace("{UserId}", self.userId)
|
url = url.replace("{UserId}", self.userId)
|
||||||
|
kwargs['url'] = url
|
||||||
|
|
||||||
# Prepare request
|
else: # User is not authenticated
|
||||||
if action_type == "GET":
|
kwargs['url'] = url
|
||||||
r = requests.get(url,
|
kwargs['headers'] = self.getHeader(authenticate=False)
|
||||||
json=postBody,
|
|
||||||
params=parameters,
|
|
||||||
headers=header,
|
|
||||||
timeout=self.timeout,
|
|
||||||
verify=verifyssl)
|
|
||||||
|
|
||||||
elif action_type == "POST":
|
|
||||||
r = requests.post(url,
|
|
||||||
json=postBody,
|
|
||||||
headers=header,
|
|
||||||
timeout=self.timeout,
|
|
||||||
verify=verifyssl)
|
|
||||||
|
|
||||||
elif action_type == "DELETE":
|
|
||||||
r = requests.delete(url,
|
|
||||||
json=postBody,
|
|
||||||
headers=header,
|
|
||||||
timeout=self.timeout,
|
|
||||||
verify=verifyssl)
|
|
||||||
|
|
||||||
# If user is not authenticated
|
|
||||||
elif not authenticate:
|
|
||||||
|
|
||||||
header = self.getHeader(authenticate=False)
|
|
||||||
verifyssl = False
|
|
||||||
|
|
||||||
# If user enables ssl verification
|
# If user enables ssl verification
|
||||||
try:
|
try:
|
||||||
|
@ -294,32 +262,21 @@ class DownloadUtils():
|
||||||
if self.sslclient is not None:
|
if self.sslclient is not None:
|
||||||
verifyssl = self.sslclient
|
verifyssl = self.sslclient
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
verifyssl = False
|
||||||
|
finally:
|
||||||
# Prepare request
|
kwargs['verify'] = verifyssl
|
||||||
if action_type == "GET":
|
|
||||||
r = requests.get(url,
|
|
||||||
json=postBody,
|
|
||||||
params=parameters,
|
|
||||||
headers=header,
|
|
||||||
timeout=self.timeout,
|
|
||||||
verify=verifyssl)
|
|
||||||
|
|
||||||
elif action_type == "POST":
|
|
||||||
r = requests.post(url,
|
|
||||||
json=postBody,
|
|
||||||
headers=header,
|
|
||||||
timeout=self.timeout,
|
|
||||||
verify=verifyssl)
|
|
||||||
|
|
||||||
##### THE RESPONSE #####
|
##### THE RESPONSE #####
|
||||||
log(r.url, 2)
|
log(kwargs, 2)
|
||||||
|
r = self.__requests(action_type, session, **kwargs)
|
||||||
|
|
||||||
if r.status_code == 204:
|
if r.status_code == 204:
|
||||||
# No body in the response
|
# No body in the response
|
||||||
log("====== 204 Success ======", 2)
|
log("====== 204 Success ======", 2)
|
||||||
|
# Read response to release connection
|
||||||
|
r.content
|
||||||
|
|
||||||
elif r.status_code == requests.codes.ok:
|
elif r.status_code == requests.codes.ok:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# UNICODE - JSON object
|
# UNICODE - JSON object
|
||||||
r = r.json()
|
r = r.json()
|
||||||
|
@ -330,7 +287,8 @@ class DownloadUtils():
|
||||||
except:
|
except:
|
||||||
if r.headers.get('content-type') != "text/html":
|
if r.headers.get('content-type') != "text/html":
|
||||||
log("Unable to convert the response for: %s" % url, 1)
|
log("Unable to convert the response for: %s" % url, 1)
|
||||||
else:
|
|
||||||
|
else: # Bad status code
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
|
|
||||||
##### EXCEPTIONS #####
|
##### EXCEPTIONS #####
|
||||||
|
@ -394,3 +352,14 @@ class DownloadUtils():
|
||||||
log(e, 1)
|
log(e, 1)
|
||||||
|
|
||||||
return default_link
|
return default_link
|
||||||
|
|
||||||
|
def __requests(self, action, session=requests, **kwargs):
|
||||||
|
|
||||||
|
if action == "GET":
|
||||||
|
r = session.get(**kwargs)
|
||||||
|
elif action == "POST":
|
||||||
|
r = session.post(**kwargs)
|
||||||
|
elif action == "DELETE":
|
||||||
|
r = session.delete(**kwargs)
|
||||||
|
|
||||||
|
return r
|
Loading…
Reference in a new issue