2015-03-13 21:24:59 +00:00
|
|
|
#################################################################################################
|
|
|
|
# connection manager class
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import xbmc
|
|
|
|
import xbmcgui
|
|
|
|
import xbmcaddon
|
|
|
|
|
|
|
|
import json
|
|
|
|
import urllib
|
|
|
|
import sys
|
|
|
|
import socket
|
2015-04-03 10:13:01 +00:00
|
|
|
import threading
|
|
|
|
from datetime import datetime
|
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
import Utils as utils
|
2015-04-03 10:13:01 +00:00
|
|
|
from DownloadUtils import DownloadUtils
|
|
|
|
from UserClient import UserClient
|
|
|
|
from ClientInformation import ClientInformation
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
class ConnectionManager():
|
2015-04-03 10:13:01 +00:00
|
|
|
|
|
|
|
clientInfo = ClientInformation()
|
2015-04-22 02:31:16 +00:00
|
|
|
uc = UserClient()
|
2015-04-03 10:13:01 +00:00
|
|
|
doUtils = DownloadUtils()
|
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
addonName = clientInfo.getAddonName()
|
|
|
|
addonId = clientInfo.getAddonId()
|
|
|
|
addon = xbmcaddon.Addon(id=addonId)
|
2015-04-22 02:20:02 +00:00
|
|
|
WINDOW = xbmcgui.Window(10000)
|
2015-04-03 10:13:01 +00:00
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
logLevel = 0
|
2015-04-03 10:13:01 +00:00
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
def __init__(self):
|
2015-04-22 02:17:58 +00:00
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
self.className = self.__class__.__name__
|
|
|
|
self.__language__ = self.addon.getLocalizedString
|
2015-04-22 02:20:02 +00:00
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
def logMsg(self, msg, lvl=1):
|
|
|
|
|
|
|
|
utils.logMsg("%s %s" % (self.addonName, self.className), msg, int(lvl))
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
def checkServer(self):
|
|
|
|
|
2015-04-03 10:13:01 +00:00
|
|
|
self.WINDOW.setProperty("Server_Checked", "True")
|
2015-04-22 02:31:16 +00:00
|
|
|
self.logMsg("Connection Manager Called", 2)
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-04-03 10:13:01 +00:00
|
|
|
addon = self.addon
|
2015-04-22 02:31:16 +00:00
|
|
|
server = self.uc.getServer()
|
2015-04-03 10:13:01 +00:00
|
|
|
|
|
|
|
if (server != ""):
|
|
|
|
self.logMsg("Server already set", 2)
|
2015-03-13 21:24:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
serverInfo = self.getServerDetails()
|
|
|
|
|
2015-04-03 10:13:01 +00:00
|
|
|
if (serverInfo == None):
|
|
|
|
self.logMsg("getServerDetails failed", 1)
|
2015-04-03 12:36:41 +00:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
|
2015-03-13 21:24:59 +00:00
|
|
|
return
|
2015-04-03 12:36:41 +00:00
|
|
|
|
|
|
|
prefix,ip,port = serverInfo.split(":")
|
2015-04-03 10:13:01 +00:00
|
|
|
setServer = xbmcgui.Dialog().yesno(self.__language__(30167), "Proceed with the following server?", self.__language__(30169) + serverInfo)
|
2015-03-13 21:24:59 +00:00
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
if (setServer == 1):
|
2015-04-03 10:13:01 +00:00
|
|
|
self.logMsg("Server selected. Saving information.", 1)
|
|
|
|
addon.setSetting("ipaddress", ip.replace("/", ""))
|
|
|
|
addon.setSetting("port", port)
|
|
|
|
# If https is enabled
|
|
|
|
if (prefix == 'https'):
|
|
|
|
addon.setSetting('https', "true")
|
|
|
|
else:
|
|
|
|
self.logMsg("No server selected.", 1)
|
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Get List of public users
|
|
|
|
self.logMsg("Getting user list", 1)
|
|
|
|
server = ip.replace("/", "") + ":" + port
|
2015-04-22 02:31:16 +00:00
|
|
|
url = "%s/mediabrowser/Users/Public?format=json" % serverInfo
|
2015-04-03 10:13:01 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
try:
|
2015-04-22 02:31:16 +00:00
|
|
|
result = self.doUtils.downloadUrl(url, authenticate=False)
|
2015-03-13 21:24:59 +00:00
|
|
|
except Exception, msg:
|
2015-04-22 02:31:16 +00:00
|
|
|
error = "Unable to connect to %s: %s" % (server, msg)
|
|
|
|
self.logMsg(error, 1)
|
2015-03-13 21:24:59 +00:00
|
|
|
return ""
|
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
if (result == ""):
|
2015-03-13 21:24:59 +00:00
|
|
|
return
|
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
self.logMsg("jsonData: %s" % result, 2)
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
names = []
|
|
|
|
userList = []
|
|
|
|
for user in result:
|
2015-04-22 02:31:16 +00:00
|
|
|
name = user[u'Name']
|
2015-03-13 21:24:59 +00:00
|
|
|
userList.append(name)
|
2015-04-22 02:31:16 +00:00
|
|
|
|
|
|
|
if(user[u'HasPassword'] == True):
|
2015-03-13 21:24:59 +00:00
|
|
|
name = name + " (Secure)"
|
|
|
|
names.append(name)
|
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
self.logMsg("User List: %s" % names, 1)
|
|
|
|
self.logMsg("User List: %s" % userList, 2)
|
2015-03-13 21:24:59 +00:00
|
|
|
return_value = xbmcgui.Dialog().select(self.__language__(30200), names)
|
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
if (return_value > -1):
|
2015-03-13 21:24:59 +00:00
|
|
|
selected_user = userList[return_value]
|
2015-04-22 02:31:16 +00:00
|
|
|
self.logMsg("Selected User: %s" % selected_user, 1)
|
2015-04-03 10:13:01 +00:00
|
|
|
self.addon.setSetting("username", selected_user)
|
2015-03-27 09:58:05 +00:00
|
|
|
else:
|
2015-04-22 02:31:16 +00:00
|
|
|
self.logMsg("No user selected.", 1)
|
2015-04-03 10:13:01 +00:00
|
|
|
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
|
2015-03-27 09:58:05 +00:00
|
|
|
return
|
2015-04-12 08:34:00 +00:00
|
|
|
|
2015-04-09 01:28:28 +00:00
|
|
|
# Option to play from http
|
2015-05-03 18:19:25 +00:00
|
|
|
#setPlayback = xbmcgui.Dialog().yesno("Playback option", "Play your files using HTTP?")
|
|
|
|
#if setPlayback == 1:
|
|
|
|
#self.logMsg("Playback will be set using HTTP.", 1)
|
|
|
|
#addon.setSetting("playFromStream", "true")
|
|
|
|
#else:
|
|
|
|
#self.logMsg("Playback will be set using SMB.", 1)
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
def getServerDetails(self):
|
|
|
|
|
2015-04-03 10:13:01 +00:00
|
|
|
self.logMsg("Getting Server Details from Network")
|
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
MULTI_GROUP = ("<broadcast>", 7359)
|
2015-04-03 10:13:01 +00:00
|
|
|
MESSAGE = "who is EmbyServer?"
|
2015-03-13 21:24:59 +00:00
|
|
|
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
sock.settimeout(6.0)
|
2015-04-03 10:13:01 +00:00
|
|
|
|
2015-03-13 21:24:59 +00:00
|
|
|
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)
|
|
|
|
|
2015-04-22 02:31:16 +00:00
|
|
|
self.logMsg("MutliGroup : %s" % str(MULTI_GROUP), 2);
|
|
|
|
self.logMsg("Sending UDP Data: %s" % MESSAGE, 2);
|
2015-03-13 21:24:59 +00:00
|
|
|
sock.sendto(MESSAGE, MULTI_GROUP)
|
|
|
|
|
|
|
|
try:
|
|
|
|
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
|
2015-04-03 10:13:01 +00:00
|
|
|
self.logMsg("Received Response: %s" % data)
|
|
|
|
# Get the address
|
|
|
|
data = json.loads(data)
|
|
|
|
return data['Address']
|
2015-03-13 21:24:59 +00:00
|
|
|
except:
|
2015-04-03 10:13:01 +00:00
|
|
|
self.logMsg("No UDP Response")
|
2015-03-13 21:24:59 +00:00
|
|
|
pass
|
|
|
|
|
2015-04-03 10:13:01 +00:00
|
|
|
return None
|