-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchInput.fsx
36 lines (29 loc) · 1.21 KB
/
fetchInput.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
open System
open System.IO
open System.Net
open System.Net.Http
let fetchWebsiteContent (httpClient:HttpClient) (url: string) =
async {
let! response = httpClient.GetAsync(url) |> Async.AwaitTask
let! content = response.Content.ReadAsStringAsync() |> Async.AwaitTask
return content
}
let fetchAocInput (year:int) (day:int) (sessionCookie:string) =
let aocUrl = "https://adventofcode.com"
use handler = new HttpClientHandler()
handler.CookieContainer.Add(new Uri(aocUrl), Cookie("session", sessionCookie))
use httpClient = new HttpClient(handler)
fetchWebsiteContent httpClient $"{aocUrl}/{year}/day/{day}/input"
|> Async.RunSynchronously
let usage = """Usage: dotnet fsi fetchInput.fsx [year] [day] [sessionCoockie]
Downloads AoC input and saves it into 'AoC-[year]/inputs/day[day].txt'"""
match Environment.GetCommandLineArgs() with
| [| _; _; year; day; sessionCookie |] ->
match year |> Int32.TryParse, day |> Int32.TryParse with
| (true, year), (true, day) ->
use writer = new StreamWriter($"AoC-{year}/inputs/day{day}.txt")
fetchAocInput year day sessionCookie
|> writer.Write
| _ -> printfn "%s" usage
| _s ->
printfn "%s" usage