#!/bin/sh -eu _mode="wget" _cookies="${HOME}/.adventofcode.json" usage() { print "%s\n\n%s\n%s\n%s\n" "$0 [-h] [-y ] [-d ]" \ '-h | --help Output this help.' \ '-y | --help Use another year than the current one.' \ '-d | --help Use another day than the current one.' } _opts=$( getopt --options "y:d:h" --long 'year:,day:,help' -- "$@" ) eval set -- "${_opts}" unset _opts while [ $1 != '--' ]; do case "$1" in '-d' | '--day') shift _day="$1" ;; '-y' | '--year') shift _year="$1" ;; '-h' | '--help') usage exit 0 ;; esac shift done _year=${_year:-$(date +%Y)} [ "${_day:-}" ] || _day=${_day:-$(TZ=EST date '+%d')} [ "${#_day}" != 1 ] || _day="0${_day}" _natural_day="$( echo "${_day}" | sed 's/^0//' )" # https://stackoverflow.com/q/21919156/how-do-i-copy-cookies-from-chrome _session=$(jq < "${_cookies}" '."session-cookie"') check_dependency_availability() { for bin in 'elinks' \ 'jq' \ 'wget' do if ! which "${bin}" >/dev/null; then echo "Missing dependency: ${bin}" >&2 exit 1 fi done unset bin } concat() { for part in "$@"; do printf '%s' "${part}" done unset part } 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_${_natural_day}.html" \ "https://adventofcode.com/${_year}/day/${_natural_day}" _settings=$(authenticated_wget --output-document='-' \ "https://adventofcode.com/${_year}/settings") _username=$(echo "${_settings}" | sed -n 's#.*display_name.*checked.*\(.*\).*#\1#p') echo "Found username: ${_username}" authenticated_wget --output-document "input/${_username}" \ "https://adventofcode.com/${_year}/day/${_natural_day}/input" elinks -dump "./input/page_${_natural_day}.html" \ >"input/page_${_natural_day}.txt" sed -n 's/^ \([^ ]\)/\1/p' <"input/page_${_natural_day}.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 unset _cookies _day _mode _natural_day _session _settings _username _year # vim: sw=2 et