jellyfin-server/MediaBrowser.Providers/Lyric/LyricManager.cs

71 lines
1.9 KiB
C#
Raw Normal View History

2022-09-16 00:49:25 +00:00
using System.Collections.Generic;
using System.Linq;
2022-09-22 12:13:53 +00:00
using System.Threading.Tasks;
2022-09-16 00:49:25 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
2022-09-17 21:37:38 +00:00
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// Lyric Manager.
/// </summary>
public class LyricManager : ILyricManager
2022-09-16 00:49:25 +00:00
{
2022-09-17 21:37:38 +00:00
private readonly ILyricProvider[] _lyricProviders;
2023-06-20 14:51:07 +00:00
private readonly ILyricParser[] _lyricParsers;
2022-09-16 00:49:25 +00:00
2022-09-17 21:37:38 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="LyricManager"/> class.
/// </summary>
/// <param name="lyricProviders">All found lyricProviders.</param>
2023-06-20 14:51:07 +00:00
/// <param name="lyricParsers">All found lyricParsers.</param>
public LyricManager(IEnumerable<ILyricProvider> lyricProviders, IEnumerable<ILyricParser> lyricParsers)
2022-09-17 21:37:38 +00:00
{
2022-09-18 17:13:01 +00:00
_lyricProviders = lyricProviders.OrderBy(i => i.Priority).ToArray();
2023-06-20 14:51:07 +00:00
_lyricParsers = lyricParsers.OrderBy(i => i.Priority).ToArray();
2022-09-17 21:37:38 +00:00
}
2022-09-16 00:49:25 +00:00
2022-09-17 21:37:38 +00:00
/// <inheritdoc />
2022-09-22 12:13:53 +00:00
public async Task<LyricResponse?> GetLyrics(BaseItem item)
2022-09-17 21:37:38 +00:00
{
foreach (ILyricProvider provider in _lyricProviders)
2022-09-16 00:49:25 +00:00
{
2023-06-20 14:51:07 +00:00
var lyrics = await provider.GetLyrics(item).ConfigureAwait(false);
if (lyrics is null)
2022-09-16 00:49:25 +00:00
{
2023-06-20 14:51:07 +00:00
continue;
}
foreach (ILyricParser parser in _lyricParsers)
{
var result = parser.ParseLyrics(lyrics);
if (result is not null)
{
return result;
}
2022-09-16 00:49:25 +00:00
}
}
2022-09-17 21:37:38 +00:00
return null;
}
/// <inheritdoc />
public bool HasLyricFile(BaseItem item)
{
foreach (ILyricProvider provider in _lyricProviders)
2022-09-16 00:49:25 +00:00
{
2022-09-17 21:37:38 +00:00
if (item is null)
2022-09-16 00:49:25 +00:00
{
2022-09-17 21:37:38 +00:00
continue;
2022-09-16 00:49:25 +00:00
}
2023-06-20 14:51:07 +00:00
if (provider.HasLyrics(item))
2022-09-17 21:37:38 +00:00
{
return true;
}
2022-09-16 00:49:25 +00:00
}
2022-09-17 21:37:38 +00:00
return false;
2022-09-16 00:49:25 +00:00
}
}