diff options
author | cos <cos> | 2022-12-04 11:41:29 +0000 |
---|---|---|
committer | cos <cos> | 2022-12-05 11:14:05 +0000 |
commit | 5886b1144dac92767b49028bd0d7093667fdafb3 (patch) | |
tree | 49c9b7afdf44721e17c0aaa31df399b9341c4211 /bin/fetch_input | |
parent | b08254347f9d9d7812fb513f046918b0cd00c501 (diff) | |
download | adventofcode-5886b1144dac92767b49028bd0d7093667fdafb3.zip |
Adapt fetch_input and init_rust to use getopt
Diffstat (limited to 'bin/fetch_input')
-rwxr-xr-x | bin/fetch_input | 92 |
1 files changed, 64 insertions, 28 deletions
diff --git a/bin/fetch_input b/bin/fetch_input index ec09328..7c3ac3f 100755 --- a/bin/fetch_input +++ b/bin/fetch_input @@ -1,20 +1,48 @@ #!/bin/sh -eu -# vim: sw=2 et -COOKIES="${HOME}/.adventofcode.json" +_mode="wget" +_cookies="${HOME}/.adventofcode.json" + +usage() { + print "%s\n\n%s\n%s\n%s\n" "$0 [-h] [-y <year>] [-d <day>]" \ + '-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' -- "$@" ) -YEAR=${YEAR:-$(date +%Y)} -DAY=${DAY:-$(TZ=EST date '+%d')} -DEC=$(echo "${DAY}" | sed 's/^0//') +eval set -- "${_opts}" +unset _opts -MODE="wget" +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"') +_session=$(jq < "${_cookies}" '."session-cookie"') check_dependency_availability() { - local bin - for bin in 'elinks' \ 'jq' \ 'wget' @@ -24,51 +52,55 @@ check_dependency_availability() { exit 1 fi done + + unset bin } concat() { - local part - for part in "$@"; do printf '%s' "${part}" done + + unset part } authenticated_wget() { wget \ --server-response \ - --header "Cookie: session=${SESSION}" \ + --header "Cookie: session=${_session}" \ "${@}" } check_dependency_availability -if [ "${MODE}" = 'wget' ]; then - cd "${YEAR}" +if [ "${_mode}" = 'wget' ]; then + cd "${_year}" cd 'rust' - mkdir "day${DAY}" || : - cd "day${DAY}" + 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 '"') + _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}" | + 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.*<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" + 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 +elif [ "${_mode}" = 'aocdl' ]; then # One could possibly use the golang implemenation instead of wget: # https://github.com/GreenLightning/advent-of-code-downloader # @@ -77,3 +109,7 @@ elif [ "${MODE}" = 'aocdl' ]; then # 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 |