diff options
Diffstat (limited to 'bin/fetch_input.sh')
-rwxr-xr-x | bin/fetch_input.sh | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/bin/fetch_input.sh b/bin/fetch_input.sh new file mode 100755 index 0000000..76bdddd --- /dev/null +++ b/bin/fetch_input.sh @@ -0,0 +1,55 @@ +#!/bin/sh -e + +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"') + +concat() { + local part + + for part in "$@"; do + printf '%s' "${part}" + done +} + +authenticated_wget() { + wget \ + --server-response \ + --header "Cookie: session=${SESSION}" \ + "${@}" +} + +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 '"') + + [ -e "input" ] || { + mkdir 'input' + SETTINGS=$(authenticated_wget --output-document='-' \ + "https://adventofcode.com/${YEAR}/settings") + USERNAME=$(echo "${SETTINGS}" | + sed -n 's#.*display_name.*checked.*<span>\(.*\)</span>.*#\1#p') + authenticated_wget --output-document "input/${USERNAME}" \ + "https://adventofcode.com/${YEAR}/day/${DEC}/input" + } +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 |