From db45faed248aee3db9e933f97b8a42e9bef568cd Mon Sep 17 00:00:00 2001 From: angelblue05 Date: Fri, 14 Aug 2015 22:16:38 -0500 Subject: [PATCH] Add userdata update only for music Only songs have playcount and lastplayed --- resources/lib/LibrarySync.py | 25 +++++++++++++-------- resources/lib/WriteKodiMusicDB.py | 36 ++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index 7525e56d..2a436bd2 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -869,12 +869,13 @@ class LibrarySync(threading.Thread): connectionvideo = utils.KodiSQL() cursorvideo = connectionvideo.cursor() # Database connection to myMusicXX.db - #connectionmusic = utils.KodiSQL('music') - #cursormusic = connectionmusic.cursor() + connectionmusic = utils.KodiSQL('music') + cursormusic = connectionmusic.cursor() count = 1 total = len(listItems) + 1 for userdata in listItems: + # Sort between video and music itemId = userdata['ItemId'] if(pDialog != None): @@ -889,13 +890,12 @@ class LibrarySync(threading.Thread): mediatype = cursorvideo.fetchone()[0] video.append(userdata) except: - self.logMsg("Item %s is not found in Kodi database." % itemId, 2) - '''cursormusic.execute("SELECT media_type FROM emby WHERE emby_id = ?", (itemId,)) + cursormusic.execute("SELECT media_type FROM emby WHERE emby_id = ?", (itemId,)) try: # Search music database self.logMsg("Check the music database.", 1) mediatype = cursormusic.fetchone()[0] music.append(userdata) - except: self.logMsg("Item %s is not found in Kodi database." % itemId, 2)''' + except: self.logMsg("Item %s is not found in Kodi database." % itemId, 2) if len(video) > 0: connection = connectionvideo @@ -908,7 +908,7 @@ class LibrarySync(threading.Thread): progressTitle = "Incremental Sync "+ " (" + str(count) + " of " + str(total) + ")" percentage = int(((float(count) / float(total)) * 100)) pDialog.update(percentage, "Emby for Kodi - Incremental Sync User Data ", progressTitle) - count = count + 1 + count = count + 1 WriteKodiVideoDB().updateUserdata(userdata, connection, cursor) connection.commit() @@ -916,20 +916,27 @@ class LibrarySync(threading.Thread): # Close connection cursorvideo.close() - '''if len(music) > 0: + if len(music) > 0: connection = connectionmusic cursor = cursormusic #Process music library + count = 1 + total = len(video) + 1 musicenabled = utils.settings('enableMusicSync') == "true" # Process the userdata update for music library if musicenabled: for userdata in music: + if(pDialog != None): + progressTitle = "Incremental Sync "+ " (" + str(count) + " of " + str(total) + ")" + percentage = int(((float(count) / float(total)) * 100)) + pDialog.update(percentage, "Emby for Kodi - Incremental Sync User Data ", progressTitle) + count = count + 1 WriteKodiMusicDB().updateUserdata(userdata, connection, cursor) connection.commit() - xbmc.executebuiltin("UpdateLibrary(music)")''' + #xbmc.executebuiltin("UpdateLibrary(music)") # Close connection - #cursormusic.close() + cursormusic.close() if(pDialog != None): pDialog.close() diff --git a/resources/lib/WriteKodiMusicDB.py b/resources/lib/WriteKodiMusicDB.py index 989e65bf..2c35d9f6 100644 --- a/resources/lib/WriteKodiMusicDB.py +++ b/resources/lib/WriteKodiMusicDB.py @@ -445,4 +445,38 @@ class WriteKodiMusicDB(): cursor.execute(query, (idGenre, id)) elif "song" in mediatype: query = "INSERT OR REPLACE INTO song_genre(idGenre, idSong) values(?, ?)" - cursor.execute(query, (idGenre, id)) \ No newline at end of file + cursor.execute(query, (idGenre, id)) + + def updateUserdata(self, userdata, connection, cursor): + # This updates: LastPlayedDate, Playcount + embyId = userdata['ItemId'] + MBitem = ReadEmbyDB().getItem(embyId) + + if not MBitem: + self.logMsg("UPDATE userdata to Kodi library FAILED, Item %s not found on server!" % embyId, 1) + return + + # Get details + checksum = API().getChecksum(MBitem) + userdata = API().getUserData(MBitem) + + # Find the Kodi Id + cursor.execute("SELECT kodi_id, media_type FROM emby WHERE emby_id = ?", (embyId,)) + try: + result = cursor.fetchone() + kodiid = result[0] + mediatype = result[1] + self.logMsg("Found embyId: %s in database - kodiId: %s type: %s" % (embyId, kodiid, mediatype), 1) + except: + self.logMsg("Id: %s not found in the emby database table." % embyId, 1) + else: + if mediatype in ("song"): + playcount = userdata['PlayCount'] + dateplayed = userdata['LastPlayedDate'] + + query = "UPDATE song SET iTimesPlayed = ?, lastplayed = ? WHERE idSong = ?" + cursor.execute(query, (playcount, dateplayed, kodiid)) + + #update the checksum in emby table + query = "UPDATE emby SET checksum = ? WHERE emby_id = ?" + cursor.execute(query, (checksum, embyId)) \ No newline at end of file