From 10606f733defc194d6e0c9609adb33df304af77c Mon Sep 17 00:00:00 2001
From: angelblue05 <tamara.angel05@gmail.com>
Date: Tue, 12 Jan 2016 23:09:55 -0600
Subject: [PATCH] Fix crash during initial sync for songs

---
 resources/lib/itemtypes.py | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py
index e08080a3..a6d5b6e6 100644
--- a/resources/lib/itemtypes.py
+++ b/resources/lib/itemtypes.py
@@ -2209,22 +2209,31 @@ class Music(Items):
         emby_db.updateReference(itemid, checksum)
 
     def getSongRatingAndComment(self, embyid, emby_rating, API):
+        
+        kodicursor = self.kodicursor
+
         previous_values = None
         filename = API.getFilePath()
         rating = 0
-        emby_rating = int(round(emby_rating,0))
+        emby_rating = int(round(emby_rating, 0))
         file_rating, comment = musicutils.getSongTags(filename)
         
-        currentvalue = None
-        kodiid = self.emby_db.getItem_byId(embyid)[0]
-        query = ' '.join(("SELECT rating", "FROM song", "WHERE idSong = ?" ))
-        self.kodicursor.execute(query, (kodiid,))
-        currentvalue = int(round(float(self.kodicursor.fetchone()[0]),0))
+
+        emby_dbitem = self.emby_db.getItem_byId(embyid)
+        try:
+            kodiid = emby_dbitem[0]
+        except TypeError:
+            # Item is not in database.
+            currentvalue = None
+        else:
+            query = "SELECT rating FROM song WHERE idSong = ?"
+            kodicursor.execute(query, (kodiid,))
+            currentvalue = int(round(float(kodicursor.fetchone()[0]),0))
         
-        #only proceed if we actually have a rating from the file
-        if file_rating == None and currentvalue:
+        # Only proceed if we actually have a rating from the file
+        if file_rating is None and currentvalue:
             return (currentvalue, comment)
-        elif file_rating == None and not currentvalue:
+        elif file_rating is None and currentvalue is None:
             return (emby_rating, comment)
         
         file_rating = int(round(file_rating,0))