From 7b85d4aafca1152bc7dce07fa9670086fcf514c8 Mon Sep 17 00:00:00 2001 From: shaun Date: Mon, 20 Apr 2015 14:01:58 +1000 Subject: [PATCH] fix a lock up on DB access if you do an insert or update you MUST call commit() before returning --- resources/lib/WriteKodiDB.py | 83 ++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/resources/lib/WriteKodiDB.py b/resources/lib/WriteKodiDB.py index cfad50c7..dbcd306d 100644 --- a/resources/lib/WriteKodiDB.py +++ b/resources/lib/WriteKodiDB.py @@ -417,7 +417,6 @@ class WriteKodiDB(): #update artwork changes = False - artwork = {} artwork["thumb"] = API().getArtwork(MBitem, "Primary") @@ -692,13 +691,18 @@ class WriteKodiDB(): cursor.execute(pathsql, (movieid, fileid, title, plot, shortplot, rating, year, MBitem["Id"], sorttitle, runtime, title, trailerUrl)) + actionPerformed = False + try: connection.commit() utils.logMsg("Emby","Added movie to Kodi Library",MBitem["Id"] + " - " + MBitem["Name"]) + actionPerformed = True except: utils.logMsg("Emby","Error adding movie to Kodi Library",MBitem["Id"] + " - " + MBitem["Name"]) actionPerformed = False + return actionPerformed + def addMusicVideoToKodiLibrary( self, MBitem, connection, cursor ): #adds a musicvideo to Kodi by directly inserting it to connectionthe DB while there is no addMusicVideo available on the json API @@ -768,25 +772,55 @@ class WriteKodiDB(): pathsql="insert into musicvideo(idMVideo, idFile, c00, c04, c08, c23) values(?, ?, ?, ?, ?, ?)" cursor.execute(pathsql, (musicvideoid, fileid, title, runtime, plot, MBitem["Id"])) + actionPerformed = False + try: connection.commit() utils.logMsg("Emby","Added musicvideo to Kodi Library",MBitem["Id"] + " - " + MBitem["Name"]) + actionPerformed = True except: utils.logMsg("Emby","Error adding musicvideo to Kodi Library",MBitem["Id"] + " - " + MBitem["Name"]) - actionPerformed = False + return actionPerformed + def addEpisodeToKodiLibrary(self, MBitem, connection, cursor): #adds a Episode to Kodi by directly inserting it to the DB while there is no addEpisode available on the json API #TODO: PR at Kodi team for a addEpisode endpoint on their API + # check season + season = 0 + if MBitem.get("ParentIndexNumber") != None: + season = int(MBitem.get("ParentIndexNumber")) + else: + utils.logMsg("Emby","Error adding episode to Kodi Library, no ParentIndexNumber - ID: " + MBitem["Id"] + " - " + MBitem["Name"]) + return False + # first check the episode is not already in the DB using the Emby ID which is stored in c20 cursor.execute("SELECT idEpisode FROM episode WHERE c20 = ?",(MBitem["Id"],)) result = cursor.fetchone() if result != None: utils.logMsg("Emby", "Episode already exists in DB : " + MBitem["Id"] + " - " + MBitem["Name"], 2) - return + return False + # get the showid + cursor.execute("SELECT idShow as showid FROM tvshow WHERE c12 = ?",(MBitem["SeriesId"],)) + result = cursor.fetchone() + showid = -1 + if(result == None): + utils.logMsg("Emby","Error adding episode to Kodi Library, couldn't find show - ID: " + MBitem["Id"] + " - " + MBitem["Name"]) + return False + else: + showid = result[0] + + # check season + cursor.execute("SELECT idSeason FROM seasons WHERE idShow = ? and season = ?",(showid, season)) + result = cursor.fetchone() + if(result == None): + utils.logMsg("Emby","Error adding episode to Kodi Library, season does not exist - ShowId: " + str(showid) + " SeasonNo: " + str(season) + " EmbyId: " + MBitem["Id"] + " Name: " + MBitem["Name"]) + return False + + # do add addon = xbmcaddon.Addon(id='plugin.video.emby') port = addon.getSetting('port') host = addon.getSetting('ipaddress') @@ -844,33 +878,7 @@ class WriteKodiDB(): fileid = fileid + 1 sql="INSERT OR REPLACE into files(idFile, idPath, strFilename, playCount, lastPlayed, dateAdded) values(?, ?, ?, ?, ?, ?)" cursor.execute(sql, (fileid,pathid,filename,playcount,lastplayed,dateadded)) - - #get the showid - cursor.execute("SELECT idShow as showid FROM tvshow WHERE c12 = ?",(MBitem["SeriesId"],)) - result = cursor.fetchone() - showid = -1 - if(result == None): - utils.logMsg("Emby","Error adding episode to Kodi Library, couldn't find show - ID: " + MBitem["Id"] + " - " + MBitem["Name"]) - actionPerformed = False - return False - else: - showid = result[0] - - # check season - season = 0 - if MBitem.get("ParentIndexNumber") != None: - season = int(MBitem.get("ParentIndexNumber")) - else: - utils.logMsg("Emby","Error adding episode to Kodi Library, no ParentIndexNumber - ID: " + MBitem["Id"] + " - " + MBitem["Name"]) - return False - - cursor.execute("SELECT idSeason FROM seasons WHERE idShow = ? and season = ?",(showid, season)) - result = cursor.fetchone() - if(result == None): - utils.logMsg("Emby","Error adding episode to Kodi Library, season does not exist - ShowId: " + str(showid) + " SeasonNo: " + str(season) + " EmbyId: " + MBitem["Id"] + " Name: " + MBitem["Name"]) - actionPerformed = False - return False - + # build info episode = 0 if MBitem.get("IndexNumber") != None: @@ -893,13 +901,17 @@ class WriteKodiDB(): pathsql = "INSERT into episode(idEpisode, idFile, c00, c01, c03, c05, c09, c20, c12, c13, c14, idShow, c15, c16) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" cursor.execute(pathsql, (episodeid, fileid, title, plot, rating, premieredate, runtime, MBitem["Id"], season, episode, title, showid, "-1", "-1")) + actionPerformed = False + try: connection.commit() utils.logMsg("Emby","Added episode to Kodi Library - ID: " + MBitem["Id"] + " - " + MBitem["Name"]) + actionPerformed = True except: utils.logMsg("Emby","Error adding episode to Kodi Library - ID: " + MBitem["Id"] + " - " + MBitem["Name"]) - actionPerformed = False - + + return actionPerformed + def deleteMovieFromKodiLibrary(self, id ): kodiItem = ReadKodiDB().getKodiMovie(id) utils.logMsg("deleting movie from Kodi library",id) @@ -970,6 +982,7 @@ class WriteKodiDB(): elif "/" in path: toplevelpathstr = path.rsplit("/",2)[1] toplevelpath = path.replace(toplevelpathstr + "/","") + cursor.execute("SELECT idPath as tlpathid FROM path WHERE strPath = ?",(toplevelpath,)) result = cursor.fetchone() if result == None: @@ -997,12 +1010,16 @@ class WriteKodiDB(): pathsql="insert into tvshowlinkpath(idShow,idPath) values(?, ?)" cursor.execute(pathsql, (showid,pathid)) + actionPerformed = False + try: connection.commit() utils.logMsg("Emby","Added TV Show to Kodi Library: " + MBitem["Id"] + " - " + MBitem["Name"]) + actionPerformed = True except: utils.logMsg("Emby","Error adding tvshow to Kodi Library: " + MBitem["Id"] + " - " + MBitem["Name"]) - actionPerformed = False + + return actionPerformed def deleteTVShowFromKodiLibrary(self, id): xbmc.sleep(sleepVal)