1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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 LyricProvider
- {
- 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 bool GetDetail(LyricSearchResultItem input);
- public static List<LyricDetailExt> ParseLrc(string input, int duration)
- {
- var matches = RegexLrc.Matches(input);
- var ret = new List<LyricDetailExt>();
- foreach (Match match in matches)
- {
- var sameList = new List<LyricDetailExt>();
- var p = match;
- string text;
- do
- {
- var timeStamp = $"{p.Groups[1]}:{p.Groups[2]},{p.Groups[3].Value.PadLeft(3, '0')}";
- text = p.Groups[4].Value;
- var item = new LyricDetailExt
- {
- TimeMs = (int)TimeSpan.ParseExact(timeStamp, @"mm\:ss\,fff", null).TotalMilliseconds,
- Text = text,
- };
- ret.Add(item);
- sameList.Add(item);
- p = RegexLrc.Match(text); // handle multi time stamp tag
- } while (p.Success);
- if (sameList.Count <= 1) continue;
- foreach (var same in sameList) same.Text = text;
- }
- ret = ret.GroupBy(p => new { p.TimeMs, p.Text }).Select(q => q.First()).ToList();
- ret = ret.Where(p => false == string.IsNullOrWhiteSpace(p.Text)).OrderBy(p => p.TimeMs).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;
- }
- }
- }
|