mirror of
https://github.com/jellyfin/jellyfin-kodi.git
synced 2025-05-30 04:46:13 +00:00
Recreated Branch
This commit is contained in:
parent
2fbdd191d5
commit
417b9fb938
3 changed files with 196 additions and 217 deletions
|
@ -1,5 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import division, absolute_import, print_function, unicode_literals
|
||||
import requests
|
||||
import json
|
||||
import logging
|
||||
from helper.utils import settings
|
||||
|
||||
LOG = logging.getLogger('JELLYFIN.' + __name__)
|
||||
|
||||
|
||||
def jellyfin_url(client, handler):
|
||||
|
@ -35,6 +40,9 @@ class API(object):
|
|||
'''
|
||||
def __init__(self, client, *args, **kwargs):
|
||||
self.client = client
|
||||
self.config = client.config
|
||||
self.default_timeout = 5
|
||||
|
||||
|
||||
def _http(self, action, url, request={}):
|
||||
request.update({'type': action, 'handler': url})
|
||||
|
@ -344,3 +352,97 @@ class API(object):
|
|||
return self._delete("Videos/ActiveEncodings", params={
|
||||
'DeviceId': device_id
|
||||
})
|
||||
|
||||
|
||||
#################################################################################################
|
||||
|
||||
# New API calls
|
||||
|
||||
#################################################################################################
|
||||
def get_default_headers(self):
|
||||
auth = "MediaBrowser "
|
||||
auth += "Client=%s, " % self.config.data['app.name'].encode('utf-8')
|
||||
auth += "Device=%s, " % self.config.data['app.device_name'].encode('utf-8')
|
||||
auth += "DeviceId=%s, " % self.config.data['app.device_id'].encode('utf-8')
|
||||
auth += "Version=%s" % self.config.data['app.version'].encode('utf-8')
|
||||
|
||||
return {
|
||||
"Accept": "application/json",
|
||||
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
|
||||
"X-Application": "%s/%s" % (self.config.data['app.name'], self.config.data['app.version']),
|
||||
"Accept-Charset": "UTF-8,*",
|
||||
"Accept-encoding": "gzip",
|
||||
"User-Agent": self.config.data['http.user_agent'] or "%s/%s" % (self.config.data['app.name'], self.config.data['app.version']),
|
||||
"x-emby-authorization": auth
|
||||
}
|
||||
|
||||
def send_request(self, url, path, method=None, timeout=None, headers=None, data=None):
|
||||
if not timeout:
|
||||
LOG.debug("No timeout set, using default")
|
||||
timeout = self.default_timeout
|
||||
if not headers:
|
||||
LOG.debug("No headers set, using default")
|
||||
headers = self.get_default_headers()
|
||||
if not method:
|
||||
LOG.debug("No method set, using default")
|
||||
method = "get" #Defaults to get request if none specified
|
||||
|
||||
request_method = getattr(requests, method.lower())
|
||||
url = "%s/%s" % (url, path)
|
||||
request_settings = {
|
||||
"timeout": timeout,
|
||||
"headers": headers,
|
||||
"data": data
|
||||
}
|
||||
|
||||
if not settings('sslverify'):
|
||||
request_settings["verify"] = False
|
||||
|
||||
LOG.info("Sending %s request to %s" % (method, url))
|
||||
LOG.debug(request_settings)
|
||||
|
||||
return request_method(url, **request_settings)
|
||||
|
||||
|
||||
def login(self, server_url, username, password):
|
||||
path = "Users/AuthenticateByName"
|
||||
authData = {
|
||||
"username": username,
|
||||
"Pw": password or ""
|
||||
}
|
||||
|
||||
headers = self.get_default_headers()
|
||||
headers.update({'Content-type': "application/json"})
|
||||
|
||||
try:
|
||||
LOG.info("Trying to login to %s/%s as %s" % (server_url, path, username))
|
||||
response = self.send_request(server_url, path, method="post", headers=headers, data=json.dumps(authData))
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
LOG.error("Failed to login to server with status code: "+str(response.status_code))
|
||||
LOG.error(response.text)
|
||||
LOG.debug(headers)
|
||||
|
||||
return {}
|
||||
except Exception as e: #Find exceptions for likely cases i.e, server timeout, etc
|
||||
LOG.error(e)
|
||||
|
||||
return {}
|
||||
|
||||
def validate_authentication_token(self, server):
|
||||
|
||||
url = "%s/%s" % (server['address'], "system/info")
|
||||
authTokenHeader = {
|
||||
'X-MediaBrowser-Token': server['AccessToken']
|
||||
}
|
||||
headers = self.get_default_headers()
|
||||
headers.update(authTokenHeader)
|
||||
|
||||
response = self.send_request(server['address'], "system/info", headers=headers)
|
||||
return response.json() if response.status_code == 200 else {}
|
||||
|
||||
def get_public_info(self, server_address):
|
||||
response = self.send_request(server_address, "system/info/public")
|
||||
return response.json() if response.status_code == 200 else {}
|
Loading…
Add table
Add a link
Reference in a new issue