예제 #1
0
        private async Task <Either <BaseError, Unit> > ScanEpisodes(
            PlexLibrary plexMediaSourceLibrary,
            PlexSeason season,
            PlexConnection connection,
            PlexServerAuthToken token)
        {
            Either <BaseError, List <PlexEpisode> > entries = await _plexServerApiClient.GetSeasonEpisodes(
                plexMediaSourceLibrary,
                season,
                connection,
                token);

            return(await entries.Match <Task <Either <BaseError, Unit> > >(
                       async episodeEntries =>
            {
                foreach (PlexEpisode incoming in episodeEntries)
                {
                    incoming.SeasonId = season.Id;

                    // TODO: figure out how to rebuild playlists
                    Either <BaseError, PlexEpisode> maybeEpisode = await _televisionRepository
                                                                   .GetOrAddPlexEpisode(plexMediaSourceLibrary, incoming)
                                                                   .BindT(existing => UpdateStatistics(existing, incoming, connection, token))
                                                                   .BindT(existing => UpdateArtwork(existing, incoming));

                    maybeEpisode.IfLeft(
                        error => _logger.LogWarning(
                            "Error processing plex episode at {Key}: {Error}",
                            incoming.Key,
                            error.Value));
                }

                var episodeKeys = episodeEntries.Map(s => s.Key).ToList();
                await _televisionRepository.RemoveMissingPlexEpisodes(season.Key, episodeKeys);

                return Unit.Default;
            },
                       error =>
            {
                _logger.LogWarning(
                    "Error synchronizing plex library {Path}: {Error}",
                    plexMediaSourceLibrary.Name,
                    error.Value);

                return Left <BaseError, Unit>(error).AsTask();
            }));
        }