jellyfin/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs
2022-09-15 20:49:25 -04:00

83 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Jellyfin.Api.Helpers;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
namespace MediaBrowser.Providers.Lyric
{
/// <summary>
/// TXT File Lyric Provider.
/// </summary>
public class TxtLyricProvider : ILyricProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="TxtLyricProvider"/> class.
/// </summary>
public TxtLyricProvider()
{
Name = "TxtLyricProvider";
SupportedMediaTypes = new Collection<string>
{
"lrc", "txt"
};
}
/// <summary>
/// Gets a value indicating the provider name.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets a value indicating the File Extenstions this provider works with.
/// </summary>
public IEnumerable<string> SupportedMediaTypes { get; }
/// <summary>
/// Gets or Sets Data object generated by Process() method.
/// </summary>
/// <returns><c>Object</c> with data if no error occured; otherwise, <c>null</c>.</returns>
public object? Data { get; set; }
/// <summary>
/// Opens lyric file for [the specified item], and processes it for API return.
/// </summary>
/// <param name="item">The item to to process.</param>
/// <returns><placeholder>A <see cref="Task"/> representing the asynchronous operation.</placeholder></returns>
public LyricResponse? GetLyrics(BaseItem item)
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
}
List<MediaBrowser.Controller.Lyrics.Lyric> lyricsList = new List<MediaBrowser.Controller.Lyrics.Lyric>();
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);
if (!lyricTextLines.Any())
{
return null;
}
foreach (string lyricLine in lyricTextLines)
{
lyricsList.Add(new MediaBrowser.Controller.Lyrics.Lyric { Text = lyricLine });
}
return new LyricResponse { Lyrics = lyricsList };
}
}
}