2015-12-24 19:51:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
2016-07-24 08:59:48 +00:00
|
|
|
import logging
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
|
|
|
|
import clientinfo
|
2016-09-04 10:18:31 +00:00
|
|
|
import connectmanager
|
|
|
|
import connect.connectionmanager as connectionmanager
|
2015-12-24 19:51:47 +00:00
|
|
|
import userclient
|
2016-07-24 08:59:48 +00:00
|
|
|
from utils import settings, language as lang, passwordsXML
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
log = logging.getLogger("EMBY."+__name__)
|
2016-09-04 10:18:31 +00:00
|
|
|
STATE = connectionmanager.ConnectionState
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
|
2016-09-04 10:18:31 +00:00
|
|
|
class InitialSetup(object):
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2016-09-05 03:33:34 +00:00
|
|
|
self.addon_id = clientinfo.ClientInfo().get_addon_id()
|
2016-09-04 10:18:31 +00:00
|
|
|
self.user_client = userclient.UserClient()
|
|
|
|
self.connectmanager = connectmanager.ConnectManager()
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
# Check server, user, direct paths, music, direct stream if not direct path.
|
2016-06-19 21:24:34 +00:00
|
|
|
dialog = xbmcgui.Dialog()
|
2015-12-24 19:51:47 +00:00
|
|
|
|
2016-09-07 01:21:43 +00:00
|
|
|
log.debug("Initial setup called")
|
2016-09-04 10:18:31 +00:00
|
|
|
|
2016-09-07 01:21:43 +00:00
|
|
|
if self._server_verification() and settings('userId'):
|
|
|
|
# Setup is already completed
|
|
|
|
return
|
|
|
|
|
|
|
|
if not self._user_identification():
|
|
|
|
# User failed to identify
|
|
|
|
return
|
|
|
|
|
|
|
|
##### ADDITIONAL PROMPTS #####
|
|
|
|
|
|
|
|
direct_paths = dialog.yesno(heading=lang(30511),
|
|
|
|
line1=lang(33035),
|
|
|
|
nolabel=lang(33036),
|
|
|
|
yeslabel=lang(33037))
|
|
|
|
if direct_paths:
|
|
|
|
log.info("User opted to use direct paths")
|
|
|
|
settings('useDirectPaths', value="1")
|
|
|
|
|
|
|
|
# ask for credentials
|
|
|
|
credentials = dialog.yesno(heading=lang(30517), line1=lang(33038))
|
|
|
|
if credentials:
|
|
|
|
log.info("Presenting network credentials dialog")
|
|
|
|
passwordsXML()
|
|
|
|
|
|
|
|
music_disabled = dialog.yesno(heading=lang(29999), line1=lang(33039))
|
|
|
|
if music_disabled:
|
|
|
|
log.info("User opted to disable Emby music library")
|
|
|
|
settings('enableMusic', value="false")
|
|
|
|
else:
|
|
|
|
# Only prompt if the user didn't select direct paths for videos
|
|
|
|
if not direct_paths:
|
|
|
|
music_access = dialog.yesno(heading=lang(29999), line1=lang(33040))
|
|
|
|
if music_access:
|
|
|
|
log.info("User opted to direct stream music")
|
|
|
|
settings('streamMusic', value="true")
|
|
|
|
|
|
|
|
def _server_verification(self):
|
2015-12-24 19:51:47 +00:00
|
|
|
|
2016-09-05 02:20:47 +00:00
|
|
|
###$ Begin migration $###
|
2016-09-04 10:18:31 +00:00
|
|
|
if settings('server') == "":
|
|
|
|
current_server = self.user_client.get_server()
|
2016-09-05 03:33:34 +00:00
|
|
|
if current_server is not None:
|
2016-10-20 08:50:26 +00:00
|
|
|
server = self.connectmanager.get_server(current_server,
|
|
|
|
{'ssl': self.user_client.get_ssl()})
|
2016-09-09 06:33:56 +00:00
|
|
|
log.info("Detected: %s", server)
|
2016-10-20 08:50:26 +00:00
|
|
|
try:
|
|
|
|
server_id = server['Servers'][0]['Id']
|
|
|
|
settings('serverId', value=server_id)
|
|
|
|
except Exception as error:
|
|
|
|
log.error(error)
|
2016-09-07 01:21:43 +00:00
|
|
|
log.info("server migration completed")
|
2016-10-08 00:11:15 +00:00
|
|
|
|
|
|
|
self.user_client.get_userid()
|
|
|
|
self.user_client.get_token()
|
2016-09-05 02:20:47 +00:00
|
|
|
###$ End migration $###
|
2016-09-04 10:18:31 +00:00
|
|
|
|
2016-09-05 00:14:25 +00:00
|
|
|
if settings('server'):
|
|
|
|
current_state = self.connectmanager.get_state()
|
2016-10-23 01:21:00 +00:00
|
|
|
try:
|
2016-10-21 00:26:19 +00:00
|
|
|
for server in current_state['Servers']:
|
|
|
|
if server['Id'] == settings('serverId'):
|
|
|
|
# Update token
|
|
|
|
server['UserId'] = settings('userId') or None
|
|
|
|
server['AccessToken'] = settings('token') or None
|
2016-10-23 02:22:18 +00:00
|
|
|
self.connectmanager.update_token(server)
|
2016-10-21 00:26:19 +00:00
|
|
|
|
|
|
|
server_address = self.connectmanager.get_address(server)
|
|
|
|
self._set_server(server_address, server)
|
|
|
|
log.info("Found server!")
|
2016-10-23 01:21:00 +00:00
|
|
|
except Exception as error:
|
|
|
|
log.error(error)
|
2016-10-19 23:23:49 +00:00
|
|
|
|
|
|
|
return True
|
2016-09-07 03:07:14 +00:00
|
|
|
|
2016-09-07 01:21:43 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _user_identification(self):
|
2016-09-04 10:18:31 +00:00
|
|
|
|
2015-12-24 19:51:47 +00:00
|
|
|
try:
|
2016-09-04 10:18:31 +00:00
|
|
|
server = self.connectmanager.select_servers()
|
|
|
|
log.info("Server: %s", server)
|
|
|
|
server_address = self.connectmanager.get_address(server)
|
2016-09-05 00:14:25 +00:00
|
|
|
self._set_server(server_address, server)
|
2016-09-04 10:18:31 +00:00
|
|
|
|
|
|
|
if not server.get('AccessToken') and not server.get('UserId'):
|
2016-09-07 01:21:43 +00:00
|
|
|
user = self.connectmanager.login(server)
|
|
|
|
log.info("User authenticated: %s", user)
|
2016-09-04 10:18:31 +00:00
|
|
|
settings('username', value=user['User']['Name'])
|
|
|
|
self._set_user(user['User']['Id'], user['AccessToken'])
|
2016-09-07 01:21:43 +00:00
|
|
|
else: # Logged with Emby Connect
|
2016-09-04 10:18:31 +00:00
|
|
|
user = self.connectmanager.get_state()
|
|
|
|
settings('connectUsername', value=user['ConnectUser']['Name'])
|
|
|
|
self._set_user(server['UserId'], server['AccessToken'])
|
2015-12-24 19:51:47 +00:00
|
|
|
|
2016-09-07 01:21:43 +00:00
|
|
|
return True
|
2016-09-04 10:18:31 +00:00
|
|
|
|
2016-09-07 01:21:43 +00:00
|
|
|
except RuntimeError as error:
|
|
|
|
log.exception(error)
|
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addon_id)
|
|
|
|
return False
|
2016-09-04 10:18:31 +00:00
|
|
|
|
|
|
|
@classmethod
|
2016-09-05 00:14:25 +00:00
|
|
|
def _set_server(cls, server_address, server):
|
2016-09-04 10:18:31 +00:00
|
|
|
|
2016-09-05 00:14:25 +00:00
|
|
|
settings('serverName', value=server['Name'])
|
|
|
|
settings('serverId', value=server['Id'])
|
|
|
|
settings('server', value=server_address)
|
2016-09-04 10:18:31 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _set_user(cls, user_id, token):
|
|
|
|
|
|
|
|
settings('userId', value=user_id)
|
|
|
|
settings('token', value=token)
|