2022-09-10 18:58:03 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Linq;
|
2022-09-15 23:45:26 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Jellyfin.Api.Helpers;
|
2022-09-10 18:58:03 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2022-09-15 23:45:26 +00:00
|
|
|
using MediaBrowser.Controller.Lyrics;
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-15 23:45:26 +00:00
|
|
|
namespace MediaBrowser.Providers.Lyric
|
2022-09-10 18:58:03 +00: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()
|
|
|
|
{
|
2022-09-15 23:45:26 +00:00
|
|
|
SupportedMediaTypes = new Collection<string>
|
2022-09-10 18:58:03 +00:00
|
|
|
{
|
|
|
|
"lrc", "txt"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets a value indicating the File Extenstions this provider works with.
|
|
|
|
/// </summary>
|
2022-09-15 23:45:26 +00:00
|
|
|
public IEnumerable<string> SupportedMediaTypes { get; }
|
2022-09-10 18:58:03 +00: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>
|
2022-09-15 23:45:26 +00:00
|
|
|
/// <returns><placeholder>A <see cref="Task"/> representing the asynchronous operation.</placeholder></returns>
|
|
|
|
public LyricResponse? GetLyrics(BaseItem item)
|
2022-09-10 18:58:03 +00:00
|
|
|
{
|
2022-09-15 23:45:26 +00:00
|
|
|
string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);
|
2022-09-10 18:58:03 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(lyricFilePath))
|
|
|
|
{
|
2022-09-15 23:45:26 +00:00
|
|
|
return null;
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 23:45:26 +00:00
|
|
|
List<MediaBrowser.Controller.Lyrics.Lyric> lyricsList = new List<MediaBrowser.Controller.Lyrics.Lyric>();
|
2022-09-10 18:58:03 +00: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())
|
|
|
|
{
|
2022-09-15 23:45:26 +00:00
|
|
|
return null;
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (string lyricLine in lyricTextLines)
|
|
|
|
{
|
2022-09-15 23:45:26 +00:00
|
|
|
lyricsList.Add(new MediaBrowser.Controller.Lyrics.Lyric { Text = lyricLine });
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 23:45:26 +00:00
|
|
|
return new LyricResponse { Lyrics = lyricsList };
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|