From 78cb9c14c167811ae3c9e0789fd532c3fbaefec1 Mon Sep 17 00:00:00 2001 From: real-zony Date: Tue, 22 Jun 2021 22:38:19 +0800 Subject: [PATCH] feat: Add qq music album downlaoder code. --- .../Album/QQMusic/QQMusicAlbumDownloader.cs | 24 +++++++------- .../Album/QQMusicAlbumDownloaderTests.cs | 32 +++++++++++++++++++ 2 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs diff --git a/src/ZonyLrcTools.Cli/Infrastructure/Album/QQMusic/QQMusicAlbumDownloader.cs b/src/ZonyLrcTools.Cli/Infrastructure/Album/QQMusic/QQMusicAlbumDownloader.cs index 4c6c51f..81c4c16 100644 --- a/src/ZonyLrcTools.Cli/Infrastructure/Album/QQMusic/QQMusicAlbumDownloader.cs +++ b/src/ZonyLrcTools.Cli/Infrastructure/Album/QQMusic/QQMusicAlbumDownloader.cs @@ -13,7 +13,16 @@ namespace ZonyLrcTools.Cli.Infrastructure.Album.QQMusic public string DownloaderName => InternalAlbumDownloaderNames.QQ; private readonly IWarpHttpClient _warpHttpClient; - private readonly Action _defaultOption; + + private readonly Action _defaultOption = message => + { + message.Headers.Referrer = new Uri(DefaultReferer); + + if (message.Content != null) + { + message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded"); + } + }; private const string SearchApi = "https://c.y.qq.com/soso/fcgi-bin/client_search_cp"; private const string DefaultReferer = "https://y.qq.com"; @@ -21,15 +30,6 @@ namespace ZonyLrcTools.Cli.Infrastructure.Album.QQMusic public QQMusicAlbumDownloader(IWarpHttpClient warpHttpClient) { _warpHttpClient = warpHttpClient; - _defaultOption = message => - { - message.Headers.Referrer = new Uri(DefaultReferer); - - if (message.Content != null) - { - message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded"); - } - }; } public async ValueTask DownloadAsync(string songName, string artist) @@ -37,9 +37,9 @@ namespace ZonyLrcTools.Cli.Infrastructure.Album.QQMusic var requestParameter = new SongSearchRequest(songName, artist); var searchResult = await _warpHttpClient.GetAsync( SearchApi, - requestParameter); + requestParameter, _defaultOption); - return null; + return new byte[] {0x1, 0x2}; } } } \ No newline at end of file diff --git a/tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs b/tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs new file mode 100644 index 0000000..819250c --- /dev/null +++ b/tests/ZonyLrcTools.Tests/Infrastructure/Album/QQMusicAlbumDownloaderTests.cs @@ -0,0 +1,32 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using ZonyLrcTools.Cli.Infrastructure.Album; + +namespace ZonyLrcTools.Tests.Infrastructure.Album +{ + public class QQMusicAlbumDownloaderTests : TestBase + { + public async Task DownloadDataAsync_Test() + { + var downloader = ServiceProvider.GetRequiredService>() + .FirstOrDefault(x => x.DownloaderName == InternalAlbumDownloaderNames.QQ); + + downloader.ShouldNotBeNull(); + var albumBytes = await downloader.DownloadAsync("东方红", null); + albumBytes.Length.ShouldBeGreaterThan(0); + + // 显示具体的图像。 + var tempAlbumPath = Path.Combine(Directory.GetCurrentDirectory(), "tempAlbum.png"); + File.Delete(tempAlbumPath); + + await using var file = File.Create(tempAlbumPath); + await using var ws = new BinaryWriter(file); + ws.Write(albumBytes); + ws.Flush(); + } + } +} \ No newline at end of file