From 2c2c34f1a61452f99e7636ee54937d9fdb6dbac4 Mon Sep 17 00:00:00 2001 From: real-zony Date: Sat, 19 Mar 2022 11:38:10 +0800 Subject: [PATCH] feat: NetEase Cloud Music Downloader support dual language lyrics. --- .../Lyric/ILyricItemCollectionFactory.cs | 8 ++++ .../Lyric/LyricItemCollectionFactory.cs | 38 ++++++++++++++++--- .../Lyric/NetEase/NetEaseLyricDownloader.cs | 3 +- src/ZonyLrcTools.Cli/config.yaml | 2 +- .../Lyric/NetEaseLyricDownloaderTests.cs | 13 ++++++- .../Lyric/QQLyricDownloaderTests.cs | 4 +- 6 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/ILyricItemCollectionFactory.cs b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/ILyricItemCollectionFactory.cs index 8177ba8..e8324dd 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/ILyricItemCollectionFactory.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/ILyricItemCollectionFactory.cs @@ -11,5 +11,13 @@ namespace ZonyLrcTools.Cli.Infrastructure.Lyric /// 原始歌词数据。 /// 构建完成的 对象。 LyricItemCollection Build(string sourceLyric); + + /// + /// 根据指定的歌曲数据构建新的 实例。 + /// + /// 原始歌词数据。 + /// 翻译歌词数据。 + /// 构建完成的 对象。 + LyricItemCollection Build(string sourceLyric, string translationLyric); } } \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollectionFactory.cs b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollectionFactory.cs index 1bfabb0..861d482 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollectionFactory.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/LyricItemCollectionFactory.cs @@ -19,19 +19,45 @@ namespace ZonyLrcTools.Cli.Infrastructure.Lyric public LyricItemCollection Build(string sourceLyric) { - var items = new LyricItemCollection(_options.Provider.Lyric.Config); + var lyric = new LyricItemCollection(_options.Provider.Lyric.Config); if (string.IsNullOrEmpty(sourceLyric)) { - return items; + return lyric; } - var regex = new Regex(@"\[\d+:\d+.\d+\].+\n?"); - foreach (Match match in regex.Matches(sourceLyric)) + InternalBuildLyricObject(lyric, sourceLyric); + + return lyric; + } + + public LyricItemCollection Build(string sourceLyric, string translationLyric) + { + var lyric = new LyricItemCollection(_options.Provider.Lyric.Config); + if (string.IsNullOrEmpty(sourceLyric)) { - items.Add(new LyricItem(match.Value)); + return lyric; } - return items; + lyric = InternalBuildLyricObject(lyric, sourceLyric); + + if (_options.Provider.Lyric.Config.IsEnableTranslation && !string.IsNullOrEmpty(translationLyric)) + { + var translatedLyric = InternalBuildLyricObject(new LyricItemCollection(_options.Provider.Lyric.Config), translationLyric); + return lyric + translatedLyric; + } + + return lyric; + } + + private LyricItemCollection InternalBuildLyricObject(LyricItemCollection lyric, string sourceText) + { + var regex = new Regex(@"\[\d+:\d+.\d+\].+\n?"); + foreach (Match match in regex.Matches(sourceText)) + { + lyric.Add(new LyricItem(match.Value)); + } + + return lyric; } } } \ No newline at end of file diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs index 0f64890..a67dac0 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs @@ -70,7 +70,8 @@ namespace ZonyLrcTools.Cli.Infrastructure.Lyric.NetEase } return _lyricItemCollectionFactory.Build( - json.OriginalLyric.Text); + json.OriginalLyric.Text, + json.TranslationLyric.Text); } protected virtual void ValidateSongSearchResponse(SongSearchResponse response, LyricDownloaderArgs args) diff --git a/src/ZonyLrcTools.Cli/config.yaml b/src/ZonyLrcTools.Cli/config.yaml index 980d178..c92b1a0 100644 --- a/src/ZonyLrcTools.Cli/config.yaml +++ b/src/ZonyLrcTools.Cli/config.yaml @@ -43,4 +43,4 @@ globalOption: config: isOneLine: true # 双语歌词是否合并为一行展示。 lineBreak: '\n' # 换行符的类型。 - isEnableTranslation: false # 是否启用翻译歌词。 \ No newline at end of file + isEnableTranslation: true # 是否启用翻译歌词。 \ No newline at end of file diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs index 9068517..99665f7 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Newtonsoft.Json; using Shouldly; using Xunit; using ZonyLrcTools.Cli.Infrastructure.Lyric; @@ -50,5 +49,17 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyric lyric.ShouldNotBeNull(); lyric.IsPruneMusic.ShouldBe(false); } + + // About the new feature mentioned in issue #87. + // Github Issue: https://github.com/real-zony/ZonyLrcToolsX/issues/87 + [Fact] + public async Task DownloadAsync_Issue85_Test() + { + var lyric = await _lyricDownloader.DownloadAsync("Looking at Me", "Sabrina Carpenter"); + + lyric.ShouldNotBeNull(); + lyric.IsPruneMusic.ShouldBeFalse(); + lyric.ToString().ShouldContain("你看起来失了呼吸"); + } } } \ No newline at end of file diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/QQLyricDownloaderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/QQLyricDownloaderTests.cs index 64f8669..303bbc2 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/QQLyricDownloaderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/QQLyricDownloaderTests.cs @@ -17,11 +17,11 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyric _lyricDownloader = GetService>() .FirstOrDefault(t => t.DownloaderName == InternalLyricDownloaderNames.QQ); } - + [Fact] public async Task DownloadAsync_Test() { - var lyric = await _lyricDownloader.DownloadAsync("东风破", "胡歌"); + var lyric = await _lyricDownloader.DownloadAsync("东风破", "周杰伦"); lyric.ShouldNotBeNull(); lyric.IsPruneMusic.ShouldBe(false); }