From ab718bae2a981d2166be1aece1023960f3d4ec96 Mon Sep 17 00:00:00 2001 From: croneter Date: Fri, 9 Nov 2018 09:26:54 +0100 Subject: [PATCH] Optimize sync of video filename --- resources/lib/itemtypes/movies.py | 10 ++++++---- resources/lib/itemtypes/tvshows.py | 15 ++++++++------- resources/lib/kodi_db/video.py | 23 +++++++++++++++-------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/resources/lib/itemtypes/movies.py b/resources/lib/itemtypes/movies.py index 57926f9e..d9d41de6 100644 --- a/resources/lib/itemtypes/movies.py +++ b/resources/lib/itemtypes/movies.py @@ -80,12 +80,11 @@ class Movie(ItemBase): playurl = filename kodi_pathid = self.kodidb.get_path(path) - file_id = self.kodidb.add_file(filename, - kodi_pathid, - api.date_created()) - if update_item: LOG.info('UPDATE movie plex_id: %s - %s', plex_id, api.title()) + file_id = self.kodidb.modify_file(filename, + kodi_pathid, + api.date_created()) if file_id != old_kodi_fileid: self.kodidb.remove_file(old_kodi_fileid) rating_id = self.kodidb.get_ratingid(kodi_id, @@ -116,6 +115,9 @@ class Movie(ItemBase): v.KODI_TYPE_MOVIE) else: LOG.info("ADD movie plex_id: %s - %s", plex_id, title) + file_id = self.kodidb.add_file(filename, + kodi_pathid, + api.date_created()) rating_id = self.kodidb.get_ratingid(kodi_id, v.KODI_TYPE_MOVIE) self.kodidb.add_ratings(rating_id, diff --git a/resources/lib/itemtypes/tvshows.py b/resources/lib/itemtypes/tvshows.py index 596981e6..270c76c2 100644 --- a/resources/lib/itemtypes/tvshows.py +++ b/resources/lib/itemtypes/tvshows.py @@ -407,15 +407,13 @@ class Episode(ItemBase, TvShowMixin): # Root path tvshows/ already saved in Kodi DB kodi_pathid = self.kodidb.add_video_path(path) - # add/retrieve kodi_pathid and fileid - # if the path or file already exists, the calls return current value - kodi_fileid = self.kodidb.add_file(filename, - kodi_pathid, - api.date_created()) - # UPDATE THE EPISODE ##### if update_item: LOG.info("UPDATE episode plex_id: %s - %s", plex_id, api.title()) + kodi_fileid = self.kodidb.modify_file(filename, + kodi_pathid, + api.date_created()) + if kodi_fileid != old_kodi_fileid: self.kodidb.remove_file(old_kodi_fileid) ratingid = self.kodidb.get_ratingid(kodi_id, @@ -462,7 +460,10 @@ class Episode(ItemBase, TvShowMixin): # OR ADD THE EPISODE ##### else: LOG.info("ADD episode plex_id: %s - %s", plex_id, api.title()) - # Create the episode entry + kodi_fileid = self.kodidb.add_file(filename, + kodi_pathid, + api.date_created()) + rating_id = self.kodidb.get_ratingid(kodi_id, v.KODI_TYPE_EPISODE) self.kodidb.add_ratings(rating_id, diff --git a/resources/lib/kodi_db/video.py b/resources/lib/kodi_db/video.py index b7d0fe45..5e67ee7b 100644 --- a/resources/lib/kodi_db/video.py +++ b/resources/lib/kodi_db/video.py @@ -136,19 +136,26 @@ class KodiVideoDB(common.KodiDBBase): Adds the filename [unicode] to the table files if not already added and returns the idFile. """ + self.cursor.execute('SELECT COALESCE(MAX(idFile), 0) FROM files') + file_id = self.cursor.fetchone()[0] + 1 + self.cursor.execute(''' + INSERT INTO files( + idFile, + idPath, + strFilename, + dateAdded) + VALUES (?, ?, ?, ?) + ''', + (file_id, path_id, filename, date_added)) + return file_id + + def modify_file(self, filename, path_id, date_added): self.cursor.execute('SELECT idFile FROM files WHERE idPath = ? AND strFilename = ?', (path_id, filename)) try: file_id = self.cursor.fetchone()[0] except TypeError: - self.cursor.execute('SELECT COALESCE(MAX(idFile), 0) FROM files') - file_id = self.cursor.fetchone()[0] + 1 - self.cursor.execute(''' - INSERT INTO files( - idFile, idPath, strFilename, dateAdded) - VALUES (?, ?, ?, ?) - ''', - (file_id, path_id, filename, date_added)) + file_id = self.add_file(filename, path_id, date_added) return file_id def obsolete_file_ids(self):