Browse Source

Merge pull request #6912 from crobibero/filename-tvmaze

Add additional provider id parsing to file name
Claus Vium 3 years ago
parent
commit
4a58582ad5

+ 22 - 8
Emby.Server.Implementations/Library/PathExtensions.cs

@@ -16,7 +16,7 @@ namespace Emby.Server.Implementations.Library
         /// <param name="attribute">The attrib.</param>
         /// <returns>System.String.</returns>
         /// <exception cref="ArgumentException"><paramref name="str" /> or <paramref name="attribute" /> is empty.</exception>
-        public static string? GetAttributeValue(this string str, string attribute)
+        public static string? GetAttributeValue(this ReadOnlySpan<char> str, ReadOnlySpan<char> attribute)
         {
             if (str.Length == 0)
             {
@@ -28,17 +28,31 @@ 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 attributeIndex = str.IndexOf(attribute, StringComparison.OrdinalIgnoreCase);
+
+            // Must be at least 3 characters after the attribute =, ], any character.
+            var maxIndex = str.Length - attribute.Length - 3;
+            while (attributeIndex > -1 && attributeIndex < maxIndex)
             {
-                start += srch.Length;
-                int end = str.IndexOf(']', start);
-                return str.Substring(start, end - start);
+                var attributeEnd = attributeIndex + attribute.Length;
+                if (attributeIndex > 0
+                    && str[attributeIndex - 1] == '['
+                    && str[attributeEnd] == '=')
+                {
+                    var closingIndex = str[attributeEnd..].IndexOf(']');
+                    // Must be at least 1 character before the closing bracket.
+                    if (closingIndex > 1)
+                    {
+                        return str[(attributeEnd + 1)..(attributeEnd + closingIndex)].Trim().ToString();
+                    }
+                }
+
+                str = str[attributeEnd..];
+                attributeIndex = str.IndexOf(attribute, StringComparison.OrdinalIgnoreCase);
             }
 
             // 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;

+ 1 - 1
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");
 

+ 3 - 3
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))
                     {

+ 33 - 4
Emby.Server.Implementations/Library/Resolvers/TV/SeriesResolver.cs

@@ -185,13 +185,42 @@ namespace Emby.Server.Implementations.Library.Resolvers.TV
         /// <param name="path">The path.</param>
         private static void SetProviderIdFromPath(Series item, string path)
         {
-            var justName = Path.GetFileName(path);
+            var justName = Path.GetFileName(path.AsSpan());
 
-            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);
             }
         }
     }

+ 12 - 0
tests/Jellyfin.Server.Implementations.Tests/Library/PathExtensionsTests.cs

@@ -11,6 +11,18 @@ 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")]
+        [InlineData("[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)]
+        [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));