From 3431b5d28d493ebec6d1c51e1d1a91f34b1e3c03 Mon Sep 17 00:00:00 2001
From: angelblue05 <tamara.angel05@gmail.com>
Date: Sun, 8 Nov 2015 05:42:53 -0600
Subject: [PATCH] Use fallback artists

If album artists is missing
---
 resources/lib/WriteKodiMusicDB.py | 56 ++++++++++++++++++++-----------
 service.py                        |  1 +
 2 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/resources/lib/WriteKodiMusicDB.py b/resources/lib/WriteKodiMusicDB.py
index 3af43dbd..efb22e96 100644
--- a/resources/lib/WriteKodiMusicDB.py
+++ b/resources/lib/WriteKodiMusicDB.py
@@ -323,30 +323,48 @@ class WriteKodiMusicDB():
             albumid = cursor.fetchone()[0] + 1
             if kodiVersion == 15:
                 # Kodi Isengard
-                query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?, ?)"
-                cursor.execute(query, (albumid, artists, genre, year, dateadded, "single"))
+                query = "INSERT INTO album(idAlbum, strGenres, iYear, dateAdded, strReleaseType) values(?, ?, ?, ?, ?)"
+                cursor.execute(query, (albumid, genre, year, dateadded, "single"))
             elif kodiVersion == 16:
-                query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, strReleaseType) values(?, ?, ?, ?, ?)"
-                cursor.execute(query, (albumid, artists, genre, year, "single"))
+                query = "INSERT INTO album(idAlbum, strGenres, iYear, strReleaseType) values(?, ?, ?, ?)"
+                cursor.execute(query, (albumid, genre, year, "single"))
             else:
                 # Kodi Gotham and Helix
-                query = "INSERT INTO album(idAlbum, strArtists, strGenres, iYear, dateAdded) values(?, ?, ?, ?, ?)"
-                cursor.execute(query, (albumid, artists, genre, year, dateadded))
+                query = "INSERT INTO album(idAlbum, strGenres, iYear, dateAdded) values(?, ?, ?, ?)"
+                cursor.execute(query, (albumid, genre, year, dateadded))
         finally:
-            # Link album to artists
-            if MBitem['AlbumArtists']:
-                album_artists = MBitem['AlbumArtists']
-            else:
-                album_artists = MBitem['ArtistItems']
-
-            for artist in album_artists:
-                cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],))
-                try:
-                    artistid = cursor.fetchone()[0]
-                except: pass
+            cursor.execute("SELECT strArtists FROM album WHERE idAlbum = ?", (albumid,))
+            result = cursor.fetchone()
+            if result and result[0] == "":
+                # Link album to artists
+                if MBitem['AlbumArtists']:
+                    album_artists = MBitem['AlbumArtists']
                 else:
-                    query = "INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)"
-                    cursor.execute(query, (artistid, albumid, artist['Name']))
+                    album_artists = MBitem['ArtistItems']
+
+                MBartists = []
+                for artist in album_artists:
+                    MBartists.append(artist['Name'])
+                    cursor.execute("SELECT kodi_id FROM emby WHERE emby_id = ?", (artist['Id'],))
+                    try:
+                        artistid = cursor.fetchone()[0]
+                    except: pass
+                    else:
+                        query = "INSERT OR REPLACE INTO album_artist(idArtist, idAlbum, strArtist) values(?, ?, ?)"
+                        cursor.execute(query, (artistid, albumid, artist['Name']))
+
+                artists_onalbum = " / ".join(MBartists)
+                if kodiVersion == 15:
+                    # Kodi Isengard
+                    query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?"
+                    cursor.execute(query, (artists_onalbum, albumid))
+                elif kodiVersion == 16:
+                    query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?"
+                    cursor.execute(query, (artists_onalbum, albumid))
+                else:
+                    # Kodi Gotham and Helix
+                    query = "UPDATE album SET strArtists = ? WHERE idAlbum = ?"
+                    cursor.execute(query, (artists_onalbum, albumid))
 
 
         ##### UPDATE THE SONG #####
diff --git a/service.py b/service.py
index e845829d..3ef5ca3c 100644
--- a/service.py
+++ b/service.py
@@ -70,6 +70,7 @@ class Service():
         utils.window('Server_status', clear=True)
         utils.window('startup', clear=True)
         utils.window('OnWakeSync', clear=True)
+        utils.window('kodiScan', clear=True)
         utils.window('minDBVersionCheck', clear=True)
         
         # Set min DB version