FF-1484 - fixing tv show lookup

This commit is contained in:
John Andrews
2024-04-12 20:32:50 +12:00
parent be94cfd7d6
commit b9b60aff2b
7 changed files with 89 additions and 31 deletions

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>FileFlows.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>

Binary file not shown.

Binary file not shown.

View File

@@ -14,4 +14,35 @@ internal static class ExtensionMethods
{
return str == string.Empty ? null : str;
}
/// <summary>
/// Trims the specified characters from the beginning and end of the string.
/// </summary>
/// <param name="input">The input string to trim.</param>
/// <param name="charsToTrim">The characters to trim from the string.</param>
/// <returns>A new string that has the specified characters removed from the beginning and end.</returns>
/// <exception cref="ArgumentNullException">Thrown when input is null.</exception>
public static string TrimExtra(this string input, string charsToTrim)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
if (string.IsNullOrEmpty(charsToTrim))
return input.Trim();
int startIndex = 0;
int endIndex = input.Length - 1;
while (startIndex <= endIndex && (input[startIndex] == ' ' || charsToTrim.Contains(input[startIndex])))
{
startIndex++;
}
while (endIndex >= startIndex && (input[endIndex] == ' ' || charsToTrim.Contains(input[endIndex])))
{
endIndex--;
}
return input.Substring(startIndex, endIndex - startIndex + 1);
}
}

View File

@@ -180,7 +180,43 @@ public class TVEpisodeLookupTests
string nfo = (string)args.Variables["NFO"];
}
[TestMethod]
public void VariousTests()
{
var logger = new TestLogger();
foreach (var test in new TVLookupTestData[]
{
new("See",
"/media/TV/See (2019) [tvdbid-361565]/Season 02/S02E06 - The Truth About Unicorns ATVP WEBDL-2160pDV]EAC3 Atmos 5.1h265]-MP4.mp4"),
new("The Walking Dead",
"/media/TV/The Walking Dead (2010) [tvdbid-153021]/Season 07/S07E04 - Service HDTV-1080pAAC 5.1]h265mkv"),
new("Teenage Mutant Ninja Turtles",
"/media/Anime/Teenage Mutant Ninja Turtles (1987) [tvdbid-74582]/Season 04/S04E11 - 089 - Menace Maestro Please SDTV10bit]x264AC3 2.0].mkv"),
new("Yowamushi Pedal",
"/media/Anime/Yowamushi Pedal (2013) [tvdbid-272309]/Season 03/S03E12 - 074 - Trouble! HDTV-1080p8bit]x264Opus 2.0]JAmkv"),
new("Law & Order - Special Victims Unit",
"/media/TV/Law & Order - Special Victims Unit (1999) [tvdbid-75692]/Season 07/S07E04 - Ripped AMZN WEBRip-1080p ProperEAC3 5.1]x264NTb.mkv"),
new("X-Men '97", "/media/tv/X-Men '97 (2024) [tvdb-3423432]/xmen97.3x03.mkv"),
new("Phantom 2040", "/media/tv/Phantom 2040 (1992)/Phantom 2040.1x03.mkv"),
})
{
(string lookupName, string year) = TVShowLookup.GetLookupName(test.Path, true);
Assert.AreEqual(test.Show, lookupName);
//
// var args = new FileFlows.Plugin.NodeParameters(test.Path, logger, false, string.Empty, null);
//
// var element = new TVEpisodeLookup();
//
// var result = element.Execute(args);
// Assert.AreEqual(1, result);
//
// Assert.AreEqual(test.Show, args.Variables["tvepisode.Title"]);
}
}
private record TVLookupTestData(string Show, string Path);
}
#endif

View File

@@ -70,7 +70,7 @@ public class TVEpisodeLookup : Node
(string lookupName, string year) = TVShowLookup.GetLookupName(filename, false);
(string showName, int? season, int? episode, int? lastEpisode) = TVShowLookup.GetTVShowInfo(filename);
(string showName, int? season, int? episode, int? lastEpisode, string year2) = TVShowLookup.GetTVShowInfo(filename);
if (season == null)
{
@@ -103,6 +103,10 @@ public class TVEpisodeLookup : Node
{
return year == x.FirstAirDate.Year.ToString() ? 0 : 1;
}
if (string.IsNullOrEmpty(year2) == false)
{
return year2 == x.FirstAirDate.Year.ToString() ? 0 : 1;
}
return 0;
})
.ThenBy(x => x.Name.ToLower().Trim().Replace(" ", "") == lookupName.ToLower().Trim().Replace(" ", "") ? 0 : 1)

View File

@@ -132,25 +132,11 @@ public class TVShowLookup : Node
}
else
{
lookupName = fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf(fileInfo.Extension));
lookupName = fileInfo.Name[..fileInfo.Name.LastIndexOf(fileInfo.Extension, StringComparison.Ordinal)];
}
lookupName = GetTVShowInfo(lookupName).ShowName;
lookupName = lookupName.Replace(".", " ").Replace("_", " ");
// look for year
string year = string.Empty;
var match = Regex.Matches(lookupName, @"((19[2-9][0-9])|(20[0-9]{2}))(?=([\.\s_\-\)\]]|$))").LastOrDefault();
if (match != null)
{
year = match.Value;
lookupName = lookupName.Substring(0, lookupName.IndexOf(year)).Trim();
}
// remove double spaces in case they were added when removing the year
while (lookupName.IndexOf(" ", StringComparison.Ordinal) > 0)
lookupName = lookupName.Replace(" ", " ");
return (lookupName, year);
var result = GetTVShowInfo(lookupName);
return (result.ShowName, result.Year);
}
@@ -199,18 +185,18 @@ public class TVShowLookup : Node
/// </summary>
/// <param name="text">the input text</param>
/// <returns>the tv show name</returns>
internal static (string ShowName, int? Season, int? Episode, int? LastEpisode) GetTVShowInfo(string text)
internal static (string ShowName, int? Season, int? Episode, int? LastEpisode, string? Year) GetTVShowInfo(string text)
{
// Replace "1x02" format with "s1e02"
text = Regex.Replace(text, @"(?<season>\d+)x(?<episode>\d+)", "s${season}e${episode}", RegexOptions.IgnoreCase);
string year = null;
var reYear = Regex.Match(text, @"(19|20)[\d]{2}", RegexOptions.CultureInvariant);
var reYear = Regex.Match(text, @"\((19|20)[\d]{2}\)", RegexOptions.CultureInvariant);
if (reYear.Success)
{
year = reYear.Value;
text = text.Replace("(" + year + ")", string.Empty);
text = text.Replace(year, string.Empty);
year = year[1..^1]; // remove the ()
}
string pattern = @"^(?<showName>[\w\s.-]+)[. _-]?(?:(s|S)(?<season>\d+)(e|E)(?<episode>\d+)(?:-(?<lastEpisode>\d+))?)";
@@ -218,19 +204,20 @@ public class TVShowLookup : Node
Match match = Regex.Match(text, pattern);
if (match.Success == false)
return (text, null, null, null);
{
text = Regex.Replace(text, @"\[[^\]]+\]", string.Empty);
text = text.TrimExtra("-");
return (text, null, null, null, year);
}
string show = match.Groups["showName"].Value.Replace(".", " ").TrimEnd();
if (show.EndsWith(" -"))
show = show[..^2];
show = Regex.Replace(show, @"\[[^\]]+", string.Empty);
show = show.TrimExtra("-");
int season = int.Parse(match.Groups["season"].Value);
int episode = int.Parse(match.Groups["episode"].Value);
string lastEpisodeStr = match.Groups["lastEpisode"].Value;
int? lastEpisode = string.IsNullOrEmpty(lastEpisodeStr) ? (int?)null : int.Parse(lastEpisodeStr);
if (year != null)
show += " (" + year + ")";
return (show, season, episode, lastEpisode);
return (show, season, episode, lastEpisode, year);
}
}