2022-09-10 18:58:03 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Linq;
|
|
|
|
using LrcParser.Model;
|
|
|
|
using LrcParser.Parser;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2022-09-15 23:45:26 +00:00
|
|
|
using MediaBrowser.Controller.Lyrics;
|
2022-09-18 15:47:57 +00:00
|
|
|
using Newtonsoft.Json.Linq;
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
namespace MediaBrowser.Providers.Lyric;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// LRC Lyric Provider.
|
|
|
|
/// </summary>
|
|
|
|
public class LrcLyricProvider : ILyricProvider
|
2022-09-10 18:58:03 +00:00
|
|
|
{
|
2022-09-17 21:37:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-09-18 16:38:24 +00:00
|
|
|
public string Name => "LrcLyricProvider";
|
2022-09-16 00:49:25 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-09-18 16:39:38 +00:00
|
|
|
public IEnumerable<string> SupportedMediaTypes { get; } = new[] { "lrc" };
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
/// <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);
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
if (string.IsNullOrEmpty(lyricFilePath))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>();
|
|
|
|
List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
|
2022-09-17 00:52:40 +00:00
|
|
|
|
2022-09-18 15:47:57 +00:00
|
|
|
IDictionary<string, string> fileMetaData = new Dictionary<string, string>();
|
2022-09-17 21:37:38 +00:00
|
|
|
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
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)
|
2022-09-10 18:58:03 +00:00
|
|
|
{
|
2022-09-17 21:37:38 +00:00
|
|
|
var metaDataField = metaDataRow.Split(':');
|
|
|
|
if (metaDataField.Length != 2)
|
2022-09-10 18:58:03 +00:00
|
|
|
{
|
2022-09-17 21:37:38 +00:00
|
|
|
continue;
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 15:47:57 +00:00
|
|
|
string metaDataFieldName = metaDataField[0][1..].Trim().ToLowerInvariant();
|
2022-09-17 21:37:38 +00:00
|
|
|
string metaDataFieldValue = metaDataField[1][..^1].Trim();
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-18 15:47:57 +00:00
|
|
|
fileMetaData.Add(metaDataFieldName, metaDataFieldValue);
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
2022-09-17 21:37:38 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-10 18:58:03 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
if (sortedLyricData.Count == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-15 23:45:26 +00:00
|
|
|
|
2022-09-17 21:37:38 +00:00
|
|
|
for (int i = 0; i < sortedLyricData.Count; i++)
|
|
|
|
{
|
2022-09-17 21:48:27 +00:00
|
|
|
var timeData = sortedLyricData[i].TimeTags.First().Value;
|
|
|
|
if (timeData is null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-18 16:39:19 +00:00
|
|
|
long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks;
|
2022-09-17 21:37:38 +00:00
|
|
|
lyricList.Add(new Controller.Lyrics.Lyric(sortedLyricData[i].Text, ticks));
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
2022-09-17 21:37:38 +00:00
|
|
|
|
2022-09-18 16:39:07 +00:00
|
|
|
if (fileMetaData.Count != 0)
|
2022-09-17 21:37:38 +00:00
|
|
|
{
|
2022-09-18 15:47:57 +00:00
|
|
|
// Map metaData values from LRC file to LyricMetadata properties
|
|
|
|
LyricMetadata lyricMetadata = MapMetadataValues(fileMetaData);
|
|
|
|
|
|
|
|
return new LyricResponse { Metadata = lyricMetadata, Lyrics = lyricList };
|
2022-09-17 21:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new LyricResponse { Lyrics = lyricList };
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|
2022-09-18 15:47:57 +00:00
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
|
|
}
|
2022-09-10 18:58:03 +00:00
|
|
|
}
|