jellyfin/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs

56 lines
1.6 KiB
C#
Raw Normal View History

2022-09-10 20:58:03 +02:00
using System.Collections.Generic;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
2022-09-18 19:13:01 +02:00
using MediaBrowser.Controller.Resolvers;
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
namespace MediaBrowser.Providers.Lyric;
/// <summary>
/// TXT Lyric Provider.
/// </summary>
public class TxtLyricProvider : ILyricProvider
2022-09-10 20:58:03 +02:00
{
2022-09-17 23:37:38 +02:00
/// <inheritdoc />
public string Name => "TxtLyricProvider";
2022-09-16 02:49:25 +02:00
2022-09-18 19:13:01 +02:00
/// <summary>
/// Gets the priority.
/// </summary>
/// <value>The priority.</value>
public ResolverPriority Priority => ResolverPriority.Second;
2022-09-17 23:37:38 +02:00
/// <inheritdoc />
2022-09-19 03:17:53 +02:00
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc", "txt" };
2022-09-16 02:49:25 +02:00
2022-09-17 23:37:38 +02:00
/// <summary>
/// Opens lyric file for the requested item, and processes it for API return.
/// </summary>
/// <param name="item">The item to to process.</param>
/// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/>; otherwise, null.</returns>
public LyricResponse? GetLyrics(BaseItem item)
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
if (string.IsNullOrEmpty(lyricFilePath))
2022-09-10 20:58:03 +02:00
{
2022-09-17 23:37:38 +02:00
return null;
}
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath);
2022-09-10 20:58:03 +02:00
2022-09-19 03:17:53 +02:00
List<LyricLine> lyricList = new();
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
if (lyricTextLines.Length == 0)
{
return null;
}
2022-09-10 20:58:03 +02:00
2022-09-17 23:37:38 +02:00
foreach (string lyricTextLine in lyricTextLines)
{
2022-09-18 22:05:50 +02:00
lyricList.Add(new LyricLine(lyricTextLine));
2022-09-10 20:58:03 +02:00
}
2022-09-17 23:37:38 +02:00
return new LyricResponse { Lyrics = lyricList };
2022-09-10 20:58:03 +02:00
}
}