From 5882a6ef3beea9cbf7db0db7425c55275b977f36 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 10 Mar 2018 15:44:08 +0100 Subject: [PATCH] Optimize code --- resources/lib/itemtypes.py | 69 +++++++++++-------------------- resources/lib/kodidb_functions.py | 39 +++++++---------- 2 files changed, 38 insertions(+), 70 deletions(-) diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 1aaf08cf..e3dc0a52 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -298,7 +298,7 @@ class Movies(Items): # add/retrieve pathid and fileid # if the path or file already exists, the calls return current value pathid = self.kodi_db.add_video_path(path) - fileid = self.kodi_db.addFile(filename, pathid) + fileid = self.kodi_db.add_file(filename, pathid) # UPDATE THE MOVIE ##### if update_item: @@ -785,10 +785,6 @@ class TVShows(Items): plex_db = self.plex_db artwork = self.artwork api = API(item) - - # 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 itemid = api.plex_id() if not itemid: @@ -802,18 +798,18 @@ class TVShows(Items): except TypeError: update_item = False # episodeid - kodicursor.execute("select coalesce(max(idEpisode),0) from episode") + kodicursor.execute('SELECT COALESCE(MAX(idEpisode),0) FROM episode') episodeid = kodicursor.fetchone()[0] + 1 else: # Verification the item is still in Kodi - query = "SELECT * FROM episode WHERE idEpisode = ?" + query = 'SELECT * FROM episode WHERE idEpisode = ?' kodicursor.execute(query, (episodeid,)) try: kodicursor.fetchone()[0] except TypeError: # item is not found, let's recreate it. update_item = False - LOG.info("episodeid: %s missing from Kodi, repairing entry.", + LOG.info('episodeid: %s missing from Kodi, repairing entry.', episodeid) # fileId information @@ -855,38 +851,33 @@ class TVShows(Items): seasonid = self.kodi_db.add_season(showid, season) # GET THE FILE AND PATH ##### - do_indirect = not state.DIRECT_PATHS - playurl = api.file_path(force_first_media=True) if state.DIRECT_PATHS: - # Direct paths is set the Kodi way + playurl = api.file_path(force_first_media=True) + playurl = api.validate_playurl(playurl, api.plex_type()) if playurl is None: - # Something went wrong, trying to use non-direct paths - do_indirect = True + return False + if "\\" in playurl: + # Local path + filename = playurl.rsplit("\\", 1)[1] else: - playurl = api.validate_playurl(playurl, api.plex_type()) - if playurl is None: - return False - if "\\" in playurl: - # Local path - filename = playurl.rsplit("\\", 1)[1] - else: - # Network share - filename = playurl.rsplit("/", 1)[1] - path = playurl.replace(filename, "") - parent_path_id = self.kodi_db.getParentPathId(path) - if do_indirect: + # Network share + filename = playurl.rsplit("/", 1)[1] + path = playurl.replace(filename, "") + parent_path_id = self.kodi_db.parent_path_id(path) + else: # Set plugin path - do NOT use "intermediate" paths for the show # as with direct paths! path = 'plugin://%s.tvshows/%s/' % (v.ADDON_ID, series_id) filename = ('%s?plex_id=%s&plex_type=%s&mode=play' % (path, itemid, v.PLEX_TYPE_EPISODE)) playurl = filename - parent_path_id = self.kodi_db.getParentPathId(path) + parent_path_id = self.kodi_db.parent_path_id(path) # add/retrieve pathid and fileid # if the path or file already exists, the calls return current value - pathid = self.kodi_db.add_video_path(path) - fileid = self.kodi_db.addFile(filename, pathid) + pathid = self.kodi_db.add_video_path(path, + id_parent_path=parent_path_id) + fileid = self.kodi_db.add_file(filename, pathid) # UPDATE THE EPISODE ##### if update_item: @@ -1024,24 +1015,12 @@ class TVShows(Items): parent_id=seasonid, checksum=checksum, view_id=viewid) - - # Update the path for Direct Paths only - if not do_indirect: - query = ''' - UPDATE path - SET strPath = ?, strContent = ?, strScraper = ?, noUpdate = ?, - idParentPath = ? - WHERE idPath = ? - ''' - kodicursor.execute(query, (path, None, None, 1, parent_path_id, - pathid)) # Update the file - query = ' '.join(( - - "UPDATE files", - "SET idPath = ?, strFilename = ?, dateAdded = ?", - "WHERE idFile = ?" - )) + query = ''' + UPDATE files + SET idPath = ?, strFilename = ?, dateAdded = ? + WHERE idFile = ? + ''' kodicursor.execute(query, (pathid, filename, dateadded, fileid)) # Process cast self.kodi_db.modify_people(episodeid, diff --git a/resources/lib/kodidb_functions.py b/resources/lib/kodidb_functions.py index f4d814dc..d6953065 100644 --- a/resources/lib/kodidb_functions.py +++ b/resources/lib/kodidb_functions.py @@ -100,10 +100,10 @@ class KodiDBMethods(object): 1, 0)) - def getParentPathId(self, path): + def parent_path_id(self, path): """ - Video DB: Adds all subdirectories to SQL path while setting a "trail" - of parentPathId + Video DB: Adds all subdirectories to path table while setting a "trail" + of parent path ids """ if "\\" in path: # Local path @@ -121,9 +121,9 @@ class KodiDBMethods(object): VALUES (?, ?, ?) ''' self.cursor.execute(query, (pathid, parentpath, datetime)) - parent_path_id = self.getParentPathId(parentpath) + parent_id = self.parent_path_id(parentpath) query = 'UPDATE path SET idParentPath = ? WHERE idPath = ?' - self.cursor.execute(query, (parent_path_id, pathid)) + self.cursor.execute(query, (parent_id, pathid)) return pathid def add_video_path(self, path, date_added=None, id_parent_path=None, @@ -185,31 +185,20 @@ class KodiDBMethods(object): pathid = None return pathid - def addFile(self, filename, pathid): - - query = ' '.join(( - - "SELECT idFile", - "FROM files", - "WHERE strFilename = ?", - "AND idPath = ?" - )) - self.cursor.execute(query, (filename, pathid,)) + def add_file(self, filename, path_id): + """ + Adds the filename [unicode] to the table files if not already added + and returns the idFile. + """ + query = 'SELECT idFile FROM files WHERE strFilename = ? AND idPath = ?' + self.cursor.execute(query, (filename, path_id)) try: fileid = self.cursor.fetchone()[0] except TypeError: - self.cursor.execute("select coalesce(max(idFile),0) from files") + self.cursor.execute('SELECT COALESCE(MAX(idFile), 0) FROM files') fileid = self.cursor.fetchone()[0] + 1 - query = ( - ''' - INSERT INTO files( - idFile, strFilename) - - VALUES (?, ?) - ''' - ) + query = 'INSERT INTO files(idFile, strFilename) VALUES (?, ?)' self.cursor.execute(query, (fileid, filename)) - return fileid def getFile(self, fileid):