-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchinput
executable file
·38 lines (36 loc) · 1.1 KB
/
fetchinput
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
#!/bin/zsh
# Copyright 2024 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
# Fetch the personal input file for an Advent of Code day if the time has come.
# Requires a copy of the session cookie in a .cookie-jar file.
set -e
if [ $# -lt 2 ]; then
print -u 2 "Usage: fetchinput year day"
exit 1
fi
YEAR="${1//[^0-9]}"
DAY="${2//[^0-9]}"
BASEDIR="${0:h}"
if [[ -z "$DAY" ]]; then
print -u 2 "Non-numeric year/day $1/$2"
exit 1
fi
# Include contact information per request from @topaz
USER_AGENT="https://github.com/flwyd/adventofcode by [email protected]"
COOKIES="$BASEDIR/.cookie-jar"
if [[ ! -s "$COOKIES" ]]; then
print -u 2 "Who stole the cookies from ${COOKIES}?"
exit 1
fi
TARGETDAY="$YEAR-12-${(l:2::0:)DAY}"
SERVERDAY=$(TZ=America/New_York date +'%Y-%m-%d')
if [[ "$SERVERDAY" < "$TARGETDAY" ]]; then
print -u 2 "It's not $TARGETDAY yet, it's $SERVERDAY"
exit 2
fi
URL="https://adventofcode.com/$YEAR/day/$DAY/input"
print -u 2 "Fetching $URL"
curl -f -b "$COOKIES" -A "$USER_AGENT" "$URL"