protected override async ValueTask <byte[]> DownloadDataAsync(LyricDownloaderArgs args) { var searchResult = await _warpHttpClient.GetAsync <SongSearchResponse>( QQSearchMusicUrl, new SongSearchRequest(args.SongName, args.Artist)); ValidateSongSearchResponse(searchResult, args); var lyricJsonString = await _warpHttpClient.GetAsync(QQGetLyricUrl, new GetLyricRequest(searchResult.Data.Song.SongItems.FirstOrDefault()?.SongId), op => op.Headers.Referrer = new Uri(QQMusicRequestReferer)); return(Encoding.UTF8.GetBytes(lyricJsonString)); }
protected override async ValueTask <byte[]> DownloadDataAsync(LyricDownloaderArgs args) { var searchResult = await _warpHttpClient.GetAsync <SongSearchResponse>(KuGouSearchMusicUrl, new SongSearchRequest(args.SongName, args.Artist)); ValidateSongSearchResponse(searchResult, args); // 获得特殊的 AccessToken 与 Id,真正请求歌词数据。 var accessKeyResponse = await _warpHttpClient.GetAsync <GetLyricAccessKeyResponse>(KuGouGetLyricAccessKeyUrl, new GetLyricAccessKeyRequest(searchResult.Data.List[0].FileHash)); var accessKeyObject = accessKeyResponse.AccessKeyDataObjects[0]; var lyricResponse = await _warpHttpClient.GetAsync(KuGouGetLyricUrl, new GetLyricRequest(accessKeyObject.Id, accessKeyObject.AccessKey)); return(Encoding.UTF8.GetBytes(lyricResponse)); }
protected override async ValueTask <byte[]> DownloadDataAsync(LyricDownloaderArgs args) { var searchResult = await _warpHttpClient.PostAsync <SongSearchResponse>( NetEaseSearchMusicUrl, new SongSearchRequest(args.SongName, args.Artist), true, msg => { msg.Headers.Referrer = new Uri(NetEaseRequestReferer); if (msg.Content != null) { msg.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(NetEaseRequestContentType); } }); ValidateSongSearchResponse(searchResult, args); var lyricResponse = await _warpHttpClient.GetAsync( NetEaseGetLyricUrl, new GetLyricRequest(searchResult.GetFirstMatchSongId(args.SongName)), msg => msg.Headers.Referrer = new Uri(NetEaseRequestReferer)); return(Encoding.UTF8.GetBytes(lyricResponse)); }
protected virtual void ValidateSongSearchResponse(SongSearchResponse response, LyricDownloaderArgs args) { if (response?.StatusCode != SongSearchResponseStatusCode.Success) { throw new ErrorCodeException(ErrorCodes.TheReturnValueIsIllegal, attachObj: args); } if (response.Items?.SongCount <= 0) { throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args); } }
protected override async ValueTask <LyricItemCollection> GenerateLyricAsync(byte[] data, LyricDownloaderArgs args) { await ValueTask.CompletedTask; var json = JsonConvert.DeserializeObject <GetLyricResponse>(Encoding.UTF8.GetString(data)); if (json?.OriginalLyric == null) { return(new LyricItemCollection(null)); } if (json.OriginalLyric.Text.Contains("纯音乐,请欣赏")) { return(new LyricItemCollection(null)); } return(_lyricItemCollectionFactory.Build( json.OriginalLyric.Text, json.TranslationLyric.Text)); }
protected virtual void ValidateSongSearchResponse(SongSearchResponse response, LyricDownloaderArgs args) { if (response.ErrorCode != 0 && response.Status != 1 || response.Data.List == null) { throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args); } }
protected override async ValueTask <LyricItemCollection> GenerateLyricAsync(byte[] data, LyricDownloaderArgs args) { await ValueTask.CompletedTask; var lyricJsonObj = JObject.Parse(Encoding.UTF8.GetString(data)); if (lyricJsonObj.SelectToken("$.status").Value <int>() != 200) { throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args); } var lyricText = Encoding.UTF8.GetString(Convert.FromBase64String(lyricJsonObj.SelectToken("$.content").Value <string>())); return(_lyricItemCollectionFactory.Build(lyricText)); }
protected virtual void ValidateSongSearchResponse(SongSearchResponse response, LyricDownloaderArgs args) { if (response is not { StatusCode : 0 } || response.Data.Song.SongItems == null)
protected override async ValueTask <LyricItemCollection> GenerateLyricAsync(byte[] data, LyricDownloaderArgs args) { await ValueTask.CompletedTask; var lyricJsonString = Encoding.UTF8.GetString(data); lyricJsonString = lyricJsonString.Replace(@"MusicJsonCallback(", string.Empty).TrimEnd(')'); if (lyricJsonString.Contains("\"code\":-1901")) { throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args); } if (lyricJsonString.Contains("此歌曲为没有填词的纯音乐,请您欣赏")) { return(_lyricItemCollectionFactory.Build(null)); } var lyricJsonObj = JObject.Parse(lyricJsonString); var sourceLyric = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(lyricJsonObj.SelectToken("$.lyric").Value <string>())); var translateLyric = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(lyricJsonObj.SelectToken("$.trans").Value <string>())); return(_lyricItemCollectionFactory.Build(sourceLyric, translateLyric)); }