summaryrefslogtreecommitdiff
path: root/bin/fetch_input.sh
blob: 6b27931ace4ffc6f9d6deb75104dea2d4b17fbf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/sh -eu

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 '"')

  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}" |
       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"
  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