Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShellっぽく陸上自衛隊のイラク派遣日報をまとめてダウンロードしてみるのPowerShell非同期版
$cs = @"
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
public class Test
{
private static HttpClient client = new HttpClient() { Timeout = TimeSpan.FromMinutes(5) };
public static async Task DownloadASync(string[] uriStrings, string directory)
{
await Task.WhenAll(uriStrings.Select(x => new Uri(x))
.Select(async x =>
{
var file = Path.Combine(directory, x.Segments.Last());
using (var res = await client.GetAsync(x, HttpCompletionOption.ResponseHeadersRead))
using (var fileStream = new FileStream(file, FileMode.OpenOrCreate))
{
var s = await res.Content.ReadAsStreamAsync();
await s.CopyToAsync(fileStream);
}
}));
}
}
"@
Add-Type -TypeDefinition $cs
$uris = (Invoke-WebRequest -Uri https://www.asahi.com/articles/ASL4J669JL4JUEHF016.html).Links.href |
Where {$_.EndsWith(".pdf")} |
Sort-Object -Unique
[System.Threading.Tasks.Task]::WaitsAll([Test]::DownloadASync($uris, $pwd))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment