Use providers in order of priority

pull/8381/head
1hitsong 2 years ago
parent 10b07ed9a5
commit f737581d49

@ -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
/// </summary>
string Name { get; }
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
ResolverPriority Priority { get; }
/// <summary>
/// Gets the supported media types for this provider.
/// </summary>

@ -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;
/// </summary>
public class LrcLyricProvider : ILyricProvider
{
private readonly ILogger<LrcLyricProvider> _logger;
/// <summary>
/// Initializes a new instance of the <see cref="LrcLyricProvider"/> class.
/// </summary>
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
public LrcLyricProvider(ILogger<LrcLyricProvider> logger)
{
_logger = logger;
}
/// <inheritdoc />
public string Name => "LrcLyricProvider";
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.First;
/// <inheritdoc />
public IEnumerable<string> SupportedMediaTypes { get; } = new[] { "lrc" };
@ -38,7 +55,7 @@ public class LrcLyricProvider : ILyricProvider
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
IDictionary<string, string> fileMetaData = new Dictionary<string, string>();
IDictionary<string, string> fileMetaData = new Dictionary<string, string>(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;
}
}

@ -18,7 +18,7 @@ public class LyricManager : ILyricManager
/// <param name="lyricProviders">All found lyricProviders.</param>
public LyricManager(IEnumerable<ILyricProvider> lyricProviders)
{
_lyricProviders = lyricProviders.ToArray();
_lyricProviders = lyricProviders.OrderBy(i => i.Priority).ToArray();
}
/// <inheritdoc />

@ -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
/// <inheritdoc />
public string Name => "TxtLyricProvider";
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.Second;
/// <inheritdoc />
public IEnumerable<string> SupportedMediaTypes { get; } = new[] { "lrc", "txt" };

Loading…
Cancel
Save