2015-12-24 19:51:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import json
|
|
|
|
import socket
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
|
|
|
|
import clientinfo
|
|
|
|
import downloadutils
|
|
|
|
import userclient
|
2016-06-18 03:03:28 +00:00
|
|
|
from utils import Logging, settings, language as lang, passwordsXML
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class InitialSetup():
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
global log
|
|
|
|
log = Logging(self.__class__.__name__).log
|
2015-12-24 19:51:47 +00:00
|
|
|
|
2016-06-19 21:24:34 +00:00
|
|
|
self.addonId = clientinfo.ClientInfo().getAddonId()
|
|
|
|
self.doUtils = downloadutils.DownloadUtils().downloadUrl
|
2015-12-24 19:51:47 +00:00
|
|
|
self.userClient = userclient.UserClient()
|
|
|
|
|
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
# Check server, user, direct paths, music, direct stream if not direct path.
|
|
|
|
addonId = self.addonId
|
2016-06-19 21:24:34 +00:00
|
|
|
dialog = xbmcgui.Dialog()
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
##### SERVER INFO #####
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Initial setup called.", 2)
|
2015-12-24 19:51:47 +00:00
|
|
|
server = self.userClient.getServer()
|
|
|
|
|
|
|
|
if server:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Server is already set.", 2)
|
2015-12-24 19:51:47 +00:00
|
|
|
return
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Looking for server...", 2)
|
2015-12-24 19:51:47 +00:00
|
|
|
server = self.getServerDetails()
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Found: %s" % server, 2)
|
2015-12-24 19:51:47 +00:00
|
|
|
try:
|
|
|
|
prefix, ip, port = server.replace("/", "").split(":")
|
|
|
|
except: # Failed to retrieve server information
|
2016-06-18 03:03:28 +00:00
|
|
|
log("getServerDetails failed.", 1)
|
2015-12-24 19:51:47 +00:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % addonId)
|
|
|
|
return
|
|
|
|
else:
|
2016-06-19 21:24:34 +00:00
|
|
|
server_confirm = dialog.yesno(
|
|
|
|
heading=lang(29999),
|
|
|
|
line1=lang(33034),
|
|
|
|
line2="%s %s" % (lang(30169), server))
|
2015-12-24 19:51:47 +00:00
|
|
|
if server_confirm:
|
|
|
|
# Correct server found
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Server is selected. Saving the information.", 1)
|
|
|
|
settings('ipaddress', value=ip)
|
|
|
|
settings('port', value=port)
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
if prefix == "https":
|
2016-06-18 03:03:28 +00:00
|
|
|
settings('https', value="true")
|
2015-12-24 19:51:47 +00:00
|
|
|
else:
|
|
|
|
# User selected no or cancelled the dialog
|
2016-06-18 03:03:28 +00:00
|
|
|
log("No server selected.", 1)
|
2015-12-24 19:51:47 +00:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % addonId)
|
|
|
|
return
|
|
|
|
|
|
|
|
##### USER INFO #####
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Getting user list.", 1)
|
2016-06-19 21:24:34 +00:00
|
|
|
|
|
|
|
result = self.doUtils("%s/emby/Users/Public?format=json" % server, authenticate=False)
|
2015-12-24 19:51:47 +00:00
|
|
|
if result == "":
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Unable to connect to %s" % server, 1)
|
2015-12-24 19:51:47 +00:00
|
|
|
return
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Response: %s" % result, 2)
|
2015-12-24 19:51:47 +00:00
|
|
|
# Process the list of users
|
|
|
|
usernames = []
|
|
|
|
users_hasPassword = []
|
|
|
|
|
|
|
|
for user in result:
|
|
|
|
# Username
|
|
|
|
name = user['Name']
|
|
|
|
usernames.append(name)
|
|
|
|
# Password
|
|
|
|
if user['HasPassword']:
|
|
|
|
name = "%s (secure)" % name
|
|
|
|
users_hasPassword.append(name)
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Presenting user list: %s" % users_hasPassword, 1)
|
2016-06-19 21:24:34 +00:00
|
|
|
user_select = dialog.select(lang(30200), users_hasPassword)
|
2015-12-24 19:51:47 +00:00
|
|
|
if user_select > -1:
|
|
|
|
selected_user = usernames[user_select]
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Selected user: %s" % selected_user, 1)
|
|
|
|
settings('username', value=selected_user)
|
2015-12-24 19:51:47 +00:00
|
|
|
else:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("No user selected.", 1)
|
2015-12-24 19:51:47 +00:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % addonId)
|
2016-06-19 21:24:34 +00:00
|
|
|
return
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
##### ADDITIONAL PROMPTS #####
|
|
|
|
|
|
|
|
directPaths = dialog.yesno(
|
2016-06-19 21:24:34 +00:00
|
|
|
heading=lang(30511),
|
|
|
|
line1=lang(33035),
|
|
|
|
nolabel=lang(33036),
|
|
|
|
yeslabel=lang(33037))
|
2015-12-24 19:51:47 +00:00
|
|
|
if directPaths:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("User opted to use direct paths.", 1)
|
|
|
|
settings('useDirectPaths', value="1")
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
# ask for credentials
|
|
|
|
credentials = dialog.yesno(
|
2016-06-19 21:24:34 +00:00
|
|
|
heading=lang(30517),
|
|
|
|
line1= lang(33038))
|
2015-12-24 19:51:47 +00:00
|
|
|
if credentials:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Presenting network credentials dialog.", 1)
|
|
|
|
passwordsXML()
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
musicDisabled = dialog.yesno(
|
2016-06-19 21:24:34 +00:00
|
|
|
heading=lang(29999),
|
|
|
|
line1=lang(33039))
|
2015-12-24 19:51:47 +00:00
|
|
|
if musicDisabled:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("User opted to disable Emby music library.", 1)
|
|
|
|
settings('enableMusic', value="false")
|
2015-12-24 19:51:47 +00:00
|
|
|
else:
|
|
|
|
# Only prompt if the user didn't select direct paths for videos
|
|
|
|
if not directPaths:
|
|
|
|
musicAccess = dialog.yesno(
|
2016-06-19 21:24:34 +00:00
|
|
|
heading=lang(29999),
|
|
|
|
line1=lang(33040))
|
2015-12-24 19:51:47 +00:00
|
|
|
if musicAccess:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("User opted to direct stream music.", 1)
|
|
|
|
settings('streamMusic', value="true")
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
def getServerDetails(self):
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Getting Server Details from Network", 1)
|
2015-12-24 19:51:47 +00:00
|
|
|
|
|
|
|
MULTI_GROUP = ("<broadcast>", 7359)
|
|
|
|
MESSAGE = "who is EmbyServer?"
|
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
sock.settimeout(6.0)
|
|
|
|
|
|
|
|
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 20)
|
|
|
|
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
|
|
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
|
|
|
|
sock.setsockopt(socket.IPPROTO_IP, socket.SO_REUSEADDR, 1)
|
|
|
|
|
2016-06-18 03:03:28 +00:00
|
|
|
log("MultiGroup : %s" % str(MULTI_GROUP), 2)
|
|
|
|
log("Sending UDP Data: %s" % MESSAGE, 2)
|
2015-12-24 19:51:47 +00:00
|
|
|
sock.sendto(MESSAGE, MULTI_GROUP)
|
|
|
|
|
|
|
|
try:
|
|
|
|
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
|
2016-06-18 03:03:28 +00:00
|
|
|
log("Received Response: %s" % data)
|
2015-12-24 19:51:47 +00:00
|
|
|
except:
|
2016-06-18 03:03:28 +00:00
|
|
|
log("No UDP Response")
|
2015-12-24 19:51:47 +00:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
# Get the address
|
|
|
|
data = json.loads(data)
|
|
|
|
return data['Address']
|