summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorcos <cos>2020-12-01 08:49:34 +0100
committercos <cos>2020-12-01 11:41:29 +0100
commit7b0a1642a13077d5931469d6281ea20a98ec0ace (patch)
treebea6d9125f996332af0885741495a130c254bc71 /bin
parent2dee27ef7a77b0806c8a3142a191c19bb827d000 (diff)
downloadadventofcode-7b0a1642a13077d5931469d6281ea20a98ec0ace.zip
Initial addition of support scripts
Diffstat (limited to 'bin')
-rwxr-xr-xbin/cookies_from_chromium.sh13
-rwxr-xr-xbin/fetch_input.sh55
-rwxr-xr-xbin/init_rust.sh26
3 files changed, 94 insertions, 0 deletions
diff --git a/bin/cookies_from_chromium.sh b/bin/cookies_from_chromium.sh
new file mode 100755
index 0000000..208cf14
--- /dev/null
+++ b/bin/cookies_from_chromium.sh
@@ -0,0 +1,13 @@
+#!/bin/sh -e
+
+for C in "${HOME}/.config/chrom"*/*'/Cookies'
+do
+ echo "${C}"
+ printf '%s\n%s%s\n' '.headers on' \
+ 'SELECT name, value, hex(encrypted_value) FROM cookies ' \
+ 'WHERE host_key=".adventofcode.com";' | sqlite3 "${C}"
+done
+
+echo 'This is a reminder of how cookies are encrypted.' \
+ 'Manual export is required.' >&2
+exit 1
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
diff --git a/bin/init_rust.sh b/bin/init_rust.sh
new file mode 100755
index 0000000..38825b1
--- /dev/null
+++ b/bin/init_rust.sh
@@ -0,0 +1,26 @@
+#!/bin/sh -e
+
+YEAR=${YEAR:-$(date +%Y)}
+DAY=${DAY:-$(TZ=EST date '+%d')}
+
+GIT_USER=$(git config --get 'user.name')
+GIT_EMAIL=$(git config --get 'user.email')
+
+CARGO=$( sed "s/^#\( *${DAY}\)/\1/" <"${YEAR}/rust/Cargo.toml")
+echo "${CARGO}" >"${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}"
+version = "0.1.0"
+authors = ["${GIT_USER} <${GIT_EMAIL}>"]
+edition = "2018"
+
+[dependencies]
+END_OF_TOML