2022-09-10 18:58:03 +00:00
|
|
|
using System.Collections.Generic;
|
2022-09-21 21:49:28 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2022-09-22 12:13:53 +00:00
|
|
|
using System.Threading.Tasks;
|
2022-09-10 18:58:03 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2022-09-15 23:45:26 +00:00
|
|
|
using MediaBrowser.Controller.Lyrics;
|
2022-09-18 17:13:01 +00:00
|
|
|
using MediaBrowser.Controller.Resolvers;
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
namespace MediaBrowser.Providers.Lyric;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// TXT Lyric Provider.
|
|
|
|
/// </summary>
|
|
|
|
public class TxtLyricProvider : ILyricProvider
|
2022-09-10 18:58:03 +00:00
|
|
|
{
|
2022-09-17 21:37:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-09-18 16:38:10 +00:00
|
|
|
public string Name => "TxtLyricProvider";
|
2022-09-16 00:49:25 +00:00
|
|
|
|
2022-09-18 17:13:01 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the priority.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
public ResolverPriority Priority => ResolverPriority.Second;
|
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-09-19 01:17:53 +00:00
|
|
|
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc", "txt" };
|
2022-09-16 00:49:25 +00:00
|
|
|
|
2022-09-17 21:37:38 +00: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>
|
2022-09-22 12:13:53 +00:00
|
|
|
public async Task<LyricResponse?> GetLyrics(BaseItem item)
|
2022-09-17 21:37:38 +00:00
|
|
|
{
|
2022-09-20 12:36:43 +00:00
|
|
|
string? lyricFilePath = this.GetLyricFilePath(item.Path);
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
if (string.IsNullOrEmpty(lyricFilePath))
|
2022-09-10 18:58:03 +00:00
|
|
|
{
|
2022-09-17 21:37:38 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-22 13:00:07 +00:00
|
|
|
string[] lyricTextLines = await File.ReadAllLinesAsync(lyricFilePath).ConfigureAwait(false);
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
if (lyricTextLines.Length == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-21 21:49:28 +00:00
|
|
|
LyricLine[] lyricList = new LyricLine[lyricTextLines.Length];
|
2022-09-19 20:26:38 +00:00
|
|
|
|
2022-09-21 21:49:28 +00:00
|
|
|
for (int lyricLine = 0; lyricLine < lyricTextLines.Length; lyricLine++)
|
2022-09-17 21:37:38 +00:00
|
|
|
{
|
2022-09-21 21:49:28 +00:00
|
|
|
lyricList[lyricLine] = new LyricLine(lyricTextLines[lyricLine]);
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
2022-09-17 21:37:38 +00:00
|
|
|
|
|
|
|
return new LyricResponse { Lyrics = lyricList };
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
|
|
|
}
|