From cde774fc9e62012f2c5bc5a88afc2c6a1e07a204 Mon Sep 17 00:00:00 2001 From: shaun Date: Thu, 16 Apr 2015 10:44:43 +1000 Subject: [PATCH] Add/Update TV Shows for Incremental TV Sync --- resources/lib/LibrarySync.py | 196 ++++++++++++++++++++--------------- resources/lib/ReadEmbyDB.py | 2 +- resources/lib/WriteKodiDB.py | 22 ++-- 3 files changed, 122 insertions(+), 98 deletions(-) diff --git a/resources/lib/LibrarySync.py b/resources/lib/LibrarySync.py index 81614829..3571f8e2 100644 --- a/resources/lib/LibrarySync.py +++ b/resources/lib/LibrarySync.py @@ -299,11 +299,10 @@ class LibrarySync(): for view in views: - progressTitle = "Sync DB : Processing " + view.get('title') + " " + str(viewCurrent) + " of " + str(viewCount) # incremental sync --> new episodes only - if not fullsync: + if fullsync == False: latestMBEpisodes = ReadEmbyDB().getLatestEpisodes(fullinfo = True, itemList = itemList) utils.logMsg("Sync TV", "Inc Sync Started on : " + str(len(latestMBEpisodes)) + " : " + str(itemList), 1) @@ -311,28 +310,57 @@ class LibrarySync(): if latestMBEpisodes != None: allKodiTvShowsIds = set(ReadKodiDB().getKodiTvShowsIds(True)) + # get included TV Shows + showList = [] + for episode in latestMBEpisodes: + if(episode["SeriesId"] not in showList): + showList.append(episode["SeriesId"]) + + utils.logMsg("Incremental TV Sync", "Included TV Show List : " + str(showList), 0) + + if(pDialog != None): + pDialog.update(0, progressTitle) + total = len(showList) + 1 + count = 1 + + # process included TV Shows + for showID in showList: + + embyTvShow = ReadEmbyDB().getFullItem(showID) + + if(showID not in allKodiTvShowsIds): + utils.logMsg("Incremental TV Sync", "Adding TV Show : " + embyTvShow.get("Name"), 1) + WriteKodiDB().addTVShowToKodiLibrary(embyTvShow, connection, cursor) + + kodiTvShow = ReadKodiDB().getKodiTVShow(showID) + utils.logMsg("Incremental TV Sync", "Updating TV Show : " + embyTvShow.get("Name"), 1) + WriteKodiDB().updateTVShowToKodiLibrary(embyTvShow, kodiTvShow, connection, cursor) + + # update progress bar + if(pDialog != None): + percentage = int(((float(count) / float(total)) * 100)) + pDialog.update(percentage, progressTitle, "Processing TV Shows : " + str(count)) + count += 1 + if(pDialog != None): pDialog.update(0, progressTitle) total = len(latestMBEpisodes) + 1 - count = 1 - + count = 1 + # process new episodes - for episode in latestMBEpisodes: - if episode["SeriesId"] in allKodiTvShowsIds: - #only process tvshows that already exist in the db at incremental updates - - if(self.ShouldStop(pDialog)): - return False - - WriteKodiDB().addEpisodeToKodiLibrary(episode, connection, cursor) - progressAction = "Adding" - totalItemsAdded += 1 - - # update progress bar - if(pDialog != None): - percentage = int(((float(count) / float(total)) * 100)) - pDialog.update(percentage, progressTitle, progressAction + " Episode: " + str(count)) - count += 1 + for episode in latestMBEpisodes: + if(self.ShouldStop(pDialog)): + return False + + WriteKodiDB().addEpisodeToKodiLibrary(episode, connection, cursor) + progressAction = "Adding" + totalItemsAdded += 1 + + # update progress bar + if(pDialog != None): + percentage = int(((float(count) / float(total)) * 100)) + pDialog.update(percentage, progressTitle, progressAction + " Episode: " + str(count)) + count += 1 #process updates if(pDialog != None): @@ -342,34 +370,34 @@ class LibrarySync(): count = 1 for episode in latestMBEpisodes: - if episode["SeriesId"] in allKodiTvShowsIds: - #only process tvshows that already exist in the db at incremental updates - allKodiTVShows = ReadKodiDB().getKodiTvShows(False) - kodishow = allKodiTVShows.get(episode["SeriesId"],None) - kodiEpisodes = ReadKodiDB().getKodiEpisodes(kodishow["tvshowid"],True,True) - - if(self.ShouldStop(pDialog)): - return False - - userData = API().getUserData(episode) - WINDOW.setProperty("EmbyUserKey" + userData.get("Key"), episode.get('Id') + ";;" + episode.get("Type")) - - #we have to compare the lists somehow - comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber")) - - if kodiEpisodes != None: - KodiItem = kodiEpisodes.get(comparestring1, None) - if(KodiItem != None): - WriteKodiDB().updateEpisodeToKodiLibrary(episode, KodiItem, connection, cursor) - - if(self.ShouldStop(pDialog)): - return False - - # update progress bar - if(pDialog != None): - percentage = int(((float(count) / float(total)) * 100)) - pDialog.update(percentage, progressTitle, "Updating Episode: " + str(count)) - count += 1 + if(self.ShouldStop(pDialog)): + return False + allKodiTVShows = ReadKodiDB().getKodiTvShows(False) + kodishow = allKodiTVShows.get(episode["SeriesId"],None) + kodiEpisodes = ReadKodiDB().getKodiEpisodes(kodishow["tvshowid"],True,True) + + if(self.ShouldStop(pDialog)): + return False + + userData = API().getUserData(episode) + WINDOW.setProperty("EmbyUserKey" + userData.get("Key"), episode.get('Id') + ";;" + episode.get("Type")) + + #we have to compare the lists somehow + comparestring1 = str(episode.get("ParentIndexNumber")) + "-" + str(episode.get("IndexNumber")) + + if kodiEpisodes != None: + KodiItem = kodiEpisodes.get(comparestring1, None) + if(KodiItem != None): + WriteKodiDB().updateEpisodeToKodiLibrary(episode, KodiItem, connection, cursor) + + if(self.ShouldStop(pDialog)): + return False + + # update progress bar + if(pDialog != None): + percentage = int(((float(count) / float(total)) * 100)) + pDialog.update(percentage, progressTitle, "Updating Episode: " + str(count)) + count += 1 # full sync --> Tv shows and Episodes @@ -390,6 +418,7 @@ class LibrarySync(): total = len(tvShowData) + 1 count = 1 + # add TV Shows for item in tvShowData: if item.get('IsFolder') and item.get('RecursiveItemCount') != 0: allTVShows.append(item["Id"]) @@ -410,13 +439,42 @@ class LibrarySync(): pDialog.update(percentage, progressTitle, "Adding Tv Show: " + str(count)) count += 1 - #process episodes first before updating tvshows - allEpisodes = list() - - showTotal = len(viewTVShows) - showCurrent = 1 + if(pDialog != None): + progressTitle = "Sync DB : Processing TV Shows" + pDialog.update(0, progressTitle, "") + total = len(viewTVShows) + 1 + count = 1 + # update TV Shows + allKodiTVShows = ReadKodiDB().getKodiTvShows(True) + for item in tvShowData: + if item.get('IsFolder'): + item['Tag'] = [] + item['Tag'].append(view.get('title')) + if allKodiTVShows != None: + kodishow = allKodiTVShows.get(item["Id"],None) + else: + kodishow = None + + if(kodishow != None): + updated = WriteKodiDB().updateTVShowToKodiLibrary(item,kodishow,connection, cursor) + if(updated): + totalItemsUpdated += 1 + + if(self.ShouldStop(pDialog)): + return False + + # update progress bar + if(pDialog != None): + percentage = int(((float(count) / float(total)) * 100)) + pDialog.update(percentage, progressTitle, "Updating Tv Show: " + str(count)) + count += 1 + + # do episode adds + allEpisodes = list() + showTotal = len(viewTVShows) + showCurrent = 1 for tvshow in viewTVShows: episodeData = ReadEmbyDB().getEpisodes(tvshow,True) @@ -447,38 +505,6 @@ class LibrarySync(): showCurrent += 1 - - if(pDialog != None): - progressTitle = "Sync DB : Processing TV Shows" - pDialog.update(0, progressTitle, "") - total = len(viewTVShows) + 1 - count = 1 - - #process updates at TV Show level - allKodiTVShows = ReadKodiDB().getKodiTvShows(True) - for item in tvShowData: - if item.get('IsFolder'): - item['Tag'] = [] - item['Tag'].append(view.get('title')) - if allKodiTVShows != None: - kodishow = allKodiTVShows.get(item["Id"],None) - else: - kodishow = None - - if(kodishow != None): - updated = WriteKodiDB().updateTVShowToKodiLibrary(item,kodishow,connection, cursor) - if(updated): - totalItemsUpdated += 1 - - if(self.ShouldStop(pDialog)): - return False - - # update progress bar - if(pDialog != None): - percentage = int(((float(count) / float(total)) * 100)) - pDialog.update(percentage, progressTitle, "Updating Tv Show: " + str(count)) - count += 1 - # do episode updates showCurrent = 1 for tvshow in viewTVShows: diff --git a/resources/lib/ReadEmbyDB.py b/resources/lib/ReadEmbyDB.py index 9581c11b..1e888325 100644 --- a/resources/lib/ReadEmbyDB.py +++ b/resources/lib/ReadEmbyDB.py @@ -107,7 +107,7 @@ class ReadEmbyDB(): downloadUtils = DownloadUtils() - url = "%s/mediabrowser/Users/%s/Items/%s?format=json" % (server, userid, id) + url = "%s/mediabrowser/Users/%s/Items/%s?format=json&Fields=Path,Genres,SortName,Studios,Writer,ProductionYear,Taglines,CommunityRating,OfficialRating,CumulativeRunTimeTicks,Metascore,AirTime,DateCreated,MediaStreams,People,Overview" % (server, userid, id) jsonData = downloadUtils.downloadUrl(url, suppress=False, popup=1 ) if jsonData != None and jsonData != "": result = json.loads(jsonData) diff --git a/resources/lib/WriteKodiDB.py b/resources/lib/WriteKodiDB.py index 1569a474..31a4a976 100644 --- a/resources/lib/WriteKodiDB.py +++ b/resources/lib/WriteKodiDB.py @@ -285,7 +285,7 @@ class WriteKodiDB(): utils.logMsg("Updated item to Kodi Library", MBitem["Id"] + " - " + MBitem["Name"]) def updateTVShowToKodiLibrary( self, MBitem, KodiItem,connection, cursor ): - + addon = xbmcaddon.Addon(id='plugin.video.emby') port = addon.getSetting('port') host = addon.getSetting('ipaddress') @@ -303,22 +303,21 @@ class WriteKodiDB(): changes = False - #set Filename playurl = PlayUtils().getPlayUrl(server, MBitem["Id"], MBitem) #make sure that the path always ends with a slash playurl = playurl + "/" self.setKodiFilename(KodiItem["tvshowid"], KodiItem["file"], playurl, "tvshow", MBitem["Id"], connection, cursor) - + #update/check all artwork changes |= self.updateArtWork(KodiItem,MBitem) - + #update common properties if MBitem.get("PremiereDate") != None: premieredatelist = (MBitem.get("PremiereDate")).split("T") premieredate = premieredatelist[0] changes |= self.updateProperty(KodiItem,"premiered",premieredate,"tvshow") - + changes |= self.updatePropertyArray(KodiItem,"tag",MBitem.get("Tag"),"tvshow") changes |= self.updateProperty(KodiItem,"mpaa",MBitem.get("OfficialRating"),"tvshow") changes |= self.updateProperty(KodiItem,"lastplayed",MBitem.get("LastPlayedDate"),"tvshow") @@ -330,10 +329,8 @@ class WriteKodiDB(): changes |= self.updateProperty(KodiItem,"title",utils.convertEncoding(MBitem["Name"]),"tvshow") changes |= self.updateProperty(KodiItem,"plot",utils.convertEncoding(API().getOverview(MBitem)),"tvshow") - - if MBitem.get("ProviderIds") != None: - if MBitem.get("ProviderIds").get("Imdb") != None: - changes |= self.updateProperty(KodiItem,"imdbnumber",MBitem.get("ProviderIds").get("Imdb"),"tvshow") + # we use this to store the Emby ID so make sure we use that + changes |= self.updateProperty(KodiItem, "imdbnumber", MBitem.get("Id"), "tvshow") changes |= self.updatePropertyArray(KodiItem,"genre",MBitem.get("Genres"),"tvshow") @@ -343,7 +340,7 @@ class WriteKodiDB(): changes |= self.updatePropertyArray(KodiItem,"studio",studios,"tvshow") # FIXME --> ProductionLocations not returned by MB3 server !? - changes |= self.updatePropertyArray(KodiItem,"country",MBitem.get("ProductionLocations"),"tvshow") + changes |= self.updatePropertyArray(KodiItem, "country", MBitem.get("ProductionLocations"), "tvshow") #add actors changes |= self.AddActorsToMedia(KodiItem,MBitem.get("People"),"tvshow", connection, cursor) @@ -353,7 +350,7 @@ class WriteKodiDB(): if changes: utils.logMsg("Updated item to Kodi Library", MBitem["Id"] + " - " + MBitem["Name"]) - + return changes def updateEpisodeToKodiLibrary( self, MBitem, KodiItem, connection, cursor ): @@ -672,7 +669,7 @@ class WriteKodiDB(): jsoncommand = '{"jsonrpc": "2.0", "method": "VideoLibrary.SetMovieDetails", "params": { "movieid": %i, "%s": %s}, "id": 1 }' pendingChanges = False - if propertyCollection != None: + if (propertyCollection != None and KodiItem.get(propertyName) != None): currentvalues = set(KodiItem[propertyName]) for item in propertyCollection: if not item in currentvalues: @@ -944,6 +941,7 @@ class WriteKodiDB(): season = 0 if MBitem.get("ParentIndexNumber") != None: season = int(MBitem.get("ParentIndexNumber")) + else: utils.logMsg("Emby","Error adding episode to Kodi Library, no ParentIndexNumber - ID: " + MBitem["Id"] + " - " + MBitem["Name"]) return False