refactor: Use Object to replace byte arrays when generating lyrics.

This commit is contained in:
real-zony
2022-10-28 10:13:14 +08:00
parent b7b1f36bf5
commit 5d1e90f638
14 changed files with 37 additions and 42 deletions

View File

@@ -29,7 +29,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.KuGou
_options = options.Value;
}
protected override async ValueTask<byte[]> DownloadDataAsync(LyricsProviderArgs args)
protected override async ValueTask<object> DownloadDataAsync(LyricsProviderArgs args)
{
var searchResult = await _warpHttpClient.GetAsync<SongSearchResponse>(KuGouSearchMusicUrl,
new SongSearchRequest(args.SongName, args.Artist, _options.Provider.Lyric.GetLyricProviderOption(DownloaderName).Depth));
@@ -41,16 +41,14 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.KuGou
new GetLyricAccessKeyRequest(searchResult.Data.List[0].FileHash));
var accessKeyObject = accessKeyResponse.AccessKeyDataObjects[0];
var lyricResponse = await _warpHttpClient.GetAsync(KuGouGetLyricUrl,
return await _warpHttpClient.GetAsync(KuGouGetLyricUrl,
new GetLyricRequest(accessKeyObject.Id, accessKeyObject.AccessKey));
return Encoding.UTF8.GetBytes(lyricResponse);
}
protected override async ValueTask<LyricsItemCollection> GenerateLyricAsync(byte[] data, LyricsProviderArgs args)
protected override async ValueTask<LyricsItemCollection> GenerateLyricAsync(object data, LyricsProviderArgs args)
{
await ValueTask.CompletedTask;
var lyricJsonObj = JObject.Parse(Encoding.UTF8.GetString(data));
var lyricJsonObj = JObject.Parse((data as string)!);
if (lyricJsonObj.SelectToken("$.status").Value<int>() != 200)
{
throw new ErrorCodeException(ErrorCodes.NoMatchingSong, attachObj: args);

View File

@@ -32,7 +32,7 @@ public class KuWoLyricsProvider : LyricsProvider
_options = options.Value;
}
protected override async ValueTask<byte[]> DownloadDataAsync(LyricsProviderArgs args)
protected override async ValueTask<object> DownloadDataAsync(LyricsProviderArgs args)
{
var songSearchResponse = await _warpHttpClient.GetAsync<SongSearchResponse>(KuWoSearchMusicUrl,
new SongSearchRequest(args.SongName, args.Artist, pageSize: _options.Provider.Lyric.GetLyricProviderOption(DownloaderName).Depth),
@@ -46,18 +46,16 @@ public class KuWoLyricsProvider : LyricsProvider
ValidateSongSearchResponse(songSearchResponse, args);
var songLyricsResponse = await _warpHttpClient.GetAsync<GetLyricsResponse>(KuWoSearchLyricsUrl,
return await _warpHttpClient.GetAsync<GetLyricsResponse>(KuWoSearchLyricsUrl,
new GetLyricsRequest(songSearchResponse.GetMatchedMusicId(args.SongName, args.Artist, args.Duration)),
op =>
{
op.Headers.UserAgent.Add(UserAgent);
op.Headers.Referrer = new Uri("https://m.kuwo.cn/yinyue/");
});
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(songLyricsResponse.Lyrics));
}
protected override ValueTask<LyricsItemCollection> GenerateLyricAsync(byte[] data, LyricsProviderArgs args)
protected override ValueTask<LyricsItemCollection> GenerateLyricAsync(object lyricsObject, LyricsProviderArgs args)
{
throw new NotImplementedException();
}

View File

@@ -1,5 +1,4 @@
using System.Net.Http.Headers;
using System.Text;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using ZonyLrcTools.Common.Configuration;
@@ -32,7 +31,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
_options = options.Value;
}
protected override async ValueTask<byte[]> DownloadDataAsync(LyricsProviderArgs args)
protected override async ValueTask<object> DownloadDataAsync(LyricsProviderArgs args)
{
var searchResult = await _warpHttpClient.PostAsync<SongSearchResponse>(
NetEaseSearchMusicUrl,
@@ -49,19 +48,17 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
ValidateSongSearchResponse(searchResult, args);
var lyricResponse = await _warpHttpClient.GetAsync(
return await _warpHttpClient.GetAsync(
NetEaseGetLyricUrl,
new GetLyricRequest(searchResult.GetFirstMatchSongId(args.SongName, args.Duration)),
msg => msg.Headers.Referrer = new Uri(NetEaseRequestReferer));
return Encoding.UTF8.GetBytes(lyricResponse);
}
protected override async ValueTask<LyricsItemCollection> GenerateLyricAsync(byte[] data, LyricsProviderArgs args)
protected override async ValueTask<LyricsItemCollection> GenerateLyricAsync(object lyricsObject, LyricsProviderArgs args)
{
await ValueTask.CompletedTask;
var json = JsonConvert.DeserializeObject<GetLyricResponse>(Encoding.UTF8.GetString(data));
var json = JsonConvert.DeserializeObject<GetLyricResponse>((lyricsObject as string)!);
if (json?.OriginalLyric == null || string.IsNullOrEmpty(json.OriginalLyric.Text))
{
return new LyricsItemCollection(null);
@@ -73,8 +70,8 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.NetEase
}
return _lyricsItemCollectionFactory.Build(
json.OriginalLyric?.Text,
json.TranslationLyric?.Text);
json.OriginalLyric.Text,
json.TranslationLyric.Text);
}
protected virtual void ValidateSongSearchResponse(SongSearchResponse response, LyricsProviderArgs args)

View File

@@ -6,7 +6,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.QQMusic.JsonModel
{
[JsonProperty("nobase64")] public int IsNoBase64Encoding { get; set; }
[JsonProperty("songmid")] public string SongId { get; set; }
[JsonProperty("songmid")] public string? SongId { get; set; }
[JsonProperty("platform")] public string ClientPlatform { get; set; }
@@ -20,7 +20,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.QQMusic.JsonModel
{
}
public GetLyricRequest(string songId)
public GetLyricRequest(string? songId)
{
IsNoBase64Encoding = 1;
SongId = songId;

View File

@@ -1,4 +1,3 @@
using System.Text;
using System.Web;
using Newtonsoft.Json.Linq;
using ZonyLrcTools.Common.Infrastructure.Exceptions;
@@ -27,7 +26,7 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.QQMusic
_lyricsItemCollectionFactory = lyricsItemCollectionFactory;
}
protected override async ValueTask<byte[]> DownloadDataAsync(LyricsProviderArgs args)
protected override async ValueTask<object> DownloadDataAsync(LyricsProviderArgs args)
{
var searchResult = await _warpHttpClient.GetAsync<SongSearchResponse>(
QQSearchMusicUrl,
@@ -35,18 +34,16 @@ namespace ZonyLrcTools.Common.Lyrics.Providers.QQMusic
ValidateSongSearchResponse(searchResult, args);
var lyricJsonString = await _warpHttpClient.GetAsync(QQGetLyricUrl,
return 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<LyricsItemCollection> GenerateLyricAsync(byte[] data, LyricsProviderArgs args)
protected override async ValueTask<LyricsItemCollection> GenerateLyricAsync(object lyricsObject, LyricsProviderArgs args)
{
await ValueTask.CompletedTask;
var lyricJsonString = Encoding.UTF8.GetString(data);
var lyricJsonString = (lyricsObject as string)!;
lyricJsonString = lyricJsonString.Replace(@"MusicJsonCallback(", string.Empty).TrimEnd(')');
if (lyricJsonString.Contains("\"code\":-1901"))