From f737581d49dd9f6ab0c68269f8be073df27fb0ba Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Sun, 18 Sep 2022 13:13:01 -0400 Subject: [PATCH] Use providers in order of priority --- .../Lyrics/ILyricProvider.cs | 7 +++ .../Lyric/LrcLyricProvider.cs | 48 ++++++++++++------- MediaBrowser.Providers/Lyric/LyricManager.cs | 2 +- .../Lyric/TxtLyricProvider.cs | 7 +++ 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/MediaBrowser.Controller/Lyrics/ILyricProvider.cs b/MediaBrowser.Controller/Lyrics/ILyricProvider.cs index 1b52de255f..651fe507f9 100644 --- a/MediaBrowser.Controller/Lyrics/ILyricProvider.cs +++ b/MediaBrowser.Controller/Lyrics/ILyricProvider.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Resolvers; namespace MediaBrowser.Controller.Lyrics; @@ -13,6 +14,12 @@ public interface ILyricProvider /// string Name { get; } + /// + /// Gets the priority. + /// + /// The priority. + ResolverPriority Priority { get; } + /// /// Gets the supported media types for this provider. /// diff --git a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs index 90396e5532..4690c3e209 100644 --- a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs +++ b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs @@ -1,12 +1,12 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Linq; using LrcParser.Model; using LrcParser.Parser; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Lyrics; -using Newtonsoft.Json.Linq; +using MediaBrowser.Controller.Resolvers; +using Microsoft.Extensions.Logging; namespace MediaBrowser.Providers.Lyric; @@ -15,9 +15,26 @@ namespace MediaBrowser.Providers.Lyric; /// public class LrcLyricProvider : ILyricProvider { + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// Instance of the interface. + public LrcLyricProvider(ILogger logger) + { + _logger = logger; + } + /// public string Name => "LrcLyricProvider"; + /// + /// Gets the priority. + /// + /// The priority. + public ResolverPriority Priority => ResolverPriority.First; + /// public IEnumerable SupportedMediaTypes { get; } = new[] { "lrc" }; @@ -38,7 +55,7 @@ public class LrcLyricProvider : ILyricProvider List lyricList = new List(); List sortedLyricData = new List(); - IDictionary fileMetaData = new Dictionary(); + IDictionary fileMetaData = new Dictionary(StringComparer.OrdinalIgnoreCase); string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath); try @@ -63,15 +80,15 @@ public class LrcLyricProvider : ILyricProvider continue; } - string metaDataFieldName = metaDataField[0][1..].Trim().ToLowerInvariant(); + string metaDataFieldName = metaDataField[0][1..].Trim(); string metaDataFieldValue = metaDataField[1][..^1].Trim(); fileMetaData.Add(metaDataFieldName, metaDataFieldValue); } } - catch + catch (Exception ex) { - return null; + _logger.LogError(ex, "Error parsing lyric data from {Provider}", Name); } if (sortedLyricData.Count == 0) @@ -111,52 +128,51 @@ public class LrcLyricProvider : ILyricProvider { LyricMetadata lyricMetadata = new LyricMetadata(); - if (metaData.TryGetValue("ar", out var artist) && artist is not null) + if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist)) { lyricMetadata.Artist = artist; } - if (metaData.TryGetValue("al", out var album) && album is not null) + if (metaData.TryGetValue("al", out var album) && !string.IsNullOrEmpty(album)) { lyricMetadata.Album = album; } - if (metaData.TryGetValue("ti", out var title) && title is not null) + if (metaData.TryGetValue("ti", out var title) && !string.IsNullOrEmpty(title)) { lyricMetadata.Title = title; } - if (metaData.TryGetValue("au", out var author) && author is not null) + if (metaData.TryGetValue("au", out var author) && !string.IsNullOrEmpty(author)) { lyricMetadata.Author = author; } - if (metaData.TryGetValue("length", out var length) && length is not null) + if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length)) { lyricMetadata.Length = length; } - if (metaData.TryGetValue("by", out var by) && by is not null) + if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by)) { lyricMetadata.By = by; } - if (metaData.TryGetValue("offset", out var offset) && offset is not null) + if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset)) { lyricMetadata.Offset = offset; } - if (metaData.TryGetValue("re", out var creator) && creator is not null) + if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator)) { lyricMetadata.Creator = creator; } - if (metaData.TryGetValue("ve", out var version) && version is not null) + if (metaData.TryGetValue("ve", out var version) && !string.IsNullOrEmpty(version)) { lyricMetadata.Version = version; } return lyricMetadata; - } } diff --git a/MediaBrowser.Providers/Lyric/LyricManager.cs b/MediaBrowser.Providers/Lyric/LyricManager.cs index 0de008db7f..7487c68615 100644 --- a/MediaBrowser.Providers/Lyric/LyricManager.cs +++ b/MediaBrowser.Providers/Lyric/LyricManager.cs @@ -18,7 +18,7 @@ public class LyricManager : ILyricManager /// All found lyricProviders. public LyricManager(IEnumerable lyricProviders) { - _lyricProviders = lyricProviders.ToArray(); + _lyricProviders = lyricProviders.OrderBy(i => i.Priority).ToArray(); } /// diff --git a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs index a2161c427f..c690086862 100644 --- a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs +++ b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs @@ -4,6 +4,7 @@ using System.Collections.ObjectModel; using System.Linq; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Lyrics; +using MediaBrowser.Controller.Resolvers; namespace MediaBrowser.Providers.Lyric; @@ -15,6 +16,12 @@ public class TxtLyricProvider : ILyricProvider /// public string Name => "TxtLyricProvider"; + /// + /// Gets the priority. + /// + /// The priority. + public ResolverPriority Priority => ResolverPriority.Second; + /// public IEnumerable SupportedMediaTypes { get; } = new[] { "lrc", "txt" };