fixed the sources generation to more failsafe

This commit is contained in:
Marcel van der Veldt 2015-03-14 12:28:11 +01:00
parent 4214b3d289
commit f1e393d0f2
1 changed files with 39 additions and 41 deletions

View File

@ -72,43 +72,20 @@ def checkKodiSources():
rebootRequired = False rebootRequired = False
if not "mediabrowser_movies" in allKodiSources: if not "mediabrowser_movies" in allKodiSources:
addKodiSource("mediabrowser_movies",movieLibrary,"movies") rebootRequired = addKodiSource("mediabrowser_movies",movieLibrary,"movies")
rebootRequired = True
if not "mediabrowser_tvshows" in allKodiSources: if not "mediabrowser_tvshows" in allKodiSources:
addKodiSource("mediabrowser_tvshows",tvLibrary,"tvshows") rebootRequired = addKodiSource("mediabrowser_tvshows",tvLibrary,"tvshows")
rebootRequired = True
if rebootRequired: if rebootRequired:
ret = xbmcgui.Dialog().yesno(heading="MediaBrowser Sync service", line1="A restart of Kodi is needed to apply changes. Do you want to reboot now ?") ret = xbmcgui.Dialog().yesno(heading="MediaBrowser Sync service", line1="A restart of Kodi is needed to apply changes. After the reboot you need to manually assign the MediaBrowser sources to your library. See documentation. Do you want to reboot now ?")
if ret: if ret:
xbmc.executebuiltin("RestartApp") xbmc.executebuiltin("RestartApp")
def addKodiSource(name, path, type): def addKodiSource(name, path, type):
userDataPath = xbmc.translatePath( "special://profile" ) #add new source to database, common way is to add it directly to the Kodi DB. Fallback to adding it to the sources.xml
sourcesFile = os.path.join(userDataPath,'sources.xml') #return boolean wether a manual reboot is required.
#todo: Do feature request with Kodi team to get support for adding a source by the json API
print "####parsing sources file #####" + sourcesFile
tree = ET.ElementTree(file=sourcesFile)
root = tree.getroot()
videosources = root.find("video")
#remove any existing entries
allsources = videosources.findall("source")
if allsources != None:
for source in allsources:
if source.find("name").text == name:
videosources.remove(source)
# add new source
source = SubElement(videosources,'source')
SubElement(source, "name").text = name
SubElement(source, "path").text = path
tree.write(sourcesFile)
#add new source to database
dbPath = xbmc.translatePath("special://userdata/Database/MyVideos90.db") dbPath = xbmc.translatePath("special://userdata/Database/MyVideos90.db")
if xbmcvfs.exists(dbPath):
connection = sqlite3.connect(dbPath) connection = sqlite3.connect(dbPath)
cursor = connection.cursor( ) cursor = connection.cursor( )
cursor.execute("select coalesce(max(idPath),0) as pathId from path") cursor.execute("select coalesce(max(idPath),0) as pathId from path")
@ -119,6 +96,29 @@ def addKodiSource(name, path, type):
connection.commit() connection.commit()
cursor.close() cursor.close()
return False
else:
# if adding to the database failed, manually add it to sources.xml
sourcesFile = xbmc.translatePath( "special://profile/sources.xml" )
if xbmcvfs.exists(sourcesFile):
tree = ET.ElementTree(file=sourcesFile)
root = tree.getroot()
videosources = root.find("video")
#remove any existing entries for this path
allsources = videosources.findall("source")
if allsources != None:
for source in allsources:
if source.find("name").text == name:
videosources.remove(source)
# add the new source
source = SubElement(videosources,'source')
SubElement(source, "name").text = name
SubElement(source, "path").text = path
tree.write(sourcesFile)
return True
def checkAuthentication(): def checkAuthentication():
#check authentication #check authentication
if addonSettings.getSetting('username') != "" and addonSettings.getSetting('ipaddress') != "": if addonSettings.getSetting('username') != "" and addonSettings.getSetting('ipaddress') != "":
@ -143,7 +143,6 @@ def doKodiCleanup():
movies = result['movies'] movies = result['movies']
for movie in movies: for movie in movies:
if (xbmcvfs.exists(movie["file"]) == False) or ("plugin.video.xbmb3c" in movie["file"]): if (xbmcvfs.exists(movie["file"]) == False) or ("plugin.video.xbmb3c" in movie["file"]):
print "deleting --> " + movie["file"]
xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.RemoveMovie", "params": { "movieid": %i}, "id": 1 }' %(movie["movieid"])) xbmc.executeJSONRPC('{"jsonrpc": "2.0", "method": "VideoLibrary.RemoveMovie", "params": { "movieid": %i}, "id": 1 }' %(movie["movieid"]))
@ -169,7 +168,6 @@ def get_params( paramstring ):
param[splitparams[0]]=splitparams[1] param[splitparams[0]]=splitparams[1]
elif (len(splitparams))==3: elif (len(splitparams))==3:
param[splitparams[0]]=splitparams[1]+"="+splitparams[2] param[splitparams[0]]=splitparams[1]+"="+splitparams[2]
xbmc.log("XBMB3C -> Detected parameters: " + str(param))
return param return param