From 4890454935f6c56ff4d2fab0b71646311319c0ac Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Sat, 27 Nov 2021 07:28:58 -0700 Subject: [PATCH 1/9] Add additional provider parsing to series file name --- .../Library/Resolvers/TV/SeriesResolver.cs | 35 +++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs b/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs index 4e15acd182..64272fde1a 100644 --- a/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs +++ b/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs @@ -187,11 +187,40 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV { var justName = Path.GetFileName(path); - var id = justName.GetAttributeValue("tvdbid"); + var tvdbId = justName.GetAttributeValue("tvdbid"); + if (!string.IsNullOrEmpty(tvdbId)) + { + item.SetProviderId(MetadataProvider.Tvdb, tvdbId); + } + + var tvmazeId = justName.GetAttributeValue("tvmazeid"); + if (!string.IsNullOrEmpty(tvmazeId)) + { + item.SetProviderId(MetadataProvider.TvMaze, tvmazeId); + } + + var tmdbId = justName.GetAttributeValue("tmdbid"); + if (!string.IsNullOrEmpty(tmdbId)) + { + item.SetProviderId(MetadataProvider.Tmdb, tmdbId); + } + + var anidbId = justName.GetAttributeValue("anidbid"); + if (!string.IsNullOrEmpty(anidbId)) + { + item.SetProviderId("AniDB", anidbId); + } + + var aniListId = justName.GetAttributeValue("anilistid"); + if (!string.IsNullOrEmpty(aniListId)) + { + item.SetProviderId("AniList", aniListId); + } - if (!string.IsNullOrEmpty(id)) + var aniSearchId = justName.GetAttributeValue("anisearchid"); + if (!string.IsNullOrEmpty(aniSearchId)) { - item.SetProviderId(MetadataProvider.Tvdb, id); + item.SetProviderId("AniSearch", aniSearchId); } } } From 065d3fa837bf5d66d4b4e5382257b4ce76b9d0b3 Mon Sep 17 00:00:00 2001 From: Claus Vium Date: Sat, 27 Nov 2021 16:10:43 -0700 Subject: [PATCH 2/9] performance++ --- .../Library/PathExtensions.cs | 23 ++++++++++++------- .../Resolvers/Movies/BoxSetResolver.cs | 2 +- .../Library/Resolvers/Movies/MovieResolver.cs | 6 ++--- .../Library/Resolvers/TV/SeriesResolver.cs | 2 +- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs index d5b855cdf2..8ce054c38c 100644 --- a/Emby.Server.Implementations/Library/PathExtensions.cs +++ b/Emby.Server.Implementations/Library/PathExtensions.cs @@ -16,7 +16,7 @@ namespace Emby.Server.Implementations.Library /// The attrib. /// System.String. /// or is empty. - public static string? GetAttributeValue(this string str, string attribute) + public static string? GetAttributeValue(this ReadOnlySpan str, ReadOnlySpan attribute) { if (str.Length == 0) { @@ -28,17 +28,24 @@ namespace Emby.Server.Implementations.Library throw new ArgumentException("String can't be empty.", nameof(attribute)); } - string srch = "[" + attribute + "="; - int start = str.IndexOf(srch, StringComparison.OrdinalIgnoreCase); - if (start != -1) + var openBracketIndex = str.IndexOf('['); + var equalsIndex = str.IndexOf('='); + var closingBracketIndex = str.IndexOf(']'); + while (openBracketIndex < equalsIndex && equalsIndex < closingBracketIndex) { - start += srch.Length; - int end = str.IndexOf(']', start); - return str.Substring(start, end - start); + if (str[(openBracketIndex + 1)..equalsIndex].Equals(attribute, StringComparison.OrdinalIgnoreCase)) + { + return str[(equalsIndex + 1)..closingBracketIndex].Trim().ToString(); + } + + str = str[(closingBracketIndex+ 1)..]; + openBracketIndex = str.IndexOf('['); + equalsIndex = str.IndexOf('='); + closingBracketIndex = str.IndexOf(']'); } // for imdbid we also accept pattern matching - if (string.Equals(attribute, "imdbid", StringComparison.OrdinalIgnoreCase)) + if (attribute.Equals("imdbid", StringComparison.OrdinalIgnoreCase)) { var match = ProviderIdParsers.TryFindImdbId(str, out var imdbId); return match ? imdbId.ToString() : null; diff --git a/Emby.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs b/Emby.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs index e7abe1e6da..6cc04ea810 100644 --- a/Emby.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs +++ b/Emby.Server.Implementations/Library/Resolvers/Movies/BoxSetResolver.cs @@ -65,7 +65,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.Movies private static void SetProviderIdFromPath(BaseItem item) { // we need to only look at the name of this actual item (not parents) - var justName = Path.GetFileName(item.Path); + var justName = Path.GetFileName(item.Path.AsSpan()); var id = justName.GetAttributeValue("tmdbid"); diff --git a/Emby.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs b/Emby.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs index 732be0fe5c..ddd3fae882 100644 --- a/Emby.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs +++ b/Emby.Server.Implementations/Library/Resolvers/Movies/MovieResolver.cs @@ -342,9 +342,9 @@ namespace Emby.Server.Implementations.Library.Resolvers.Movies if (item is Movie || item is MusicVideo) { // We need to only look at the name of this actual item (not parents) - var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path) : Path.GetFileName(item.ContainingFolderPath); + var justName = item.IsInMixedFolder ? Path.GetFileName(item.Path.AsSpan()) : Path.GetFileName(item.ContainingFolderPath.AsSpan()); - if (!string.IsNullOrEmpty(justName)) + if (!justName.IsEmpty) { // check for tmdb id var tmdbid = justName.GetAttributeValue("tmdbid"); @@ -358,7 +358,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.Movies if (!string.IsNullOrEmpty(item.Path)) { // check for imdb id - we use full media path, as we can assume, that this will match in any use case (wither id in parent dir or in file name) - var imdbid = item.Path.GetAttributeValue("imdbid"); + var imdbid = item.Path.AsSpan().GetAttributeValue("imdbid"); if (!string.IsNullOrWhiteSpace(imdbid)) { diff --git a/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs b/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs index 64272fde1a..46e36847dc 100644 --- a/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs +++ b/Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs @@ -185,7 +185,7 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV /// The path. private static void SetProviderIdFromPath(Series item, string path) { - var justName = Path.GetFileName(path); + var justName = Path.GetFileName(path.AsSpan()); var tvdbId = justName.GetAttributeValue("tvdbid"); if (!string.IsNullOrEmpty(tvdbId)) From 1df56335eec0430bef0d4dface75f69691134c10 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Sat, 27 Nov 2021 16:25:21 -0700 Subject: [PATCH 3/9] Add more tests --- .../Library/PathExtensionsTests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs index c5cc056f5d..d6cbe96474 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs @@ -11,6 +11,9 @@ namespace Jellyfin.Server.Implementations.Tests.Library [InlineData("Superman: Red Son - tt10985510", "imdbid", "tt10985510")] [InlineData("Superman: Red Son", "imdbid", null)] [InlineData("Superman: Red Son", "something", null)] + [InlineData("Superman: Red Son [imdbid1=tt11111111][imdbid=tt10985510]", "imdbid", "tt10985510")] + [InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "imdbid", "tt10985510")] + [InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "tmdbid", "618355")] public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? expectedResult) { Assert.Equal(expectedResult, PathExtensions.GetAttributeValue(input, attribute)); From 3513f5a84bf21c64b5f909352e260bda2e9ab057 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Tue, 7 Dec 2021 17:10:27 -0700 Subject: [PATCH 4/9] Search for attribute text --- .../Library/PathExtensions.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs index 8ce054c38c..73a658186a 100644 --- a/Emby.Server.Implementations/Library/PathExtensions.cs +++ b/Emby.Server.Implementations/Library/PathExtensions.cs @@ -29,18 +29,19 @@ namespace Emby.Server.Implementations.Library } var openBracketIndex = str.IndexOf('['); - var equalsIndex = str.IndexOf('='); + var attributeIndex = str.IndexOf(attribute); var closingBracketIndex = str.IndexOf(']'); - while (openBracketIndex < equalsIndex && equalsIndex < closingBracketIndex) + while (openBracketIndex < attributeIndex && attributeIndex < closingBracketIndex) { - if (str[(openBracketIndex + 1)..equalsIndex].Equals(attribute, StringComparison.OrdinalIgnoreCase)) + if (openBracketIndex + 1 == attributeIndex + && str[attributeIndex + attribute.Length] == '=') { - return str[(equalsIndex + 1)..closingBracketIndex].Trim().ToString(); + return str[(attributeIndex + attribute.Length + 1)..closingBracketIndex].Trim().ToString(); } - str = str[(closingBracketIndex+ 1)..]; + str = str[(closingBracketIndex + 1)..]; openBracketIndex = str.IndexOf('['); - equalsIndex = str.IndexOf('='); + attributeIndex = str.IndexOf(attribute); closingBracketIndex = str.IndexOf(']'); } From 593b2fd359de378a2588a8d54d11ebc0bb2cd3f1 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Thu, 9 Dec 2021 08:06:06 -0700 Subject: [PATCH 5/9] Add more speed and more tests --- .../Library/PathExtensions.cs | 23 +++++++++++-------- .../Library/PathExtensionsTests.cs | 8 +++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs index 73a658186a..78850c149e 100644 --- a/Emby.Server.Implementations/Library/PathExtensions.cs +++ b/Emby.Server.Implementations/Library/PathExtensions.cs @@ -28,21 +28,26 @@ namespace Emby.Server.Implementations.Library throw new ArgumentException("String can't be empty.", nameof(attribute)); } - var openBracketIndex = str.IndexOf('['); var attributeIndex = str.IndexOf(attribute); - var closingBracketIndex = str.IndexOf(']'); - while (openBracketIndex < attributeIndex && attributeIndex < closingBracketIndex) + + // Must be at least 3 characters after the attribute =, ], any character. + var maxIndex = str.Length - attribute.Length - 3; + while (attributeIndex > -1 && attributeIndex < maxIndex) { - if (openBracketIndex + 1 == attributeIndex - && str[attributeIndex + attribute.Length] == '=') + var attributeEnd = attributeIndex + attribute.Length; + if (attributeIndex > 0 + && str[attributeIndex - 1] == '[' + && str[attributeEnd] == '=') { - return str[(attributeIndex + attribute.Length + 1)..closingBracketIndex].Trim().ToString(); + var closingIndex = str[attributeEnd..].IndexOf(']'); + if (closingIndex != -1) + { + return str[(attributeEnd + 1)..(attributeEnd + closingIndex)].Trim().ToString(); + } } - str = str[(closingBracketIndex + 1)..]; - openBracketIndex = str.IndexOf('['); + str = str[attributeEnd..]; attributeIndex = str.IndexOf(attribute); - closingBracketIndex = str.IndexOf(']'); } // for imdbid we also accept pattern matching diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs index d6cbe96474..950f0e76d5 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs @@ -14,6 +14,14 @@ namespace Jellyfin.Server.Implementations.Tests.Library [InlineData("Superman: Red Son [imdbid1=tt11111111][imdbid=tt10985510]", "imdbid", "tt10985510")] [InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "imdbid", "tt10985510")] [InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "tmdbid", "618355")] + [InlineData("[tmdbid=618355]", "tmdbid", "618355")] + [InlineData("tmdbid=618355][tmdbid=618355]", "tmdbid", "618355")] + [InlineData("[tmdbid=618355]tmdbid=618355]", "tmdbid", "618355")] + [InlineData("tmdbid=618355]", "tmdbid", null)] + [InlineData("[tmdbid=618355", "tmdbid", null)] + [InlineData("tmdbid=618355", "tmdbid", null)] + [InlineData("tmdbid=", "tmdbid", null)] + [InlineData("tmdbid", "tmdbid", null)] public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? expectedResult) { Assert.Equal(expectedResult, PathExtensions.GetAttributeValue(input, attribute)); From eeb8192602627e912b491f3e7e6696ce415f3ce3 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Thu, 9 Dec 2021 08:38:00 -0700 Subject: [PATCH 6/9] Add StringComparison --- Emby.Server.Implementations/Library/PathExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs index 78850c149e..3fcbb4d65f 100644 --- a/Emby.Server.Implementations/Library/PathExtensions.cs +++ b/Emby.Server.Implementations/Library/PathExtensions.cs @@ -28,7 +28,7 @@ namespace Emby.Server.Implementations.Library throw new ArgumentException("String can't be empty.", nameof(attribute)); } - var attributeIndex = str.IndexOf(attribute); + var attributeIndex = str.IndexOf(attribute, StringComparison.OrdinalIgnoreCase); // Must be at least 3 characters after the attribute =, ], any character. var maxIndex = str.Length - attribute.Length - 3; @@ -47,7 +47,7 @@ namespace Emby.Server.Implementations.Library } str = str[attributeEnd..]; - attributeIndex = str.IndexOf(attribute); + attributeIndex = str.IndexOf(attribute, StringComparison.OrdinalIgnoreCase); } // for imdbid we also accept pattern matching From d707a201c9568df374579d6dde56d8068a070da0 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Thu, 9 Dec 2021 12:31:32 -0700 Subject: [PATCH 7/9] update tests --- .../Library/PathExtensionsTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs index 950f0e76d5..295a3c83e1 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs @@ -15,8 +15,8 @@ namespace Jellyfin.Server.Implementations.Tests.Library [InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "imdbid", "tt10985510")] [InlineData("Superman: Red Son [tmdbid=618355][imdbid=tt10985510]", "tmdbid", "618355")] [InlineData("[tmdbid=618355]", "tmdbid", "618355")] - [InlineData("tmdbid=618355][tmdbid=618355]", "tmdbid", "618355")] - [InlineData("[tmdbid=618355]tmdbid=618355]", "tmdbid", "618355")] + [InlineData("tmdbid=111111][tmdbid=618355]", "tmdbid", "618355")] + [InlineData("[tmdbid=618355]tmdbid=111111]", "tmdbid", "618355")] [InlineData("tmdbid=618355]", "tmdbid", null)] [InlineData("[tmdbid=618355", "tmdbid", null)] [InlineData("tmdbid=618355", "tmdbid", null)] From 58f788e8abfce620db5a9b70165ff6cbe0931a46 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Sat, 11 Dec 2021 19:43:01 -0700 Subject: [PATCH 8/9] Update tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs Co-authored-by: Bond-009 --- .../Library/PathExtensionsTests.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs index 295a3c83e1..54a63a5f2f 100644 --- a/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs +++ b/tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs @@ -22,6 +22,7 @@ namespace Jellyfin.Server.Implementations.Tests.Library [InlineData("tmdbid=618355", "tmdbid", null)] [InlineData("tmdbid=", "tmdbid", null)] [InlineData("tmdbid", "tmdbid", null)] + [InlineData("[tmdbid=][imdbid=tt10985510]", "tmdbid", null)] public void GetAttributeValue_ValidArgs_Correct(string input, string attribute, string? expectedResult) { Assert.Equal(expectedResult, PathExtensions.GetAttributeValue(input, attribute)); From a04f8f5efb3f067de2ce27e8804aab5c950ac284 Mon Sep 17 00:00:00 2001 From: Cody Robibero Date: Sat, 11 Dec 2021 20:35:43 -0700 Subject: [PATCH 9/9] Fix new test --- Emby.Server.Implementations/Library/PathExtensions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Emby.Server.Implementations/Library/PathExtensions.cs b/Emby.Server.Implementations/Library/PathExtensions.cs index 3fcbb4d65f..6f61dc7135 100644 --- a/Emby.Server.Implementations/Library/PathExtensions.cs +++ b/Emby.Server.Implementations/Library/PathExtensions.cs @@ -40,7 +40,8 @@ namespace Emby.Server.Implementations.Library && str[attributeEnd] == '=') { var closingIndex = str[attributeEnd..].IndexOf(']'); - if (closingIndex != -1) + // Must be at least 1 character before the closing bracket. + if (closingIndex > 1) { return str[(attributeEnd + 1)..(attributeEnd + closingIndex)].Trim().ToString(); }