mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-11-16 10:29:01 -07:00
75 lines
2.5 KiB
C#
75 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Jellyfin.Api.Helpers;
|
|
using MediaBrowser.Controller.Entities;
|
|
using MediaBrowser.Controller.Lyrics;
|
|
|
|
namespace MediaBrowser.Providers.Lyric
|
|
{
|
|
/// <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>
|
|
{
|
|
"lrc", "txt"
|
|
};
|
|
}
|
|
|
|
/// <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 };
|
|
}
|
|
}
|
|
}
|