-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_input.sh
executable file
·33 lines (25 loc) · 978 Bytes
/
fetch_input.sh
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
#!/bin/sh
#
# Fetch the input for a given day. Run from the day dir (e.g. foo/2022/d01 will
# fetch for day 1 of 2022)
# Store value of session cookie from site in .session file next to this script
set -e
if [ -f "$(pwd)/input.txt" ]; then
echo "Looks like you already have the input for today."
exit 1
fi
script_path="$(dirname "$(readlink -f "$0")")"
if [ ! -e "$script_path/.session" ]; then
echo "Session data missing"
exit 1
fi
session_key="$(cat "$script_path/.session")"
year=$(basename "$(readlink -f "$(pwd)/..")")
day=$(echo "$(basename "$(pwd)" | grep --only-matching '[0-9]\+')" | sed 's/^0//')
if ! echo "$year" | grep --quiet '^[0-9]\+$' && ! echo "$day" | grep --quiet '^[0-9]\+$'; then
echo "Couldn't find year and day to fetch input for. Got year=$year day=$day"
exit 1
fi
url="https://adventofcode.com/$year/day/$day/input"
curl -o input.txt --user-agent 'fetch_input.sh <[email protected]>' --cookie "session=$session_key" "$url"
echo "Done."