Optimize code

This commit is contained in:
croneter 2018-03-10 15:44:08 +01:00
parent 5a2d3f4238
commit 5882a6ef3b
2 changed files with 38 additions and 70 deletions

View file

@ -298,7 +298,7 @@ class Movies(Items):
# add/retrieve pathid and fileid # add/retrieve pathid and fileid
# if the path or file already exists, the calls return current value # if the path or file already exists, the calls return current value
pathid = self.kodi_db.add_video_path(path) 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 ##### # UPDATE THE MOVIE #####
if update_item: if update_item:
@ -785,10 +785,6 @@ class TVShows(Items):
plex_db = self.plex_db plex_db = self.plex_db
artwork = self.artwork artwork = self.artwork
api = API(item) 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 update_item = True
itemid = api.plex_id() itemid = api.plex_id()
if not itemid: if not itemid:
@ -802,18 +798,18 @@ class TVShows(Items):
except TypeError: except TypeError:
update_item = False update_item = False
# episodeid # episodeid
kodicursor.execute("select coalesce(max(idEpisode),0) from episode") kodicursor.execute('SELECT COALESCE(MAX(idEpisode),0) FROM episode')
episodeid = kodicursor.fetchone()[0] + 1 episodeid = kodicursor.fetchone()[0] + 1
else: else:
# Verification the item is still in Kodi # Verification the item is still in Kodi
query = "SELECT * FROM episode WHERE idEpisode = ?" query = 'SELECT * FROM episode WHERE idEpisode = ?'
kodicursor.execute(query, (episodeid,)) kodicursor.execute(query, (episodeid,))
try: try:
kodicursor.fetchone()[0] kodicursor.fetchone()[0]
except TypeError: except TypeError:
# item is not found, let's recreate it. # item is not found, let's recreate it.
update_item = False update_item = False
LOG.info("episodeid: %s missing from Kodi, repairing entry.", LOG.info('episodeid: %s missing from Kodi, repairing entry.',
episodeid) episodeid)
# fileId information # fileId information
@ -855,38 +851,33 @@ class TVShows(Items):
seasonid = self.kodi_db.add_season(showid, season) seasonid = self.kodi_db.add_season(showid, season)
# GET THE FILE AND PATH ##### # GET THE FILE AND PATH #####
do_indirect = not state.DIRECT_PATHS
playurl = api.file_path(force_first_media=True)
if state.DIRECT_PATHS: 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: if playurl is None:
# Something went wrong, trying to use non-direct paths return False
do_indirect = True if "\\" in playurl:
# Local path
filename = playurl.rsplit("\\", 1)[1]
else: else:
playurl = api.validate_playurl(playurl, api.plex_type()) # Network share
if playurl is None: filename = playurl.rsplit("/", 1)[1]
return False path = playurl.replace(filename, "")
if "\\" in playurl: parent_path_id = self.kodi_db.parent_path_id(path)
# Local path else:
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:
# Set plugin path - do NOT use "intermediate" paths for the show # Set plugin path - do NOT use "intermediate" paths for the show
# as with direct paths! # as with direct paths!
path = 'plugin://%s.tvshows/%s/' % (v.ADDON_ID, series_id) path = 'plugin://%s.tvshows/%s/' % (v.ADDON_ID, series_id)
filename = ('%s?plex_id=%s&plex_type=%s&mode=play' filename = ('%s?plex_id=%s&plex_type=%s&mode=play'
% (path, itemid, v.PLEX_TYPE_EPISODE)) % (path, itemid, v.PLEX_TYPE_EPISODE))
playurl = filename 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 # add/retrieve pathid and fileid
# if the path or file already exists, the calls return current value # if the path or file already exists, the calls return current value
pathid = self.kodi_db.add_video_path(path) pathid = self.kodi_db.add_video_path(path,
fileid = self.kodi_db.addFile(filename, pathid) id_parent_path=parent_path_id)
fileid = self.kodi_db.add_file(filename, pathid)
# UPDATE THE EPISODE ##### # UPDATE THE EPISODE #####
if update_item: if update_item:
@ -1024,24 +1015,12 @@ class TVShows(Items):
parent_id=seasonid, parent_id=seasonid,
checksum=checksum, checksum=checksum,
view_id=viewid) 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 # Update the file
query = ' '.join(( query = '''
UPDATE files
"UPDATE files", SET idPath = ?, strFilename = ?, dateAdded = ?
"SET idPath = ?, strFilename = ?, dateAdded = ?", WHERE idFile = ?
"WHERE idFile = ?" '''
))
kodicursor.execute(query, (pathid, filename, dateadded, fileid)) kodicursor.execute(query, (pathid, filename, dateadded, fileid))
# Process cast # Process cast
self.kodi_db.modify_people(episodeid, self.kodi_db.modify_people(episodeid,

View file

@ -100,10 +100,10 @@ class KodiDBMethods(object):
1, 1,
0)) 0))
def getParentPathId(self, path): def parent_path_id(self, path):
""" """
Video DB: Adds all subdirectories to SQL path while setting a "trail" Video DB: Adds all subdirectories to path table while setting a "trail"
of parentPathId of parent path ids
""" """
if "\\" in path: if "\\" in path:
# Local path # Local path
@ -121,9 +121,9 @@ class KodiDBMethods(object):
VALUES (?, ?, ?) VALUES (?, ?, ?)
''' '''
self.cursor.execute(query, (pathid, parentpath, datetime)) 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 = ?' query = 'UPDATE path SET idParentPath = ? WHERE idPath = ?'
self.cursor.execute(query, (parent_path_id, pathid)) self.cursor.execute(query, (parent_id, pathid))
return pathid return pathid
def add_video_path(self, path, date_added=None, id_parent_path=None, def add_video_path(self, path, date_added=None, id_parent_path=None,
@ -185,31 +185,20 @@ class KodiDBMethods(object):
pathid = None pathid = None
return pathid return pathid
def addFile(self, filename, pathid): def add_file(self, filename, path_id):
"""
query = ' '.join(( Adds the filename [unicode] to the table files if not already added
and returns the idFile.
"SELECT idFile", """
"FROM files", query = 'SELECT idFile FROM files WHERE strFilename = ? AND idPath = ?'
"WHERE strFilename = ?", self.cursor.execute(query, (filename, path_id))
"AND idPath = ?"
))
self.cursor.execute(query, (filename, pathid,))
try: try:
fileid = self.cursor.fetchone()[0] fileid = self.cursor.fetchone()[0]
except TypeError: 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 fileid = self.cursor.fetchone()[0] + 1
query = ( query = 'INSERT INTO files(idFile, strFilename) VALUES (?, ?)'
'''
INSERT INTO files(
idFile, strFilename)
VALUES (?, ?)
'''
)
self.cursor.execute(query, (fileid, filename)) self.cursor.execute(query, (fileid, filename))
return fileid return fileid
def getFile(self, fileid): def getFile(self, fileid):