public async Task <Either <BaseError, Unit> > ScanLibrary( string address, string apiKey, EmbyLibrary library, string ffmpegPath, string ffprobePath, CancellationToken cancellationToken) { List <EmbyPathReplacement> pathReplacements = await _mediaSourceRepository.GetEmbyPathReplacements(library.MediaSourceId); string GetLocalPath(EmbyMovie movie) { return(_pathReplacementService.GetReplacementEmbyPath( pathReplacements, movie.GetHeadVersion().MediaFiles.Head().Path, false)); } return(await ScanLibrary( _embyMovieRepository, new EmbyConnectionParameters(address, apiKey), library, GetLocalPath, ffmpegPath, ffprobePath, false, cancellationToken)); }
public async Task <Either <BaseError, Unit> > ScanLibrary( string address, string apiKey, EmbyLibrary library, string ffmpegPath, string ffprobePath, CancellationToken cancellationToken) { try { List <EmbyItemEtag> existingShows = await _televisionRepository.GetExistingShows(library); // TODO: maybe get quick list of item ids and etags from api to compare first // TODO: paging? List <EmbyPathReplacement> pathReplacements = await _mediaSourceRepository .GetEmbyPathReplacements(library.MediaSourceId); Either <BaseError, List <EmbyShow> > maybeShows = await _embyApiClient.GetShowLibraryItems( address, apiKey, library.ItemId); foreach (BaseError error in maybeShows.LeftToSeq()) { _logger.LogWarning( "Error synchronizing emby library {Path}: {Error}", library.Name, error.Value); } foreach (List <EmbyShow> shows in maybeShows.RightToSeq()) { Either <BaseError, Unit> scanResult = await ProcessShows( address, apiKey, library, ffmpegPath, ffprobePath, pathReplacements, existingShows, shows, cancellationToken); foreach (ScanCanceled error in scanResult.LeftToSeq().OfType <ScanCanceled>()) { return(error); } foreach (Unit _ in scanResult.RightToSeq()) { var incomingShowIds = shows.Map(s => s.ItemId).ToList(); var showIds = existingShows .Filter(i => !incomingShowIds.Contains(i.ItemId)) .Map(m => m.ItemId) .ToList(); List <int> missingShowIds = await _televisionRepository.RemoveMissingShows(library, showIds); await _searchIndex.RemoveItems(missingShowIds); await _televisionRepository.DeleteEmptySeasons(library); List <int> emptyShowIds = await _televisionRepository.DeleteEmptyShows(library); await _searchIndex.RemoveItems(emptyShowIds); await _mediator.Publish(new LibraryScanProgress(library.Id, 0), cancellationToken); } } return(Unit.Default); } catch (Exception ex) when(ex is TaskCanceledException or OperationCanceledException) { return(new ScanCanceled()); } finally { _searchIndex.Commit(); } }