From ab5ab966e41cd6eeb2702a5a8811cd9a77cd89d2 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 27 Apr 2019 10:59:51 +0200 Subject: [PATCH] Use section for sync process --- resources/lib/itemtypes/movies.py | 9 ++++---- resources/lib/itemtypes/music.py | 29 ++++++++++--------------- resources/lib/itemtypes/tvshows.py | 28 ++++++++++-------------- resources/lib/library_sync/full_sync.py | 6 ++--- resources/lib/library_sync/websocket.py | 11 ++++++++-- 5 files changed, 37 insertions(+), 46 deletions(-) diff --git a/resources/lib/itemtypes/movies.py b/resources/lib/itemtypes/movies.py index 6a388d26..2527a753 100644 --- a/resources/lib/itemtypes/movies.py +++ b/resources/lib/itemtypes/movies.py @@ -14,8 +14,7 @@ class Movie(ItemBase): """ Used for plex library-type movies """ - def add_update(self, xml, section_name=None, section_id=None, - children=None): + def add_update(self, xml, section, children=None): """ Process single movie """ @@ -174,7 +173,7 @@ class Movie(ItemBase): self.kodidb.modify_streams(file_id, api.mediastreams(), runtime) self.kodidb.modify_studios(kodi_id, v.KODI_TYPE_MOVIE, studios) - tags = [section_name] + tags = [section.name] if collections: for plex_set_id, set_name in collections: set_api = None @@ -189,7 +188,7 @@ class Movie(ItemBase): # e.g. when added via websocket LOG.debug('Costly looking up Plex collection %s: %s', plex_set_id, set_name) - for index, coll_plex_id in api.collections_match(section_id): + for index, coll_plex_id in api.collections_match(section.id): # Get Plex artwork for collections - a pain if index == plex_set_id: set_xml = PF.GetPlexMetadata(coll_plex_id) @@ -217,7 +216,7 @@ class Movie(ItemBase): dateplayed) self.plexdb.add_movie(plex_id=plex_id, checksum=api.checksum(), - section_id=section_id, + section_id=section.id, kodi_id=kodi_id, kodi_fileid=file_id, kodi_pathid=kodi_pathid, diff --git a/resources/lib/itemtypes/music.py b/resources/lib/itemtypes/music.py index f54fac45..81247a17 100644 --- a/resources/lib/itemtypes/music.py +++ b/resources/lib/itemtypes/music.py @@ -154,8 +154,7 @@ class Artist(MusicMixin, ItemBase): """ For Plex library-type artists """ - def add_update(self, xml, section_name=None, section_id=None, - children=None): + def add_update(self, xml, section, children=None): """ Process a single artist """ @@ -210,14 +209,13 @@ class Artist(MusicMixin, ItemBase): v.KODI_TYPE_ARTIST) self.plexdb.add_artist(plex_id, api.checksum(), - section_id, + section.id, kodi_id, self.last_sync) class Album(MusicMixin, ItemBase): - def add_update(self, xml, section_name=None, section_id=None, - children=None, scan_children=True): + def add_update(self, xml, section, children=None, scan_children=True): """ Process a single album scan_children: set to False if you don't want to add children, e.g. to @@ -250,8 +248,7 @@ class Album(MusicMixin, ItemBase): Artist(self.last_sync, plexdb=self.plexdb, kodidb=self.kodidb).add_update(artist_xml[0], - section_name, - section_id) + section=section) artist = self.plexdb.artist(parent_id) if not artist: LOG.error('Adding artist %s failed for %s', @@ -363,7 +360,7 @@ class Album(MusicMixin, ItemBase): v.KODI_TYPE_ALBUM) self.plexdb.add_album(plex_id, api.checksum(), - section_id, + section.id, artist_id, parent_id, kodi_id, @@ -375,8 +372,7 @@ class Album(MusicMixin, ItemBase): kodidb=self.kodidb) for song in children: context.add_update(song, - section_name=section_name, - section_id=section_id, + section=section, album_xml=xml, genres=genres, genre=genre, @@ -384,9 +380,8 @@ class Album(MusicMixin, ItemBase): class Song(MusicMixin, ItemBase): - def add_update(self, xml, section_name=None, section_id=None, - children=None, album_xml=None, genres=None, genre=None, - compilation=None): + def add_update(self, xml, section, children=None, album_xml=None, + genres=None, genre=None, compilation=None): """ Process single song/track """ @@ -421,8 +416,7 @@ class Song(MusicMixin, ItemBase): Artist(self.last_sync, plexdb=self.plexdb, kodidb=self.kodidb).add_update(artist_xml[0], - section_name, - section_id) + section=section) artist = self.plexdb.artist(artist_id) if not artist: LOG.error('Still could not find grandparent artist %s for %s', @@ -477,8 +471,7 @@ class Song(MusicMixin, ItemBase): Album(self.last_sync, plexdb=self.plexdb, kodidb=self.kodidb).add_update(album_xml[0], - section_name, - section_id, + section=section, children=[xml], scan_children=False) album = self.plexdb.album(album_id) @@ -659,7 +652,7 @@ class Song(MusicMixin, ItemBase): v.KODI_TYPE_ALBUM) self.plexdb.add_song(plex_id, api.checksum(), - section_id, + section.id, artist_id, grandparent_id, album_id, diff --git a/resources/lib/itemtypes/tvshows.py b/resources/lib/itemtypes/tvshows.py index 2fd7acd6..7d0de95f 100644 --- a/resources/lib/itemtypes/tvshows.py +++ b/resources/lib/itemtypes/tvshows.py @@ -143,8 +143,7 @@ class Show(TvShowMixin, ItemBase): """ For Plex library-type TV shows """ - def add_update(self, xml, section_name=None, section_id=None, - children=None): + def add_update(self, xml, section, children=None): """ Process a single show """ @@ -274,20 +273,19 @@ class Show(TvShowMixin, ItemBase): # Process studios self.kodidb.modify_studios(kodi_id, v.KODI_TYPE_SHOW, studios) # Process tags: view, PMS collection tags - tags = [section_name] + tags = [section.name] tags.extend([i for _, i in api.collection_list()]) self.kodidb.modify_tags(kodi_id, v.KODI_TYPE_SHOW, tags) self.plexdb.add_show(plex_id=plex_id, checksum=api.checksum(), - section_id=section_id, + section_id=section.id, kodi_id=kodi_id, kodi_pathid=kodi_pathid, last_sync=self.last_sync) class Season(TvShowMixin, ItemBase): - def add_update(self, xml, section_name=None, section_id=None, - children=None): + def add_update(self, xml, section, children=None): """ Process a single season of a certain tv show """ @@ -315,8 +313,7 @@ class Season(TvShowMixin, ItemBase): Show(self.last_sync, plexdb=self.plexdb, kodidb=self.kodidb).add_update(show_xml[0], - section_name, - section_id) + section=section) show = self.plexdb.show(show_id) if not show: LOG.error('Still could not find parent tv show %s', show_id) @@ -346,7 +343,7 @@ class Season(TvShowMixin, ItemBase): v.KODI_TYPE_SEASON) self.plexdb.add_season(plex_id=plex_id, checksum=api.checksum(), - section_id=section_id, + section_id=section.id, show_id=show_id, parent_id=parent_id, kodi_id=kodi_id, @@ -354,8 +351,7 @@ class Season(TvShowMixin, ItemBase): class Episode(TvShowMixin, ItemBase): - def add_update(self, xml, section_name=None, section_id=None, - children=None): + def add_update(self, xml, section, children=None): """ Process single episode """ @@ -402,8 +398,7 @@ class Episode(TvShowMixin, ItemBase): Show(self.last_sync, plexdb=self.plexdb, kodidb=self.kodidb).add_update(show_xml[0], - section_name, - section_id) + section=section) show = self.plexdb.show(show_id) if not show: LOG.error('Still could not find grandparent tv show %s', show_id) @@ -423,8 +418,7 @@ class Episode(TvShowMixin, ItemBase): Season(self.last_sync, plexdb=self.plexdb, kodidb=self.kodidb).add_update(season_xml[0], - section_name, - section_id) + section=section) season = self.plexdb.season(season_id) if not season: LOG.error('Still could not find parent season %s', season_id) @@ -543,7 +537,7 @@ class Episode(TvShowMixin, ItemBase): userdata['LastPlayedDate']) self.plexdb.add_episode(plex_id=plex_id, checksum=api.checksum(), - section_id=section_id, + section_id=section.id, show_id=show_id, grandparent_id=grandparent_id, season_id=season_id, @@ -619,7 +613,7 @@ class Episode(TvShowMixin, ItemBase): userdata['LastPlayedDate']) self.plexdb.add_episode(plex_id=plex_id, checksum=api.checksum(), - section_id=section_id, + section_id=section.id, show_id=show_id, grandparent_id=grandparent_id, season_id=season_id, diff --git a/resources/lib/library_sync/full_sync.py b/resources/lib/library_sync/full_sync.py index 218bf23d..44dcdbbe 100644 --- a/resources/lib/library_sync/full_sync.py +++ b/resources/lib/library_sync/full_sync.py @@ -140,8 +140,7 @@ class FullSync(common.fullsync_mixin): self.queue.task_done() if isinstance(item, dict): context.add_update(item['xml'][0], - section_name=section.name, - section_id=section.id, + section=section, children=item['children']) self.title = item['xml'][0].get('title') self.processed += 1 @@ -233,8 +232,7 @@ class FullSync(common.fullsync_mixin): if not itemtype.update_userdata(xml_item, section.plex_type): # Somehow did not sync this item yet itemtype.add_update(xml_item, - section_name=section.name, - section_id=section.section_id) + section=section) itemtype.plexdb.update_last_sync(int(xml_item.attrib['ratingKey']), section.plex_type, self.current_sync) diff --git a/resources/lib/library_sync/websocket.py b/resources/lib/library_sync/websocket.py index db096d23..f7fcf4f4 100644 --- a/resources/lib/library_sync/websocket.py +++ b/resources/lib/library_sync/websocket.py @@ -3,6 +3,7 @@ from __future__ import absolute_import, division, unicode_literals from logging import getLogger +from . import sections from .common import update_kodi_library, PLAYLIST_SYNC_ENABLED from .fanart import SYNC_FANART, FanartTask from ..plex_api import API @@ -122,10 +123,16 @@ def process_new_item_message(message): LOG.error('Could not download metadata for %s', message['plex_id']) return False, False, False LOG.debug("Processing new/updated PMS item: %s", message['plex_id']) + section_id = utils.cast(int, xml.get('librarySectionID')) + for section in sections.SECTIONS: + if section.id == section_id: + break + else: + LOG.error('Section id %s not yet encountered', section_id) + return False, False, False with itemtypes.ITEMTYPE_FROM_PLEXTYPE[plex_type](timing.unix_timestamp()) as typus: typus.add_update(xml[0], - section_name=xml.get('librarySectionTitle'), - section_id=xml.get('librarySectionID')) + section=section) cache_artwork(message['plex_id'], plex_type) return True, plex_type in v.PLEX_VIDEOTYPES, plex_type in v.PLEX_AUDIOTYPES