public void RenameFiles() { List <EpisodeForComparingDto> sourceEps = _localservice.GetFilesAsDtosToCompare(); foreach (EpisodeForComparingDto epDto in sourceEps) { try { Episode epEntity = _episodeService.FindByEpisodeForComparingDto(epDto); if (epEntity != null) { EpisodeForComparingDto targetDto = CreateEpisodeForComparingDtoFromEntity(epEntity); string localFile = Path.GetFileName(epDto.FilePath); string targetName = targetDto.GetFormattedFilename(); bool shouldRename = _prompter.PromptForRename(localFile, targetName); if (shouldRename) { _localservice.RenameFile(epDto.FilePath, targetDto); } } else { Log.Information("Could not find a matching database entry for {epFileName}. Make sure the series name matches.", epDto.FilePath); } } catch (Exception e) { Log.Warning(e, "Could not rename file {a}", epDto.FilePath); } } }
public EpisodeForComparingDto CreateEpisodeForComparingDtoFromEntity(Episode ep) { TVShow show = _context.Shows .FirstOrDefault(b => b.SeriesId == ep.SeriesId); string seriesName = GetSeriesTitleForRenaming(show); EpisodeForComparingDto targetEpisodeDto = ep.ToEpisodeForComparingDto(seriesName); return(targetEpisodeDto); }
public EpisodeForComparingDto CreateEpisodeObjectFromPath(string filePath) { EpisodeForComparingDto ep = CreateEpisodeObjectFromFilename(Path.GetFileName(filePath)); if (ep != null) { ep.FilePath = filePath; } return(ep); }
public EpisodeForComparingDto CreateEpisodeObjectFromFilename(string filename) { EpisodeForComparingDto ep = ExtractSeasonEpisodeFromFilenameAsEpisodeObject(filename); if (ep != null) { ep.SeriesName = ExtractSeriesNameFromFilename(filename).ReplacePeriodsWithSpaces(); } return(ep); }
public void FindByEpisodeForComparingDto_EpInDB_ReturnsEp() { TVShow show = new TVShow() { SeriesId = 328487, SeriesName = "The Orville" }; Episode ep1 = new Episode() { SeriesId = 328487, TVDBEpisodeId = 6089488, Season = 1, AiredEpisodeNumber = 1, EpisodeName = "Old Wounds" }; Episode ep2 = new Episode() { SeriesId = 328487, TVDBEpisodeId = 6123044, Season = 1, AiredEpisodeNumber = 2, EpisodeName = "Command Performance" }; EpisodeForComparingDto epDto = new EpisodeForComparingDto() { SeriesName = "the orville", SeasonNumber = 1, EpisodeNumberInSeason = 1 }; string expectedEpisodeName = "Old Wounds"; var options = new DbContextOptionsBuilder <EpisodeContext>() .UseInMemoryDatabase(databaseName: "FindByEpisodeForComparingDto_EpInDB_ReturnsEp") .Options; using (var context = new EpisodeContext(options)) { var service = new EpisodeService(context); var TVService = new TVShowService(context); TVService.Add(show); service.Add(ep1); service.Add(ep2); } using (var context = new EpisodeContext(options)) { var service = new EpisodeService(context); Episode result = service.FindByEpisodeForComparingDto(epDto); Assert.NotNull(result); Assert.Equal(expectedEpisodeName, result.EpisodeName); } }
public void GetSeasonEpisodeInNumberFormat_Valid_ReturnTrue() { EpisodeForComparingDto episodeObject = new EpisodeForComparingDto() { SeriesName = "The Orville", SeasonNumber = 2, EpisodeNumberInSeason = 9, EpisodeTitle = "Identity, Part 2" }; TitleComparer comparer = new TitleComparer(); string expected = "2.09"; Assert.Equal(expected, comparer.GetSeasonEpisodeInNumberFormat(episodeObject)); }
public void RenameFile(string filePath, EpisodeForComparingDto ep) { try { string extension = Path.GetExtension(filePath); string sourceDirectory = Path.GetDirectoryName(filePath); string newFilename = ep.GetFormattedFilename(); string newPath = Path.Combine(sourceDirectory, $"{newFilename}{extension}"); File.Move(filePath, newPath); Log.Information("Renamed {a} to {b}", Path.GetFileName(filePath), Path.GetFileName(newPath)); } catch (Exception e) { Log.Error(e.Message); } }
public void FilenameContainsSeriesName_ContainsSeriesName_ReturnsTrue() { TitleComparer comparer = new TitleComparer(); EpisodeForComparingDto episodeObject = new EpisodeForComparingDto() { SeriesName = "The Chilling Adventures of Sabrina", SeasonNumber = 1, EpisodeNumberInSeason = 4, EpisodeTitle = "Witch Academy" }; string fileName = @"the.chilling.adventures.of.sabrina.s01e04.720p.webrip.hevc.x265.rmteam.mkv"; Assert.True(comparer.FilenameContainsSeriesName(episodeObject, fileName)); }
public void FilenameContainsSeriesName_DoesNotContainSeriesName_ReturnsFalse() { TitleComparer comparer = new TitleComparer(); EpisodeForComparingDto episodeObject = new EpisodeForComparingDto() { SeriesName = "The Chilling Adventures of Sabrina", SeasonNumber = 1, EpisodeNumberInSeason = 4, EpisodeTitle = "Witch Academy" }; string fileName = @"doctor.who.2005.s11e10.the.battle.of.ranskoor.av.kolos.720p.web.dl.hevc.x265.rmteam.mkv"; Assert.False(comparer.FilenameContainsSeriesName(episodeObject, fileName)); }
public void CreateEpisodeObjectFromFilename_ValidFile_CreatesObject() { TitleComparer comparer = new TitleComparer(); string fileName = @"the.chilling.adventures.of.sabrina.s01e07.720p.webrip.hevc.x265.rmteam.mkv"; int expectedSeason = 1; int expectedEpisode = 7; string expectedTitle = "the chilling adventures of sabrina"; EpisodeForComparingDto epDto = comparer.CreateEpisodeObjectFromFilename(fileName); Assert.Equal(expectedSeason, epDto.SeasonNumber); Assert.Equal(expectedEpisode, epDto.EpisodeNumberInSeason); Assert.Equal(expectedTitle, epDto.SeriesName); }
public void GetSeriesNameInLowercaseAndPeriodsWithoutStartingArticle_ReturnsTrue() { TitleComparer comparer = new TitleComparer(); EpisodeForComparingDto episodeObject = new EpisodeForComparingDto() { SeriesName = "The Chilling Adventures of Sabrina", SeasonNumber = 1, EpisodeNumberInSeason = 4, EpisodeTitle = "Witch Academy" }; string expected = "chilling.adventures.of.sabrina"; string result = comparer.GetSeriesNameInLowercaseAndPeriodsWithoutStartingArticle(episodeObject); Assert.Equal(expected, result); }
public EpisodeForComparingDto ExtractSeasonEpisodeFromFilenameAsEpisodeObject(string filename) { EpisodeForComparingDto ep = new EpisodeForComparingDto(); GroupCollection groups = GetSeasonEpisodeMatchFromFilename(filename).Groups; if (RegexGroupsContainSeasonEpisodeFormat(groups)) { ep.SeasonNumber = groups[1].ToString().ToInt(); ep.EpisodeNumberInSeason = groups[2].ToString().ToInt(); return(ep); } else { Log.Information("Filename {a} does not contain episode number in s##e## format.", filename); return(null); } }
public Episode FindByEpisodeForComparingDto(EpisodeForComparingDto epForComparing) { TVShowService tvshowService = new TVShowService(_context); TVShow show = tvshowService.FindByName(epForComparing.SeriesName); if (show != null) { return(_context.Episodes .FirstOrDefault(b => b.SeriesId == show.SeriesId && b.Season == epForComparing.SeasonNumber && b.AiredEpisodeNumber == epForComparing.EpisodeNumberInSeason)); } else { return(null); } }
public void ContainsSeasonEpisode_DoesContain_ReturnTrue() { EpisodeForComparingDto episodeObject = new EpisodeForComparingDto() { SeriesName = "Counterpart", SeasonNumber = 2, EpisodeNumberInSeason = 9, EpisodeTitle = "You to You" }; string fileName = @"counterpart.s02e09.you.to.you.720p.webrip.hevc.x265.rmteam.mkv"; bool expected = true; TitleComparer comparer = new TitleComparer(); bool result = comparer.FilenameContainsSeasonEpisode(episodeObject, fileName); Assert.Equal(expected, result); }
public List <EpisodeForComparingDto> GetFilesAsDtosToCompare() { List <string> files = GetFiles(); List <EpisodeForComparingDto> epsToCompare = new List <EpisodeForComparingDto>(); foreach (string file in files) { try { EpisodeForComparingDto epFromFilenameDto = _titleComparer.CreateEpisodeObjectFromPath(file); if (epFromFilenameDto != null) { epsToCompare.Add(epFromFilenameDto); } } catch (Exception e) { Log.Warning(e, "Can't create EpisodeForComparingFromDto object from file {a}", file); } } return(epsToCompare); }
public void FindByEpisodeForComparingDto_EpNotInDB_ReturnsNull() { TVShow show = new TVShow() { SeriesId = 328487, SeriesName = "The Orville" }; Episode ep1 = new Episode() { SeriesId = 328487, TVDBEpisodeId = 6089488, Season = 1, AiredEpisodeNumber = 1, EpisodeName = "Old Wounds" }; EpisodeForComparingDto epDto = new EpisodeForComparingDto() { SeriesName = "the chilling adventures of sabrina", SeasonNumber = 1, EpisodeNumberInSeason = 1 }; var options = new DbContextOptionsBuilder <EpisodeContext>() .UseInMemoryDatabase(databaseName: "FindByEpisodeForComparingDto_EpNotInDB_ReturnsNull") .Options; using (var context = new EpisodeContext(options)) { var service = new EpisodeService(context); var TVService = new TVShowService(context); TVService.Add(show); service.Add(ep1); } using (var context = new EpisodeContext(options)) { var service = new EpisodeService(context); Episode result = service.FindByEpisodeForComparingDto(epDto); Assert.Null(result); } }
public string GetSeriesNameInLowercaseAndPeriodsWithoutStartingArticle(EpisodeForComparingDto ep) { return(ep.SeriesName.RemoveFirstWordArticlesFromTitle().ReplaceSpaces().ToLower()); }
public bool FilenameContainsSeriesName(EpisodeForComparingDto ep, string filename) { string seriesNameToCompare = GetSeriesNameInLowercaseAndPeriodsWithoutStartingArticle(ep); return(filename.ToLower().Contains(seriesNameToCompare)); }
public string GetSeasonEpisodeInSEFormat(EpisodeForComparingDto ep) { return($"s{ep.SeasonNumber.ToString("D2")}e{ep.EpisodeNumberInSeason.ToString("D2")}"); }
public bool FilenameContainsSeasonEpisode(EpisodeForComparingDto ep, string fileName) { string seasonEpisode = GetSeasonEpisodeInSEFormat(ep); return(fileName.Contains(seasonEpisode)); }
public bool FilenameMatchesEpisode(EpisodeForComparingDto ep, string filename) { return(FilenameContainsSeriesName(ep, filename) && FilenameContainsSeasonEpisode(ep, filename)); }