From 1bcba15cd983cc167ee818e416c22c29bcc5dce6 Mon Sep 17 00:00:00 2001
From: angelblue05 <tamara.angel05@gmail.com>
Date: Sat, 13 Feb 2016 22:42:16 -0600
Subject: [PATCH] Repair entries

For the video library. This is for the scenario where Kodi erases the
entry, but it still exists in the emby database.
---
 resources/lib/itemtypes.py | 58 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 56 insertions(+), 2 deletions(-)

diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py
index aacac99b..98c9ac5e 100644
--- a/resources/lib/itemtypes.py
+++ b/resources/lib/itemtypes.py
@@ -283,6 +283,17 @@ class Movies(Items):
             kodicursor.execute("select coalesce(max(idMovie),0) from movie")
             movieid = kodicursor.fetchone()[0] + 1
 
+        else:
+            # Verification the item is still in Kodi
+            query = "SELECT * FROM movie WHERE idMovie = ?"
+            kodicursor.execute(query, (movieid,))
+            try:
+                kodicursor.fetchone()[0]
+            except TypeError:
+                # item is not found, let's recreate it.
+                update_item = False
+                self.logMsg("movieid: %s missing from Kodi, repairing the entry." % movieid, 1)
+
         if not viewtag or not viewid:
             # Get view tag from emby
             viewtag, viewid, mediatype = self.emby.getView_embyId(itemid)
@@ -665,6 +676,17 @@ class MusicVideos(Items):
             kodicursor.execute("select coalesce(max(idMVideo),0) from musicvideo")
             mvideoid = kodicursor.fetchone()[0] + 1
 
+        else:
+            # Verification the item is still in Kodi
+            query = "SELECT * FROM musicvideo WHERE idMVideo = ?"
+            kodicursor.execute(query, (mvideoid,))
+            try:
+                kodicursor.fetchone()[0]
+            except TypeError:
+                # item is not found, let's recreate it.
+                update_item = False
+                self.logMsg("mvideoid: %s missing from Kodi, repairing the entry." % mvideoid, 1)
+
         if not viewtag or not viewid:
             # Get view tag from emby
             viewtag, viewid, mediatype = self.emby.getView_embyId(itemid)
@@ -991,6 +1013,7 @@ class TVShows(Items):
         # If the item already exist in the local Kodi DB we'll perform a full item update
         # If the item doesn't exist, we'll add it to the database
         update_item = True
+        force_episodes = False
         itemid = item['Id']
         emby_dbitem = emby_db.getItem_byId(itemid)
         try:
@@ -1001,6 +1024,22 @@ class TVShows(Items):
         except TypeError:
             update_item = False
             self.logMsg("showid: %s not found." % itemid, 2)
+            kodicursor.execute("select coalesce(max(idShow),0) from tvshow")
+            showid = kodicursor.fetchone()[0] + 1
+
+        else:
+            # Verification the item is still in Kodi
+            query = "SELECT * FROM tvshow WHERE idShow = ?"
+            kodicursor.execute(query, (showid,))
+            try:
+                kodicursor.fetchone()[0]
+            except TypeError:
+                # item is not found, let's recreate it.
+                update_item = False
+                self.logMsg("showid: %s missing from Kodi, repairing the entry." % showid, 1)
+                # Force re-add episodes after the show is re-created.
+                force_episodes = True
+
 
         if viewtag is None or viewid is None:
             # Get view tag from emby
@@ -1099,8 +1138,6 @@ class TVShows(Items):
             pathid = kodi_db.addPath(path)
             
             # Create the tvshow entry
-            kodicursor.execute("select coalesce(max(idShow),0) from tvshow")
-            showid = kodicursor.fetchone()[0] + 1
             query = (
                 '''
                 INSERT INTO tvshow(
@@ -1154,6 +1191,12 @@ class TVShows(Items):
             # Process artwork
             artwork.addArtwork(artwork.getAllArtwork(item), seasonid, "season", kodicursor)
 
+        if force_episodes:
+            # We needed to recreate the show entry. Re-add episodes now.
+            self.logMsg("Repairing episodes for showid: %s %s" % (showid, title), 1)
+            all_episodes = emby.getEpisodesbyShow(itemid)
+            self.added_episode(all_episodes['Items'], None)
+
     def add_updateSeason(self, item, showid=None):
 
         kodicursor = self.kodicursor
@@ -1214,6 +1257,17 @@ class TVShows(Items):
             kodicursor.execute("select coalesce(max(idEpisode),0) from episode")
             episodeid = kodicursor.fetchone()[0] + 1
 
+        else:
+            # Verification the item is still in Kodi
+            query = "SELECT * FROM episode WHERE idEpisode = ?"
+            kodicursor.execute(query, (episodeid,))
+            try:
+                kodicursor.fetchone()[0]
+            except TypeError:
+                # item is not found, let's recreate it.
+                update_item = False
+                self.logMsg("episodeid: %s missing from Kodi, repairing the entry." % episodeid, 1)
+
         # fileId information
         checksum = API.getChecksum()
         dateadded = API.getDateCreated()