From cd7b5092488228cee8ec5c5c7ca25ec1f8555fff Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 19:27:14 +0100 Subject: [PATCH 01/19] Fix UnicodeDecodeError for non-ASCII filenames - Fixes #222 --- resources/lib/player.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/resources/lib/player.py b/resources/lib/player.py index 7e5d05ff..56123e49 100644 --- a/resources/lib/player.py +++ b/resources/lib/player.py @@ -8,7 +8,7 @@ import xbmc import xbmcgui from utils import window, settings, language as lang, DateToKodi, \ - getUnixTimestamp + getUnixTimestamp, tryDecode, tryEncode import downloadutils import plexdb_functions as plexdb import kodidb_functions as kodidb @@ -48,7 +48,7 @@ class Player(xbmc.Player): # Get current file (in utf-8!) try: - currentFile = self.getPlayingFile() + currentFile = tryDecode(self.getPlayingFile()) xbmc.sleep(300) except: currentFile = "" @@ -56,7 +56,7 @@ class Player(xbmc.Player): while not currentFile: xbmc.sleep(100) try: - currentFile = self.getPlayingFile() + currentFile = tryDecode(self.getPlayingFile()) except: pass if count == 20: @@ -69,13 +69,13 @@ class Player(xbmc.Player): # Save currentFile for cleanup later and for references self.currentFile = currentFile - window('plex_lastPlayedFiled', value=currentFile) + window('plex_lastPlayedFiled', value=tryEncode(currentFile)) # We may need to wait for info to be set in kodi monitor - itemId = window("plex_%s.itemid" % currentFile) + itemId = window("plex_%s.itemid" % tryEncode(currentFile)) count = 0 while not itemId: xbmc.sleep(200) - itemId = window("plex_%s.itemid" % currentFile) + itemId = window("plex_%s.itemid" % tryEncode(currentFile)) if count == 5: log.warn("Could not find itemId, cancelling playback report!") return @@ -83,7 +83,7 @@ class Player(xbmc.Player): log.info("ONPLAYBACK_STARTED: %s itemid: %s" % (currentFile, itemId)) - plexitem = "plex_%s" % currentFile + plexitem = "plex_%s" % tryEncode(currentFile) runtime = window("%s.runtime" % plexitem) refresh_id = window("%s.refreshid" % plexitem) playMethod = window("%s.playmethod" % plexitem) @@ -146,8 +146,10 @@ class Player(xbmc.Player): # Get the current audio track and subtitles if playMethod == "Transcode": # property set in PlayUtils.py - postdata['AudioStreamIndex'] = window("%sAudioStreamIndex" % currentFile) - postdata['SubtitleStreamIndex'] = window("%sSubtitleStreamIndex" % currentFile) + postdata['AudioStreamIndex'] = window("%sAudioStreamIndex" + % tryEncode(currentFile)) + postdata['SubtitleStreamIndex'] = window("%sSubtitleStreamIndex" + % tryEncode(currentFile)) else: # Get the current kodi audio and subtitles and convert to plex equivalent tracks_query = { @@ -385,15 +387,16 @@ class Player(xbmc.Player): # Clean the WINDOW properties for filename in self.played_info: + plex_item = 'plex_%s' % tryEncode(filename) cleanup = ( - 'plex_%s.itemid' % filename, - 'plex_%s.runtime' % filename, - 'plex_%s.refreshid' % filename, - 'plex_%s.playmethod' % filename, - 'plex_%s.type' % filename, - 'plex_%s.runtime' % filename, - 'plex_%s.playcount' % filename, - 'plex_%s.playlistPosition' % filename + '%s.itemid' % plex_item, + '%s.runtime' % plex_item, + '%s.refreshid' % plex_item, + '%s.playmethod' % plex_item, + '%s.type' % plex_item, + '%s.runtime' % plex_item, + '%s.playcount' % plex_item, + '%s.playlistPosition' % plex_item ) for item in cleanup: window(item, clear=True) From d54014cff8854e043b5c30d839b227fe2e8a8fb7 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 19:54:27 +0100 Subject: [PATCH 02/19] Remove obsolete encoding --- resources/lib/player.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lib/player.py b/resources/lib/player.py index 56123e49..bba39909 100644 --- a/resources/lib/player.py +++ b/resources/lib/player.py @@ -69,7 +69,7 @@ class Player(xbmc.Player): # Save currentFile for cleanup later and for references self.currentFile = currentFile - window('plex_lastPlayedFiled', value=tryEncode(currentFile)) + window('plex_lastPlayedFiled', value=currentFile) # We may need to wait for info to be set in kodi monitor itemId = window("plex_%s.itemid" % tryEncode(currentFile)) count = 0 From aa5cb37a8f32c9748a08270d379eb0269f08454e Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 20:19:22 +0100 Subject: [PATCH 03/19] Krypton: add ratings for episodes - Fixes #71 --- resources/lib/itemtypes.py | 55 +++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index cd0f6bad..f2b92b70 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -809,13 +809,13 @@ class TVShows(Items): userdata = API.getUserData() playcount = userdata['PlayCount'] dateplayed = userdata['LastPlayedDate'] + tvdb = API.getProvider('tvdb') + votecount = None # item details peoples = API.getPeople() director = API.joinList(peoples['Director']) writer = API.joinList(peoples['Writer']) - cast = API.joinList(peoples['Cast']) - producer = API.joinList(peoples['Producer']) title, sorttitle = API.getTitle() plot = API.getPlot() rating = userdata['Rating'] @@ -916,7 +916,21 @@ class TVShows(Items): log.info("UPDATE episode itemid: %s" % (itemid)) # Update the movie entry if v.KODIVERSION >= 17: - # Kodi Krypton + # update new ratings Kodi 17 + ratingid = self.kodi_db.get_ratingid(episodeid) + self.kodi_db.update_ratings(episodeid, + v.KODI_TYPE_EPISODE, + "default", + rating, + votecount, + ratingid) + # update new uniqueid Kodi 17 + uniqueid = self.kodi_db.get_uniqueid(episodeid) + self.kodi_db.update_uniqueid(episodeid, + v.KODI_TYPE_EPISODE, + tvdb, + "tvdb", + uniqueid) query = ''' UPDATE episode SET c00 = ?, c01 = ?, c03 = ?, c04 = ?, c05 = ?, c09 = ?, @@ -962,7 +976,19 @@ class TVShows(Items): log.info("ADD episode itemid: %s - Title: %s" % (itemid, title)) # Create the episode entry if v.KODIVERSION >= 17: - # Kodi Krypton + # add new ratings Kodi 17 + self.kodi_db.add_ratings(self.kodi_db.create_entry_rating(), + episodeid, + v.KODI_TYPE_EPISODE, + "default", + rating, + votecount) + # add new uniqueid Kodi 17 + self.kodi_db.add_uniqueid(self.kodi_db.create_entry_uniqueid(), + episodeid, + v.KODI_TYPE_EPISODE, + tvdb, + "tvdb") query = ''' INSERT INTO episode( idEpisode, idFile, c00, c01, c03, c04, c05, c09, c10, c12, c13, c14, idShow, c15, c16, c18, @@ -1193,18 +1219,23 @@ class TVShows(Items): self.kodi_db.remove_ratings(kodi_id, v.KODI_TYPE_SHOW) log.info("Removed tvshow: %s." % kodi_id) - def removeSeason(self, kodiid): + def removeSeason(self, kodi_id): kodicursor = self.kodicursor - self.artwork.deleteArtwork(kodiid, "season", kodicursor) - kodicursor.execute("DELETE FROM seasons WHERE idSeason = ?", (kodiid,)) - log.info("Removed season: %s." % kodiid) + self.artwork.deleteArtwork(kodi_id, "season", kodicursor) + kodicursor.execute("DELETE FROM seasons WHERE idSeason = ?", + (kodi_id,)) + log.info("Removed season: %s." % kodi_id) - def removeEpisode(self, kodiid, fileid): + def removeEpisode(self, kodi_id, fileid): kodicursor = self.kodicursor - self.artwork.deleteArtwork(kodiid, "episode", kodicursor) - kodicursor.execute("DELETE FROM episode WHERE idEpisode = ?", (kodiid,)) + self.artwork.deleteArtwork(kodi_id, "episode", kodicursor) + kodicursor.execute("DELETE FROM episode WHERE idEpisode = ?", + (kodi_id,)) kodicursor.execute("DELETE FROM files WHERE idFile = ?", (fileid,)) - log.info("Removed episode: %s." % kodiid) + if v.KODIVERSION >= 17: + self.kodi_db.remove_uniqueid(kodi_id, v.KODI_TYPE_EPISODE) + self.kodi_db.remove_ratings(kodi_id, v.KODI_TYPE_EPISODE) + log.info("Removed episode: %s." % kodi_id) class Music(Items): From 9612ee78abbad5d63d70b93fb1ca6335eaa508b4 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 20:23:02 +0100 Subject: [PATCH 04/19] Fix getting the rating id --- resources/lib/itemtypes.py | 8 +++++--- resources/lib/kodidb_functions.py | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index f2b92b70..1b265cc1 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -314,7 +314,8 @@ class Movies(Items): # Update the movie entry if v.KODIVERSION >= 17: # update new ratings Kodi 17 - ratingid = self.kodi_db.get_ratingid(movieid) + ratingid = self.kodi_db.get_ratingid(movieid, + v.KODI_TYPE_MOVIE) self.kodi_db.update_ratings(movieid, v.KODI_TYPE_MOVIE, "default", @@ -596,7 +597,7 @@ class TVShows(Items): % (itemid, title)) if v.KODIVERSION >= 17: # update new ratings Kodi 17 - ratingid = self.kodi_db.get_ratingid(showid) + ratingid = self.kodi_db.get_ratingid(showid, v.KODI_TYPE_SHOW) self.kodi_db.update_ratings(showid, v.KODI_TYPE_SHOW, "default", @@ -917,7 +918,8 @@ class TVShows(Items): # Update the movie entry if v.KODIVERSION >= 17: # update new ratings Kodi 17 - ratingid = self.kodi_db.get_ratingid(episodeid) + ratingid = self.kodi_db.get_ratingid(episodeid, + v.KODI_TYPE_EPISODE) self.kodi_db.update_ratings(episodeid, v.KODI_TYPE_EPISODE, "default", diff --git a/resources/lib/kodidb_functions.py b/resources/lib/kodidb_functions.py index ac14853b..7f5262fa 100644 --- a/resources/lib/kodidb_functions.py +++ b/resources/lib/kodidb_functions.py @@ -1465,9 +1465,12 @@ class Kodidb_Functions(): self.cursor.execute("select coalesce(max(rating_id),0) from rating") return self.cursor.fetchone()[0] + 1 - def get_ratingid(self, media_id): - query = "SELECT rating_id FROM rating WHERE media_id = ?" - self.cursor.execute(query, (media_id,)) + def get_ratingid(self, kodi_id, kodi_type): + query = ''' + SELECT rating_id FROM rating + WHERE media_id = ? AND media_type = ? + ''' + self.cursor.execute(query, (kodi_id, kodi_type)) try: ratingid = self.cursor.fetchone()[0] except TypeError: From 922f4150a0c0082884a8607bd9a69ca2a0af679a Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 20:26:30 +0100 Subject: [PATCH 05/19] Fix getting unique id --- resources/lib/itemtypes.py | 8 +++++--- resources/lib/kodidb_functions.py | 9 ++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 1b265cc1..e729e80a 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -323,7 +323,8 @@ class Movies(Items): votecount, ratingid) # update new uniqueid Kodi 17 - uniqueid = self.kodi_db.get_uniqueid(movieid) + uniqueid = self.kodi_db.get_uniqueid(movieid, + v.KODI_TYPE_MOVIE) self.kodi_db.update_uniqueid(movieid, v.KODI_TYPE_MOVIE, imdb, @@ -605,7 +606,7 @@ class TVShows(Items): None, # votecount ratingid) # update new uniqueid Kodi 17 - uniqueid = self.kodi_db.get_uniqueid(showid) + uniqueid = self.kodi_db.get_uniqueid(showid, v.KODI_TYPE_SHOW) self.kodi_db.update_uniqueid(showid, v.KODI_TYPE_SHOW, tvdb, @@ -927,7 +928,8 @@ class TVShows(Items): votecount, ratingid) # update new uniqueid Kodi 17 - uniqueid = self.kodi_db.get_uniqueid(episodeid) + uniqueid = self.kodi_db.get_uniqueid(episodeid, + v.KODI_TYPE_EPISODE) self.kodi_db.update_uniqueid(episodeid, v.KODI_TYPE_EPISODE, tvdb, diff --git a/resources/lib/kodidb_functions.py b/resources/lib/kodidb_functions.py index 7f5262fa..322588fc 100644 --- a/resources/lib/kodidb_functions.py +++ b/resources/lib/kodidb_functions.py @@ -1434,9 +1434,12 @@ class Kodidb_Functions(): ''' self.cursor.execute(query, (args)) - def get_uniqueid(self, media_id): - query = "SELECT uniqueid_id FROM uniqueid WHERE media_id = ?" - self.cursor.execute(query, (media_id,)) + def get_uniqueid(self, kodi_id, kodi_type): + query = ''' + SELECT uniqueid_id FROM uniqueid + WHERE media_id = ? AND media_type = ? + ''' + self.cursor.execute(query, (kodi_id, kodi_type)) try: uniqueid = self.cursor.fetchone()[0] except TypeError: From f5198bf9379a25a5b515c2cfac6f40f88e42fdea Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 20:27:39 +0100 Subject: [PATCH 06/19] Elaborate on function description --- resources/lib/kodidb_functions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lib/kodidb_functions.py b/resources/lib/kodidb_functions.py index 322588fc..cf5b0542 100644 --- a/resources/lib/kodidb_functions.py +++ b/resources/lib/kodidb_functions.py @@ -1425,7 +1425,7 @@ class Kodidb_Functions(): Feed with: uniqueid_id, media_id, media_type, value, type - type: e.g. 'imdb' + type: e.g. 'imdb' or 'tvdb' """ query = ''' INSERT INTO uniqueid( From 7956639a0efe320dd26fec7a19f62d6d0654745a Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 20:46:51 +0100 Subject: [PATCH 07/19] Code optimization --- resources/lib/itemtypes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index e729e80a..e8dea36d 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -638,8 +638,7 @@ class TVShows(Items): log.info("ADD tvshow itemid: %s - Title: %s" % (itemid, title)) if v.KODIVERSION >= 17: # add new ratings Kodi 17 - ratingid = self.kodi_db.create_entry_rating() - self.kodi_db.add_ratings(ratingid, + self.kodi_db.add_ratings(self.kodi_db.create_entry_rating(), showid, v.KODI_TYPE_SHOW, "default", From 43b8866c446b5d9bab534e772d1dd31f2c0d1ed3 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 20:47:21 +0100 Subject: [PATCH 08/19] Code optimization --- resources/lib/itemtypes.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index e8dea36d..80061bd9 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -645,8 +645,7 @@ class TVShows(Items): rating, None) # votecount # add new uniqueid Kodi 17 - uniqueid = self.kodi_db.create_entry_uniqueid() - self.kodi_db.add_uniqueid(uniqueid, + self.kodi_db.add_uniqueid(self.kodi_db.create_entry_uniqueid(), showid, v.KODI_TYPE_SHOW, tvdb, From a7adfa1cc5fd8f4eb1c3f2900a06a2ac98334bdb Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Mon, 13 Feb 2017 20:50:10 +0100 Subject: [PATCH 09/19] Elaborate on function --- resources/lib/kodidb_functions.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/resources/lib/kodidb_functions.py b/resources/lib/kodidb_functions.py index cf5b0542..5148fb47 100644 --- a/resources/lib/kodidb_functions.py +++ b/resources/lib/kodidb_functions.py @@ -1423,9 +1423,11 @@ class Kodidb_Functions(): def add_uniqueid(self, *args): """ Feed with: - uniqueid_id, media_id, media_type, value, type - - type: e.g. 'imdb' or 'tvdb' + uniqueid_id: int + media_id: int + media_type: string + value: string + type: e.g. 'imdb' or 'tvdb' """ query = ''' INSERT INTO uniqueid( From 5a6f0411f247d16c8669a985b3c2f6a4e036c625 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Fri, 17 Feb 2017 19:35:51 +0100 Subject: [PATCH 10/19] Fix ratings for episodes --- resources/lib/itemtypes.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 80061bd9..fa28b359 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -979,7 +979,8 @@ class TVShows(Items): # Create the episode entry if v.KODIVERSION >= 17: # add new ratings Kodi 17 - self.kodi_db.add_ratings(self.kodi_db.create_entry_rating(), + rating_id = self.kodi_db.create_entry_rating() + self.kodi_db.add_ratings(rating_id, episodeid, v.KODI_TYPE_EPISODE, "default", @@ -999,7 +1000,7 @@ class TVShows(Items): ?, ?) ''' kodicursor.execute(query, (episodeid, fileid, title, plot, - rating, writer, premieredate, runtime, director, season, + rating_id, writer, premieredate, runtime, director, season, episode, title, showid, airsBeforeSeason, airsBeforeEpisode, playurl, pathid, seasonid, userdata['UserRating'])) From 806f237b709b8a1ea41478ceaf08d279281592a2 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 14:45:31 +0100 Subject: [PATCH 11/19] Use unique TheTVDB link for episodes --- resources/lib/PlexAPI.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lib/PlexAPI.py b/resources/lib/PlexAPI.py index 6a62f5be..37350607 100644 --- a/resources/lib/PlexAPI.py +++ b/resources/lib/PlexAPI.py @@ -57,7 +57,7 @@ import variables as v log = logging.getLogger("PLEX."+__name__) REGEX_IMDB = re_compile(r'''/(tt\d+)''') -REGEX_TVDB = re_compile(r'''tvdb://(\d+)''') +REGEX_TVDB = re_compile(r'''thetvdb:\/\/(.+?)\?''') ############################################################################### From c359c6fff8946118dee4535a0a5b682fde1ae536 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 16:30:06 +0100 Subject: [PATCH 12/19] Fix ratings for TV shows --- resources/lib/itemtypes.py | 175 ++++++++++++++++++++----------------- 1 file changed, 94 insertions(+), 81 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index fa28b359..a2437546 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -514,8 +514,6 @@ class TVShows(Items): if not itemid: log.error("Cannot parse XML data for TV show") return - # If the item already exist in the local Kodi DB we'll perform a full item update - # If the item doesn't exist, we'll add it to the database update_item = True force_episodes = False plex_dbitem = plex_db.getItem_byId(itemid) @@ -549,6 +547,7 @@ class TVShows(Items): title, sorttitle = API.getTitle() plot = API.getPlot() rating = API.getAudienceRating() + votecount = None premieredate = API.getPremiereDate() tvdb = API.getProvider('tvdb') mpaa = API.getMpaa() @@ -596,33 +595,6 @@ class TVShows(Items): if update_item: log.info("UPDATE tvshow itemid: %s - Title: %s" % (itemid, title)) - if v.KODIVERSION >= 17: - # update new ratings Kodi 17 - ratingid = self.kodi_db.get_ratingid(showid, v.KODI_TYPE_SHOW) - self.kodi_db.update_ratings(showid, - v.KODI_TYPE_SHOW, - "default", - rating, - None, # votecount - ratingid) - # update new uniqueid Kodi 17 - uniqueid = self.kodi_db.get_uniqueid(showid, v.KODI_TYPE_SHOW) - self.kodi_db.update_uniqueid(showid, - v.KODI_TYPE_SHOW, - tvdb, - "tvdb", - uniqueid) - # Update the tvshow entry - query = ' '.join(( - - "UPDATE tvshow", - "SET c00 = ?, c01 = ?, c04 = ?, c05 = ?, c08 = ?, c09 = ?,", - "c12 = ?, c13 = ?, c14 = ?, c15 = ?", - "WHERE idShow = ?" - )) - kodicursor.execute(query, (title, plot, rating, premieredate, genre, title, - tvdb, mpaa, studio, sorttitle, showid)) - # Add reference is idempotent; the call here updates also fileid # and pathid when item is moved or renamed plex_db.addReference(itemid, @@ -632,48 +604,60 @@ class TVShows(Items): kodi_pathid=pathid, checksum=checksum, view_id=viewid) - - ##### OR ADD THE TVSHOW ##### + if v.KODIVERSION >= 17: + # update new ratings Kodi 17 + rating_id = self.kodi_db.get_ratingid(showid, v.KODI_TYPE_SHOW) + self.kodi_db.update_ratings(showid, + v.KODI_TYPE_SHOW, + "default", + rating, + votecount, + rating_id) + # update new uniqueid Kodi 17 + uniqueid = self.kodi_db.get_uniqueid(showid, v.KODI_TYPE_SHOW) + self.kodi_db.update_uniqueid(showid, + v.KODI_TYPE_SHOW, + tvdb, + "tvdb", + uniqueid) + # Update the tvshow entry + query = ''' + UPDATE tvshow + SET c00 = ?, c01 = ?, c04 = ?, c05 = ?, c08 = ?, c09 = ?, + c12 = ?, c13 = ?, c14 = ?, c15 = ? + WHERE idShow = ? + ''' + kodicursor.execute(query, (title, plot, rating_id, + premieredate, genre, title, tvdb, + mpaa, studio, sorttitle, showid)) + else: + # Update the tvshow entry + query = ''' + UPDATE tvshow + SET c00 = ?, c01 = ?, c04 = ?, c05 = ?, c08 = ?, c09 = ?, + c12 = ?, c13 = ?, c14 = ?, c15 = ? + WHERE idShow = ? + ''' + kodicursor.execute(query, (title, plot, rating, premieredate, + genre, title, tvdb, mpaa, studio, + sorttitle, showid)) + + # OR ADD THE TVSHOW ##### else: log.info("ADD tvshow itemid: %s - Title: %s" % (itemid, title)) - if v.KODIVERSION >= 17: - # add new ratings Kodi 17 - self.kodi_db.add_ratings(self.kodi_db.create_entry_rating(), - showid, - v.KODI_TYPE_SHOW, - "default", - rating, - None) # votecount - # add new uniqueid Kodi 17 - self.kodi_db.add_uniqueid(self.kodi_db.create_entry_uniqueid(), - showid, - v.KODI_TYPE_SHOW, - tvdb, - "tvdb") - query = ' '.join(( - - "UPDATE path", - "SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?", - "WHERE idPath = ?" - )) - kodicursor.execute(query, (toplevelpath, "tvshows", "metadata.local", 1, toppathid)) - - # Create the tvshow entry - query = ( - ''' - INSERT INTO tvshow( - idShow, c00, c01, c04, c05, c08, c09, c12, c13, c14, c15) - - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - ''' - ) - kodicursor.execute(query, (showid, title, plot, rating, premieredate, genre, - title, tvdb, mpaa, studio, sorttitle)) - + query = ''' + UPDATE path + SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ? + WHERE idPath = ? + ''' + kodicursor.execute(query, (toplevelpath, + "tvshows", + "metadata.local", + 1, + toppathid)) # Link the path - query = "INSERT INTO tvshowlinkpath(idShow, idPath) values(?, ?)" + query = "INSERT INTO tvshowlinkpath(idShow, idPath) values (?, ?)" kodicursor.execute(query, (showid, pathid)) - # Create the reference in plex table plex_db.addReference(itemid, v.PLEX_TYPE_SHOW, @@ -682,16 +666,51 @@ class TVShows(Items): kodi_pathid=pathid, checksum=checksum, view_id=viewid) + if v.KODIVERSION >= 17: + # add new ratings Kodi 17 + rating_id = self.kodi_db.create_entry_rating() + self.kodi_db.add_ratings(rating_id, + showid, + v.KODI_TYPE_SHOW, + "default", + rating, + votecount) + # add new uniqueid Kodi 17 + self.kodi_db.add_uniqueid(self.kodi_db.create_entry_uniqueid(), + showid, + v.KODI_TYPE_SHOW, + tvdb, + "tvdb") + # Create the tvshow entry + query = ''' + INSERT INTO tvshow( + idShow, c00, c01, c04, c05, c08, c09, c12, c13, c14, + c15) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ''' + kodicursor.execute(query, (showid, title, plot, rating_id, + premieredate, genre, title, tvdb, + mpaa, studio, sorttitle)) + else: + # Create the tvshow entry + query = ''' + INSERT INTO tvshow( + idShow, c00, c01, c04, c05, c08, c09, c12, c13, c14, + c15) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + ''' + kodicursor.execute(query, (showid, title, plot, rating, + premieredate, genre, title, tvdb, + mpaa, studio, sorttitle)) # Update the path - query = ' '.join(( - - "UPDATE path", - "SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?, ", - "idParentPath = ?" - "WHERE idPath = ?" - )) + query = ''' + UPDATE path + SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?, + idParentPath = ? + WHERE idPath = ? + ''' kodicursor.execute(query, (path, None, None, 1, toppathid, pathid)) - + # Process cast people = API.getPeopleList() self.kodi_db.addPeople(showid, people, "tvshow") @@ -707,12 +726,6 @@ class TVShows(Items): tags.extend(collections) self.kodi_db.addTags(showid, tags, "tvshow") - # if force_episodes: - # # We needed to recreate the show entry. Re-add episodes now. - # log.info("Repairing episodes for showid: %s %s" % (showid, title)) - # all_episodes = embyserver.getEpisodesbyShow(itemid) - # self.added_episode(all_episodes['Items'], None) - @CatchExceptions(warnuser=True) def add_updateSeason(self, item, viewtag=None, viewid=None): API = PlexAPI.API(item) From 1a24146e486c3bedca995df80f267eabf0e74172 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 16:44:09 +0100 Subject: [PATCH 13/19] Move mark_played_at --- resources/lib/itemtypes.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index a2437546..19ee59c8 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -21,6 +21,7 @@ import variables as v log = logging.getLogger("PLEX."+__name__) +MARK_PLAYED_AT = 0.90 ############################################################################### @@ -159,12 +160,11 @@ class Items(object): # If the playback was stopped, check whether we need to increment the # playcount. PMS won't tell us the playcount via websockets if item['state'] in ('stopped', 'ended'): - markPlayed = 0.90 complete = float(item['viewOffset']) / float(item['duration']) log.info('Item %s stopped with completion rate %s percent.' 'Mark item played at %s percent.' - % (item['ratingKey'], str(complete), markPlayed), 1) - if complete >= markPlayed: + % (item['ratingKey'], str(complete), MARK_PLAYED_AT), 1) + if complete >= MARK_PLAYED_AT: log.info('Marking as completely watched in Kodi', 1) try: item['viewCount'] += 1 From 81f0fe115774cc9a2d13a8a0c3b7bb51f55c46d4 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 16:45:57 +0100 Subject: [PATCH 14/19] Sleep a bit before marking an item watched --- resources/lib/itemtypes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 19ee59c8..35809efe 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -6,6 +6,7 @@ import logging from urllib import urlencode from ntpath import dirname from datetime import datetime +from xbmc import sleep import artwork from utils import tryEncode, tryDecode, settings, window, kodiSQL, \ @@ -166,6 +167,7 @@ class Items(object): % (item['ratingKey'], str(complete), MARK_PLAYED_AT), 1) if complete >= MARK_PLAYED_AT: log.info('Marking as completely watched in Kodi', 1) + sleep(500) try: item['viewCount'] += 1 except TypeError: From b93925d678442e03ed6cd7f8be6cadd5677227f5 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 17:04:30 +0100 Subject: [PATCH 15/19] Rename function --- resources/lib/PlexFunctions.py | 4 ++-- resources/lib/playbackutils.py | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/resources/lib/PlexFunctions.py b/resources/lib/PlexFunctions.py index 904648d9..04e1a6ff 100644 --- a/resources/lib/PlexFunctions.py +++ b/resources/lib/PlexFunctions.py @@ -314,8 +314,8 @@ def GetPlexCollections(mediatype): return collections -def GetPlexPlaylist(itemid, librarySectionUUID, mediatype='movie', - trailers=False): +def init_plex_playqueue(itemid, librarySectionUUID, mediatype='movie', + trailers=False): """ Returns raw API metadata XML dump for a playlist with e.g. trailers. """ diff --git a/resources/lib/playbackutils.py b/resources/lib/playbackutils.py index 37bb9db9..d9cd867e 100644 --- a/resources/lib/playbackutils.py +++ b/resources/lib/playbackutils.py @@ -14,7 +14,7 @@ from utils import window, settings, tryEncode, tryDecode, language as lang import downloadutils from PlexAPI import API -from PlexFunctions import GetPlexPlaylist +from PlexFunctions import init_plex_playqueue from PKC_listitem import PKC_ListItem as ListItem, convert_PKC_to_listitem from playlist_func import add_item_to_kodi_playlist, \ get_playlist_details_from_xml, add_listitem_to_Kodi_playlist, \ @@ -134,11 +134,10 @@ class PlaybackUtils(): else: trailers = True # Post to the PMS. REUSE THE PLAYQUEUE! - xml = GetPlexPlaylist( - plex_id, - plex_lib_UUID, - mediatype=api.getType(), - trailers=trailers) + xml = init_plex_playqueue(plex_id, + plex_lib_UUID, + mediatype=api.getType(), + trailers=trailers) get_playlist_details_from_xml(playqueue, xml=xml) if (not homeScreen and not seektime and sizePlaylist < 2 and From 990e21ac99a0951670f6e80d2d49685a6402d4af Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 17:15:37 +0100 Subject: [PATCH 16/19] Plex Companion: Fix KeyError for Plex Web --- resources/lib/PlexCompanion.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/resources/lib/PlexCompanion.py b/resources/lib/PlexCompanion.py index 64814eef..6d996151 100644 --- a/resources/lib/PlexCompanion.py +++ b/resources/lib/PlexCompanion.py @@ -9,7 +9,8 @@ from xbmc import sleep from utils import settings, ThreadMethodsAdditionalSuspend, ThreadMethods from plexbmchelper import listener, plexgdm, subscribers, functions, \ httppersist, plexsettings -from PlexFunctions import ParseContainerKey +from PlexFunctions import ParseContainerKey, GetPlexMetadata +from PlexAPI import API import player from entrypoint import Plex_Node from variables import KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE @@ -95,8 +96,16 @@ class PlexCompanion(Thread): import traceback log.error("Traceback:\n%s" % traceback.format_exc()) return - playqueue = self.mgr.playqueue.get_playqueue_from_type( - KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[data['type']]) + try: + playqueue = self.mgr.playqueue.get_playqueue_from_type( + KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[data['type']]) + except KeyError: + # E.g. Plex web does not supply the media type + # Still need to figure out the type (video vs. music vs. pix) + xml = GetPlexMetadata(data['key']) + api = API(xml[0]) + playqueue = self.mgr.playqueue.get_playqueue_from_type( + KODI_PLAYLIST_TYPE_FROM_PLEX_TYPE[api.getType()]) self.mgr.playqueue.update_playqueue_from_PMS( playqueue, ID, From 4a2f938c0db16b3e35be0bba2b96e48a6e60a2c8 Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 17:52:08 +0100 Subject: [PATCH 17/19] Update addon.xml --- addon.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/addon.xml b/addon.xml index 70b5ffe9..8b0f65e5 100644 --- a/addon.xml +++ b/addon.xml @@ -29,6 +29,7 @@ Připojte Kodi ke svému Plex Media Serveru. Tento doplněk předpokládá, že spravujete veškerá svá videa pomocí Plexu (nikoliv pomocí Kodi). Můžete přijít o data uložená ve video a hudební databázi Kodi (tento doplněk je přímo mění). Používejte na vlastní nebezpečí! Verbindet Kodi mit deinem Plex Media Server. Dieses Addon geht davon aus, dass du all deine Videos mit Plex verwaltest (und keine direkt mit Kodi). Du wirst möglicherweise Daten verlieren, die bereits in der Kodi Video- und/oder Musik-Datenbank gespeichert sind (da dieses Addon beide Datenbanken direkt verändert). Verwende auf eigene Gefahr! Connect Kodi to your Plex Media Server. This plugin assumes that you manage all your videos with Plex (and none with Kodi). You might lose data already stored in the Kodi video and music databases (as this plugin directly changes them). Use at your own risk! + en de cs es all GPL v2.0 https://forums.plex.tv From 817d9a8743bd802ed4c0345982a0942826387ffc Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 17:54:55 +0100 Subject: [PATCH 18/19] Update addon.xml --- addon.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/addon.xml b/addon.xml index 8b0f65e5..95018099 100644 --- a/addon.xml +++ b/addon.xml @@ -22,14 +22,17 @@ Native Integration of Plex into Kodi + Native Integration of Plex into Kodi + Native Integration of Plex into Kodi Úplná integrace Plexu do Kodi Komplette Integration von Plex in Kodi Native Integration of Plex into Kodi Connect Kodi to your Plex Media Server. This plugin assumes that you manage all your videos with Plex (and none with Kodi). You might lose data already stored in the Kodi video and music databases (as this plugin directly changes them). Use at your own risk! + Connect Kodi to your Plex Media Server. This plugin assumes that you manage all your videos with Plex (and none with Kodi). You might lose data already stored in the Kodi video and music databases (as this plugin directly changes them). Use at your own risk! + Connect Kodi to your Plex Media Server. This plugin assumes that you manage all your videos with Plex (and none with Kodi). You might lose data already stored in the Kodi video and music databases (as this plugin directly changes them). Use at your own risk! Připojte Kodi ke svému Plex Media Serveru. Tento doplněk předpokládá, že spravujete veškerá svá videa pomocí Plexu (nikoliv pomocí Kodi). Můžete přijít o data uložená ve video a hudební databázi Kodi (tento doplněk je přímo mění). Používejte na vlastní nebezpečí! Verbindet Kodi mit deinem Plex Media Server. Dieses Addon geht davon aus, dass du all deine Videos mit Plex verwaltest (und keine direkt mit Kodi). Du wirst möglicherweise Daten verlieren, die bereits in der Kodi Video- und/oder Musik-Datenbank gespeichert sind (da dieses Addon beide Datenbanken direkt verändert). Verwende auf eigene Gefahr! Connect Kodi to your Plex Media Server. This plugin assumes that you manage all your videos with Plex (and none with Kodi). You might lose data already stored in the Kodi video and music databases (as this plugin directly changes them). Use at your own risk! - en de cs es all GPL v2.0 https://forums.plex.tv From 68e1e6facdae0d4ae01821f7c958bae1b902effc Mon Sep 17 00:00:00 2001 From: tomkat83 Date: Sat, 18 Feb 2017 17:58:21 +0100 Subject: [PATCH 19/19] Version bump --- addon.xml | 2 +- changelog.txt | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/addon.xml b/addon.xml index 95018099..ffa9119f 100644 --- a/addon.xml +++ b/addon.xml @@ -1,7 +1,7 @@ diff --git a/changelog.txt b/changelog.txt index c46b0806..7f14294e 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,10 @@ +version 1.5.14 (beta only) +- Krypton: Fix ratings for episodes and TV shows +- Plex Companion: Fix KeyError for Plex Web +- Fix UnicodeDecodeError for non-ASCII filenames +- Hopefully fix items not marked as entirely watched after having seen >90% +- Code optimization + version 1.5.13 (beta only) - New Spanish translation, thanks @bartolomesoriano - Fix some possible connection issues