Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor metadata ancestor gathering #362

Merged
merged 5 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions jellyfin_kodi/database/jellyfin_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,9 @@ def remove_wild_item(self, item_id):

def get_view_name(self, item_id):

try:
self.cursor.execute(QU.get_view_name, (item_id,))
self.cursor.execute(QU.get_view_name, (item_id,))

return self.cursor.fetchone()[0]
except Exception as error:
LOG.exception(error)
return
return self.cursor.fetchone()[0]

def get_view(self, *args):

Expand Down
4 changes: 2 additions & 2 deletions jellyfin_kodi/database/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@
add_reference_episode_obj = ["{Id}", "{EpisodeId}", "{FileId}", "{PathId}", "Episode", "episode", "{SeasonId}", "{Checksum}", None, "{JellyfinParentId}"]
add_reference_mvideo_obj = ["{Id}", "{MvideoId}", "{FileId}", "{PathId}", "MusicVideo", "musicvideo", None, "{Checksum}", "{LibraryId}", "{JellyfinParentId}"]
add_reference_artist_obj = ["{Id}", "{ArtistId}", None, None, "{ArtistType}", "artist", None, "{Checksum}", "{LibraryId}", "{JellyfinParentId}"]
add_reference_album_obj = ["{Id}", "{AlbumId}", None, None, "MusicAlbum", "album", None, "{Checksum}", None, "{JellyfinParentId}"]
add_reference_song_obj = ["{Id}", "{SongId}", None, "{PathId}", "Audio", "song", "{AlbumId}", "{Checksum}", None, "{JellyfinParentId}"]
add_reference_album_obj = ["{Id}", "{AlbumId}", None, None, "MusicAlbum", "album", None, "{Checksum}", "{LibraryId}", "{JellyfinParentId}"]
add_reference_song_obj = ["{Id}", "{SongId}", None, "{PathId}", "Audio", "song", "{AlbumId}", "{Checksum}", "{LibraryId}", "{JellyfinParentId}"]
add_view = """
INSERT OR REPLACE INTO view(view_id, view_name, media_type)
VALUES (?, ?, ?)
Expand Down
107 changes: 51 additions & 56 deletions jellyfin_kodi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from views import Views
from downloader import GetItemWorker
from helper import translate, api, stop, settings, window, dialog, event
from helper.utils import split_list, set_screensaver, get_screensaver, find_library
from helper.utils import split_list, set_screensaver, get_screensaver
from helper.exceptions import LibraryException
from jellyfin import Jellyfin
from helper import LazyLogger
Expand Down Expand Up @@ -605,39 +605,37 @@ def run(self):
except Queue.Empty:
break

# Verify that the updated item is in our local whitelist
library = find_library(self.server, item)
if library:
default_args = (self.server, jellyfindb, kodidb, self.direct_path, library)
try:
if item['Type'] == 'Movie':
Movies(*default_args).movie(item)
elif item['Type'] == 'BoxSet':
Movies(*default_args).boxset(item)
elif item['Type'] == 'Series':
TVShows(*default_args).tvshow(item)
elif item['Type'] == 'Season':
TVShows(*default_args).season(item)
elif item['Type'] == 'Episode':
TVShows(*default_args).episode(item)
elif item['Type'] == 'MusicVideo':
MusicVideos(*default_args).musicvideo(item)
elif item['Type'] == 'MusicAlbum':
Music(*default_args).album(item)
elif item['Type'] == 'MusicArtist':
Music(*default_args).artist(item)
elif item['Type'] == 'AlbumArtist':
Music(*default_args).albumartist(item)
elif item['Type'] == 'Audio':
Music(*default_args).song(item)

if self.notify:
self.notify_output.put((item['Type'], api.API(item).get_naming()))
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
default_args = (self.server, jellyfindb, kodidb, self.direct_path)
try:
LOG.debug('{} - {}'.format(item['Type'], item['Name']))
if item['Type'] == 'Movie':
Movies(*default_args).movie(item)
elif item['Type'] == 'BoxSet':
Movies(*default_args).boxset(item)
elif item['Type'] == 'Series':
TVShows(*default_args).tvshow(item)
elif item['Type'] == 'Season':
TVShows(*default_args).season(item)
elif item['Type'] == 'Episode':
TVShows(*default_args).episode(item)
elif item['Type'] == 'MusicVideo':
MusicVideos(*default_args).musicvideo(item)
elif item['Type'] == 'MusicAlbum':
Music(*default_args).album(item)
elif item['Type'] == 'MusicArtist':
Music(*default_args).artist(item)
elif item['Type'] == 'AlbumArtist':
Music(*default_args).albumartist(item)
elif item['Type'] == 'Audio':
Music(*default_args).song(item)

if self.notify:
self.notify_output.put((item['Type'], api.API(item).get_naming()))
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)

self.queue.task_done()

Expand Down Expand Up @@ -672,28 +670,25 @@ def run(self):
except Queue.Empty:
break

# Verify that the updated item is in our local whitelist
library = find_library(self.server, item)
if library:
default_args = (self.server, jellyfindb, kodidb, self.direct_path, library)
try:
if item['Type'] == 'Movie':
Movies(*default_args).userdata(item)
elif item['Type'] in ['Series', 'Season', 'Episode']:
TVShows(*default_args).userdata(item)
elif item['Type'] == 'MusicAlbum':
Music(*default_args).album(item)
elif item['Type'] == 'MusicArtist':
Music(*default_args).artist(item)
elif item['Type'] == 'AlbumArtist':
Music(*default_args).albumartist(item)
elif item['Type'] == 'Audio':
Music(*default_args).song(item)
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
default_args = (self.server, jellyfindb, kodidb, self.direct_path)
try:
if item['Type'] == 'Movie':
Movies(*default_args).userdata(item)
elif item['Type'] in ['Series', 'Season', 'Episode']:
TVShows(*default_args).userdata(item)
elif item['Type'] == 'MusicAlbum':
Music(*default_args).album(item)
elif item['Type'] == 'MusicArtist':
Music(*default_args).artist(item)
elif item['Type'] == 'AlbumArtist':
Music(*default_args).albumartist(item)
elif item['Type'] == 'Audio':
Music(*default_args).userdata(item)
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)

self.queue.task_done()

Expand Down
13 changes: 11 additions & 2 deletions jellyfin_kodi/objects/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from database import jellyfin_db, queries as QUEM
from helper import api, stop, validate, validate_bluray_dir, validate_dvd_dir, jellyfin_item, values, Local
from helper import LazyLogger
from helper.utils import find_library
from helper.exceptions import PathValidationException

from .obj import Objects
Expand Down Expand Up @@ -54,19 +55,27 @@ def movie(self, item, e_item):
obj['MovieId'] = e_item[0]
obj['FileId'] = e_item[1]
obj['PathId'] = e_item[2]
obj['LibraryId'] = e_item[6]
obj['LibraryName'] = self.jellyfin_db.get_view_name(obj['LibraryId'])
except TypeError:
update = False
LOG.debug("MovieId %s not found", obj['Id'])

library = self.library or find_library(self.server, item)
if not library:
# This item doesn't belong to a whitelisted library
return

obj['MovieId'] = self.create_entry()
obj['LibraryId'] = library['Id']
obj['LibraryName'] = library['Name']
else:
if self.get(*values(obj, QU.get_movie_obj)) is None:

update = False
LOG.info("MovieId %s missing from kodi. repairing the entry.", obj['MovieId'])

obj['Path'] = API.get_file_path(obj['Path'])
obj['LibraryId'] = self.library['Id']
obj['LibraryName'] = self.library['Name']
obj['Genres'] = obj['Genres'] or []
obj['Studios'] = [API.validate_studio(studio) for studio in (obj['Studios'] or [])]
obj['People'] = obj['People'] or []
Expand Down
33 changes: 31 additions & 2 deletions jellyfin_kodi/objects/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from database import jellyfin_db, queries as QUEM
from helper import api, stop, validate, jellyfin_item, values, Local
from helper import LazyLogger
from helper.utils import find_library
from helper.exceptions import PathValidationException

from .obj import Objects
Expand Down Expand Up @@ -50,18 +51,26 @@ def artist(self, item, e_item):

try:
obj['ArtistId'] = e_item[0]
obj['LibraryId'] = e_item[6]
obj['LibraryName'] = self.jellyfin_db.get_view_name(obj['LibraryId'])
except TypeError:
update = False

library = self.library or find_library(self.server, item)
if not library:
# This item doesn't belong to a whitelisted library
return

obj['ArtistId'] = None
obj['LibraryId'] = library['Id']
obj['LibraryName'] = library['Name']
LOG.debug("ArtistId %s not found", obj['Id'])
else:
if self.validate_artist(*values(obj, QU.get_artist_by_id_obj)) is None:

update = False
LOG.info("ArtistId %s missing from kodi. repairing the entry.", obj['ArtistId'])

obj['LibraryId'] = self.library['Id']
obj['LibraryName'] = self.library['Name']
obj['LastScraped'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
obj['ArtistType'] = "MusicArtist"
obj['Genre'] = " / ".join(obj['Genres'] or [])
Expand Down Expand Up @@ -116,9 +125,19 @@ def album(self, item, e_item):

try:
obj['AlbumId'] = e_item[0]
obj['LibraryId'] = e_item[6]
obj['LibraryName'] = self.jellyfin_db.get_view_name(obj['LibraryId'])
except TypeError:
update = False

library = self.library or find_library(self.server, item)
if not library:
# This item doesn't belong to a whitelisted library
return

obj['AlbumId'] = None
obj['LibraryId'] = library['Id']
obj['LibraryName'] = library['Name']
LOG.debug("AlbumId %s not found", obj['Id'])
else:
if self.validate_album(*values(obj, QU.get_album_by_id_obj)) is None:
Expand Down Expand Up @@ -224,9 +243,19 @@ def song(self, item, e_item):
obj['SongId'] = e_item[0]
obj['PathId'] = e_item[2]
obj['AlbumId'] = e_item[3]
obj['LibraryId'] = e_item[6]
obj['LibraryName'] = self.jellyfin_db.get_view_name(obj['LibraryId'])
except TypeError:
update = False

library = self.library or find_library(self.server, item)
if not library:
# This item doesn't belong to a whitelisted library
return

obj['SongId'] = self.create_entry_song()
obj['LibraryId'] = library['Id']
obj['LibraryName'] = library['Name']
LOG.debug("SongId %s not found", obj['Id'])
else:
if self.validate_song(*values(obj, QU.get_song_by_id_obj)) is None:
Expand Down
11 changes: 11 additions & 0 deletions jellyfin_kodi/objects/musicvideos.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from database import jellyfin_db, queries as QUEM
from helper import api, stop, validate, jellyfin_item, values, Local
from helper import LazyLogger
from helper.utils import find_library
from helper.exceptions import PathValidationException

from .obj import Objects
Expand Down Expand Up @@ -59,10 +60,20 @@ def musicvideo(self, item, e_item):
obj['MvideoId'] = e_item[0]
obj['FileId'] = e_item[1]
obj['PathId'] = e_item[2]
obj['LibraryId'] = e_item[6]
obj['LibraryName'] = self.jellyfin_db.get_view_name(obj['LibraryId'])
except TypeError:
update = False

library = self.library or find_library(self.server, item)
if not library:
# This item doesn't belong to a whitelisted library
return

LOG.debug("MvideoId for %s not found", obj['Id'])
obj['MvideoId'] = self.create_entry()
obj['LibraryId'] = library['Id']
obj['LibraryName'] = library['Name']
else:
if self.get(*values(obj, QU.get_musicvideo_obj)) is None:

Expand Down
19 changes: 17 additions & 2 deletions jellyfin_kodi/objects/tvshows.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from database import jellyfin_db, queries as QUEM
from helper import api, stop, validate, jellyfin_item, values, Local
from helper import LazyLogger
from helper.utils import find_library
from helper.exceptions import PathValidationException

from .obj import Objects
Expand Down Expand Up @@ -61,19 +62,27 @@ def tvshow(self, item, e_item):
try:
obj['ShowId'] = e_item[0]
obj['PathId'] = e_item[2]
obj['LibraryId'] = e_item[6]
obj['LibraryName'] = self.jellyfin_db.get_view_name(obj['LibraryId'])
except TypeError:
update = False
LOG.debug("ShowId %s not found", obj['Id'])

library = self.library or find_library(self.server, item)
if not library:
# This item doesn't belong to a whitelisted library
return

obj['ShowId'] = self.create_entry()
obj['LibraryId'] = library['Id']
obj['LibraryName'] = library['Name']
else:
if self.get(*values(obj, QU.get_tvshow_obj)) is None:

update = False
LOG.info("ShowId %s missing from kodi. repairing the entry.", obj['ShowId'])

obj['Path'] = API.get_file_path(obj['Path'])
obj['LibraryId'] = self.library['Id']
obj['LibraryName'] = self.library['Name']
obj['Genres'] = obj['Genres'] or []
obj['People'] = obj['People'] or []
obj['Mpaa'] = API.get_mpaa(obj['Mpaa'])
Expand Down Expand Up @@ -267,6 +276,12 @@ def episode(self, item, e_item):
except TypeError:
update = False
LOG.debug("EpisodeId %s not found", obj['Id'])

library = self.library or find_library(self.server, item)
if not library:
# This item doesn't belong to a whitelisted library
return

obj['EpisodeId'] = self.create_entry_episode()
else:
if self.get_episode(*values(obj, QU.get_episode_obj)) is None:
Expand Down