summaryrefslogtreecommitdiff
path: root/bin/fetch_input
blob: 7c3ac3f14064dc4ae2b468f8554a01286724cc73 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/sh -eu

_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' -- "$@"  )

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.*<span>\(.*\)</span>.*#\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