From 6de56f05186b77042a611112d82208b8fa8675fb Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Tue, 20 Jun 2023 16:51:07 +0200 Subject: Add support for lyric provider plugins --- MediaBrowser.Controller/Lyrics/ILyricParser.cs | 28 ++++++++++++++ MediaBrowser.Controller/Lyrics/ILyricProvider.cs | 36 ----------------- MediaBrowser.Controller/Lyrics/LyricFile.cs | 28 ++++++++++++++ MediaBrowser.Controller/Lyrics/LyricInfo.cs | 49 ------------------------ 4 files changed, 56 insertions(+), 85 deletions(-) create mode 100644 MediaBrowser.Controller/Lyrics/ILyricParser.cs delete mode 100644 MediaBrowser.Controller/Lyrics/ILyricProvider.cs create mode 100644 MediaBrowser.Controller/Lyrics/LyricFile.cs delete mode 100644 MediaBrowser.Controller/Lyrics/LyricInfo.cs (limited to 'MediaBrowser.Controller') diff --git a/MediaBrowser.Controller/Lyrics/ILyricParser.cs b/MediaBrowser.Controller/Lyrics/ILyricParser.cs new file mode 100644 index 000000000..65a9471a3 --- /dev/null +++ b/MediaBrowser.Controller/Lyrics/ILyricParser.cs @@ -0,0 +1,28 @@ +using MediaBrowser.Controller.Resolvers; +using MediaBrowser.Providers.Lyric; + +namespace MediaBrowser.Controller.Lyrics; + +/// +/// Interface ILyricParser. +/// +public interface ILyricParser +{ + /// + /// Gets a value indicating the provider name. + /// + string Name { get; } + + /// + /// Gets the priority. + /// + /// The priority. + ResolverPriority Priority { get; } + + /// + /// Parses the raw lyrics into a response. + /// + /// The raw lyrics content. + /// The parsed lyrics or null if invalid. + LyricResponse? ParseLyrics(LyricFile lyrics); +} diff --git a/MediaBrowser.Controller/Lyrics/ILyricProvider.cs b/MediaBrowser.Controller/Lyrics/ILyricProvider.cs deleted file mode 100644 index 2a04c6152..000000000 --- a/MediaBrowser.Controller/Lyrics/ILyricProvider.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Resolvers; - -namespace MediaBrowser.Controller.Lyrics; - -/// -/// Interface ILyricsProvider. -/// -public interface ILyricProvider -{ - /// - /// Gets a value indicating the provider name. - /// - string Name { get; } - - /// - /// Gets the priority. - /// - /// The priority. - ResolverPriority Priority { get; } - - /// - /// Gets the supported media types for this provider. - /// - /// The supported media types. - IReadOnlyCollection SupportedMediaTypes { get; } - - /// - /// Gets the lyrics. - /// - /// The media item. - /// A task representing found lyrics. - Task GetLyrics(BaseItem item); -} diff --git a/MediaBrowser.Controller/Lyrics/LyricFile.cs b/MediaBrowser.Controller/Lyrics/LyricFile.cs new file mode 100644 index 000000000..21096797a --- /dev/null +++ b/MediaBrowser.Controller/Lyrics/LyricFile.cs @@ -0,0 +1,28 @@ +namespace MediaBrowser.Providers.Lyric; + +/// +/// The information for a raw lyrics file before parsing. +/// +public class LyricFile +{ + /// + /// Initializes a new instance of the class. + /// + /// The name. + /// The content. + public LyricFile(string name, string content) + { + Name = name; + Content = content; + } + + /// + /// Gets or sets the name of the lyrics file. This must include the file extension. + /// + public string Name { get; set; } + + /// + /// Gets or sets the contents of the file. + /// + public string Content { get; set; } +} diff --git a/MediaBrowser.Controller/Lyrics/LyricInfo.cs b/MediaBrowser.Controller/Lyrics/LyricInfo.cs deleted file mode 100644 index 6ec6df582..000000000 --- a/MediaBrowser.Controller/Lyrics/LyricInfo.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.IO; -using Jellyfin.Extensions; - -namespace MediaBrowser.Controller.Lyrics; - -/// -/// Lyric helper methods. -/// -public static class LyricInfo -{ - /// - /// Gets matching lyric file for a requested item. - /// - /// The lyricProvider interface to use. - /// Path of requested item. - /// Lyric file path if passed lyric provider's supported media type is found; otherwise, null. - 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; - } -} -- cgit v1.2.3 From 0ae4d175a184b86ec0267b1bdb89c8c59d40c325 Mon Sep 17 00:00:00 2001 From: Niels van Velzen Date: Sat, 1 Jul 2023 11:16:21 +0200 Subject: Check for empty string in DefaultLyricProvider --- MediaBrowser.Controller/Lyrics/LyricFile.cs | 2 +- MediaBrowser.Providers/Lyric/DefaultLyricProvider.cs | 5 ++++- MediaBrowser.Providers/Lyric/TxtLyricParser.cs | 6 ------ 3 files changed, 5 insertions(+), 8 deletions(-) (limited to 'MediaBrowser.Controller') diff --git a/MediaBrowser.Controller/Lyrics/LyricFile.cs b/MediaBrowser.Controller/Lyrics/LyricFile.cs index 21096797a..ede89403c 100644 --- a/MediaBrowser.Controller/Lyrics/LyricFile.cs +++ b/MediaBrowser.Controller/Lyrics/LyricFile.cs @@ -9,7 +9,7 @@ public class LyricFile /// Initializes a new instance of the class. /// /// The name. - /// The content. + /// The content, must not be empty. public LyricFile(string name, string content) { Name = name; diff --git a/MediaBrowser.Providers/Lyric/DefaultLyricProvider.cs b/MediaBrowser.Providers/Lyric/DefaultLyricProvider.cs index ae6350dd7..a26f2babb 100644 --- a/MediaBrowser.Providers/Lyric/DefaultLyricProvider.cs +++ b/MediaBrowser.Providers/Lyric/DefaultLyricProvider.cs @@ -32,7 +32,10 @@ public class DefaultLyricProvider : ILyricProvider if (path is not null) { var content = await File.ReadAllTextAsync(path).ConfigureAwait(false); - return new LyricFile(path, content); + if (content.Length != 0) + { + return new LyricFile(path, content); + } } return null; diff --git a/MediaBrowser.Providers/Lyric/TxtLyricParser.cs b/MediaBrowser.Providers/Lyric/TxtLyricParser.cs index ed4f2cb7a..706f13dbc 100644 --- a/MediaBrowser.Providers/Lyric/TxtLyricParser.cs +++ b/MediaBrowser.Providers/Lyric/TxtLyricParser.cs @@ -32,12 +32,6 @@ public class TxtLyricParser : ILyricParser } string[] lyricTextLines = lyrics.Content.Split(_lineBreakCharacters, StringSplitOptions.None); - - if (lyricTextLines.Length == 0) - { - return null; - } - LyricLine[] lyricList = new LyricLine[lyricTextLines.Length]; for (int lyricLineIndex = 0; lyricLineIndex < lyricTextLines.Length; lyricLineIndex++) -- cgit v1.2.3