aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs
blob: 311a8e21d8268b71b3e7824c9aa9acaafdfa7be1 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using LrcParser.Model;
using LrcParser.Parser;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics;
using Newtonsoft.Json.Linq;

namespace MediaBrowser.Providers.Lyric;

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

    /// <inheritdoc />
    public IEnumerable<string> SupportedMediaTypes
    {
        get => new Collection<string>
            {
                "lrc"
            };
    }

    /// <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"/> with or without metadata; otherwise, null.</returns>
    public LyricResponse? GetLyrics(BaseItem item)
    {
        string? lyricFilePath = LyricInfo.GetLyricFilePath(this, item.Path);

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

        List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
        List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();

        IDictionary<string, string> fileMetaData = new Dictionary<string, string>();
        string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);

        try
        {
            // Parse and sort lyric rows
            LyricParser lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
            Song lyricData = lrcLyricParser.Decode(lrcFileContent);
            sortedLyricData = lyricData.Lyrics.Where(x => x.TimeTags.Count > 0).OrderBy(x => x.TimeTags.First().Value).ToList();

            // Parse metadata rows
            var metaDataRows = lyricData.Lyrics
                .Where(x => x.TimeTags.Count == 0)
                .Where(x => x.Text.StartsWith('[') && x.Text.EndsWith(']'))
                .Select(x => x.Text)
                .ToList();

            foreach (string metaDataRow in metaDataRows)
            {
                var metaDataField = metaDataRow.Split(':');
                if (metaDataField.Length != 2)
                {
                    continue;
                }

                string metaDataFieldName = metaDataField[0][1..].Trim().ToLowerInvariant();
                string metaDataFieldValue = metaDataField[1][..^1].Trim();

                fileMetaData.Add(metaDataFieldName, metaDataFieldValue);
            }
        }
        catch
        {
            return null;
        }

        if (sortedLyricData.Count == 0)
        {
            return null;
        }

        for (int i = 0; i < sortedLyricData.Count; i++)
        {
            var timeData = sortedLyricData[i].TimeTags.First().Value;
            if (timeData is null)
            {
                continue;
            }

            long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks;
            lyricList.Add(new Controller.Lyrics.Lyric(sortedLyricData[i].Text, ticks));
        }

        if (fileMetaData.Count != 0)
        {
            // Map metaData values from LRC file to LyricMetadata properties
            LyricMetadata lyricMetadata = MapMetadataValues(fileMetaData);

            return new LyricResponse { Metadata = lyricMetadata, Lyrics = lyricList };
        }

        return new LyricResponse { Lyrics = lyricList };
    }

    /// <summary>
    /// Converts metadata from an LRC file to LyricMetadata properties.
    /// </summary>
    /// <param name="metaData">The metadata from the LRC file.</param>
    /// <returns>A lyricMetadata object with mapped property data.</returns>
    private LyricMetadata MapMetadataValues(IDictionary<string, string> metaData)
    {
        LyricMetadata lyricMetadata = new LyricMetadata();

        if (metaData.TryGetValue("ar", out var artist) && artist is not null)
        {
            lyricMetadata.Artist = artist;
        }

        if (metaData.TryGetValue("al", out var album) && album is not null)
        {
            lyricMetadata.Album = album;
        }

        if (metaData.TryGetValue("ti", out var title) && title is not null)
        {
            lyricMetadata.Title = title;
        }

        if (metaData.TryGetValue("au", out var author) && author is not null)
        {
            lyricMetadata.Author = author;
        }

        if (metaData.TryGetValue("length", out var length) && length is not null)
        {
            lyricMetadata.Length = length;
        }

        if (metaData.TryGetValue("by", out var by) && by is not null)
        {
            lyricMetadata.By = by;
        }

        if (metaData.TryGetValue("offset", out var offset) && offset is not null)
        {
            lyricMetadata.Offset = offset;
        }

        if (metaData.TryGetValue("re", out var creator) && creator is not null)
        {
            lyricMetadata.Creator = creator;
        }

        if (metaData.TryGetValue("ve", out var version) && version is not null)
        {
            lyricMetadata.Version = version;
        }

        return lyricMetadata;

    }
}