2023-06-20 14:51:07 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using Jellyfin.Extensions;
|
|
|
|
using MediaBrowser.Controller.Lyrics;
|
|
|
|
using MediaBrowser.Controller.Resolvers;
|
2024-02-26 12:09:40 +00:00
|
|
|
using MediaBrowser.Model.Lyrics;
|
2023-06-20 14:51:07 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Lyric;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// TXT Lyric Parser.
|
|
|
|
/// </summary>
|
|
|
|
public class TxtLyricParser : ILyricParser
|
|
|
|
{
|
2024-02-26 12:09:40 +00:00
|
|
|
private static readonly string[] _supportedMediaTypes = [".lrc", ".elrc", ".txt"];
|
|
|
|
private static readonly string[] _lineBreakCharacters = ["\r\n", "\r", "\n"];
|
2023-06-20 14:51:07 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public string Name => "TxtLyricProvider";
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the priority.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
public ResolverPriority Priority => ResolverPriority.Fifth;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2024-02-26 12:09:40 +00:00
|
|
|
public LyricDto? ParseLyrics(LyricFile lyrics)
|
2023-06-20 14:51:07 +00:00
|
|
|
{
|
2023-06-29 19:16:29 +00:00
|
|
|
if (!_supportedMediaTypes.Contains(Path.GetExtension(lyrics.Name.AsSpan()), StringComparison.OrdinalIgnoreCase))
|
2023-06-20 14:51:07 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-06-24 07:25:25 +00:00
|
|
|
string[] lyricTextLines = lyrics.Content.Split(_lineBreakCharacters, StringSplitOptions.None);
|
2023-06-20 14:51:07 +00:00
|
|
|
LyricLine[] lyricList = new LyricLine[lyricTextLines.Length];
|
|
|
|
|
|
|
|
for (int lyricLineIndex = 0; lyricLineIndex < lyricTextLines.Length; lyricLineIndex++)
|
|
|
|
{
|
2024-02-26 12:09:40 +00:00
|
|
|
lyricList[lyricLineIndex] = new LyricLine(lyricTextLines[lyricLineIndex].Trim());
|
2023-06-20 14:51:07 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 12:09:40 +00:00
|
|
|
return new LyricDto { Lyrics = lyricList };
|
2023-06-20 14:51:07 +00:00
|
|
|
}
|
|
|
|
}
|