LyricProviderBase.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using BeatLyrics.Tool.DataProvider.OnlineLyric.Models;
  2. using BeatLyrics.Tool.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. namespace BeatLyrics.Tool.DataProvider.OnlineLyric
  8. {
  9. public abstract class LyricProviderBase
  10. {
  11. private static readonly Regex RegexLrc = new Regex(@"\[(\d{2})\:(\d{2})[\.\:](\d{2,3})\](.+)", RegexOptions.Compiled);
  12. public abstract LyricSearchResultItem[] Search(string songName, string artist);
  13. public abstract void GetDetail(LyricSearchResultItem input);
  14. protected static List<LyricDetailExt> ParseLrc(string input, int duration)
  15. {
  16. var matches = RegexLrc.Matches(input);
  17. var ret = matches.Cast<Match>().Select(p =>
  18. {
  19. var timeStamp = $"{p.Groups[1]}:{p.Groups[2]},{p.Groups[3].Value.PadLeft(3, '0')}";
  20. var text = p.Groups[4].Value;
  21. return new LyricDetailExt
  22. {
  23. TimeMs = (int)TimeSpan.ParseExact(timeStamp, @"mm\:ss\,fff", null).TotalMilliseconds,
  24. Text = text,
  25. };
  26. }).ToList();
  27. for (var i = 0; i < ret.Count; i++)
  28. {
  29. var item = ret[i];
  30. var nextItem = ret.Count > i + 1 ? ret[i + 1] : null;
  31. if (nextItem != null) item.DurationMs = nextItem.TimeMs - item.TimeMs;
  32. else item.DurationMs = duration - item.TimeMs;
  33. if (item.DurationMs < 0) item.DurationMs = 1000;
  34. }
  35. return ret;
  36. }
  37. }
  38. }