diff options
Diffstat (limited to 'MediaBrowser.Providers/Lyric/TxtLyricProvider.cs')
| -rw-r--r-- | MediaBrowser.Providers/Lyric/TxtLyricProvider.cs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs new file mode 100644 index 0000000000..939d8708b2 --- /dev/null +++ b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Threading.Tasks; +using System.Xml.Linq; +using Jellyfin.Api.Helpers; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Lyrics; + +namespace MediaBrowser.Providers.Lyric +{ + /// <summary> + /// TXT File Lyric Provider. + /// </summary> + public class TxtLyricProvider : ILyricProvider + { + /// <summary> + /// Initializes a new instance of the <see cref="TxtLyricProvider"/> class. + /// </summary> + public TxtLyricProvider() + { + Name = "TxtLyricProvider"; + + SupportedMediaTypes = new Collection<string> + { + "lrc", "txt" + }; + } + + /// <summary> + /// Gets a value indicating the provider name. + /// </summary> + public string Name { get; } + + /// <summary> + /// Gets a value indicating the File Extenstions this provider works with. + /// </summary> + public IEnumerable<string> SupportedMediaTypes { get; } + + /// <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> + /// <returns><placeholder>A <see cref="Task"/> representing the asynchronous operation.</placeholder></returns> + public LyricResponse? GetLyrics(BaseItem item) + { + string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path); + + if (string.IsNullOrEmpty(lyricFilePath)) + { + return null; + } + + List<MediaBrowser.Controller.Lyrics.Lyric> lyricsList = new List<MediaBrowser.Controller.Lyrics.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 null; + } + + foreach (string lyricLine in lyricTextLines) + { + lyricsList.Add(new MediaBrowser.Controller.Lyrics.Lyric { Text = lyricLine }); + } + + return new LyricResponse { Lyrics = lyricsList }; + } + } +} |
