Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Created May 28, 2018 00:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save guitarrapc/bea0c0dd67fa55330e33cfc630b9ece5 to your computer and use it in GitHub Desktop.
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