jellyfin-kodi/resources/lib/ConnectionManager.py

172 lines
5.6 KiB
Python
Raw Normal View History

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
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()
userClient = UserClient()
doUtils = DownloadUtils()
logLevel = 0
addon = None
WINDOW = xbmcgui.Window(10000)
def __init__(self):
clientInfo = self.clientInfo
self.addonId = clientInfo.getAddonId()
self.addonName = clientInfo.getAddonName()
self.addon = xbmcaddon.Addon(id=self.addonId)
self.__language__ = self.addon.getLocalizedString
2015-03-13 21:24:59 +00:00
2015-04-03 10:13:01 +00:00
def logMsg(self, msg, level=1):
addonName = self.addonName
className = self.__class__.__name__
s_logLevel = self.userClient.getLogLevel()
# Attempt to change logLevel live
if (self.logLevel != s_logLevel):
self.logLevel = s_logLevel
if (self.logLevel >= level):
try:
xbmc.log("%s %s -> %s" % (addonName, className, str(msg)))
except UnicodeEncodeError:
2015-03-13 21:24:59 +00:00
try:
2015-04-03 10:13:01 +00:00
xbmc.log("%s %s -> %s" % (addonName, className, str(msg.encode('utf-8'))))
except:
pass
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")
self.logMsg("Connection Manager Called")
2015-03-13 21:24:59 +00:00
2015-04-03 10:13:01 +00:00
addon = self.addon
server = self.userClient.getServer()
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)
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
2015-03-13 21:24:59 +00:00
return
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-03 10:13:01 +00:00
if setServer == 1:
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-03-13 21:24:59 +00:00
try:
2015-04-03 10:13:01 +00:00
jsonData = self.doUtils.downloadUrl(serverInfo + "/mediabrowser/Users/Public?format=json", authenticate=False)
2015-03-13 21:24:59 +00:00
except Exception, msg:
2015-04-03 10:13:01 +00:00
error = "Get User unable to connect to " + server + " : " + str(msg)
2015-03-13 21:24:59 +00:00
xbmc.log (error)
return ""
2015-04-03 10:13:01 +00:00
if (jsonData == False):
2015-03-13 21:24:59 +00:00
return
2015-04-03 10:13:01 +00:00
self.logMsg("jsonData : " + str(jsonData), level=2)
2015-03-13 21:24:59 +00:00
result = json.loads(jsonData)
names = []
userList = []
for user in result:
name = user.get("Name")
userList.append(name)
if(user.get("HasPassword") == True):
name = name + " (Secure)"
names.append(name)
2015-04-03 10:13:01 +00:00
self.logMsg("User List: " + str(names))
self.logMsg("User List: " + str(userList))
2015-03-13 21:24:59 +00:00
return_value = xbmcgui.Dialog().select(self.__language__(30200), names)
if(return_value > -1):
selected_user = userList[return_value]
self.logMsg("elected User: %s" % selected_user)
2015-04-03 10:13:01 +00:00
self.addon.setSetting("username", selected_user)
else:
xbmc.log("No user selected.")
2015-04-03 10:13:01 +00:00
xbmc.executebuiltin('Addon.OpenSettings(%s)' % self.addonId)
return
# Option to play from http
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")
return
else:
self.logMsg("Playback will be set using SMB.", 1)
return
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-03 10:13:01 +00:00
self.logMsg("MutliGroup : %s" % str(MULTI_GROUP));
self.logMsg("Sending UDP Data: %s" % MESSAGE);
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