Use numeric values for metadata values

This commit is contained in:
1hitsong 2022-09-18 16:05:50 -04:00
parent dddebec794
commit 7e923e2688
6 changed files with 56 additions and 22 deletions

View File

@ -24,7 +24,7 @@ public interface ILyricProvider
/// Gets the supported media types for this provider. /// Gets the supported media types for this provider.
/// </summary> /// </summary>
/// <value>The supported media types.</value> /// <value>The supported media types.</value>
IEnumerable<string> SupportedMediaTypes { get; } IReadOnlyCollection<string> SupportedMediaTypes { get; }
/// <summary> /// <summary>
/// Gets the lyrics. /// Gets the lyrics.

View File

@ -3,14 +3,14 @@ namespace MediaBrowser.Controller.Lyrics;
/// <summary> /// <summary>
/// Lyric model. /// Lyric model.
/// </summary> /// </summary>
public class Lyric public class LyricLine
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Lyric"/> class. /// Initializes a new instance of the <see cref="LyricLine"/> class.
/// </summary> /// </summary>
/// <param name="start">The lyric start time in ticks.</param> /// <param name="start">The lyric start time in ticks.</param>
/// <param name="text">The lyric text.</param> /// <param name="text">The lyric text.</param>
public Lyric(string text, long? start = null) public LyricLine(string text, long? start = null)
{ {
Start = start; Start = start;
Text = text; Text = text;

View File

@ -1,3 +1,5 @@
using System;
namespace MediaBrowser.Controller.Lyrics; namespace MediaBrowser.Controller.Lyrics;
/// <summary> /// <summary>
@ -28,7 +30,7 @@ public class LyricMetadata
/// <summary> /// <summary>
/// Gets or sets Length - How long the song is. /// Gets or sets Length - How long the song is.
/// </summary> /// </summary>
public string? Length { get; set; } public long? Length { get; set; }
/// <summary> /// <summary>
/// Gets or sets By - Creator of the LRC file. /// Gets or sets By - Creator of the LRC file.
@ -38,7 +40,7 @@ public class LyricMetadata
/// <summary> /// <summary>
/// Gets or sets Offset - Offset:+/- Timestamp adjustment in milliseconds. /// Gets or sets Offset - Offset:+/- Timestamp adjustment in milliseconds.
/// </summary> /// </summary>
public string? Offset { get; set; } public long? Offset { get; set; }
/// <summary> /// <summary>
/// Gets or sets Creator - The Software used to create the LRC file. /// Gets or sets Creator - The Software used to create the LRC file.

View File

@ -15,5 +15,5 @@ public class LyricResponse
/// <summary> /// <summary>
/// Gets or sets Lyrics. /// Gets or sets Lyrics.
/// </summary> /// </summary>
public IEnumerable<Lyric> Lyrics { get; set; } public IEnumerable<LyricLine> Lyrics { get; set; }
} }

View File

@ -1,11 +1,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using LrcParser.Model; using LrcParser.Model;
using LrcParser.Parser; using LrcParser.Parser;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics; using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers; using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.Lyric; namespace MediaBrowser.Providers.Lyric;
@ -36,7 +38,7 @@ public class LrcLyricProvider : ILyricProvider
public ResolverPriority Priority => ResolverPriority.First; public ResolverPriority Priority => ResolverPriority.First;
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<string> SupportedMediaTypes { get; } = new[] { "lrc" }; public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc" };
/// <summary> /// <summary>
/// Opens lyric file for the requested item, and processes it for API return. /// Opens lyric file for the requested item, and processes it for API return.
@ -52,7 +54,7 @@ public class LrcLyricProvider : ILyricProvider
return null; return null;
} }
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>(); List<LyricLine> lyricList = new List<LyricLine>();
List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>(); List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>();
IDictionary<string, string> fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); IDictionary<string, string> fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
@ -74,14 +76,28 @@ public class LrcLyricProvider : ILyricProvider
foreach (string metaDataRow in metaDataRows) foreach (string metaDataRow in metaDataRows)
{ {
var metaDataField = metaDataRow.Split(':'); int colonCount = metaDataRow.Count(f => (f == ':'));
if (metaDataField.Length != 2) if (colonCount == 0)
{ {
continue; continue;
} }
string metaDataFieldName = metaDataField[0][1..].Trim(); string[] metaDataField;
string metaDataFieldValue = metaDataField[1][..^1].Trim(); string metaDataFieldName;
string metaDataFieldValue;
if (colonCount == 1)
{
metaDataField = metaDataRow.Split(':');
metaDataFieldName = metaDataField[0][1..].Trim();
metaDataFieldValue = metaDataField[1][..^1].Trim();
}
else
{
int colonIndex = metaDataRow.IndexOf(':', StringComparison.OrdinalIgnoreCase);
metaDataFieldName = metaDataRow[..colonIndex][1..].Trim();
metaDataFieldValue = metaDataRow[(colonIndex + 1)..][..^1].Trim();
}
fileMetaData.Add(metaDataFieldName, metaDataFieldValue); fileMetaData.Add(metaDataFieldName, metaDataFieldValue);
} }
@ -105,7 +121,7 @@ public class LrcLyricProvider : ILyricProvider
} }
long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks; long ticks = TimeSpan.FromMilliseconds(timeData.Value).Ticks;
lyricList.Add(new Controller.Lyrics.Lyric(sortedLyricData[i].Text, ticks)); lyricList.Add(new LyricLine(sortedLyricData[i].Text, ticks));
} }
if (fileMetaData.Count != 0) if (fileMetaData.Count != 0)
@ -150,7 +166,23 @@ public class LrcLyricProvider : ILyricProvider
if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length)) if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length))
{ {
lyricMetadata.Length = length; // Ensure minutes include leading zero
var lengthData = length.Split(':');
if (lengthData[0].Length == 1)
{
length = "0" + length;
}
// If only Minutes and Seconds were provided, prepend zeros for hours
if (lengthData.Length == 2)
{
length = "00:" + length;
}
if (DateTime.TryParseExact(length, "HH:mm:ss", null, DateTimeStyles.None, out var value))
{
lyricMetadata.Length = value.TimeOfDay.Ticks;
}
} }
if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by)) if (metaData.TryGetValue("by", out var by) && !string.IsNullOrEmpty(by))
@ -160,7 +192,10 @@ public class LrcLyricProvider : ILyricProvider
if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset)) if (metaData.TryGetValue("offset", out var offset) && !string.IsNullOrEmpty(offset))
{ {
lyricMetadata.Offset = offset; if (int.TryParse(offset, out var value))
{
lyricMetadata.Offset = TimeSpan.FromMilliseconds(value).Ticks;
}
} }
if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator)) if (metaData.TryGetValue("re", out var creator) && !string.IsNullOrEmpty(creator))

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Lyrics; using MediaBrowser.Controller.Lyrics;
using MediaBrowser.Controller.Resolvers; using MediaBrowser.Controller.Resolvers;
@ -23,7 +20,7 @@ public class TxtLyricProvider : ILyricProvider
public ResolverPriority Priority => ResolverPriority.Second; public ResolverPriority Priority => ResolverPriority.Second;
/// <inheritdoc /> /// <inheritdoc />
public IEnumerable<string> SupportedMediaTypes { get; } = new[] { "lrc", "txt" }; public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "txt" };
/// <summary> /// <summary>
/// Opens lyric file for the requested item, and processes it for API return. /// Opens lyric file for the requested item, and processes it for API return.
@ -41,7 +38,7 @@ public class TxtLyricProvider : ILyricProvider
string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath); string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath);
List<Controller.Lyrics.Lyric> lyricList = new List<Controller.Lyrics.Lyric>(); List<LyricLine> lyricList = new List<LyricLine>();
if (lyricTextLines.Length == 0) if (lyricTextLines.Length == 0)
{ {
@ -50,7 +47,7 @@ public class TxtLyricProvider : ILyricProvider
foreach (string lyricTextLine in lyricTextLines) foreach (string lyricTextLine in lyricTextLines)
{ {
lyricList.Add(new Controller.Lyrics.Lyric(lyricTextLine)); lyricList.Add(new LyricLine(lyricTextLine));
} }
return new LyricResponse { Lyrics = lyricList }; return new LyricResponse { Lyrics = lyricList };