Created
November 28, 2019 14:42
-
-
Save vabka/88a9b054ee756a53a44691366b16b474 to your computer and use it in GitHub Desktop.
Как скачать файл при помощи puppeteersharp, чтобы он скачался в нужную папку
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using PuppeteerSharp; | |
namespace pptrTest | |
{ | |
class Program | |
{ | |
static async Task Main() | |
{ | |
var revision = await new BrowserFetcher().DownloadAsync(706915); | |
using var browser = await Puppeteer.LaunchAsync(new LaunchOptions | |
{ | |
Headless = false, | |
ExecutablePath = revision.ExecutablePath, | |
}); | |
using var page = (await browser.PagesAsync())[0]; | |
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); | |
var downloadDirectory = Path.Combine(desktop, "downloadChrome"); | |
var downloadManager = new DownloadManager(downloadDirectory); | |
downloadManager.CleanDownloadDirectory(); | |
await downloadManager.SetupPageAsync(page); | |
await page.GoToAsync("https://dotnet.microsoft.com/download/dotnet-core/current/runtime"); | |
const string anchorForDownloadSelector = "a[href='/download/dotnet-core/thank-you/runtime-3.0.1-windows-x64-installer']"; | |
var btn = await page.WaitForSelectorAsync(anchorForDownloadSelector); | |
await btn.ClickAsync(); | |
var file = await downloadManager.WaitForDownload(page); | |
Console.WriteLine(file); | |
} | |
} | |
public class DownloadManager | |
{ | |
private readonly string _downloadDirectory; | |
public DownloadManager(string downloadDirectory) | |
{ | |
_downloadDirectory = downloadDirectory; | |
} | |
public async Task SetupPageAsync(Page page) | |
{ | |
await page.Client.SendAsync("Page.setDownloadBehavior", new | |
{ | |
behavior = "allow", | |
downloadPath = _downloadDirectory, | |
}); | |
} | |
public void CleanDownloadDirectory() | |
{ | |
foreach (var file in Directory.EnumerateFiles(_downloadDirectory)) | |
{ | |
File.Delete(file); | |
} | |
} | |
public async Task<string> WaitForDownload(Page page) | |
{ | |
var responseData = await GetResponseWithFile(page); | |
var contentDisposition = responseData.Headers["content-disposition"]; | |
var fileName = contentDisposition.Split(";")[1].Split("=")[1]; | |
var filePath = Path.Combine(_downloadDirectory, fileName); | |
await WaitForFile(filePath); | |
return filePath; | |
} | |
private static async Task<Response> GetResponseWithFile(Page page) | |
{ | |
var response = await page.WaitForResponseAsync(r => r.Headers.ContainsKey("content-disposition"), | |
new WaitForOptions | |
{ | |
Timeout = 10000 | |
}); | |
return response; | |
} | |
private static async ValueTask WaitForFile(string filePath) | |
{ | |
while (!File.Exists(filePath)) | |
{ | |
await Task.Delay(100); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment