From 7e923e268865d8c0933a22247424d205429a474b Mon Sep 17 00:00:00 2001 From: 1hitsong <3330318+1hitsong@users.noreply.github.com> Date: Sun, 18 Sep 2022 16:05:50 -0400 Subject: [PATCH] Use numeric values for metadata values --- .../Lyrics/ILyricProvider.cs | 2 +- .../Lyrics/{Lyric.cs => LyricLine.cs} | 6 +-- .../Lyrics/LyricMetadata.cs | 6 ++- .../Lyrics/LyricResponse.cs | 2 +- .../Lyric/LrcLyricProvider.cs | 53 +++++++++++++++---- .../Lyric/TxtLyricProvider.cs | 9 ++-- 6 files changed, 56 insertions(+), 22 deletions(-) rename MediaBrowser.Controller/Lyrics/{Lyric.cs => LyricLine.cs} (77%) diff --git a/MediaBrowser.Controller/Lyrics/ILyricProvider.cs b/MediaBrowser.Controller/Lyrics/ILyricProvider.cs index 651fe507f9..c5b6252267 100644 --- a/MediaBrowser.Controller/Lyrics/ILyricProvider.cs +++ b/MediaBrowser.Controller/Lyrics/ILyricProvider.cs @@ -24,7 +24,7 @@ public interface ILyricProvider /// Gets the supported media types for this provider. /// /// The supported media types. - IEnumerable SupportedMediaTypes { get; } + IReadOnlyCollection SupportedMediaTypes { get; } /// /// Gets the lyrics. diff --git a/MediaBrowser.Controller/Lyrics/Lyric.cs b/MediaBrowser.Controller/Lyrics/LyricLine.cs similarity index 77% rename from MediaBrowser.Controller/Lyrics/Lyric.cs rename to MediaBrowser.Controller/Lyrics/LyricLine.cs index f39fbb0221..43997f6564 100644 --- a/MediaBrowser.Controller/Lyrics/Lyric.cs +++ b/MediaBrowser.Controller/Lyrics/LyricLine.cs @@ -3,14 +3,14 @@ namespace MediaBrowser.Controller.Lyrics; /// /// Lyric model. /// -public class Lyric +public class LyricLine { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The lyric start time in ticks. /// The lyric text. - public Lyric(string text, long? start = null) + public LyricLine(string text, long? start = null) { Start = start; Text = text; diff --git a/MediaBrowser.Controller/Lyrics/LyricMetadata.cs b/MediaBrowser.Controller/Lyrics/LyricMetadata.cs index 36a833f85c..0ba7779750 100644 --- a/MediaBrowser.Controller/Lyrics/LyricMetadata.cs +++ b/MediaBrowser.Controller/Lyrics/LyricMetadata.cs @@ -1,3 +1,5 @@ +using System; + namespace MediaBrowser.Controller.Lyrics; /// @@ -28,7 +30,7 @@ public class LyricMetadata /// /// Gets or sets Length - How long the song is. /// - public string? Length { get; set; } + public long? Length { get; set; } /// /// Gets or sets By - Creator of the LRC file. @@ -38,7 +40,7 @@ public class LyricMetadata /// /// Gets or sets Offset - Offset:+/- Timestamp adjustment in milliseconds. /// - public string? Offset { get; set; } + public long? Offset { get; set; } /// /// Gets or sets Creator - The Software used to create the LRC file. diff --git a/MediaBrowser.Controller/Lyrics/LyricResponse.cs b/MediaBrowser.Controller/Lyrics/LyricResponse.cs index 405e8cac18..b3c65ac8c0 100644 --- a/MediaBrowser.Controller/Lyrics/LyricResponse.cs +++ b/MediaBrowser.Controller/Lyrics/LyricResponse.cs @@ -15,5 +15,5 @@ public class LyricResponse /// /// Gets or sets Lyrics. /// - public IEnumerable Lyrics { get; set; } + public IEnumerable Lyrics { get; set; } } diff --git a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs index 4690c3e209..50fa519b3e 100644 --- a/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs +++ b/MediaBrowser.Providers/Lyric/LrcLyricProvider.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using LrcParser.Model; using LrcParser.Parser; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Lyrics; using MediaBrowser.Controller.Resolvers; +using MediaBrowser.Model.Entities; using Microsoft.Extensions.Logging; namespace MediaBrowser.Providers.Lyric; @@ -36,7 +38,7 @@ public class LrcLyricProvider : ILyricProvider public ResolverPriority Priority => ResolverPriority.First; /// - public IEnumerable SupportedMediaTypes { get; } = new[] { "lrc" }; + public IReadOnlyCollection SupportedMediaTypes { get; } = new[] { "lrc" }; /// /// Opens lyric file for the requested item, and processes it for API return. @@ -52,7 +54,7 @@ public class LrcLyricProvider : ILyricProvider return null; } - List lyricList = new List(); + List lyricList = new List(); List sortedLyricData = new List(); IDictionary fileMetaData = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -74,14 +76,28 @@ public class LrcLyricProvider : ILyricProvider foreach (string metaDataRow in metaDataRows) { - var metaDataField = metaDataRow.Split(':'); - if (metaDataField.Length != 2) + int colonCount = metaDataRow.Count(f => (f == ':')); + if (colonCount == 0) { continue; } - string metaDataFieldName = metaDataField[0][1..].Trim(); - string metaDataFieldValue = metaDataField[1][..^1].Trim(); + string[] metaDataField; + 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); } @@ -105,7 +121,7 @@ public class LrcLyricProvider : ILyricProvider } 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) @@ -150,7 +166,23 @@ public class LrcLyricProvider : ILyricProvider 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)) @@ -160,7 +192,10 @@ public class LrcLyricProvider : ILyricProvider 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)) diff --git a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs index c690086862..cd0e1599e7 100644 --- a/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs +++ b/MediaBrowser.Providers/Lyric/TxtLyricProvider.cs @@ -1,7 +1,4 @@ -using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Lyrics; using MediaBrowser.Controller.Resolvers; @@ -23,7 +20,7 @@ public class TxtLyricProvider : ILyricProvider public ResolverPriority Priority => ResolverPriority.Second; /// - public IEnumerable SupportedMediaTypes { get; } = new[] { "lrc", "txt" }; + public IReadOnlyCollection SupportedMediaTypes { get; } = new[] { "lrc", "txt" }; /// /// 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); - List lyricList = new List(); + List lyricList = new List(); if (lyricTextLines.Length == 0) { @@ -50,7 +47,7 @@ public class TxtLyricProvider : ILyricProvider foreach (string lyricTextLine in lyricTextLines) { - lyricList.Add(new Controller.Lyrics.Lyric(lyricTextLine)); + lyricList.Add(new LyricLine(lyricTextLine)); } return new LyricResponse { Lyrics = lyricList };