Merge pull request #9920 from nielsvanvelzen/lyric-parser

This commit is contained in:
Claus Vium
2023-07-01 18:28:35 +02:00
committed by GitHub
10 changed files with 213 additions and 158 deletions

View File

@@ -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);
}

View File

@@ -1,36 +0,0 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Resolvers;
namespace MediaBrowser.Controller.Lyrics;
/// <summary>
/// Interface ILyricsProvider.
/// </summary>
public interface ILyricProvider
{
/// <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>
/// Gets the supported media types for this provider.
/// </summary>
/// <value>The supported media types.</value>
IReadOnlyCollection<string> SupportedMediaTypes { get; }
/// <summary>
/// Gets the lyrics.
/// </summary>
/// <param name="item">The media item.</param>
/// <returns>A task representing found lyrics.</returns>
Task<LyricResponse?> GetLyrics(BaseItem item);
}

View File

@@ -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; }
}

View File

@@ -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;
}
}