public List <ImportDecision> Import(List <ImportDecision> decisions, bool newDownload = false) { var qualifiedImports = GetQualifiedImports(decisions); var imported = new List <ImportDecision>(); foreach (var importDecision in qualifiedImports) { var localEpisode = importDecision.LocalEpisode; try { if (imported.SelectMany(r => r.LocalEpisode.Episodes) .Select(e => e.Id) .ToList() .Intersect(localEpisode.Episodes.Select(e => e.Id)) .Any()) { continue; } var episodeFile = new EpisodeFile(); episodeFile.DateAdded = DateTime.UtcNow; episodeFile.SeriesId = localEpisode.Series.Id; episodeFile.Path = localEpisode.Path.CleanFilePath(); episodeFile.Size = _diskProvider.GetFileSize(localEpisode.Path); episodeFile.Quality = localEpisode.Quality; episodeFile.SeasonNumber = localEpisode.SeasonNumber; episodeFile.Episodes = localEpisode.Episodes; if (newDownload) { episodeFile.SceneName = Path.GetFileNameWithoutExtension(localEpisode.Path.CleanFilePath()); episodeFile.Path = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode); } _mediaFileService.Add(episodeFile); imported.Add(importDecision); if (newDownload) { _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile)); _eventAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode)); } } catch (Exception e) { _logger.WarnException("Couldn't import episode " + localEpisode, e); } } return(imported); }
public List <ImportResult> Import(List <ImportDecision> decisions, bool newDownload, DownloadClientItem downloadClientItem = null) { var qualifiedImports = decisions.Where(c => c.Approved) .GroupBy(c => c.LocalEpisode.Series.Id, (i, s) => s .OrderByDescending(c => c.LocalEpisode.Quality, new QualityModelComparer(s.First().LocalEpisode.Series.Profile)) .ThenByDescending(c => c.LocalEpisode.Size)) .SelectMany(c => c) .ToList(); var importResults = new List <ImportResult>(); foreach (var importDecision in qualifiedImports.OrderBy(e => e.LocalEpisode.Episodes.Select(episode => episode.EpisodeNumber).MinOrDefault()) .ThenByDescending(e => e.LocalEpisode.Size)) { var localEpisode = importDecision.LocalEpisode; var oldFiles = new List <EpisodeFile>(); try { //check if already imported if (importResults.SelectMany(r => r.ImportDecision.LocalEpisode.Episodes) .Select(e => e.Id) .Intersect(localEpisode.Episodes.Select(e => e.Id)) .Any()) { importResults.Add(new ImportResult(importDecision, "Episode has already been imported")); continue; } var episodeFile = new EpisodeFile(); episodeFile.DateAdded = DateTime.UtcNow; episodeFile.SeriesId = localEpisode.Series.Id; episodeFile.Path = localEpisode.Path.CleanFilePath(); episodeFile.Size = _diskProvider.GetFileSize(localEpisode.Path); episodeFile.Quality = localEpisode.Quality; episodeFile.MediaInfo = localEpisode.MediaInfo; episodeFile.SeasonNumber = localEpisode.SeasonNumber; episodeFile.Episodes = localEpisode.Episodes; episodeFile.ReleaseGroup = localEpisode.ParsedEpisodeInfo.ReleaseGroup; if (newDownload) { bool copyOnly = downloadClientItem != null && downloadClientItem.IsReadOnly; episodeFile.SceneName = GetSceneName(downloadClientItem, localEpisode); var moveResult = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode, copyOnly); oldFiles = moveResult.OldFiles; } else { episodeFile.RelativePath = localEpisode.Series.Path.GetRelativePath(episodeFile.Path); } _mediaFileService.Add(episodeFile); importResults.Add(new ImportResult(importDecision)); if (newDownload) { _extraService.ImportExtraFiles(localEpisode, episodeFile, downloadClientItem != null && downloadClientItem.IsReadOnly); } if (downloadClientItem != null) { _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, newDownload, downloadClientItem.DownloadClient, downloadClientItem.DownloadId, downloadClientItem.IsReadOnly)); } else { _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, newDownload)); } if (newDownload) { _eventAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode, episodeFile, oldFiles)); } } catch (Exception e) { _logger.Warn(e, "Couldn't import episode " + localEpisode); importResults.Add(new ImportResult(importDecision, "Failed to import episode")); } } //Adding all the rejected decisions importResults.AddRange(decisions.Where(c => !c.Approved) .Select(d => new ImportResult(d, d.Rejections.Select(r => r.Reason).ToArray()))); return(importResults); }
public List <ImportResult> Import(List <ImportDecision> decisions, bool newDownload, DownloadClientItem downloadClientItem = null, ImportMode importMode = ImportMode.Auto) { var qualifiedImports = decisions.Where(c => c.Approved) .GroupBy(c => c.LocalEpisode.Series.Id, (i, s) => s .OrderByDescending(c => c.LocalEpisode.Quality, new QualityModelComparer(s.First().LocalEpisode.Series.QualityProfile)) .ThenByDescending(c => c.LocalEpisode.Language, new LanguageComparer(s.First().LocalEpisode.Series.LanguageProfile)) .ThenByDescending(c => c.LocalEpisode.Size)) .SelectMany(c => c) .ToList(); var importResults = new List <ImportResult>(); foreach (var importDecision in qualifiedImports.OrderBy(e => e.LocalEpisode.Episodes.Select(episode => episode.EpisodeNumber).MinOrDefault()) .ThenByDescending(e => e.LocalEpisode.Size)) { var localEpisode = importDecision.LocalEpisode; var oldFiles = new List <EpisodeFile>(); try { //check if already imported if (importResults.SelectMany(r => r.ImportDecision.LocalEpisode.Episodes) .Select(e => e.Id) .Intersect(localEpisode.Episodes.Select(e => e.Id)) .Any()) { importResults.Add(new ImportResult(importDecision, "Episode has already been imported")); continue; } var episodeFile = new EpisodeFile(); episodeFile.DateAdded = DateTime.UtcNow; episodeFile.SeriesId = localEpisode.Series.Id; episodeFile.Path = localEpisode.Path.CleanFilePath(); episodeFile.Size = _diskProvider.GetFileSize(localEpisode.Path); episodeFile.Quality = localEpisode.Quality; episodeFile.MediaInfo = localEpisode.MediaInfo; episodeFile.SeasonNumber = localEpisode.SeasonNumber; episodeFile.Episodes = localEpisode.Episodes; episodeFile.ReleaseGroup = localEpisode.ReleaseGroup; episodeFile.Language = localEpisode.Language; bool copyOnly; switch (importMode) { default: case ImportMode.Auto: copyOnly = downloadClientItem != null && !downloadClientItem.CanMoveFiles; break; case ImportMode.Move: copyOnly = false; break; case ImportMode.Copy: copyOnly = true; break; } if (newDownload) { episodeFile.OriginalFilePath = GetOriginalFilePath(downloadClientItem, localEpisode); episodeFile.SceneName = GetSceneName(downloadClientItem, localEpisode); var moveResult = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode, copyOnly); oldFiles = moveResult.OldFiles; } else { episodeFile.RelativePath = localEpisode.Series.Path.GetRelativePath(episodeFile.Path); // Delete existing files from the DB mapped to this path var previousFiles = _mediaFileService.GetFilesWithRelativePath(localEpisode.Series.Id, episodeFile.RelativePath); foreach (var previousFile in previousFiles) { _mediaFileService.Delete(previousFile, DeleteMediaFileReason.ManualOverride); } } _mediaFileService.Add(episodeFile); importResults.Add(new ImportResult(importDecision)); if (newDownload) { _extraService.ImportEpisode(localEpisode, episodeFile, copyOnly); } _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, oldFiles, newDownload, downloadClientItem)); } catch (RootFolderNotFoundException e) { _logger.Warn(e, "Couldn't import episode " + localEpisode); _eventAggregator.PublishEvent(new EpisodeImportFailedEvent(e, localEpisode, newDownload, downloadClientItem)); importResults.Add(new ImportResult(importDecision, "Failed to import episode, Root folder missing.")); } catch (DestinationAlreadyExistsException e) { _logger.Warn(e, "Couldn't import episode " + localEpisode); importResults.Add(new ImportResult(importDecision, "Failed to import episode, Destination already exists.")); } catch (Exception e) { _logger.Warn(e, "Couldn't import episode " + localEpisode); importResults.Add(new ImportResult(importDecision, "Failed to import episode")); } } //Adding all the rejected decisions importResults.AddRange(decisions.Where(c => !c.Approved) .Select(d => new ImportResult(d, d.Rejections.Select(r => r.Reason).ToArray()))); return(importResults); }
public List <ImportResult> Import(List <ImportDecision> decisions, bool newDownload, DownloadClientItem downloadClientItem = null, ImportMode importMode = ImportMode.Auto) { _logger.Debug("Decisions: {0}", decisions.Count); var qualifiedImports = decisions.Where(c => c.Approved) .GroupBy(c => c.LocalMovie.Movie.Id, (i, s) => s .OrderByDescending(c => c.LocalMovie.Quality, new QualityModelComparer(s.First().LocalMovie.Movie.Profile)) .ThenByDescending(c => c.LocalMovie.Size)) .SelectMany(c => c) .ToList(); var importResults = new List <ImportResult>(); foreach (var importDecision in qualifiedImports.OrderBy(e => e.LocalMovie.Size) .ThenByDescending(e => e.LocalMovie.Size)) { var localMovie = importDecision.LocalMovie; var oldFiles = new List <MovieFile>(); try { //check if already imported if (importResults.Select(r => r.ImportDecision.LocalMovie.Movie) .Select(m => m.Id).Contains(localMovie.Movie.Id)) { importResults.Add(new ImportResult(importDecision, "Movie has already been imported")); continue; } var movieFile = new MovieFile(); movieFile.DateAdded = DateTime.UtcNow; movieFile.MovieId = localMovie.Movie.Id; movieFile.Path = localMovie.Path.CleanFilePath(); movieFile.Size = _diskProvider.GetFileSize(localMovie.Path); movieFile.Quality = localMovie.Quality; movieFile.MediaInfo = localMovie.MediaInfo; movieFile.Movie = localMovie.Movie; movieFile.ReleaseGroup = localMovie.ParsedMovieInfo.ReleaseGroup; movieFile.Edition = localMovie.ParsedMovieInfo.Edition; bool copyOnly; switch (importMode) { default: case ImportMode.Auto: copyOnly = downloadClientItem != null && downloadClientItem.IsReadOnly; break; case ImportMode.Move: copyOnly = false; break; case ImportMode.Copy: copyOnly = true; break; } if (newDownload) { movieFile.SceneName = GetSceneName(downloadClientItem, localMovie); var moveResult = _episodeFileUpgrader.UpgradeMovieFile(movieFile, localMovie, copyOnly); //TODO: Check if this works oldFiles = moveResult.OldFiles; } else { movieFile.RelativePath = localMovie.Movie.Path.GetRelativePath(movieFile.Path); } _mediaFileService.Add(movieFile); importResults.Add(new ImportResult(importDecision)); if (newDownload) { //_extraService.ImportExtraFiles(localMovie, episodeFile, copyOnly); TODO update for movie } if (downloadClientItem != null) { _eventAggregator.PublishEvent(new MovieImportedEvent(localMovie, movieFile, newDownload, downloadClientItem.DownloadClient, downloadClientItem.DownloadId, downloadClientItem.IsReadOnly)); } else { _eventAggregator.PublishEvent(new MovieImportedEvent(localMovie, movieFile, newDownload)); } if (newDownload) { _eventAggregator.PublishEvent(new MovieDownloadedEvent(localMovie, movieFile, oldFiles)); } } catch (Exception e) { _logger.Warn(e, "Couldn't import movie " + localMovie); importResults.Add(new ImportResult(importDecision, "Failed to import movie")); } } //Adding all the rejected decisions importResults.AddRange(decisions.Where(c => !c.Approved) .Select(d => new ImportResult(d, d.Rejections.Select(r => r.Reason).ToArray()))); return(importResults); }
public List <ImportResult> Import(List <ImportDecision> decisions, bool newDownload, DownloadClientItem downloadClientItem = null, ImportMode importMode = ImportMode.Auto) { _logger.Debug("Decisions: {0}", decisions.Count); //I added a null op for the rare case that the quality is null. TODO: find out why that would even happen in the first place. var qualifiedImports = decisions.Where(c => c.Approved) .GroupBy(c => c.LocalMovie.Movie.Id, (i, s) => s .OrderByDescending(c => c.LocalMovie.Quality ?? new QualityModel { Quality = Quality.Unknown }, new QualityModelComparer(s.First().LocalMovie.Movie.Profile)) .ThenByDescending(c => c.LocalMovie.Size)) .SelectMany(c => c) .ToList(); var importResults = new List <ImportResult>(); foreach (var importDecision in qualifiedImports.OrderByDescending(e => e.LocalMovie.Size)) { var localMovie = importDecision.LocalMovie; var oldFiles = new List <MovieFile>(); try { //check if already imported if (importResults.Select(r => r.ImportDecision.LocalMovie.Movie) .Select(m => m.Id).Contains(localMovie.Movie.Id)) { importResults.Add(new ImportResult(importDecision, "Movie has already been imported")); continue; } var movieFile = new MovieFile(); movieFile.DateAdded = DateTime.UtcNow; movieFile.MovieId = localMovie.Movie.Id; movieFile.Path = localMovie.Path.CleanFilePath(); movieFile.Size = _diskProvider.GetFileSize(localMovie.Path); movieFile.Quality = localMovie.Quality; movieFile.Languages = localMovie.Languages; movieFile.MediaInfo = localMovie.MediaInfo; movieFile.Movie = localMovie.Movie; movieFile.ReleaseGroup = localMovie.ReleaseGroup; movieFile.Edition = localMovie.Edition; if (downloadClientItem?.DownloadId.IsNotNullOrWhiteSpace() == true) { var grabHistory = _historyService.FindByDownloadId(downloadClientItem.DownloadId) .OrderByDescending(h => h.Date) .FirstOrDefault(h => h.EventType == MovieHistoryEventType.Grabbed); if (Enum.TryParse(grabHistory?.Data.GetValueOrDefault("indexerFlags"), true, out IndexerFlags flags)) { movieFile.IndexerFlags = flags; } } bool copyOnly; switch (importMode) { default: case ImportMode.Auto: copyOnly = downloadClientItem != null && !downloadClientItem.CanMoveFiles; break; case ImportMode.Move: copyOnly = false; break; case ImportMode.Copy: copyOnly = true; break; } if (newDownload) { movieFile.OriginalFilePath = GetOriginalFilePath(downloadClientItem, localMovie); movieFile.SceneName = GetSceneName(downloadClientItem, localMovie); var moveResult = _movieFileUpgrader.UpgradeMovieFile(movieFile, localMovie, copyOnly); //TODO: Check if this works oldFiles = moveResult.OldFiles; } else { movieFile.RelativePath = localMovie.Movie.Path.GetRelativePath(movieFile.Path); } movieFile = _mediaFileService.Add(movieFile); importResults.Add(new ImportResult(importDecision)); if (newDownload) { _extraService.ImportMovie(localMovie, movieFile, copyOnly); } if (downloadClientItem != null) { _eventAggregator.PublishEvent(new MovieImportedEvent(localMovie, movieFile, newDownload, downloadClientItem, downloadClientItem.DownloadId)); } else { _eventAggregator.PublishEvent(new MovieImportedEvent(localMovie, movieFile, newDownload)); } if (newDownload) { _eventAggregator.PublishEvent(new MovieDownloadedEvent(localMovie, movieFile, oldFiles, downloadClientItem)); } } catch (RootFolderNotFoundException e) { _logger.Warn(e, "Couldn't import movie " + localMovie); _eventAggregator.PublishEvent(new MovieImportFailedEvent(e, localMovie, newDownload, downloadClientItem)); importResults.Add(new ImportResult(importDecision, "Failed to import movie, Root folder missing.")); } catch (DestinationAlreadyExistsException e) { _logger.Warn(e, "Couldn't import movie " + localMovie); importResults.Add(new ImportResult(importDecision, "Failed to import movie, Destination already exists.")); } catch (Exception e) { _logger.Warn(e, "Couldn't import movie " + localMovie); importResults.Add(new ImportResult(importDecision, "Failed to import movie")); } } //Adding all the rejected decisions importResults.AddRange(decisions.Where(c => !c.Approved) .Select(d => new ImportResult(d, d.Rejections.Select(r => r.Reason).ToArray()))); return(importResults); }
public List <ImportDecision> Import(List <ImportDecision> decisions, bool newDownload = false) { var qualifiedImports = decisions.Where(c => c.Approved) .GroupBy(c => c.LocalEpisode.Series.Id, (i, s) => s .OrderByDescending(c => c.LocalEpisode.Quality, new QualityModelComparer(s.First().LocalEpisode.Series.QualityProfile)) .ThenByDescending(c => c.LocalEpisode.Size)) .SelectMany(c => c) .ToList(); var imported = new List <ImportDecision>(); foreach (var importDecision in qualifiedImports.OrderByDescending(e => e.LocalEpisode.Size)) { var localEpisode = importDecision.LocalEpisode; var oldFiles = new List <EpisodeFile>(); try { //check if already imported if (imported.SelectMany(r => r.LocalEpisode.Episodes) .Select(e => e.Id) .Intersect(localEpisode.Episodes.Select(e => e.Id)) .Any()) { continue; } var episodeFile = new EpisodeFile(); episodeFile.DateAdded = DateTime.UtcNow; episodeFile.SeriesId = localEpisode.Series.Id; episodeFile.Path = localEpisode.Path.CleanFilePath(); episodeFile.Size = _diskProvider.GetFileSize(localEpisode.Path); episodeFile.Quality = localEpisode.Quality; episodeFile.SeasonNumber = localEpisode.SeasonNumber; episodeFile.Episodes = localEpisode.Episodes; episodeFile.ReleaseGroup = localEpisode.ParsedEpisodeInfo.ReleaseGroup; if (newDownload) { episodeFile.SceneName = Path.GetFileNameWithoutExtension(localEpisode.Path.CleanFilePath()); var moveResult = _episodeFileUpgrader.UpgradeEpisodeFile(episodeFile, localEpisode); oldFiles = moveResult.OldFiles; } _mediaFileService.Add(episodeFile); imported.Add(importDecision); _eventAggregator.PublishEvent(new EpisodeImportedEvent(localEpisode, episodeFile, newDownload)); if (newDownload) { _eventAggregator.PublishEvent(new EpisodeDownloadedEvent(localEpisode, episodeFile, oldFiles)); } } catch (Exception e) { _logger.WarnException("Couldn't import episode " + localEpisode, e); } } return(imported); }