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

52 lines
1.5 KiB
C#
Raw Normal View History

2022-09-10 18:58:03 +00:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
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 />
public string Name => "TxtLyricProvider";
2022-09-16 00:49:25 +00:00
2022-09-17 21:37:38 +00:00
/// <inheritdoc />
public IEnumerable<string> SupportedMediaTypes { get; } = new[] { "lrc", "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>
public LyricResponse? GetLyrics(BaseItem item)
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, 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-17 21:37:38 +00:00
string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath);
2022-09-10 18:58:03 +00:00
2022-09-17 21:37:38 +00:00
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
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-17 21:37:38 +00:00
foreach (string lyricTextLine in lyricTextLines)
{
lyricList.Add(new Controller.Lyrics.Lyric(lyricTextLine));
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
}
}