jellyfin/MediaBrowser.Providers/Lyric/TxtLyricsProvider.cs

75 lines
2.5 KiB
C#
Raw Normal View History

2022-09-10 11:58:03 -07:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Api.Helpers;
2022-09-10 11:58:03 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
2022-09-10 11:58:03 -07:00
namespace MediaBrowser.Providers.Lyric
2022-09-10 11:58:03 -07:00
{
/// <summary>
/// TXT File Lyric Provider.
/// </summary>
public class TxtLyricsProvider : ILyricsProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="TxtLyricsProvider"/> class.
/// </summary>
public TxtLyricsProvider()
{
SupportedMediaTypes = new Collection<string>
2022-09-10 11:58:03 -07:00
{
"lrc", "txt"
};
}
/// <summary>
/// Gets a value indicating the File Extenstions this provider works with.
/// </summary>
public IEnumerable<string> SupportedMediaTypes { get; }
2022-09-10 11:58:03 -07:00
/// <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)
2022-09-10 11:58:03 -07:00
{
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
2022-09-10 11:58:03 -07:00
if (string.IsNullOrEmpty(lyricFilePath))
{
return null;
2022-09-10 11:58:03 -07:00
}
List<MediaBrowser.Controller.Lyrics.Lyric> lyricsList = new List<MediaBrowser.Controller.Lyrics.Lyric>();
2022-09-10 11:58:03 -07: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);
if (!lyricTextLines.Any())
{
return null;
2022-09-10 11:58:03 -07:00
}
foreach (string lyricLine in lyricTextLines)
{
lyricsList.Add(new MediaBrowser.Controller.Lyrics.Lyric { Text = lyricLine });
2022-09-10 11:58:03 -07:00
}
return new LyricResponse { Lyrics = lyricsList };
2022-09-10 11:58:03 -07:00
}
}
}