2022-09-10 20:58:03 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2022-09-18 22:05:50 +02:00
|
|
|
using System.Globalization;
|
2022-09-10 20:58:03 +02:00
|
|
|
using System.Linq;
|
|
|
|
using LrcParser.Model;
|
|
|
|
using LrcParser.Parser;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2022-09-16 01:45:26 +02:00
|
|
|
using MediaBrowser.Controller.Lyrics;
|
2022-09-18 19:13:01 +02:00
|
|
|
using MediaBrowser.Controller.Resolvers;
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-09-10 20:58:03 +02:00
|
|
|
|
2022-09-17 23:37:38 +02:00
|
|
|
namespace MediaBrowser.Providers.Lyric;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// LRC Lyric Provider.
|
|
|
|
/// </summary>
|
|
|
|
public class LrcLyricProvider : ILyricProvider
|
2022-09-10 20:58:03 +02:00
|
|
|
{
|
2022-09-18 19:13:01 +02:00
|
|
|
private readonly ILogger<LrcLyricProvider> _logger;
|
|
|
|
|
2022-09-20 02:24:05 +02:00
|
|
|
private readonly LyricParser _lrcLyricParser;
|
|
|
|
|
2022-09-20 14:36:43 +02:00
|
|
|
private static readonly string[] _acceptedTimeFormats = { "HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss" };
|
2022-09-19 23:57:03 +02:00
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="LrcLyricProvider"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">Instance of the <see cref="ILogger"/> interface.</param>
|
|
|
|
public LrcLyricProvider(ILogger<LrcLyricProvider> logger)
|
|
|
|
{
|
|
|
|
_logger = logger;
|
2022-09-20 02:24:05 +02:00
|
|
|
_lrcLyricParser = new LrcParser.Parser.Lrc.LrcParser();
|
2022-09-18 19:13:01 +02:00
|
|
|
}
|
|
|
|
|
2022-09-17 23:37:38 +02:00
|
|
|
/// <inheritdoc />
|
2022-09-18 18:38:24 +02:00
|
|
|
public string Name => "LrcLyricProvider";
|
2022-09-16 02:49:25 +02:00
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the priority.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The priority.</value>
|
|
|
|
public ResolverPriority Priority => ResolverPriority.First;
|
|
|
|
|
2022-09-17 23:37:38 +02:00
|
|
|
/// <inheritdoc />
|
2022-09-19 03:17:53 +02:00
|
|
|
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc" };
|
2022-09-10 20:58:03 +02:00
|
|
|
|
2022-09-17 23:37:38 +02: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)
|
|
|
|
{
|
2022-09-20 14:36:43 +02:00
|
|
|
string? lyricFilePath = this.GetLyricFilePath(item.Path);
|
2022-09-10 20:58:03 +02:00
|
|
|
|
2022-09-17 23:37:38 +02:00
|
|
|
if (string.IsNullOrEmpty(lyricFilePath))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-10 20:58:03 +02:00
|
|
|
|
2022-09-20 14:36:43 +02:00
|
|
|
var fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2022-09-17 23:37:38 +02:00
|
|
|
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
|
2022-09-10 20:58:03 +02:00
|
|
|
|
2022-09-19 22:26:38 +02:00
|
|
|
Song lyricData;
|
|
|
|
|
2022-09-17 23:37:38 +02:00
|
|
|
try
|
|
|
|
{
|
2022-09-20 02:24:05 +02:00
|
|
|
lyricData = _lrcLyricParser.Decode(lrcFileContent);
|
2022-09-17 23:37:38 +02:00
|
|
|
}
|
2022-09-18 19:13:01 +02:00
|
|
|
catch (Exception ex)
|
2022-09-17 23:37:38 +02:00
|
|
|
{
|
2022-09-20 02:24:05 +02:00
|
|
|
_logger.LogError(ex, "Error parsing lyric file {LyricFilePath} from {Provider}", lyricFilePath, Name);
|
2022-09-19 22:26:38 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
List<LrcParser.Model.Lyric> 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-19 23:57:03 +02:00
|
|
|
if (!metaDataRow.Contains(':', StringComparison.OrdinalIgnoreCase))
|
2022-09-19 22:26:38 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
string[] metaDataField = metaDataRow.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
2022-09-20 14:36:43 +02:00
|
|
|
|
|
|
|
// Remove square bracket before field name, and after field value
|
|
|
|
// Example 1: [au: 1hitsong]
|
|
|
|
// Example 2: [ar: Calabrese]
|
2022-09-19 22:59:16 +02:00
|
|
|
string metaDataFieldName = metaDataField[0][1..];
|
|
|
|
string metaDataFieldValue = metaDataField[1][..^1];
|
2022-09-19 22:26:38 +02:00
|
|
|
|
2022-09-20 02:24:05 +02:00
|
|
|
if (string.IsNullOrEmpty(metaDataFieldName) || string.IsNullOrEmpty(metaDataFieldValue))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fileMetaData[metaDataFieldName] = metaDataFieldValue;
|
2022-09-17 23:37:38 +02:00
|
|
|
}
|
2022-09-10 20:58:03 +02:00
|
|
|
|
2022-09-17 23:37:38 +02:00
|
|
|
if (sortedLyricData.Count == 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-16 01:45:26 +02:00
|
|
|
|
2022-09-19 22:26:38 +02:00
|
|
|
List<LyricLine> lyricList = new();
|
|
|
|
|
2022-09-17 23:37:38 +02:00
|
|
|
for (int i = 0; i < sortedLyricData.Count; i++)
|
|
|
|
{
|
2022-09-17 23:48:27 +02:00
|
|
|
var timeData = sortedLyricData[i].TimeTags.First().Value;
|
|
|
|
if (timeData is null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-18 18:39:19 +02:00
|
|
|
long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks;
|
2022-09-18 22:05:50 +02:00
|
|
|
lyricList.Add(new LyricLine(sortedLyricData[i].Text, ticks));
|
2022-09-10 20:58:03 +02:00
|
|
|
}
|
2022-09-17 23:37:38 +02:00
|
|
|
|
2022-09-18 18:39:07 +02:00
|
|
|
if (fileMetaData.Count != 0)
|
2022-09-17 23:37:38 +02:00
|
|
|
{
|
2022-09-18 17:47:57 +02:00
|
|
|
// Map metaData values from LRC file to LyricMetadata properties
|
|
|
|
LyricMetadata lyricMetadata = MapMetadataValues(fileMetaData);
|
|
|
|
|
|
|
|
return new LyricResponse { Metadata = lyricMetadata, Lyrics = lyricList };
|
2022-09-17 23:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return new LyricResponse { Lyrics = lyricList };
|
2022-09-10 20:58:03 +02:00
|
|
|
}
|
2022-09-18 17:47:57 +02: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>
|
2022-09-19 22:26:38 +02:00
|
|
|
private static LyricMetadata MapMetadataValues(IDictionary<string, string> metaData)
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
2022-09-19 03:17:53 +02:00
|
|
|
LyricMetadata lyricMetadata = new();
|
2022-09-18 17:47:57 +02:00
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.Artist = artist;
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("al", out var album) && !string.IsNullOrEmpty(album))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.Album = album;
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("ti", out var title) && !string.IsNullOrEmpty(title))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.Title = title;
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("au", out var author) && !string.IsNullOrEmpty(author))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.Author = author;
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
2022-09-20 14:36:43 +02:00
|
|
|
if (DateTime.TryParseExact(length, _acceptedTimeFormats, null, DateTimeStyles.None, out var value))
|
2022-09-18 22:05:50 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.Length = value.TimeOfDay.Ticks;
|
|
|
|
}
|
2022-09-18 17:47:57 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.By = by;
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
2022-09-18 22:05:50 +02:00
|
|
|
if (int.TryParse(offset, out var value))
|
|
|
|
{
|
|
|
|
lyricMetadata.Offset = TimeSpan.FromMilliseconds(value).Ticks;
|
|
|
|
}
|
2022-09-18 17:47:57 +02:00
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.Creator = creator;
|
|
|
|
}
|
|
|
|
|
2022-09-18 19:13:01 +02:00
|
|
|
if (metaData.TryGetValue("ve", out var version) && !string.IsNullOrEmpty(version))
|
2022-09-18 17:47:57 +02:00
|
|
|
{
|
|
|
|
lyricMetadata.Version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lyricMetadata;
|
|
|
|
}
|
2022-09-10 20:58:03 +02:00
|
|
|
}
|