diff options
Diffstat (limited to 'Jellyfin.Api/Helpers')
| -rw-r--r-- | Jellyfin.Api/Helpers/ItemHelper.cs | 117 |
1 files changed, 68 insertions, 49 deletions
diff --git a/Jellyfin.Api/Helpers/ItemHelper.cs b/Jellyfin.Api/Helpers/ItemHelper.cs index c1b5ea6cc..622eb0b9f 100644 --- a/Jellyfin.Api/Helpers/ItemHelper.cs +++ b/Jellyfin.Api/Helpers/ItemHelper.cs @@ -23,79 +23,98 @@ namespace Jellyfin.Api.Helpers /// <returns>Collection of Lyrics.</returns> internal static object? GetLyricData(BaseItem item) { - List<Lyrics> lyricsList = new List<Lyrics>(); + List<ILyricsProvider> providerList = new List<ILyricsProvider>(); - string lrcFilePath = @Path.ChangeExtension(item.Path, "lrc"); + // Find all classes that implement ILyricsProvider Interface + var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly() + .GetTypes() + .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface); - // LRC File not found, fallback to TXT file - if (!System.IO.File.Exists(lrcFilePath)) + if (!foundLyricProviders.Any()) { - string txtFilePath = @Path.ChangeExtension(item.Path, "txt"); - if (!System.IO.File.Exists(txtFilePath)) - { - return null; - } + return null; + } - var lyricTextData = System.IO.File.ReadAllText(txtFilePath); - string[] lyricTextLines = lyricTextData.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); + foreach (var provider in foundLyricProviders) + { + providerList.Add((ILyricsProvider)Activator.CreateInstance(provider)); + } - foreach (var lyricLine in lyricTextLines) + foreach (ILyricsProvider provider in providerList) + { + provider.Process(item); + if (provider.HasData) { - lyricsList.Add(new Lyrics { Text = lyricLine }); + return provider.Data; } - - return new { lyrics = lyricsList }; } - // Process LRC File - Song lyricData; - List<Lyric> sortedLyricData = new List<Lyric>(); - var metaData = new ExpandoObject() as IDictionary<string, object>; - string lrcFileContent = System.IO.File.ReadAllText(lrcFilePath); - - try - { - LyricParser lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser(); - lyricData = lrcLyricParser.Decode(lrcFileContent); - var _metaData = lyricData.Lyrics - .Where(x => x.TimeTags.Count == 0) - .Where(x => x.Text.StartsWith("[", StringComparison.Ordinal) && x.Text.EndsWith("]", StringComparison.Ordinal)) - .Select(x => x.Text) - .ToList(); - - foreach (string dataRow in _metaData) - { - var data = dataRow.Split(":"); + return null; + } - string newPropertyName = data[0].Replace("[", string.Empty, StringComparison.Ordinal); - string newPropertyValue = data[1].Replace("]", string.Empty, StringComparison.Ordinal); + /// <summary> + /// Checks if requested item has a matching lyric file. + /// </summary> + /// <param name="itemPath">Path of requested item.</param> + /// <returns>True if item has a matching lyrics file.</returns> + public static string? GetLyricFilePath(string itemPath) + { + List<string> supportedLyricFileExtensions = new List<string>(); - metaData.Add(newPropertyName, newPropertyValue); - } + // Find all classes that implement ILyricsProvider Interface + var foundLyricProviders = System.Reflection.Assembly.GetExecutingAssembly() + .GetTypes() + .Where(type => typeof(ILyricsProvider).IsAssignableFrom(type) && !type.IsInterface); - sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.ToArray()[0].Value).ToList(); - } - catch + if (!foundLyricProviders.Any()) { return null; } - if (lyricData == null) + // Iterate over all found lyric providers + foreach (var provider in foundLyricProviders) { - return null; + var foundProvider = (ILyricsProvider)Activator.CreateInstance(provider); + if (foundProvider?.FileExtensions is null) + { + continue; + } + + if (foundProvider.FileExtensions.Any()) + { + // Gather distinct list of handled file extentions + foreach (string lyricFileExtension in foundProvider.FileExtensions) + { + if (!supportedLyricFileExtensions.Contains(lyricFileExtension)) + { + supportedLyricFileExtensions.Add(lyricFileExtension); + } + } + } } - for (int i = 0; i < sortedLyricData.Count; i++) + foreach (string lyricFileExtension in supportedLyricFileExtensions) { - if (sortedLyricData[i].TimeTags.Count > 0) + string lyricFilePath = @Path.ChangeExtension(itemPath, lyricFileExtension); + if (System.IO.File.Exists(lyricFilePath)) { - var timeData = sortedLyricData[i].TimeTags.ToArray()[0].Value; - double ticks = Convert.ToDouble(timeData, new NumberFormatInfo()) * 10000; - lyricsList.Add(new Lyrics { Start = Math.Ceiling(ticks), Text = sortedLyricData[i].Text }); + return lyricFilePath; } } - return new { MetaData = metaData, lyrics = lyricsList }; + return null; + } + + + /// <summary> + /// Checks if requested item has a matching local lyric file. + /// </summary> + /// <param name="itemPath">Path of requested item.</param> + /// <returns>True if item has a matching lyrics file; otherwise false.</returns> + public static bool HasLyricFile(string itemPath) + { + string? lyricFilePath = GetLyricFilePath(itemPath); + return !string.IsNullOrEmpty(lyricFilePath); } } } |
