diff options
| author | 1hitsong <3330318+1hitsong@users.noreply.github.com> | 2022-09-10 14:58:03 -0400 |
|---|---|---|
| committer | 1hitsong <3330318+1hitsong@users.noreply.github.com> | 2022-09-11 15:50:27 -0400 |
| commit | 9d5cf67dfe2d5871d42a55a5e114c5ead1036ff0 (patch) | |
| tree | b89a9f63b2d76314d6ae2ee8aeb43991b1958a76 /Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs | |
| parent | 23ec35d3965e950f710c7cf6294145c601a6885b (diff) | |
Create ILyricsProvider
Diffstat (limited to 'Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs')
| -rw-r--r-- | Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs b/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs new file mode 100644 index 000000000..03cce1ffb --- /dev/null +++ b/Jellyfin.Api/Models/UserDtos/TxtLyricsProvider.cs @@ -0,0 +1,81 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Dynamic; +using System.Globalization; +using System.Linq; +using LrcParser.Model; +using LrcParser.Parser; +using MediaBrowser.Controller.Entities; + +namespace Jellyfin.Api.Models.UserDtos +{ + /// <summary> + /// TXT File Lyric Provider. + /// </summary> + public class TxtLyricsProvider : ILyricsProvider + { + /// <summary> + /// Initializes a new instance of the <see cref="TxtLyricsProvider"/> class. + /// </summary> + public TxtLyricsProvider() + { + FileExtensions = new Collection<string> + { + "lrc", "txt" + }; + } + + /// <summary> + /// Gets a value indicating the File Extenstions this provider works with. + /// </summary> + public Collection<string>? FileExtensions { get; } + + /// <summary> + /// Gets or Sets a value indicating whether Process() generated data. + /// </summary> + /// <returns><c>true</c> if data generated; otherwise, <c>false</c>.</returns> + public bool HasData { get; set; } + + /// <summary> + /// Gets or Sets Data object generated by Process() method. + /// </summary> + /// <returns><c>Object</c> with data if no error occured; otherwise, <c>null</c>.</returns> + public object? Data { get; set; } + + /// <summary> + /// Opens lyric file for [the specified item], and processes it for API return. + /// </summary> + /// <param name="item">The item to to process.</param> + public void Process(BaseItem item) + { + string? lyricFilePath = Helpers.ItemHelper.GetLyricFilePath(item.Path); + + if (string.IsNullOrEmpty(lyricFilePath)) + { + return; + } + + List<Lyric> lyricsList = new List<Lyric>(); + + string lyricData = System.IO.File.ReadAllText(lyricFilePath); + + // Splitting on Environment.NewLine caused some new lines to be missed in Windows. + char[] newLinedelims = new[] { '\r', '\n' }; + string[] lyricTextLines = lyricData.Split(newLinedelims, StringSplitOptions.RemoveEmptyEntries); + + if (!lyricTextLines.Any()) + { + return; + } + + foreach (string lyricLine in lyricTextLines) + { + lyricsList.Add(new Lyric { Text = lyricLine }); + } + + this.HasData = true; + this.Data = new { lyrics = lyricsList }; + } + } +} |
