Optimize sync of video filename

This commit is contained in:
croneter 2018-11-09 09:26:54 +01:00
parent 76728d7319
commit ab718bae2a
3 changed files with 29 additions and 19 deletions

View file

@ -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,

View file

@ -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,

View file

@ -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):