2015-12-24 20:07:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
import json
|
|
|
|
import logging
|
2016-10-07 04:15:23 +00:00
|
|
|
import requests
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
import xbmcgui
|
|
|
|
|
|
|
|
import clientinfo
|
2016-09-27 02:50:58 +00:00
|
|
|
import connect.connectionmanager as connectionmanager
|
2016-09-04 10:18:31 +00:00
|
|
|
from utils import window, settings, language as lang
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
# Disable requests logging
|
2016-06-18 03:03:28 +00:00
|
|
|
from requests.packages.urllib3.exceptions import InsecureRequestWarning, InsecurePlatformWarning
|
2015-12-24 20:07:00 +00:00
|
|
|
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
|
2016-06-18 03:03:28 +00:00
|
|
|
requests.packages.urllib3.disable_warnings(InsecurePlatformWarning)
|
2016-07-24 08:59:48 +00:00
|
|
|
|
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
##################################################################################################
|
|
|
|
|
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
class DownloadUtils(object):
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
# Borg - multiple instances, shared state
|
|
|
|
_shared_state = {}
|
|
|
|
|
|
|
|
# Requests session
|
2016-09-27 02:50:58 +00:00
|
|
|
session = {}
|
|
|
|
session_requests = None
|
|
|
|
servers = {} # Multi server setup
|
2016-07-16 15:02:50 +00:00
|
|
|
default_timeout = 30
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
self.__dict__ = self._shared_state
|
2016-09-27 02:50:58 +00:00
|
|
|
self.client_info = clientinfo.ClientInfo()
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
def set_session(self, **kwargs):
|
2015-12-24 20:07:00 +00:00
|
|
|
# Reserved for userclient only
|
2016-09-27 02:50:58 +00:00
|
|
|
info = {}
|
|
|
|
for key in kwargs:
|
|
|
|
info[key] = kwargs[key]
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
self.session.update(info)
|
2016-09-29 06:59:30 +00:00
|
|
|
window('emby_server.json', value=self.session)
|
2016-09-27 06:33:09 +00:00
|
|
|
|
|
|
|
log.debug("Set info for server %s: %s", self.session['ServerId'], self.session)
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2017-05-06 00:08:04 +00:00
|
|
|
def get_token(self):
|
|
|
|
user = self._get_session_info()
|
|
|
|
token = user['Token']
|
|
|
|
return token
|
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
def add_server(self, server, ssl):
|
2015-12-24 20:07:00 +00:00
|
|
|
# Reserved for userclient only
|
2016-09-27 02:50:58 +00:00
|
|
|
server_id = server['Id']
|
|
|
|
info = {
|
|
|
|
'UserId': server['UserId'],
|
|
|
|
'Server': connectionmanager.getServerAddress(server, server['LastConnectionMode']),
|
|
|
|
'Token': server['AccessToken'],
|
|
|
|
'SSL': ssl
|
|
|
|
}
|
2016-10-07 04:15:23 +00:00
|
|
|
for server_info in self.servers:
|
|
|
|
if server_info == server_id:
|
|
|
|
server_info.update(info)
|
2016-09-27 02:50:58 +00:00
|
|
|
# Set window prop
|
2016-09-29 06:59:30 +00:00
|
|
|
self._set_server_properties(server_id, server['Name'], info)
|
2016-09-27 02:50:58 +00:00
|
|
|
log.info("updating %s to available servers: %s", server_id, self.servers)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.servers[server_id] = info
|
|
|
|
self._set_server_properties(server_id, server['Name'], json.dumps(info))
|
|
|
|
log.info("adding %s to available servers: %s", server_id, self.servers)
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
def reset_server(self, server_id):
|
2015-12-24 20:07:00 +00:00
|
|
|
# Reserved for userclient only
|
2016-09-27 02:50:58 +00:00
|
|
|
for server in self.servers:
|
|
|
|
if server['ServerId'] == server_id:
|
2016-09-27 09:10:40 +00:00
|
|
|
self.servers.pop(server)
|
2016-09-27 02:50:58 +00:00
|
|
|
window('emby_server%s.json' % server_id, clear=True)
|
|
|
|
window('emby_server%s.name' % server_id, clear=True)
|
|
|
|
log.info("removing %s from available servers", server_id)
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
@staticmethod
|
|
|
|
def _set_server_properties(server_id, name, info):
|
|
|
|
window('emby_server%s.json' % server_id, value=info)
|
|
|
|
window('emby_server%s.name' % server_id, value=name)
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
def post_capabilities(self, device_id):
|
2015-12-24 20:07:00 +00:00
|
|
|
# Post settings to session
|
|
|
|
url = "{server}/emby/Sessions/Capabilities/Full?format=json"
|
|
|
|
data = {
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
'PlayableMediaTypes': "Audio,Video",
|
|
|
|
'SupportsMediaControl': True,
|
|
|
|
'SupportedCommands': (
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
"MoveUp,MoveDown,MoveLeft,MoveRight,Select,"
|
|
|
|
"Back,ToggleContextMenu,ToggleFullscreen,ToggleOsdMenu,"
|
|
|
|
"GoHome,PageUp,NextLetter,GoToSearch,"
|
|
|
|
"GoToSettings,PageDown,PreviousLetter,TakeScreenshot,"
|
|
|
|
"VolumeUp,VolumeDown,ToggleMute,SendString,DisplayMessage,"
|
|
|
|
"SetAudioStreamIndex,SetSubtitleStreamIndex,"
|
|
|
|
|
|
|
|
"Mute,Unmute,SetVolume,"
|
|
|
|
"Play,Playstate,PlayNext"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:21:05 +00:00
|
|
|
self.downloadUrl(url, postBody=data, action_type="POST")
|
2016-10-10 11:14:10 +00:00
|
|
|
log.debug("Posted capabilities to %s", self.session['Server'])
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
# Attempt at getting sessionId
|
2016-09-27 02:50:58 +00:00
|
|
|
url = "{server}/emby/Sessions?DeviceId=%s&format=json" % device_id
|
2016-12-01 04:28:42 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
try:
|
2016-12-01 04:28:42 +00:00
|
|
|
result = self.downloadUrl(url)
|
2016-10-07 04:15:23 +00:00
|
|
|
session_id = result[0]['Id']
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2016-12-01 04:28:42 +00:00
|
|
|
except Exception as error:
|
|
|
|
log.error("Failed to retrieve the session id: " + str(error))
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
else:
|
2016-10-07 04:15:23 +00:00
|
|
|
log.info("SessionId: %s", session_id)
|
|
|
|
window('emby_sessionId', value=session_id)
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
# Post any permanent additional users
|
2016-10-07 04:15:23 +00:00
|
|
|
additional_users = settings('additionalUsers')
|
|
|
|
if additional_users:
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
additional_users = additional_users.split(',')
|
|
|
|
log.info("List of permanent users added to the session: %s", additional_users)
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
# Get the user list from server to get the userId
|
|
|
|
url = "{server}/emby/Users?format=json"
|
|
|
|
result = self.downloadUrl(url)
|
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
for additional in additional_users:
|
|
|
|
add_user = additional.decode('utf-8').lower()
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
# Compare to server users to list of permanent additional users
|
|
|
|
for user in result:
|
|
|
|
username = user['Name'].lower()
|
|
|
|
|
2016-10-07 04:15:23 +00:00
|
|
|
if username in add_user:
|
|
|
|
user_id = user['Id']
|
|
|
|
url = ("{server}/emby/Sessions/%s/Users/%s?format=json"
|
|
|
|
% (session_id, user_id))
|
2016-04-04 21:21:05 +00:00
|
|
|
self.downloadUrl(url, postBody={}, action_type="POST")
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
def start_session(self):
|
2015-12-24 20:07:00 +00:00
|
|
|
# User is identified from this point
|
|
|
|
# Attach authenticated header to the session
|
2016-09-27 06:33:09 +00:00
|
|
|
session = requests.Session()
|
|
|
|
session.headers = self.get_header()
|
|
|
|
session.verify = self.session['SSL']
|
2015-12-24 20:07:00 +00:00
|
|
|
# Retry connections to the server
|
2016-09-27 06:33:09 +00:00
|
|
|
session.mount("http://", requests.adapters.HTTPAdapter(max_retries=1))
|
|
|
|
session.mount("https://", requests.adapters.HTTPAdapter(max_retries=1))
|
|
|
|
self.session_requests = session
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
log.info("requests session started on: %s", self.session['Server'])
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
def stop_session(self):
|
2015-12-24 20:07:00 +00:00
|
|
|
try:
|
2016-09-27 02:50:58 +00:00
|
|
|
self.session_requests.close()
|
|
|
|
except Exception as error:
|
|
|
|
log.error(error)
|
|
|
|
log.warn("requests session could not be terminated")
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
def get_header(self, server_id=None, authenticate=True):
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
device_name = self.client_info.get_device_name().encode('utf-8')
|
|
|
|
device_id = self.client_info.get_device_id()
|
|
|
|
version = self.client_info.get_version()
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-07-16 15:02:50 +00:00
|
|
|
if authenticate:
|
2016-09-27 02:50:58 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
user = self._get_session_info(server_id)
|
2016-09-27 02:50:58 +00:00
|
|
|
user_id = user['UserId']
|
|
|
|
token = user['Token']
|
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
auth = (
|
2016-07-16 15:02:50 +00:00
|
|
|
'MediaBrowser UserId="%s", Client="Kodi", Device="%s", DeviceId="%s", Version="%s"'
|
2016-09-27 02:50:58 +00:00
|
|
|
% (user_id, device_name, device_id, version)
|
|
|
|
)
|
2015-12-24 20:07:00 +00:00
|
|
|
header = {
|
2016-07-16 15:02:50 +00:00
|
|
|
'Authorization': auth,
|
2016-09-27 02:50:58 +00:00
|
|
|
'X-MediaBrowser-Token': token
|
2016-03-31 03:37:37 +00:00
|
|
|
}
|
2015-12-24 20:07:00 +00:00
|
|
|
else:
|
|
|
|
auth = (
|
2016-07-16 15:02:50 +00:00
|
|
|
'MediaBrowser Client="Kodi", Device="%s", DeviceId="%s", Version="%s"'
|
2016-09-27 02:50:58 +00:00
|
|
|
% (device_name, device_id, version)
|
|
|
|
)
|
|
|
|
header = {'Authorization': auth}
|
2016-09-06 22:43:15 +00:00
|
|
|
|
2016-09-27 02:50:58 +00:00
|
|
|
header.update({
|
|
|
|
'Content-type': 'application/json',
|
|
|
|
'Accept-encoding': 'gzip',
|
|
|
|
'Accept-Charset': 'UTF-8,*',
|
|
|
|
})
|
|
|
|
return header
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
def downloadUrl(self, url, postBody=None, action_type="GET", parameters=None,
|
2016-09-27 02:50:58 +00:00
|
|
|
authenticate=True, server_id=None):
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("===== ENTER downloadUrl =====")
|
2016-09-27 09:10:40 +00:00
|
|
|
|
2016-07-16 00:02:30 +00:00
|
|
|
kwargs = {}
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 09:10:40 +00:00
|
|
|
try:
|
2016-09-27 06:33:09 +00:00
|
|
|
# Ensure server info is loaded
|
|
|
|
self._ensure_server(server_id)
|
|
|
|
server = self.session if server_id is None else self.servers[server_id]
|
|
|
|
|
2017-02-05 11:50:01 +00:00
|
|
|
requires_server = False
|
|
|
|
if url.find("{server}") > -1 or url.find("{UserId}") > -1:
|
|
|
|
requires_server = True
|
|
|
|
|
|
|
|
if requires_server and (not server or not server.get("Server") or not server.get("UserId")):
|
2017-01-30 05:05:41 +00:00
|
|
|
log.info("Aborting download, Server Details Error: %s url=%s" % (server, url))
|
|
|
|
exc = Exception("Aborting download, Server Details Error: %s url=%s" % (server, url))
|
2017-02-05 00:00:20 +00:00
|
|
|
exc.quiet = True
|
2017-01-30 05:05:41 +00:00
|
|
|
raise exc
|
2017-01-11 02:14:55 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
if server_id is None and self.session_requests is not None: # Main server
|
|
|
|
session = self.session_requests
|
2016-09-04 10:18:31 +00:00
|
|
|
else:
|
2016-10-07 04:15:23 +00:00
|
|
|
session = requests
|
2016-07-16 00:02:30 +00:00
|
|
|
kwargs.update({
|
2016-09-27 02:50:58 +00:00
|
|
|
'verify': server['SSL'],
|
|
|
|
'headers': self.get_header(server_id, authenticate)
|
2016-07-16 00:02:30 +00:00
|
|
|
})
|
2016-09-27 06:33:09 +00:00
|
|
|
|
|
|
|
# Replace for the real values
|
2017-02-05 11:50:01 +00:00
|
|
|
if requires_server:
|
|
|
|
url = url.replace("{server}", server['Server'])
|
|
|
|
url = url.replace("{UserId}", server['UserId'])
|
2016-09-04 10:18:31 +00:00
|
|
|
|
2016-11-17 05:55:44 +00:00
|
|
|
# does the URL look ok
|
|
|
|
if url.startswith('/'):
|
2016-11-22 12:05:16 +00:00
|
|
|
exc = Exception("URL Error: " + url)
|
2017-02-05 00:00:20 +00:00
|
|
|
exc.quiet = True
|
2016-11-22 12:05:16 +00:00
|
|
|
raise exc
|
2016-11-17 05:55:44 +00:00
|
|
|
|
2016-07-16 00:02:30 +00:00
|
|
|
##### PREPARE REQUEST #####
|
|
|
|
kwargs.update({
|
|
|
|
'url': url,
|
2016-07-16 15:02:50 +00:00
|
|
|
'timeout': self.default_timeout,
|
2016-07-16 00:02:30 +00:00
|
|
|
'json': postBody,
|
|
|
|
'params': parameters
|
|
|
|
})
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
##### THE RESPONSE #####
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug(kwargs)
|
2016-09-27 09:10:40 +00:00
|
|
|
response = self._requests(action_type, session, **kwargs)
|
2016-10-04 06:14:14 +00:00
|
|
|
#response = requests.get('http://httpbin.org/status/400')
|
2016-07-15 23:01:59 +00:00
|
|
|
|
2016-09-27 09:10:40 +00:00
|
|
|
if response.status_code == 204:
|
2015-12-24 20:07:00 +00:00
|
|
|
# No body in the response
|
2016-07-24 08:59:48 +00:00
|
|
|
log.debug("====== 204 Success ======")
|
2016-07-15 23:01:59 +00:00
|
|
|
# Read response to release connection
|
2016-09-27 09:10:40 +00:00
|
|
|
response.content
|
2016-11-12 23:39:14 +00:00
|
|
|
if action_type == "GET":
|
2017-02-03 23:42:22 +00:00
|
|
|
raise Exception("Response Code 204 for GET request")
|
2016-11-12 23:39:14 +00:00
|
|
|
else:
|
2016-11-14 00:00:26 +00:00
|
|
|
# this is probably valid for DELETE and PUT
|
2016-11-12 23:39:14 +00:00
|
|
|
return None
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 09:10:40 +00:00
|
|
|
elif response.status_code == requests.codes.ok:
|
2016-11-12 23:39:14 +00:00
|
|
|
# UNICODE - JSON object
|
|
|
|
json_data = response.json()
|
|
|
|
log.debug("====== 200 Success ======")
|
|
|
|
log.debug("Response: %s", json_data)
|
|
|
|
return json_data
|
2016-07-15 23:01:59 +00:00
|
|
|
|
|
|
|
else: # Bad status code
|
2016-09-27 09:10:40 +00:00
|
|
|
log.error("=== Bad status response: %s ===", response.status_code)
|
|
|
|
response.raise_for_status()
|
2016-03-31 03:37:37 +00:00
|
|
|
|
2015-12-24 20:07:00 +00:00
|
|
|
##### EXCEPTIONS #####
|
|
|
|
|
2016-09-27 09:10:40 +00:00
|
|
|
except requests.exceptions.SSLError as error:
|
2017-02-03 23:42:22 +00:00
|
|
|
log.error("Invalid SSL certificate for: %s", url)
|
2016-11-22 12:05:16 +00:00
|
|
|
error.quiet = True
|
|
|
|
raise
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
except requests.exceptions.ConnectTimeout as error:
|
2016-11-18 02:42:46 +00:00
|
|
|
log.error("ConnectTimeout at: %s", url)
|
2016-11-22 12:05:16 +00:00
|
|
|
error.quiet = True
|
|
|
|
raise
|
2016-11-18 02:42:46 +00:00
|
|
|
|
|
|
|
except requests.exceptions.ReadTimeout as error:
|
|
|
|
log.error("ReadTimeout at: %s", url)
|
2016-11-22 12:05:16 +00:00
|
|
|
error.quiet = True
|
|
|
|
raise
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 09:10:40 +00:00
|
|
|
except requests.exceptions.ConnectionError as error:
|
|
|
|
# Make the addon aware of status
|
|
|
|
if window('emby_online') != "false":
|
|
|
|
log.error("Server unreachable at: %s", url)
|
|
|
|
window('emby_online', value="false")
|
2016-11-22 12:05:16 +00:00
|
|
|
error.quiet = True
|
|
|
|
raise
|
2016-09-27 09:10:40 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
except requests.exceptions.HTTPError as error:
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 09:10:40 +00:00
|
|
|
if response.status_code == 401:
|
2015-12-24 20:07:00 +00:00
|
|
|
# Unauthorized
|
2016-06-18 03:03:28 +00:00
|
|
|
status = window('emby_serverStatus')
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2016-09-27 09:10:40 +00:00
|
|
|
if 'X-Application-Error-Code' in response.headers:
|
2015-12-24 20:07:00 +00:00
|
|
|
# Emby server errors
|
2016-09-27 09:10:40 +00:00
|
|
|
if response.headers['X-Application-Error-Code'] == "ParentalControl":
|
2015-12-24 20:07:00 +00:00
|
|
|
# Parental control - access restricted
|
2016-09-04 10:18:31 +00:00
|
|
|
if status != "restricted":
|
2016-09-27 09:10:40 +00:00
|
|
|
xbmcgui.Dialog().notification(heading=lang(29999),
|
|
|
|
message="Access restricted.",
|
|
|
|
icon=xbmcgui.NOTIFICATION_ERROR,
|
|
|
|
time=5000)
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_serverStatus', value="restricted")
|
2015-12-24 20:07:00 +00:00
|
|
|
|
|
|
|
elif status not in ("401", "Auth"):
|
|
|
|
# Tell userclient token has been revoked.
|
2016-06-18 03:03:28 +00:00
|
|
|
window('emby_serverStatus', value="401")
|
2016-09-27 06:33:09 +00:00
|
|
|
log.error("HTTP Error: %s", error)
|
2016-09-27 09:10:40 +00:00
|
|
|
xbmcgui.Dialog().notification(heading="Error connecting",
|
|
|
|
message="Unauthorized.",
|
|
|
|
icon=xbmcgui.NOTIFICATION_ERROR)
|
2015-12-24 20:07:00 +00:00
|
|
|
|
2017-02-05 00:00:20 +00:00
|
|
|
error.quiet = True
|
|
|
|
raise
|
2017-02-03 23:42:22 +00:00
|
|
|
|
|
|
|
# if we got to here and did not process the download for some reason then that is bad
|
|
|
|
exc = Exception("Unhandled Download : %s", url)
|
|
|
|
#exc.quiet = True
|
|
|
|
raise exc
|
2016-09-27 02:50:58 +00:00
|
|
|
|
|
|
|
def _ensure_server(self, server_id=None):
|
|
|
|
|
|
|
|
if server_id is None and self.session_requests is None:
|
2016-09-27 06:33:09 +00:00
|
|
|
if not self.session:
|
|
|
|
server = self._get_session_info()
|
|
|
|
self.session = server
|
2016-09-27 02:50:58 +00:00
|
|
|
|
|
|
|
elif server_id and server_id not in self.servers:
|
2016-09-27 06:33:09 +00:00
|
|
|
if server_id not in self.servers:
|
|
|
|
server = self._get_session_info(server_id)
|
|
|
|
self.servers[server_id] = server
|
2016-09-27 02:50:58 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _get_session_info(cls, server_id=None):
|
2016-09-27 09:10:40 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
info = {
|
|
|
|
'UserId': "",
|
|
|
|
'Server': "",
|
|
|
|
'Token': "",
|
|
|
|
'SSL': False
|
|
|
|
}
|
2016-09-27 02:50:58 +00:00
|
|
|
|
|
|
|
if server_id is None: # Main server
|
2016-09-27 06:33:09 +00:00
|
|
|
server = window('emby_server.json')
|
2016-09-27 02:50:58 +00:00
|
|
|
else: # Other connect servers
|
|
|
|
server = window('emby_server%s.json' % server_id)
|
2016-09-27 09:10:40 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
if server:
|
2016-09-29 06:59:30 +00:00
|
|
|
info.update(server)
|
2016-09-27 02:50:58 +00:00
|
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
@classmethod
|
2016-09-27 06:33:09 +00:00
|
|
|
def _requests(cls, action, session, **kwargs):
|
2016-07-15 23:01:59 +00:00
|
|
|
|
|
|
|
if action == "GET":
|
2016-09-27 02:50:58 +00:00
|
|
|
response = session.get(**kwargs)
|
2016-07-15 23:01:59 +00:00
|
|
|
elif action == "POST":
|
2016-09-27 02:50:58 +00:00
|
|
|
response = session.post(**kwargs)
|
2016-07-15 23:01:59 +00:00
|
|
|
elif action == "DELETE":
|
2016-09-27 02:50:58 +00:00
|
|
|
response = session.delete(**kwargs)
|
2016-07-15 23:01:59 +00:00
|
|
|
|
2016-09-27 06:33:09 +00:00
|
|
|
return response
|