aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs
blob: 9df4ec83e7f64b6d58c9f7db769306e67de89e01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers;

namespace MediaBrowser.Providers.Lyric;

/// <summary>
/// TXT Lyric Provider.
/// </summary>
public class TxtLyricProvider : ILyricProvider
{
    /// <inheritdoc />
    public string Name => "TxtLyricProvider";

    /// <summary>
    /// Gets the priority.
    /// </summary>
    /// <value>The priority.</value>
    public ResolverPriority Priority => ResolverPriority.Second;

    /// <inheritdoc />
    public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc", "txt" };

    /// <summary>
    /// Opens lyric file for the requested item, and processes it for API return.
    /// </summary>
    /// <param name="item">The item to to process.</param>
    /// <returns>If provider can determine lyrics, returns a <see cref="LyricResponse"/>; otherwise, null.</returns>
    public async Task<LyricResponse?> GetLyrics(BaseItem item)
    {
        string? lyricFilePath = this.GetLyricFilePath(item.Path);

        if (string.IsNullOrEmpty(lyricFilePath))
        {
            return null;
        }

        string[] lyricTextLines = await Task.FromResult(File.ReadAllLines(lyricFilePath)).ConfigureAwait(false);

        if (lyricTextLines.Length == 0)
        {
            return null;
        }

        LyricLine[] lyricList = new LyricLine[lyricTextLines.Length];

        for (int lyricLine = 0; lyricLine < lyricTextLines.Length; lyricLine++)
        {
            lyricList[lyricLine] = new LyricLine(lyricTextLines[lyricLine]);
        }

        return new LyricResponse { Lyrics = lyricList };
    }
}