Code Cleanup

This commit is contained in:
1hitsong 2022-09-18 21:17:53 -04:00
parent 552b6aceae
commit 28d017865b
4 changed files with 14 additions and 35 deletions

View File

@ -8,8 +8,8 @@ public class LyricLine
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="LyricLine"/> 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="text">The lyric text.</param> /// <param name="text">The lyric text.</param>
/// <param name="start">The lyric start time in ticks.</param>
public LyricLine(string text, long? start = null) public LyricLine(string text, long? start = null)
{ {
Start = start; Start = start;

View File

@ -10,10 +10,10 @@ public class LyricResponse
/// <summary> /// <summary>
/// Gets or sets Metadata. /// Gets or sets Metadata.
/// </summary> /// </summary>
public LyricMetadata Metadata { get; set; } = new LyricMetadata(); public LyricMetadata Metadata { get; set; } = new();
/// <summary> /// <summary>
/// Gets or sets Lyrics. /// Gets or sets Lyrics.
/// </summary> /// </summary>
public IReadOnlyCollection<LyricLine> Lyrics { get; set; } = new List<LyricLine>(); public IReadOnlyList<LyricLine> Lyrics { get; set; } = new List<LyricLine>();
} }

View File

@ -38,7 +38,7 @@ public class LrcLyricProvider : ILyricProvider
public ResolverPriority Priority => ResolverPriority.First; public ResolverPriority Priority => ResolverPriority.First;
/// <inheritdoc /> /// <inheritdoc />
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc" }; public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc" };
/// <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.
@ -54,8 +54,8 @@ public class LrcLyricProvider : ILyricProvider
return null; return null;
} }
List<LyricLine> lyricList = new List<LyricLine>(); List<LyricLine> lyricList = new();
List<LrcParser.Model.Lyric> sortedLyricData = new List<LrcParser.Model.Lyric>(); List<LrcParser.Model.Lyric> sortedLyricData = new();
IDictionary<string, string> fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); IDictionary<string, string> fileMetaData = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath); string lrcFileContent = System.IO.File.ReadAllText(lyricFilePath);
@ -85,19 +85,11 @@ public class LrcLyricProvider : ILyricProvider
string[] metaDataField; string[] metaDataField;
string metaDataFieldName; string metaDataFieldName;
string metaDataFieldValue; string metaDataFieldValue;
string[] test;
if (colonCount == 1) metaDataField = metaDataRow.Split(':', 2, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
{ metaDataFieldName = metaDataField[0][1..].Trim();
metaDataField = metaDataRow.Split(':'); metaDataFieldValue = metaDataField[1][..^1].Trim();
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);
} }
@ -142,7 +134,7 @@ public class LrcLyricProvider : ILyricProvider
/// <returns>A lyricMetadata object with mapped property data.</returns> /// <returns>A lyricMetadata object with mapped property data.</returns>
private LyricMetadata MapMetadataValues(IDictionary<string, string> metaData) private LyricMetadata MapMetadataValues(IDictionary<string, string> metaData)
{ {
LyricMetadata lyricMetadata = new LyricMetadata(); LyricMetadata lyricMetadata = new();
if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist)) if (metaData.TryGetValue("ar", out var artist) && !string.IsNullOrEmpty(artist))
{ {
@ -166,20 +158,7 @@ public class LrcLyricProvider : ILyricProvider
if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length)) if (metaData.TryGetValue("length", out var length) && !string.IsNullOrEmpty(length))
{ {
// Ensure minutes include leading zero if (DateTime.TryParseExact(length, new string[] { "HH:mm:ss", "H:mm:ss", "mm:ss", "m:ss" }, null, DateTimeStyles.None, out var value))
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; lyricMetadata.Length = value.TimeOfDay.Ticks;
} }

View File

@ -20,7 +20,7 @@ public class TxtLyricProvider : ILyricProvider
public ResolverPriority Priority => ResolverPriority.Second; public ResolverPriority Priority => ResolverPriority.Second;
/// <inheritdoc /> /// <inheritdoc />
public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "txt" }; public IReadOnlyCollection<string> SupportedMediaTypes { get; } = new[] { "lrc", "elrc", "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.
@ -38,7 +38,7 @@ public class TxtLyricProvider : ILyricProvider
string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath); string[] lyricTextLines = System.IO.File.ReadAllLines(lyricFilePath);
List<LyricLine> lyricList = new List<LyricLine>(); List<LyricLine> lyricList = new();
if (lyricTextLines.Length == 0) if (lyricTextLines.Length == 0)
{ {