From 5520162a4c1cb5103462f51f2d92701b1265a044 Mon Sep 17 00:00:00 2001 From: real-zony Date: Sat, 5 Jun 2021 11:52:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=BD=91=E6=98=93?= =?UTF-8?q?=E4=BA=91=E6=AD=8C=E8=AF=8D=E5=8C=B9=E9=85=8D=E6=9C=BA=E5=88=B6?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=BC=BA=E5=87=86=E7=A1=AE=E5=BA=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #75 --- .../Album/NetEase/NetEaseAlbumDownloader.cs | 2 +- .../NetEase/JsonModel/SongSearchResponse.cs | 6 +++-- .../Lyric/NetEase/NetEaseLyricDownloader.cs | 2 +- .../Lyric/KuGouLyricDownloaderTests.cs | 15 +++++++----- .../Lyric/NetEaseLyricDownloaderTests.cs | 23 +++++++++++++++---- .../Lyric/QQLyricDownloaderTests.cs | 14 +++++++---- 6 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Album/NetEase/NetEaseAlbumDownloader.cs b/src/ZonyLrcTools.Cli/Infrastructure/Album/NetEase/NetEaseAlbumDownloader.cs index efa1c60..5675749 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Album/NetEase/NetEaseAlbumDownloader.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Album/NetEase/NetEaseAlbumDownloader.cs @@ -51,7 +51,7 @@ namespace ZonyLrcTools.Cli.Infrastructure.Album.NetEase var songDetailJsonStr = await _warpHttpClient.GetAsync( GetMusicInfoApi, - new GetSongDetailsRequest(searchResult.GetFirstSongId()), + new GetSongDetailsRequest(searchResult.GetFirstMatchSongId(songName)), _defaultOption); var url = JObject.Parse(songDetailJsonStr).SelectToken("$.songs[0].album.picUrl")?.Value(); diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/JsonModel/SongSearchResponse.cs b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/JsonModel/SongSearchResponse.cs index 7217cc9..db3495d 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/JsonModel/SongSearchResponse.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/JsonModel/SongSearchResponse.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Newtonsoft.Json; namespace ZonyLrcTools.Cli.Infrastructure.Lyric.NetEase.JsonModel @@ -9,9 +10,10 @@ namespace ZonyLrcTools.Cli.Infrastructure.Lyric.NetEase.JsonModel [JsonProperty("code")] public int StatusCode { get; set; } - public int GetFirstSongId() + public int GetFirstMatchSongId(string songName) { - return Items.SongItems[0].Id; + var item = Items.SongItems.FirstOrDefault(x => x.Name == songName); + return item?.Id ?? Items.SongItems[0].Id; } } diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs index 44ba48d..3ffe104 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Lyric/NetEase/NetEaseLyricDownloader.cs @@ -48,7 +48,7 @@ namespace ZonyLrcTools.Cli.Infrastructure.Lyric.NetEase var lyricResponse = await _warpHttpClient.GetAsync( NetEaseGetLyricUrl, - new GetLyricRequest(searchResult.GetFirstSongId()), + new GetLyricRequest(searchResult.GetFirstMatchSongId(args.SongName)), msg => msg.Headers.Referrer = new Uri(NetEaseRequestReferer)); return Encoding.UTF8.GetBytes(lyricResponse); diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/KuGouLyricDownloaderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/KuGouLyricDownloaderTests.cs index ad6b338..89b8c87 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/KuGouLyricDownloaderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/KuGouLyricDownloaderTests.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.Extensions.DependencyInjection; using Shouldly; using Xunit; using ZonyLrcTools.Cli.Infrastructure.Lyric; @@ -10,14 +9,18 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyric { public class KuGouLyricDownloaderTests : TestBase { + private readonly ILyricDownloader _lyricDownloader; + + public KuGouLyricDownloaderTests() + { + _lyricDownloader = GetService>() + .FirstOrDefault(t => t.DownloaderName == InternalLyricDownloaderNames.KuGou); + } + [Fact] public async Task DownloadAsync_Test() { - var downloaderList = ServiceProvider.GetRequiredService>(); - var kuGouDownloader = downloaderList.FirstOrDefault(t => t.DownloaderName == InternalLyricDownloaderNames.KuGou); - - kuGouDownloader.ShouldNotBeNull(); - var lyric = await kuGouDownloader.DownloadAsync("东方红", null); + var lyric = await _lyricDownloader.DownloadAsync("东方红", null); lyric.ShouldNotBeNull(); lyric.IsPruneMusic.ShouldBe(false); } diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs index c8bb603..26957ff 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/NetEaseLyricDownloaderTests.cs @@ -10,16 +10,29 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyric { public class NetEaseLyricDownloaderTests : TestBase { + private readonly ILyricDownloader _lyricDownloader; + + public NetEaseLyricDownloaderTests() + { + _lyricDownloader = GetService>() + .FirstOrDefault(t => t.DownloaderName == InternalLyricDownloaderNames.NetEase); + } + [Fact] public async Task DownloadAsync_Test() { - var downloaderList = ServiceProvider.GetRequiredService>(); - var netEaseDownloader = downloaderList.FirstOrDefault(t => t.DownloaderName == InternalLyricDownloaderNames.NetEase); - - netEaseDownloader.ShouldNotBeNull(); - var lyric = await netEaseDownloader.DownloadAsync("Hollow", "Janet Leon"); + var lyric = await _lyricDownloader.DownloadAsync("Hollow", "Janet Leon"); lyric.ShouldNotBeNull(); lyric.IsPruneMusic.ShouldBe(false); } + + [Fact] + public async Task DownloadAsync_Issue_75_Test() + { + var lyric = await _lyricDownloader.DownloadAsync("Daybreak", "samfree,初音ミク"); + lyric.ShouldNotBeNull(); + lyric.IsPruneMusic.ShouldBe(false); + lyric.ToString().Contains("惑う心繋ぎ止める").ShouldBeTrue(); + } } } \ 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 06b381b..de5f1b5 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/QQLyricDownloaderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Lyric/QQLyricDownloaderTests.cs @@ -10,14 +10,18 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyric { public class QQLyricDownloaderTests : TestBase { + private readonly ILyricDownloader _lyricDownloader; + + public QQLyricDownloaderTests() + { + _lyricDownloader = GetService>() + .FirstOrDefault(t => t.DownloaderName == InternalLyricDownloaderNames.QQ); + } + [Fact] public async Task DownloadAsync_Test() { - var downloaderList = ServiceProvider.GetRequiredService>(); - var qqDownloader = downloaderList.FirstOrDefault(t => t.DownloaderName == InternalLyricDownloaderNames.QQ); - - qqDownloader.ShouldNotBeNull(); - var lyric = await qqDownloader.DownloadAsync("Hollow", "Janet Leon"); + var lyric = await _lyricDownloader.DownloadAsync("Hollow", "Janet Leon"); lyric.ShouldNotBeNull(); lyric.IsPruneMusic.ShouldBe(false); }