summaryrefslogtreecommitdiff
path: root/bin/fetch_input
diff options
context:
space:
mode:
authorcos <cos>2022-12-01 16:14:20 +0000
committercos <cos>2022-12-01 17:27:23 +0000
commit1773627631cc873b6a09098dda1cac14c6ed03fa (patch)
tree13e34a2c0348fe20cf2283321189a59c91641f94 /bin/fetch_input
parent4a54a0d97a57df6c1525b9bbfcabdf829e44a4d1 (diff)
downloadadventofcode-1773627631cc873b6a09098dda1cac14c6ed03fa.zip
Initial commit of init_year, poke support scripts
Diffstat (limited to 'bin/fetch_input')
-rwxr-xr-xbin/fetch_input79
1 files changed, 79 insertions, 0 deletions
diff --git a/bin/fetch_input b/bin/fetch_input
new file mode 100755
index 0000000..ec09328
--- /dev/null
+++ b/bin/fetch_input
@@ -0,0 +1,79 @@
+#!/bin/sh -eu
+# vim: sw=2 et
+
+COOKIES="${HOME}/.adventofcode.json"
+
+YEAR=${YEAR:-$(date +%Y)}
+DAY=${DAY:-$(TZ=EST date '+%d')}
+DEC=$(echo "${DAY}" | sed 's/^0//')
+
+MODE="wget"
+
+# https://stackoverflow.com/q/21919156/how-do-i-copy-cookies-from-chrome
+SESSION=$(jq < "${COOKIES}" '."session-cookie"')
+
+check_dependency_availability() {
+ local bin
+
+ for bin in 'elinks' \
+ 'jq' \
+ 'wget'
+ do
+ if ! which "${bin}" >/dev/null; then
+ echo "Missing dependency: ${bin}" >&2
+ exit 1
+ fi
+ done
+}
+
+concat() {
+ local part
+
+ for part in "$@"; do
+ printf '%s' "${part}"
+ done
+}
+
+authenticated_wget() {
+ wget \
+ --server-response \
+ --header "Cookie: session=${SESSION}" \
+ "${@}"
+}
+
+check_dependency_availability
+
+if [ "${MODE}" = 'wget' ]; then
+ cd "${YEAR}"
+ cd 'rust'
+ mkdir "day${DAY}" || :
+ cd "day${DAY}"
+
+ # https://stackoverflow.com/q/21919156/how-do-i-copy-cookies-from-chrome
+ SESSION=$(jq < "${COOKIES}" '."session-cookie"' | tr -d '"')
+
+ if [ -e "input" ]; then
+ echo 'Directory input already exists. Skipping.'
+ else
+ mkdir 'input'
+ authenticated_wget --output-document "input/page_${DEC}.html" \
+ "https://adventofcode.com/${YEAR}/day/${DEC}"
+ SETTINGS=$(authenticated_wget --output-document='-' \
+ "https://adventofcode.com/${YEAR}/settings")
+ USERNAME=$(echo "${SETTINGS}" |
+ sed -n 's#.*display_name.*checked.*<span>\(.*\)</span>.*#\1#p')
+ echo "Found username: ${USERNAME}"
+ authenticated_wget --output-document "input/${USERNAME}" \
+ "https://adventofcode.com/${YEAR}/day/${DEC}/input"
+ elinks -dump "./input/page_${DEC}.html" >"input/page_${DEC}.txt"
+ sed -n 's/^ \([^ ]\)/\1/p' <"input/page_${DEC}.txt" >"input/example.txt"
+ fi
+elif [ "${MODE}" = 'aocdl' ]; then
+ # One could possibly use the golang implemenation instead of wget:
+ # https://github.com/GreenLightning/advent-of-code-downloader
+ #
+ # It has more features, but it is always a hassle to install extra things.
+ #
+ # go get github.com/GreenLightning/advent-of-code-downloader/aocdl
+ ~/.go/bin/aocdl -output "{{.Year}}/rust/{{.Day}}"
+fi