From e09c209a6bf5804caf3e4373d7911acd64b6e539 Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Sat, 22 Aug 2015 11:17:54 -0500 Subject: [PATCH] Music library make it seemless - dependant on alternate ip to be enabled to add pathsubstitution. --- resources/lib/UserClient.py | 10 +++- resources/lib/Utils.py | 78 ++++++++++++++++++++++++++++++- resources/lib/WriteKodiMusicDB.py | 18 ++++--- resources/settings.xml | 18 ++++--- service.py | 15 ++++++ 5 files changed, 123 insertions(+), 16 deletions(-) diff --git a/resources/lib/UserClient.py b/resources/lib/UserClient.py index b0a507f4..7006abcd 100644 --- a/resources/lib/UserClient.py +++ b/resources/lib/UserClient.py @@ -101,13 +101,17 @@ class UserClient(threading.Thread): def getServer(self, prefix=True): + alternate = utils.settings('altip') == "true" + # For https support HTTPS = utils.settings('https') host = utils.settings('ipaddress') port = utils.settings('port') # Alternate host - if utils.settings('altip') == "true": + if alternate: + HTTPS = utils.settings('secondhttps') host = utils.settings('secondipaddress') + port = utils.settings('secondport') server = host + ":" + port @@ -149,6 +153,8 @@ class UserClient(threading.Thread): def getSSLverify(self): # Verify host certificate s_sslverify = utils.settings('sslverify') + if utils.settings('altip') == "true": + s_sslverify = utils.settings('secondsslverify') if s_sslverify == "true": return True @@ -158,6 +164,8 @@ class UserClient(threading.Thread): def getSSL(self): # Client side certificate s_cert = utils.settings('sslcert') + if utils.settings('altip') == "true": + s_cert = utils.settings('secondsslcert') if s_cert == "None": return None diff --git a/resources/lib/Utils.py b/resources/lib/Utils.py index 4cede044..4119f9de 100644 --- a/resources/lib/Utils.py +++ b/resources/lib/Utils.py @@ -15,7 +15,7 @@ import inspect import sqlite3 import string import unicodedata - +import xml.etree.ElementTree as etree from API import API from PlayUtils import PlayUtils @@ -185,6 +185,82 @@ def createSources(): '' ) +def pathsubstitution(add=True): + + path = xbmc.translatePath('special://userdata').decode('utf-8') + xmlpath = "%sadvancedsettings.xml" % path + xmlpathexists = xbmcvfs.exists(xmlpath) + + # original address + originalServer = settings('ipaddress') + originalPort = settings('port') + originalHttp = settings('https') == "true" + + if originalHttp: + originalHttp = "https" + else: + originalHttp = "http" + + # Process add or deletion + if add: + # second address + secondServer = settings('secondipaddress') + secondPort = settings('secondport') + secondHttp = settings('secondhttps') == "true" + + if secondHttp: + secondHttp = "https" + else: + secondHttp = "http" + + logMsg("EMBY", "Original address: %s://%s:%s, alternate is: %s://%s:%s" % (originalHttp, originalServer, originalPort, secondHttp, secondServer, secondPort), 1) + + if xmlpathexists: + # we need to modify the file. + try: + xmlparse = etree.parse(xmlpath) + except: # Document is blank + root = etree.Element('advancedsettings') + else: + root = xmlparse.getroot() + + pathsubs = root.find('pathsubstitution') + if pathsubs is None: + pathsubs = etree.SubElement(root, 'pathsubstitution') + else: + # we need to create the file. + root = etree.Element('advancedsettings') + pathsubs = etree.SubElement(root, 'pathsubstitution') + + substitute = etree.SubElement(pathsubs, 'substitute') + # From original address + etree.SubElement(substitute, 'from').text = "%s://%s:%s" % (originalHttp, originalServer, originalPort) + # To secondary address + etree.SubElement(substitute, 'to').text = "%s://%s:%s" % (secondHttp, secondServer, secondPort) + + etree.ElementTree(root).write(xmlpath) + settings('pathsub', "true") + + else: # delete the path substitution, we don't need it anymore. + logMsg("EMBY", "Alternate address is disabled, removing path substitution for: %s://%s:%s" % (originalHttp, originalServer, originalPort), 1) + + xmlparse = etree.parse(xmlpath) + root = xmlparse.getroot() + + iterator = root.getiterator("pathsubstitution") + + for substitutes in iterator: + for substitute in substitutes: + frominsert = substitute.find(".//from").text == "%s://%s:%s" % (originalHttp, originalServer, originalPort) + + if frominsert: + # Found a match, in case there's more than one substitution. + substitutes.remove(substitute) + + etree.ElementTree(root).write(xmlpath) + settings('pathsub', "false") + + def settings(setting, value = None): # Get or add addon setting addon = xbmcaddon.Addon() diff --git a/resources/lib/WriteKodiMusicDB.py b/resources/lib/WriteKodiMusicDB.py index 67450501..f1e934b5 100644 --- a/resources/lib/WriteKodiMusicDB.py +++ b/resources/lib/WriteKodiMusicDB.py @@ -271,19 +271,23 @@ class WriteKodiMusicDB(): bio = API().getOverview(MBitem) duration = timeInfo.get('TotalTime') - # Get the path and filename - playurl = PlayUtils().directPlay(MBitem) - try: + if utils.settings('directstreammusic') == "true": + WINDOW = xbmcgui.Window(10000) + username = WINDOW.getProperty('currUser') + server = WINDOW.getProperty('server%s' % username) + + playurl = PlayUtils().directStream(MBitem, server, embyId, "Audio") + filename = "stream.mp3" + path = playurl.replace(filename, "") + else: + # Get the path and filename + playurl = PlayUtils().directPlay(MBitem) path, filename = ntsplit(playurl) if "/" in playurl: path = "%s/" % path elif "\\" in playurl: path = "%s\\" % path - except: # playurl returned false - using server streaming path, because could not figure out plugin paths for music DB - playurl = PlayUtils().directstream(MBitem, self.server, embyId, "Audio") - filename = "stream.mp3" - path = playurl.replace(filename, "") # Validate the path in database diff --git a/resources/settings.xml b/resources/settings.xml index 6317726c..59d9fe21 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -2,23 +2,28 @@ - - - - - + + + + + + + + + + - + @@ -46,6 +51,5 @@ - diff --git a/service.py b/service.py index 0506f741..e07ffa42 100644 --- a/service.py +++ b/service.py @@ -238,6 +238,21 @@ class Service(): if self.KodiMonitor.waitForAbort(1): # Abort was requested while waiting. We should exit break + + # If music is enable and direct stream for music is enabled + # We use Kodi pathsubstitution to allow for music to play outside network + # The setting needs to be set before Kodi starts. + if utils.settings('enableMusicSync') == "true" and utils.settings('directstreammusic') == "true": + # We need to keep track of the settings + alternate = utils.settings('altip') == "true" + pathsub = utils.settings('pathsub') == "true" + + if pathsub and not alternate: + # Path sub in place, but primary address in use, remove it + utils.pathsubstitution(False) + elif not pathsub and alternate: + # Path sub not in place, but secondary address in use, add it + utils.pathsubstitution() if (self.newWebSocketThread is not None): ws.stopClient()