From 582949e79cf47bf3ac3988748b801cc6837eaebb Mon Sep 17 00:00:00 2001
From: angelblue05 <tamara.angel05@gmail.com>
Date: Tue, 13 Oct 2015 05:05:03 -0500
Subject: [PATCH] Fix error caused by idSeason

Will properly skip the episode in the event the actual season is missing
it's number.
---
 resources/lib/WriteKodiVideoDB.py | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/resources/lib/WriteKodiVideoDB.py b/resources/lib/WriteKodiVideoDB.py
index 3b323085..7ee96632 100644
--- a/resources/lib/WriteKodiVideoDB.py
+++ b/resources/lib/WriteKodiVideoDB.py
@@ -627,18 +627,24 @@ class WriteKodiVideoDB():
             path = "plugin://plugin.video.emby/tvshows/%s/" % seriesId          
         
         # Validate the season exists in Emby and in database
-        if season is None or season == 0:
+        if season is None:
             self.logMsg("SKIP adding episode to Kodi Library, no season assigned - ID: %s - %s" % (embyId, title))
             return False
             
-        cursor.execute("SELECT idSeason FROM seasons WHERE idShow = ? and season = ?", (showid, season,))
-        try:
-            cursor.fetchone()[0]
-        except: # Season does not exist, update seasons
-            self.updateSeasons(seriesId, showid, connection, cursor)
-
-        cursor.execute("SELECT idSeason FROM seasons WHERE idShow = ? and season = ?", (showid, season,))
-        idSeason = cursor.fetchone()[0]
+        idSeason = None
+        count = 0
+        while idSeason is None:
+            cursor.execute("SELECT idSeason FROM seasons WHERE idShow = ? and season = ?", (showid, season,))
+            try:
+                idSeason = cursor.fetchone()[0]
+            except: # Season does not exist, update seasons
+                if not count:
+                    self.updateSeasons(seriesId, showid, connection, cursor)
+                    count += 1
+                else:
+                    # Season is still not found, skip episode.
+                    self.logMsg("Skipping episode: %s. Season number is missing at season level in the metadata manager." % title, 1)
+                    return False
 
         ##### UPDATE THE EPISODE #####
         if episodeid: