def store_episodes(self, key, show, episodes=None, artifact=None):
        if episodes is None:
            episodes = self.child('episode').artifacts.get(artifact or key)

        if episodes is None:
            return

        self.store(key, merge({'episodes': episodes}, show))
示例#2
0
    def store_episodes(self, key, show, episodes=None, artifact=None):
        if episodes is None:
            episodes = self.child('episode').artifacts.get(artifact or key)

        if episodes is None:
            return

        self.store(key, merge({'episodes': episodes}, show))
    def store_seasons(self, key, show, seasons=None, artifact=None):
        if seasons is None:
            seasons = self.child('season').artifacts.get(artifact or key)

        if not show or not seasons:
            return

        self.store(key, merge({'seasons': seasons}, show))
示例#4
0
    def run(self, section=None, artifacts=None):
        self.reset(artifacts)
        self.check_stopping()

        enabled_funcs = self.get_enabled_functions()
        if not enabled_funcs:
            log.info('There are no functions enabled, skipping push.show')
            return True

        p_shows = self.plex.library('show', section)
        if not p_shows:
            # No items found, no need to continue
            return True

        # Fetch library, and only get ratings and collection if enabled
        t_shows, t_shows_table = self.trakt.merged(
            'shows',
            ratings='ratings' in enabled_funcs,
            collected='collected' in enabled_funcs
        )

        if t_shows_table is None:
            log.warn('Unable to construct merged library from trakt')
            return False

        self.emit('started', len(p_shows))

        for x, (key, p_shows) in enumerate(p_shows.iteritems()):
            self.check_stopping()
            self.emit('progress', x + 1)

            t_show = t_shows_table.get(key)

            log.debug('Processing "%s" [%s]', p_shows[0].title if p_shows else None, key)

            # TODO check result
            self.trigger(enabled_funcs, key=key, p_shows=p_shows, t_show=t_show)

            show = None
            show_artifacts = {
                'collected': [],
                'watched': [],
                'ratings': []
            }

            for p_show in p_shows:
                if not show:
                    # Build data from plex show
                    data = ActionHelper.plex.to_trakt(key, p_show)

                    if data:
                        # Valid show, use data
                        show = data
                    else:
                        log.warn('Ignored unmatched show "%s" [%s]', p_show.title if p_show else None, key)
                        continue

                # Run season task
                self.child('season').run(
                    p_seasons=Library.episodes(p_show.rating_key, p_show, flat=False),
                    t_seasons=t_show.seasons if t_show else {},
                    artifacts=artifacts
                )

                # Store season artifacts
                show_artifacts['collected'].append(
                    self.child('season').artifacts.pop('collected', [])
                )

                show_artifacts['watched'].append(
                    self.child('season').artifacts.pop('watched', [])
                )

                show_artifacts['ratings'].append(
                    self.child('season').artifacts.pop('ratings', [])
                )

            if not show:
                log.warn('Unable to retrieve show details, ignoring "%s" [%s]', p_show.title if p_show else None, key)
                continue

            # Merge show artifacts
            for k, v in show_artifacts.iteritems():
                result = []

                for seasons in v:
                    result = self.merge_artifacts(result, seasons)

                show_artifacts[k] = result

            # Store merged artifacts
            self.store_seasons('collected', show, seasons=show_artifacts.get('collected'))
            self.store_seasons('watched', show, seasons=show_artifacts.get('watched'))

            show_rating = self.rate(key, p_shows, t_show, artifact='', include_metadata=False)

            self.store_seasons('ratings', merge(
                # Include show rating
                show_rating,
                show
            ), seasons=show_artifacts.get('ratings'))

        self.emit('finished')
        self.check_stopping()

        #
        # Push changes to trakt
        #
        self.add('sync/collection', shows=self.retrieve('collected'))
        self.add('sync/history', shows=self.retrieve('watched'))
        self.add('sync/ratings', shows=self.retrieve('ratings'))

        self.remove('sync/collection', shows=self.retrieve('missing.shows'))

        self.save('last_artifacts', self.artifacts.store)

        log.info('Finished pushing shows to trakt')
        return True