summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/fetch_input92
-rwxr-xr-xbin/init_rust52
2 files changed, 105 insertions, 39 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
diff --git a/bin/init_rust b/bin/init_rust
index 1431b87..6425d63 100755
--- a/bin/init_rust
+++ b/bin/init_rust
@@ -1,25 +1,53 @@
#!/bin/sh -eu
-YEAR=${YEAR:-$(date +%Y)}
-DAY=${DAY:-$(TZ=EST date '+%d')}
+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' -- "$@" )
+
+eval set -- "${_opts}"
+unset _opts
-GIT_USER=$(git config --get 'user.name')
-GIT_EMAIL=$(git config --get 'user.email')
+while [ $1 != '--' ]; do
+ case "$1" in
+ '-d' | '--day')
+ shift
+ _day="$1"
+ ;;
+ '-y' | '--year')
+ shift
+ _year="$1"
+ ;;
+ '-h' | '--help')
+ usage
+ exit 0
+ ;;
+ esac
+ shift
+done
-CARGO=$( sed "s/^#\(.*${DAY}\)/\1/" <"${YEAR}/rust/Cargo.toml")
-echo "${CARGO}" >"${YEAR}/rust/Cargo.toml"
+_year=${_year:-$(date +%Y)}
+[ "${_day:-}" ] || _day=${_day:-$(TZ=EST date '+%d')}
-cd "${YEAR}/rust"
-mkdir "day${DAY}" || :
-cd "day${DAY}"
+[ "${#_day}" != 1 ] || _day="0${_day}"
+
+_cargo_contents=$( sed "s/^#\(.*${_day}\)/\1/" <"${_year}/rust/Cargo.toml")
+echo "${_cargo_contents}" >"${_year}/rust/Cargo.toml"
+
+cd "${_year}/rust"
+mkdir "day${_day}" || :
+cd "day${_day}"
cargo init --bin
cat > 'Cargo.toml' << END_OF_TOML
[package]
-name = "day${DAY}"
+name = "day${_day}"
version = "0.1.0"
-authors = ["${GIT_USER} <${GIT_EMAIL}>"]
edition = "2021"
[dependencies]
@@ -81,3 +109,5 @@ fn main() -> Result<()> {
Ok(())
}
END_OF_SRC
+
+unset _cargo_contents _day _year