Created
May 28, 2018 00:25
-
-
Save guitarrapc/bea0c0dd67fa55330e33cfc630b9ece5 to your computer and use it in GitHub Desktop.
PowerShellっぽく陸上自衛隊のイラク派遣日報をまとめてダウンロードしてみるのPowerShell非同期版
This file contains 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
$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