Merge pull request #9920 from nielsvanvelzen/lyric-parser
commit
eae92c5acc
@ -0,0 +1,28 @@
|
|||||||
|
using MediaBrowser.Controller.Resolvers;
|
||||||
|
using MediaBrowser.Providers.Lyric;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Controller.Lyrics;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface ILyricParser.
|
||||||
|
/// </summary>
|
||||||
|
public interface ILyricParser
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets a value indicating the provider name.
|
||||||
|
/// </summary>
|
||||||
|
string Name { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the priority.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The priority.</value>
|
||||||
|
ResolverPriority Priority { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parses the raw lyrics into a response.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="lyrics">The raw lyrics content.</param>
|
||||||
|
/// <returns>The parsed lyrics or null if invalid.</returns>
|
||||||
|
LyricResponse? ParseLyrics(LyricFile lyrics);
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
namespace MediaBrowser.Providers.Lyric;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The information for a raw lyrics file before parsing.
|
||||||
|
/// </summary>
|
||||||
|
public class LyricFile
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="LyricFile"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The name.</param>
|
||||||
|
/// <param name="content">The content, must not be empty.</param>
|
||||||
|
public LyricFile(string name, string content)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
Content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the name of the lyrics file. This must include the file extension.
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the contents of the file.
|
||||||
|
/// </summary>
|
||||||
|
public string Content { get; set; }
|
||||||
|
}
|
@ -1,49 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using Jellyfin.Extensions;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Controller.Lyrics;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Lyric helper methods.
|
|
||||||
/// </summary>
|
|
||||||
public static class LyricInfo
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets matching lyric file for a requested item.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="lyricProvider">The lyricProvider interface to use.</param>
|
|
||||||
/// <param name="itemPath">Path of requested item.</param>
|
|
||||||
/// <returns>Lyric file path if passed lyric provider's supported media type is found; otherwise, null.</returns>
|
|
||||||
public static string? GetLyricFilePath(this ILyricProvider lyricProvider, string itemPath)
|
|
||||||
{
|
|
||||||
// Ensure we have a provider
|
|
||||||
if (lyricProvider is null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the path to the item is not null
|
|
||||||
string? itemDirectoryPath = Path.GetDirectoryName(itemPath);
|
|
||||||
if (itemDirectoryPath is null)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the directory path exists
|
|
||||||
if (!Directory.Exists(itemDirectoryPath))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(itemPath)}.*"))
|
|
||||||
{
|
|
||||||
if (lyricProvider.SupportedMediaTypes.Contains(Path.GetExtension(lyricFilePath.AsSpan())[1..], StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
return lyricFilePath;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,69 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Jellyfin.Extensions;
|
||||||
|
using MediaBrowser.Controller.Entities;
|
||||||
|
using MediaBrowser.Controller.Resolvers;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Providers.Lyric;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public class DefaultLyricProvider : ILyricProvider
|
||||||
|
{
|
||||||
|
private static readonly string[] _lyricExtensions = { ".lrc", ".elrc", ".txt" };
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Name => "DefaultLyricProvider";
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public ResolverPriority Priority => ResolverPriority.First;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public bool HasLyrics(BaseItem item)
|
||||||
|
{
|
||||||
|
var path = GetLyricsPath(item);
|
||||||
|
return path is not null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public async Task<LyricFile?> GetLyrics(BaseItem item)
|
||||||
|
{
|
||||||
|
var path = GetLyricsPath(item);
|
||||||
|
if (path is not null)
|
||||||
|
{
|
||||||
|
var content = await File.ReadAllTextAsync(path).ConfigureAwait(false);
|
||||||
|
if (!string.IsNullOrEmpty(content))
|
||||||
|
{
|
||||||
|
return new LyricFile(path, content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string? GetLyricsPath(BaseItem item)
|
||||||
|
{
|
||||||
|
// Ensure the path to the item is not null
|
||||||
|
string? itemDirectoryPath = Path.GetDirectoryName(item.Path);
|
||||||
|
if (itemDirectoryPath is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the directory path exists
|
||||||
|
if (!Directory.Exists(itemDirectoryPath))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var lyricFilePath in Directory.GetFiles(itemDirectoryPath, $"{Path.GetFileNameWithoutExtension(item.Path)}.*"))
|
||||||
|
{
|
||||||
|
if (_lyricExtensions.Contains(Path.GetExtension(lyricFilePath.AsSpan()), StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return lyricFilePath;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using Jellyfin.Extensions;
|
||||||
|
using MediaBrowser.Controller.Lyrics;
|
||||||
|
using MediaBrowser.Controller.Resolvers;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Providers.Lyric;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// TXT Lyric Parser.
|
||||||
|
/// </summary>
|
||||||
|
public class TxtLyricParser : ILyricParser
|
||||||
|
{
|
||||||
|
private static readonly string[] _supportedMediaTypes = { ".lrc", ".elrc", ".txt" };
|
||||||
|
private static readonly string[] _lineBreakCharacters = { "\r\n", "\r", "\n" };
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string Name => "TxtLyricProvider";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the priority.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>The priority.</value>
|
||||||
|
public ResolverPriority Priority => ResolverPriority.Fifth;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public LyricResponse? ParseLyrics(LyricFile lyrics)
|
||||||
|
{
|
||||||
|
if (!_supportedMediaTypes.Contains(Path.GetExtension(lyrics.Name.AsSpan()), StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
string[] lyricTextLines = lyrics.Content.Split(_lineBreakCharacters, StringSplitOptions.None);
|
||||||
|
LyricLine[] lyricList = new LyricLine[lyricTextLines.Length];
|
||||||
|
|
||||||
|
for (int lyricLineIndex = 0; lyricLineIndex < lyricTextLines.Length; lyricLineIndex++)
|
||||||
|
{
|
||||||
|
lyricList[lyricLineIndex] = new LyricLine(lyricTextLines[lyricLineIndex]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new LyricResponse { Lyrics = lyricList };
|
||||||
|
}
|
||||||
|
}
|
@ -1,60 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using MediaBrowser.Controller.Entities;
|
|
||||||
using MediaBrowser.Controller.Lyrics;
|
|
||||||
using MediaBrowser.Controller.Resolvers;
|
|
||||||
|
|
||||||
namespace MediaBrowser.Providers.Lyric;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// TXT Lyric Provider.
|
|
||||||
/// </summary>
|
|
||||||
public class TxtLyricProvider : ILyricProvider
|
|
||||||
{
|
|
||||||
/// <inheritdoc />
|
|
||||||
public string Name => "TxtLyricProvider";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the priority.
|
|
||||||
/// </summary>
|
|
||||||
/// <value>The priority.</value>
|
|
||||||
public ResolverPriority Priority => ResolverPriority.Second;
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc", "txt" };
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Opens lyric file for the requested item, and processes it for API return.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="item">The item to to process.</param>
|
|
||||||
/// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/>; otherwise, null.</returns>
|
|
||||||
public async Task<LyricResponse?> GetLyrics(BaseItem item)
|
|
||||||
{
|
|
||||||
string? lyricFilePath = this.GetLyricFilePath(item.Path);
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(lyricFilePath))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
string[] lyricTextLines = await File.ReadAllLinesAsync(lyricFilePath).ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (lyricTextLines.Length == 0)
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
LyricLine[] lyricList = new LyricLine[lyricTextLines.Length];
|
|
||||||
|
|
||||||
for (int lyricLineIndex = 0; lyricLineIndex < lyricTextLines.Length; lyricLineIndex++)
|
|
||||||
{
|
|
||||||
lyricList[lyricLineIndex] = new LyricLine(lyricTextLines[lyricLineIndex]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new LyricResponse
|
|
||||||
{
|
|
||||||
Lyrics = lyricList
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue