diff --git a/src/ZonyLrcTools.Common/Album/NetEase/NetEaseAlbumProvider.cs b/src/ZonyLrcTools.Common/Album/NetEase/NetEaseAlbumProvider.cs index 2d08c5c..a849fbf 100644 --- a/src/ZonyLrcTools.Common/Album/NetEase/NetEaseAlbumProvider.cs +++ b/src/ZonyLrcTools.Common/Album/NetEase/NetEaseAlbumProvider.cs @@ -41,7 +41,7 @@ namespace ZonyLrcTools.Common.Album.NetEase true, _defaultOption); - if (searchResult is not { StatusCode: 200 } || searchResult.Items?.SongCount <= 0) + if (searchResult is not { StatusCode: 200 } || searchResult.Items is not { SongCount: > 0 }) { throw new ErrorCodeException(ErrorCodes.NoMatchingSong); } diff --git a/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs b/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs index 74baf1f..a78440f 100644 --- a/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs +++ b/src/ZonyLrcTools.Common/Lyrics/LyricsDownloader.cs @@ -107,7 +107,6 @@ public class LyricsDownloader : ILyricsDownloader, ISingletonDependency { _logger.LogWarningInfo(ex); info.IsSuccessful = false; - throw; } catch (Exception ex) { diff --git a/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/JsonModel/SongSearchResponse.cs b/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/JsonModel/SongSearchResponse.cs index 6955e70..67150d5 100644 --- a/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/JsonModel/SongSearchResponse.cs +++ b/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/JsonModel/SongSearchResponse.cs @@ -4,12 +4,17 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel { public class SongSearchResponse { - [JsonProperty("result")] public InnerListItemModel Items { get; set; } + [JsonProperty("result")] public InnerListItemModel? Items { get; set; } [JsonProperty("code")] public int StatusCode { get; set; } public int GetFirstMatchSongId(string songName, long? duration) { + if (Items == null || Items.SongItems == null) + { + Console.Write("xx"); + } + var perfectMatch = Items.SongItems.FirstOrDefault(x => x.Name == songName); if (perfectMatch != null) { @@ -27,7 +32,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase.JsonModel public class InnerListItemModel { - [JsonProperty("songs")] public IList SongItems { get; set; } + [JsonProperty("songs")] public IList? SongItems { get; set; } [JsonProperty("songCount")] public int SongCount { get; set; } } diff --git a/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/NetEaseLyricsProvider.cs b/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/NetEaseLyricsProvider.cs index 3ba76b6..8e52774 100644 --- a/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/NetEaseLyricsProvider.cs +++ b/src/ZonyLrcTools.Common/Lyrics/Providers/NetEase/NetEaseLyricsProvider.cs @@ -21,7 +21,6 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase private const string NetEaseGetLyricUrl = @"https://music.163.com/weapi/song/lyric?csrf_token="; private const string NetEaseRequestReferer = @"https://music.163.com"; - private const string NetEaseRequestContentType = @"application/x-www-form-urlencoded"; public NetEaseLyricsProvider(IWarpHttpClient warpHttpClient, ILyricsItemCollectionFactory lyricsItemCollectionFactory, @@ -49,7 +48,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase ValidateSongSearchResponse(searchResult, args); - return await _warpHttpClient.PostAsync(NetEaseGetLyricUrl, + return await _warpHttpClient.PostAsync(NetEaseGetLyricUrl, requestOption: request => { request.Headers.Referrer = new Uri(NetEaseRequestReferer); @@ -87,7 +86,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase throw new ErrorCodeException(ErrorCodes.TheReturnValueIsIllegal, attachObj: args); } - if (response.Items?.SongCount <= 0) + if (response.Items is not { SongCount: > 0 }) { throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args); } diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Lyrics/NetEaseLyricsProviderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Lyrics/NetEaseLyricsProviderTests.cs index c4c8611..04f7b0f 100644 --- a/tests/ZonyLrcTools.Tests/Infrastructure/Lyrics/NetEaseLyricsProviderTests.cs +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Lyrics/NetEaseLyricsProviderTests.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using Shouldly; using Xunit; using ZonyLrcTools.Common.Configuration; +using ZonyLrcTools.Common.Infrastructure.Exceptions; using ZonyLrcTools.Common.Lyrics; namespace ZonyLrcTools.Tests.Infrastructure.Lyrics @@ -105,12 +106,19 @@ namespace ZonyLrcTools.Tests.Infrastructure.Lyrics var lyric = await _lyricsProvider.DownloadAsync("橄榄树", "苏曼"); lyric.ToString().ShouldNotBeNullOrEmpty(); } - + [Fact] public async Task DownloadAsync_Issue133_Test() { var lyric = await _lyricsProvider.DownloadAsync("Everything", "Yinyues"); lyric.ToString().ShouldNotBeNullOrEmpty(); } + + [Fact] + public async Task DownloadAsync_NullException_Test() + { + var result = await Should.ThrowAsync(_lyricsProvider.DownloadAsync("創世記", "りりィ").AsTask); + result.ErrorCode.ShouldBe(ErrorCodes.NoMatchingSong); + } } } \ No newline at end of file