Skip to content

Commit

Permalink
Merge pull request #141 from jitwxs/dev
Browse files Browse the repository at this point in the history
163MusicLyrics v5.5
  • Loading branch information
jitwxs committed Dec 10, 2022
2 parents 39900e0 + 0043c56 commit 16a05ee
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion MusicLyricApp/Bean/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace MusicLyricApp.Bean
{
public static class Constants
{
public const string Version = "v5.4";
public const string Version = "v5.5";

public static readonly string SettingPath = Environment.CurrentDirectory + "\\MusicLyricAppSetting.json";

Expand Down
17 changes: 9 additions & 8 deletions MusicLyricApp/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private void ReloadInputIdText()
}
catch (WebException ex)
{
_logger.Error(ex, "SearchBySongId network error, delay: {Delay}", NetworkUtils.GetWebRoundtripTime(50));
_logger.Error(ex, "SearchBySongId network error, songId: {SongId}, delay: {Delay}", songId, NetworkUtils.GetWebRoundtripTime(50));
songResult = ResultVo<SaveVo>.Failure(ErrorMsg.NETWORK_ERROR);
}
catch (System.Exception ex)
Expand All @@ -242,7 +242,7 @@ private void ReloadInputIdText()
/// <summary>
/// 初始化输入的歌曲 ID 列表
/// </summary>
private void InitInputSongIds()
private HashSet<string> InitInputSongIds()
{
var inputs = _globalSearchInfo.InputIds;
if (inputs.Length < 1)
Expand Down Expand Up @@ -277,6 +277,8 @@ private void InitInputSongIds()
throw new MusicLyricException(ErrorMsg.SYSTEM_ERROR);
}
}

return _globalSearchInfo.SongIds;
}

/// <summary>
Expand Down Expand Up @@ -350,9 +352,7 @@ public async void Search_Btn_Click(object sender, EventArgs e)

try
{
InitInputSongIds();

var songIds = _globalSearchInfo.SongIds;
var songIds = InitInputSongIds();
if (songIds.Count > 1)
{
BatchSearch(songIds);
Expand All @@ -364,18 +364,19 @@ public async void Search_Btn_Click(object sender, EventArgs e)
}
catch (WebException ex)
{
_logger.Error(ex, "Search Network error, delay: {Delay}", await NetworkUtils.GetWebRoundtripTimeAsync());
_logger.Error(ex, "Search network error, param: {SearchParam}, delay: {Delay}", Search_Text.Text,
await NetworkUtils.GetWebRoundtripTimeAsync());
MessageBox.Show(ErrorMsg.NETWORK_ERROR, "错误");
}
catch (MusicLyricException ex)
{
_logger.Error("Search Business failed, param: {SearchParam}, type: {SearchType}, message: {ErrorMsg}",
_logger.Error("Search music lyric failed, param: {SearchParam}, type: {SearchType}, message: {ErrorMsg}",
Search_Text.Text, _globalSearchInfo.SettingBean.Param.SearchType, ex.Message);
MessageBox.Show(ex.Message, "提示");
}
catch (System.Exception ex)
{
_logger.Error(ex);
_logger.Error("Search music lyric exception, param: {SearchParam}, message: {Ex}", Search_Text.Text, ex);
MessageBox.Show(ErrorMsg.SYSTEM_ERROR, "错误");
}
}
Expand Down
7 changes: 5 additions & 2 deletions MusicLyricApp/Utils/GlobalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,14 @@ public static string CheckInputId(string input, SearchSourceEnum searchSource, S
var sb = new StringBuilder();
foreach (var c in input.Substring(index + urlKeyword.Length).ToCharArray())
{
if (c == '/')
if (char.IsLetterOrDigit(c))
{
sb.Append(c);
}
else
{
break;
}
sb.Append(c);
}

return sb.ToString();
Expand Down
98 changes: 39 additions & 59 deletions MusicLyricApp/Utils/LyricUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MusicLyricApp.Bean;

Expand All @@ -12,6 +13,8 @@ namespace MusicLyricApp.Utils
/// </summary>
public abstract class LyricUtils
{
private static readonly Regex VerbatimRegex = new Regex(@"\(\d+,\d+\)");

/// <summary>
/// 获取输出结果
/// </summary>
Expand Down Expand Up @@ -47,77 +50,54 @@ public static async Task<string> GetOutputContent(LyricVo lyricVo, SearchInfo se
/// </summary>
public static string DealVerbatimLyric(string originLrc, SearchSourceEnum searchSource)
{
var originLyrics = SplitLrc(originLrc);

var defaultParam = new PersistParamBean();
var sb = new StringBuilder();

for (var j = 0; j < originLyrics.Length; j++)
foreach (var line in SplitLrc(originLrc))
{
var content = originLyrics[j];
// skip illegal verbatim line, eg: https://y.qq.com/n/ryqq/songDetail/000sNzbP2nHGs2
if (!line.EndsWith(")"))
{
continue;
}

while (true)
var matches = VerbatimRegex.Matches(line);
if (matches.Count > 0)
{
int i = 0, startA = 0, startB = 0;
for (; i < content.Length; i++)
{
var c = content[i];
int contentStartIndex = 0, i = 0;

bool needReplaceA = false, needReplaceB = false;

switch (c)
{
case '[':
startA = i;
break;
case '(':
startB = i;
break;
case ']':
needReplaceA = true;
break;
case ')':
needReplaceB = true;
break;
}

if (needReplaceA || needReplaceB)
do
{
var curMatch = matches[i];
var group = curMatch.Groups[0];
int leftParenthesesIndex = group.Index, parenthesesLength = group.Length;

// (404,202)
var timestamp = line.Substring(leftParenthesesIndex, parenthesesLength);
// 404
timestamp = timestamp.Split(',')[0].Trim().Substring(1);
// format
timestamp = new LyricTimestamp(long.Parse(timestamp))
.PrintTimestamp(defaultParam.LrcTimestampFormat, defaultParam.DotType);

var content = line.Substring(contentStartIndex, leftParenthesesIndex - contentStartIndex);
// 去除全行时间戳
if (i == 0)
{
var start = needReplaceA ? startA : startB;

var oldValue = content.Substring(start, i - start + 1);

var mid = oldValue.IndexOf(",");
if (mid != -1)
{
var left = oldValue.Substring(1, mid - 1);
var right = oldValue.Substring(mid + 1, oldValue.Length - mid - 2);

// 限制为数字
if (GlobalUtils.CheckNum(left) && GlobalUtils.CheckNum(right))
{
var timestamp = new LyricTimestamp(long.Parse(left));

var newValue = timestamp.PrintTimestamp(defaultParam.LrcTimestampFormat, defaultParam.DotType);

content = content.Replace(oldValue, newValue);
i = 0;

break;
}
}
content = new LyricLineVo(content).Content;
}
}

contentStartIndex = leftParenthesesIndex + parenthesesLength;

if (i >= content.Length)
{
break;
}
sb.Append(timestamp).Append(content);
} while (++i < matches.Count);
}
else
{
sb.Append(line);
}

sb
.Append(content)
.Append(Environment.NewLine);
sb.Append(Environment.NewLine);
}

return sb.ToString();
Expand Down
8 changes: 7 additions & 1 deletion MusicLyricAppTest/Utils/GlobalUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public void TestCheckInputIdWithUrl()
Assert.AreEqual("1815969317",
CheckInputId("https://music.163.com/#/song?id=1815969317", SearchSourceEnum.NET_EASE_MUSIC,
SearchTypeEnum.SONG_ID));
Assert.AreEqual("1431606759",
CheckInputId("https://music.163.com/song?id=1431606759&userid=8073630663", SearchSourceEnum.NET_EASE_MUSIC,
SearchTypeEnum.SONG_ID));
Assert.AreEqual("122305109",
CheckInputId("https://music.163.com/#/album?id=122305109", SearchSourceEnum.NET_EASE_MUSIC,
SearchTypeEnum.ALBUM_ID));
Expand All @@ -56,13 +59,16 @@ public void TestCheckInputIdWithUrl()
Assert.AreEqual("002owtOq052wu9",
CheckInputId("https://y.qq.com/n/ryqq/songDetail/002owtOq052wu9", SearchSourceEnum.QQ_MUSIC,
SearchTypeEnum.SONG_ID));
Assert.AreEqual("002owtOQQ052wu9",
CheckInputId("https://y.qq.com/n/ryqq/songDetail/002owtOQQ052wu9&userid=8073630663", SearchSourceEnum.QQ_MUSIC,
SearchTypeEnum.SONG_ID));
Assert.AreEqual("000k0h474UtgAL",
CheckInputId("https://y.qq.com/n/ryqq/albumDetail/000k0h474UtgAL", SearchSourceEnum.QQ_MUSIC,
SearchTypeEnum.ALBUM_ID));
Assert.AreEqual("8694686726",
CheckInputId("https://y.qq.com/n/ryqq/playlist/8694686726", SearchSourceEnum.QQ_MUSIC,
SearchTypeEnum.PLAYLIST_ID));

Assert.Throws(typeof(MusicLyricException),
() => CheckInputId("https://y.qq.com/n/ryqq/singer/004cyCLc1ByKPx", SearchSourceEnum.QQ_MUSIC,
SearchTypeEnum.SONG_ID));
Expand Down
12 changes: 11 additions & 1 deletion MusicLyricAppTest/Utils/LyricUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using MusicLyricApp.Bean;
using MusicLyricApp.Utils;
using NUnit.Framework;
Expand All @@ -8,6 +9,15 @@ namespace MusicLyricAppTest.Utils
[TestFixture]
public class LyricUtilsTest
{
[Test]
public void TestDealVerbatimLyric()
{
const string line = "[11562,5703]こ(11562,356)こ(11918,646)ろ(12564,270)を(12834,798) (13632,10)隠(13642,592)し(14234,356)て(14590,248)ひ(14838,260)と(15098,428)り(15526,396)で(15922,1343)";
var output = LyricUtils.DealVerbatimLyric(line, SearchSourceEnum.QQ_MUSIC);

Assert.AreEqual("[00:11.562]こ[00:11.918]こ[00:12.564]ろ[00:12.834]を[00:13.632] [00:13.642]隠[00:14.234]し[00:14.590]て[00:14.838]ひ[00:15.098]と[00:15.526]り[00:15.922]で" + Environment.NewLine, output);
}

/// <summary>
/// 译文匹配精度误差
/// </summary>
Expand Down
Binary file removed images/playlist_blur_search.gif
Binary file not shown.

0 comments on commit 16a05ee

Please sign in to comment.