jellyfin/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs

74 lines
2.3 KiB
C#
Raw Normal View History

2022-09-10 20:58:03 +02: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 20:58:03 +02:00
namespace MediaBrowser.Providers.Lyric
2022-09-10 20:58:03 +02:00
{
/// <summary>
/// TXT Lyric Provider.
2022-09-10 20:58:03 +02:00
/// </summary>
2022-09-16 02:49:25 +02:00
public class TxtLyricProvider : ILyricProvider
2022-09-10 20:58:03 +02:00
{
/// <summary>
2022-09-16 02:49:25 +02:00
/// Initializes a new instance of the <see cref="TxtLyricProvider"/> class.
2022-09-10 20:58:03 +02:00
/// </summary>
2022-09-16 02:49:25 +02:00
public TxtLyricProvider()
2022-09-10 20:58:03 +02:00
{
2022-09-16 02:49:25 +02:00
Name = "TxtLyricProvider";
SupportedMediaTypes = new Collection<string>
2022-09-10 20:58:03 +02:00
{
"lrc", "txt"
};
}
2022-09-16 02:49:25 +02:00
/// <summary>
/// Gets a value indicating the provider name.
/// </summary>
public string Name { get; }
2022-09-10 20:58:03 +02:00
/// <summary>
/// Gets a value indicating the File Extenstions this provider supports.
2022-09-10 20:58:03 +02:00
/// </summary>
public IEnumerable<string> SupportedMediaTypes { get; }
2022-09-10 20:58:03 +02:00
/// <summary>
/// Opens lyric file for the requested item, and processes it for API return.
2022-09-10 20:58:03 +02:00
/// </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)
2022-09-10 20:58:03 +02:00
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
2022-09-10 20:58:03 +02:00
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
2022-09-10 20:58:03 +02:00
}
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
2022-09-10 20:58:03 +02:00
string lyricData = System.IO.File.ReadAllText(lyricFilePath);
// Splitting on Environment.NewLine caused some new lines to be missed in Windows.
char[] newLineDelims = new[] { '\r', '\n' };
string[] lyricTextLines = lyricData.Split(newLineDelims, StringSplitOptions.RemoveEmptyEntries);
2022-09-10 20:58:03 +02:00
if (!lyricTextLines.Any())
{
return null;
2022-09-10 20:58:03 +02:00
}
foreach (string lyricTextLine in lyricTextLines)
2022-09-10 20:58:03 +02:00
{
lyricList.Add(new Controller.Lyrics.Lyric { Text = lyricTextLine });
2022-09-10 20:58:03 +02:00
}
return new LyricResponse { Lyrics = lyricList };
2022-09-10 20:58:03 +02:00
}
}
}