12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using BeatLyrics.Tool.DataProvider.OnlineLyric.Models;
- using BeatLyrics.Tool.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace BeatLyrics.Tool.DataProvider.OnlineLyric
- {
- public abstract class LyricProviderBase
- {
- private static readonly Regex RegexLrc = new Regex(@"\[(\d{2})\:(\d{2})[\.\:](\d{2,3})\](.+)", RegexOptions.Compiled);
- public abstract LyricSearchResultItem[] Search(string songName, string artist);
- public abstract void GetDetail(LyricSearchResultItem input);
- protected static List<LyricDetailExt> ParseLrc(string input, int duration)
- {
- var matches = RegexLrc.Matches(input);
- var ret = matches.Cast<Match>().Select(p =>
- {
- var timeStamp = $"{p.Groups[1]}:{p.Groups[2]},{p.Groups[3].Value.PadLeft(3, '0')}";
- var text = p.Groups[4].Value;
- return new LyricDetailExt
- {
- TimeMs = (int)TimeSpan.ParseExact(timeStamp, @"mm\:ss\,fff", null).TotalMilliseconds,
- Text = text,
- };
- }).ToList();
- for (var i = 0; i < ret.Count; i++)
- {
- var item = ret[i];
- var nextItem = ret.Count > i + 1 ? ret[i + 1] : null;
- if (nextItem != null) item.DurationMs = nextItem.TimeMs - item.TimeMs;
- else item.DurationMs = duration - item.TimeMs;
- if (item.DurationMs < 0) item.DurationMs = 1000;
- }
- return ret;
- }
- }
- }
|