public async Task <FileOrganizationResult> OrganizeEpisodeFile(string path, AutoOrganizeOptions options, bool overwriteExisting, CancellationToken cancellationToken) { _logger.Info("Sorting file {0}", path); var result = new FileOrganizationResult { Date = DateTime.UtcNow, OriginalPath = path, OriginalFileName = Path.GetFileName(path), Type = FileOrganizerType.Episode, FileSize = _fileSystem.GetFileInfo(path).Length }; try { if (_libraryMonitor.IsPathLocked(path)) { result.Status = FileSortingStatus.Failure; result.StatusMessage = "Path is locked by other processes. Please try again later."; return(result); } var namingOptions = ((LibraryManager)_libraryManager).GetNamingOptions(); var resolver = new EpisodeResolver(namingOptions, new NullLogger()); var episodeInfo = resolver.Resolve(path, false) ?? new MediaBrowser.Naming.TV.EpisodeInfo(); var seriesName = episodeInfo.SeriesName; if (!string.IsNullOrEmpty(seriesName)) { var seasonNumber = episodeInfo.SeasonNumber; result.ExtractedSeasonNumber = seasonNumber; // Passing in true will include a few extra regex's var episodeNumber = episodeInfo.EpisodeNumber; result.ExtractedEpisodeNumber = episodeNumber; var premiereDate = episodeInfo.IsByDate ? new DateTime(episodeInfo.Year.Value, episodeInfo.Month.Value, episodeInfo.Day.Value) : (DateTime?)null; if (episodeInfo.IsByDate || (seasonNumber.HasValue && episodeNumber.HasValue)) { if (episodeInfo.IsByDate) { _logger.Debug("Extracted information from {0}. Series name {1}, Date {2}", path, seriesName, premiereDate.Value); } else { _logger.Debug("Extracted information from {0}. Series name {1}, Season {2}, Episode {3}", path, seriesName, seasonNumber, episodeNumber); } var endingEpisodeNumber = episodeInfo.EndingEpsiodeNumber; result.ExtractedEndingEpisodeNumber = endingEpisodeNumber; await OrganizeEpisode(path, seriesName, seasonNumber, episodeNumber, endingEpisodeNumber, premiereDate, options, overwriteExisting, false, result, cancellationToken).ConfigureAwait(false); } else { var msg = string.Format("Unable to determine episode number from {0}", path); result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.Warn(msg); } } else { var msg = string.Format("Unable to determine series name from {0}", path); result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.Warn(msg); } var previousResult = _organizationService.GetResultBySourcePath(path); if (previousResult != null) { // Don't keep saving the same result over and over if nothing has changed if (previousResult.Status == result.Status && previousResult.StatusMessage == result.StatusMessage && result.Status != FileSortingStatus.Success) { return(previousResult); } } await _organizationService.SaveResult(result, CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { result.Status = FileSortingStatus.Failure; result.StatusMessage = ex.Message; } return(result); }
public async Task <FileOrganizationResult> OrganizeEpisodeFile(string path, TvFileOrganizationOptions options, bool overwriteExisting, CancellationToken cancellationToken) { _logger.Info("Sorting file {0}", path); var result = new FileOrganizationResult { Date = DateTime.UtcNow, OriginalPath = path, OriginalFileName = Path.GetFileName(path), Type = FileOrganizerType.Episode, FileSize = new FileInfo(path).Length }; var namingOptions = ((LibraryManager)_libraryManager).GetNamingOptions(); var resolver = new Naming.TV.EpisodeResolver(namingOptions, new PatternsLogger()); var episodeInfo = resolver.Resolve(path, false) ?? new Naming.TV.EpisodeInfo(); var seriesName = episodeInfo.SeriesName; if (!string.IsNullOrEmpty(seriesName)) { var season = episodeInfo.SeasonNumber; result.ExtractedSeasonNumber = season; if (season.HasValue) { // Passing in true will include a few extra regex's var episode = episodeInfo.EpisodeNumber; result.ExtractedEpisodeNumber = episode; if (episode.HasValue) { _logger.Debug("Extracted information from {0}. Series name {1}, Season {2}, Episode {3}", path, seriesName, season, episode); var endingEpisodeNumber = episodeInfo.EndingEpsiodeNumber; result.ExtractedEndingEpisodeNumber = endingEpisodeNumber; await OrganizeEpisode(path, seriesName, season.Value, episode.Value, endingEpisodeNumber, options, overwriteExisting, result, cancellationToken).ConfigureAwait(false); } else { var msg = string.Format("Unable to determine episode number from {0}", path); result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.Warn(msg); } } else { var msg = string.Format("Unable to determine season number from {0}", path); result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.Warn(msg); } } else { var msg = string.Format("Unable to determine series name from {0}", path); result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.Warn(msg); } var previousResult = _organizationService.GetResultBySourcePath(path); if (previousResult != null) { // Don't keep saving the same result over and over if nothing has changed if (previousResult.Status == result.Status && result.Status != FileSortingStatus.Success) { return(previousResult); } } await _organizationService.SaveResult(result, CancellationToken.None).ConfigureAwait(false); return(result); }
/// <summary> /// Organize an episode file. /// </summary> /// <param name="path">The path to the episode file.</param> /// <param name="options">The options to use for organizing the file.</param> /// <param name="cancellationToken">Cancellation token for the operation.</param> /// <returns>A task representing the file organization operation and containing the operation result.</returns> public async Task <FileOrganizationResult> OrganizeEpisodeFile( string path, TvFileOrganizationOptions options, CancellationToken cancellationToken) { _logger.LogInformation("Sorting file {0}", path); var result = new FileOrganizationResult { Date = DateTime.UtcNow, OriginalPath = path, OriginalFileName = Path.GetFileName(path), Type = FileOrganizerType.Unknown, FileSize = _fileSystem.GetFileInfo(path).Length }; try { if (_libraryMonitor.IsPathLocked(path)) { result.Status = FileSortingStatus.Failure; result.StatusMessage = "Path is locked by other processes. Please try again later."; _logger.LogInformation("Auto-organize Path is locked by other processes. Please try again later."); return(result); } var namingOptions = GetNamingOptionsInternal(); var resolver = new EpisodeResolver(namingOptions); var episodeInfo = resolver.Resolve(path, false) ?? new Naming.TV.EpisodeInfo(string.Empty); var seriesName = episodeInfo.SeriesName; int?seriesYear = null; if (!string.IsNullOrEmpty(seriesName)) { var seriesParseResult = _libraryManager.ParseName(seriesName); seriesName = seriesParseResult.Name; seriesYear = seriesParseResult.Year; } if (string.IsNullOrWhiteSpace(seriesName)) { seriesName = episodeInfo.SeriesName; } if (!string.IsNullOrEmpty(seriesName)) { var seasonNumber = episodeInfo.SeasonNumber; result.ExtractedSeasonNumber = seasonNumber; // Passing in true will include a few extra regex's var episodeNumber = episodeInfo.EpisodeNumber; result.ExtractedEpisodeNumber = episodeNumber; var premiereDate = episodeInfo.IsByDate ? new DateTime(episodeInfo.Year.Value, episodeInfo.Month.Value, episodeInfo.Day.Value) : (DateTime?)null; if (episodeInfo.IsByDate || (seasonNumber.HasValue && episodeNumber.HasValue)) { if (episodeInfo.IsByDate) { _logger.LogDebug("Extracted information from {0}. Series name {1}, Date {2}", path, seriesName, premiereDate.Value); } else { _logger.LogDebug("Extracted information from {0}. Series name {1}, Season {2}, Episode {3}", path, seriesName, seasonNumber, episodeNumber); } // We detected an airdate or (an season number and an episode number) // We have all the chance that the media type is an Episode // if an earlier result exist with an different type, we update it result.Type = CurrentFileOrganizerType; var endingEpisodeNumber = episodeInfo.EndingEpisodeNumber; result.ExtractedEndingEpisodeNumber = endingEpisodeNumber; await OrganizeEpisode( path, seriesName, seriesYear, seasonNumber, episodeNumber, endingEpisodeNumber, premiereDate, options, false, result, cancellationToken).ConfigureAwait(false); } else { var msg = "Unable to determine episode number from " + path; result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.LogWarning(msg); } } else { var msg = "Unable to determine series name from " + path; result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.LogWarning(msg); } // Handle previous result var previousResult = _organizationService.GetResultBySourcePath(path); if ((previousResult != null && result.Type == FileOrganizerType.Unknown) || (previousResult?.Status == result.Status && previousResult?.StatusMessage == result.StatusMessage && result.Status != FileSortingStatus.Success)) { // Don't keep saving the same result over and over if nothing has changed return(previousResult); } } catch (OrganizationException ex) { result.Status = FileSortingStatus.Failure; result.StatusMessage = ex.Message; } catch (Exception ex) { result.Status = FileSortingStatus.Failure; result.StatusMessage = ex.Message; _logger.LogError(ex, "Error organizing file"); } _organizationService.SaveResult(result, CancellationToken.None); return(result); }
public async Task <FileOrganizationResult> OrganizeMovieFile(string path, MovieFileOrganizationOptions options, CancellationToken cancellationToken) { _logger.Info("Sorting file {0}", path); var result = new FileOrganizationResult { Date = DateTime.UtcNow, OriginalPath = path, OriginalFileName = Path.GetFileName(path), Type = FileOrganizerType.Unknown, FileSize = _fileSystem.GetFileInfo(path).Length }; try { if (_libraryMonitor.IsPathLocked(path.AsSpan())) { result.Status = FileSortingStatus.Failure; result.StatusMessage = "Path is locked by other processes. Please try again later."; _logger.Info("Auto-organize Path is locked by other processes. Please try again later."); return(result); } var movieInfo = _libraryManager.IsVideoFile(path.AsSpan()) ? _libraryManager.ParseName(Path.GetFileName(path).AsSpan()) : new ItemLookupInfo(); var movieName = movieInfo.Name; if (!string.IsNullOrEmpty(movieName)) { var movieYear = movieInfo.Year; _logger.Debug("Extracted information from {0}. Movie {1}, Year {2}", path, movieName, movieYear); await OrganizeMovie(path, movieName, movieYear, options, result, cancellationToken).ConfigureAwait(false); } else { var msg = string.Format("Unable to determine movie name from {0}", path); result.Status = FileSortingStatus.Failure; result.StatusMessage = msg; _logger.Warn(msg); } // Handle previous result var previousResult = _organizationService.GetResultBySourcePath(path); if ((previousResult != null && result.Type == FileOrganizerType.Unknown) || (previousResult?.Status == result.Status && previousResult?.StatusMessage == result.StatusMessage && result.Status != FileSortingStatus.Success)) { // Don't keep saving the same result over and over if nothing has changed return(previousResult); } } catch (Exception ex) { result.Status = FileSortingStatus.Failure; result.StatusMessage = ex.Message; _logger.ErrorException("Error organizing file", ex); } _organizationService.SaveResult(result, CancellationToken.None); return(result); }