-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download-Input.ps1
52 lines (43 loc) · 1.28 KB
/
Download-Input.ps1
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This script requires a cookie file with the session token
# Put this in .cookie.txt:
# -- BEGIN --
# # Netscape HTTP Cookie File
# .adventofcode.com TRUE / FALSE 0 session <token-copied-from-browser-devtools>
# -- END --
Param(
[parameter(Mandatory=$true)]
[ValidateRange(1, 25)]
[int]
$Day,
[String]
$Filename="input.txt",
[String]
$CookieFilename=".cookie.txt",
[switch]
$Force=$false
)
$OutputDir="day$Day"
$OutputPath="$OutputDir/$Filename"
$UserAgent="Download-Input.ps1 by github.com/derwiath via cURL"
$URL="https://adventofcode.com/2022/day/$Day/input"
$CommitMsg ="$Day`: Add Input"
If (-Not (Test-Path -Path "$CookieFilename" -PathType Leaf)) {
Write-Error "Failed to find $CookieFilename"
Exit 1
}
ElseIf (-Not (Test-Path -Path "$OutputDir" -PathType Container)) {
Write-Error "Failed to find $OutputDir."
Exit 1
}
ElseIf ((-Not $Force) -And (Test-Path -Path "$OutputPath" -PathType Leaf)) {
Write-Error "$OutputPath already exists. Use -Force to overwrite."
Exit 1
}
Write-Host "curl --cookie $CookieFilename -A `"$UserAgent`" $URL"
curl --cookie $CookieFilename -A `"$UserAgent`" $URL | Set-Content $OutputPath
if (-Not ($LASTEXITCODE -Eq 0)) {
Write-Error "Error: curl returned exit code $LASTEXITCODE"
}
git add $OutputPath
git commit -m $CommitMsg
Exit $LASTEXITCODE