diff --git a/jellyfin_kodi/database/jellyfin_db.py b/jellyfin_kodi/database/jellyfin_db.py index a518b830f..0cbe9dd6b 100644 --- a/jellyfin_kodi/database/jellyfin_db.py +++ b/jellyfin_kodi/database/jellyfin_db.py @@ -71,6 +71,15 @@ def get_item_by_kodi_id(self, *args): except TypeError: return + def get_episode_kodi_parent_path_id(self, *args): + + try: + self.cursor.execute(QU.get_episode_kodi_parent_path_id, args) + + return self.cursor.fetchone()[0] + except TypeError: + return + def get_full_item_by_kodi_id(self, *args): try: diff --git a/jellyfin_kodi/database/queries.py b/jellyfin_kodi/database/queries.py index e1c3eb69d..52d688460 100644 --- a/jellyfin_kodi/database/queries.py +++ b/jellyfin_kodi/database/queries.py @@ -166,3 +166,17 @@ delete_version = """ DELETE FROM version """ + +get_episode_kodi_parent_path_id = """ +SELECT sh.kodi_pathid +FROM jellyfin e +JOIN jellyfin s +ON e.parent_id = s.kodi_id +JOIN jellyfin sh +ON s.parent_id = sh.kodi_id +WHERE e.media_type = ? +AND s.media_type = ? +AND sh.media_type = ? +AND e.jellyfin_id = ?; +""" +get_episode_kodi_parent_path_id_obj = ["episode", "season", "tvshow", "{Id}"] diff --git a/jellyfin_kodi/objects/kodi/kodi.py b/jellyfin_kodi/objects/kodi/kodi.py index 1782425a7..9f7c9dad1 100644 --- a/jellyfin_kodi/objects/kodi/kodi.py +++ b/jellyfin_kodi/objects/kodi/kodi.py @@ -77,6 +77,9 @@ def get_path(self, *args): except TypeError: return + def update_path_parent_id(self, path_id, parent_path_id): + self.cursor.execute(QU.update_path_parent_id, (parent_path_id, path_id)) + def update_path(self, *args): self.cursor.execute(QU.update_path, args) diff --git a/jellyfin_kodi/objects/kodi/queries.py b/jellyfin_kodi/objects/kodi/queries.py index e6b0783c2..bab23e315 100644 --- a/jellyfin_kodi/objects/kodi/queries.py +++ b/jellyfin_kodi/objects/kodi/queries.py @@ -342,17 +342,22 @@ """ add_episode = """ INSERT INTO episode(idEpisode, idFile, c00, c01, c03, c04, c05, c09, c10, c12, c13, c14, - idShow, c15, c16, idSeason) -VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + idShow, c15, c16, idSeason, c18, c19, c20) +VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """ add_episode_obj = ["{EpisodeId}", "{FileId}", "{Title}", "{Plot}", "{RatingId}", "{Writers}", "{Premiere}", "{Runtime}", "{Directors}", "{Season}", "{Index}", "{Title}", "{ShowId}", "{AirsBeforeSeason}", - "{AirsBeforeEpisode}", "{SeasonId}"] + "{AirsBeforeEpisode}", "{SeasonId}", "{FullFilePath}", "{PathId}", "{Unique}"] add_art = """ INSERT INTO art(media_id, media_type, type, url) VALUES (?, ?, ?, ?) """ +update_path_parent_id = """ +UPDATE path +SET idParentPath = ? +where idPath = ? +""" update_path = """ UPDATE path @@ -361,9 +366,11 @@ """ update_path_movie_obj = ["{Path}", "movies", "metadata.local", 1, "{PathId}"] update_path_toptvshow_obj = ["{TopLevel}", "tvshows", "metadata.local", 1, "{TopPathId}"] +update_path_toptvshow_addon_obj = ["{TopLevel}", None, None, 1, "{TopPathId}"] update_path_tvshow_obj = ["{Path}", None, None, 1, "{PathId}"] update_path_episode_obj = ["{Path}", None, None, 1, "{PathId}"] -update_path_mvideo_obj = ["{Path}", "musicvideos", None, 1, "{PathId}"] +update_path_mvideo_obj = ["{Path}", "musicvideos", "metadata.local", 1, "{PathId}"] + update_file = """ UPDATE files SET idPath = ?, strFilename = ?, dateAdded = ? @@ -483,12 +490,13 @@ update_episode = """ UPDATE episode SET c00 = ?, c01 = ?, c03 = ?, c04 = ?, c05 = ?, c09 = ?, c10 = ?, - c12 = ?, c13 = ?, c14 = ?, c15 = ?, c16 = ?, idSeason = ?, idShow = ? + c12 = ?, c13 = ?, c14 = ?, c15 = ?, c16 = ?, idSeason = ?, idShow = ?, + c18 = ?, c19 = ?, c20 = ? WHERE idEpisode = ? """ update_episode_obj = ["{Title}", "{Plot}", "{RatingId}", "{Writers}", "{Premiere}", "{Runtime}", "{Directors}", "{Season}", "{Index}", "{Title}", "{AirsBeforeSeason}", "{AirsBeforeEpisode}", "{SeasonId}", - "{ShowId}", "{EpisodeId}"] + "{ShowId}", "{FullFilePath}", "{PathId}", "{Unique}", "{EpisodeId}"] delete_path = """ diff --git a/jellyfin_kodi/objects/tvshows.py b/jellyfin_kodi/objects/tvshows.py index 3b169e1b0..31b89ba47 100644 --- a/jellyfin_kodi/objects/tvshows.py +++ b/jellyfin_kodi/objects/tvshows.py @@ -169,7 +169,19 @@ def tvshow_add(self, obj): self.add_unique_id(*values(obj, QU.add_unique_id_tvshow_obj)) obj['TopPathId'] = self.add_path(obj['TopLevel']) - self.update_path(*values(obj, QU.update_path_toptvshow_obj)) + + if self.direct_path: + # Normal way, we use the actual top path + self.update_path(*values(obj, QU.update_path_toptvshow_obj)) + else: + # Hack to allow cast information in add-on mode + # We create a path on top of all others that holds mediaType and scrapper + self.update_path(*values(obj, QU.update_path_toptvshow_addon_obj)) + temp_obj = dict() + temp_obj['TopLevel'] = 'plugin://plugin.video.jellyfin/' + temp_obj['TopPathId'] = self.add_path(temp_obj['TopLevel']) + self.update_path(*values(temp_obj, QU.update_path_toptvshow_obj)) + self.update_path_parent_id(obj['TopPathId'], temp_obj['TopPathId']) obj['PathId'] = self.add_path(*values(obj, QU.get_path_obj)) @@ -177,6 +189,8 @@ def tvshow_add(self, obj): self.jellyfin_db.add_reference(*values(obj, QUEM.add_reference_tvshow_obj)) LOG.debug("ADD tvshow [%s/%s/%s] %s: %s", obj['TopPathId'], obj['PathId'], obj['ShowId'], obj['Title'], obj['Id']) + self.update_path_parent_id(obj['PathId'], obj['TopPathId']) + def tvshow_update(self, obj): ''' Update object to kodi. @@ -187,10 +201,14 @@ def tvshow_update(self, obj): obj['Unique'] = self.get_unique_id(*values(obj, QU.get_unique_id_tvshow_obj)) self.update_unique_id(*values(obj, QU.update_unique_id_tvshow_obj)) + obj['TopPathId'] = self.get_path(obj['TopLevel']) + self.update(*values(obj, QU.update_tvshow_obj)) self.jellyfin_db.update_reference(*values(obj, QUEM.update_reference_obj)) LOG.debug("UPDATE tvshow [%s/%s] %s: %s", obj['PathId'], obj['ShowId'], obj['Title'], obj['Id']) + self.update_path_parent_id(obj['PathId'], obj['TopPathId']) + def get_path_filename(self, obj): ''' Get the path and build it into protocol://path @@ -200,6 +218,9 @@ def get_path_filename(self, obj): if '\\' in obj['Path']: obj['Path'] = "%s\\" % obj['Path'] obj['TopLevel'] = "%s\\" % dirname(dirname(obj['Path'])) + elif 'smb://' in obj['Path'] or 'nfs://' in obj['Path']: + obj['Path'] = "%s/" % obj['Path'] + obj['TopLevel'] = "%s/" % dirname(dirname(obj['Path'])) else: obj['Path'] = "%s/" % obj['Path'] obj['TopLevel'] = "plugin://plugin.video.jellyfin/" @@ -377,6 +398,12 @@ def episode_add(self, obj): return self.episode_add(obj) self.jellyfin_db.add_reference(*values(obj, QUEM.add_reference_episode_obj)) + + parentPathId = self.jellyfin_db.get_episode_kodi_parent_path_id(*values(obj, QUEM.get_episode_kodi_parent_path_id_obj)) + if obj['PathId'] != parentPathId: + LOG.debug("Setting episode pathParentId, episode %s, title %s, pathId %s, pathParentId %s", obj['Id'], obj['Title'], obj['PathId'], parentPathId) + self.update_path_parent_id(obj['PathId'], parentPathId) + LOG.debug("ADD episode [%s/%s] %s: %s", obj['PathId'], obj['FileId'], obj['Id'], obj['Title']) def episode_update(self, obj): @@ -423,8 +450,13 @@ def get_episode_path_filename(self, obj): obj['Filename'] = 'index.bdmv' LOG.debug("Bluray directory %s", obj['Path']) + obj['FullFilePath'] = obj['Path'] + obj['Filename'] + else: - obj['Path'] = "plugin://plugin.video.jellyfin/%s/" % obj['SeriesId'] + # We need LibraryId + library = self.library or find_library(self.server, obj) + obj['LibraryId'] = library['Id'] + obj['Path'] = "plugin://plugin.video.jellyfin/%s/%s/" % (obj['LibraryId'], obj['SeriesId']) params = { 'filename': py2_encode(obj['Filename'], 'utf-8'), 'id': obj['Id'], @@ -432,6 +464,7 @@ def get_episode_path_filename(self, obj): 'mode': "play" } obj['Filename'] = "%s?%s" % (obj['Path'], urlencode(params)) + obj['FullFilePath'] = obj['Filename'] def get_show_id(self, obj): obj['ShowId'] = self.jellyfin_db.get_item_by_id(*values(obj, QUEM.get_item_series_obj))