-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchinput
More file actions
executable file
·42 lines (39 loc) · 1.27 KB
/
fetchinput
File metadata and controls
executable file
·42 lines (39 loc) · 1.27 KB
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
#!/bin/zsh
# Copyright 2021 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
# Fetch the personal input file for an Advent of Code day if the time has come.
# Requires a copy of the session cookie value in a .cookie file.
set -e
if [ $# -lt 2 ]; then
print -u 2 "Usage: fetchinput year day"
exit 1
fi
BASE_DIR="${0:h}"
YEAR="${1//[^0-9]}"
DAY="${2//[^0-9]}"
TARGETDAY="$YEAR-12-${(l:2::0:)DAY}"
SERVERDAY=$(TZ=America/New_York date +'%Y-%m-%d')
if [[ "$SERVERDAY" < "$TARGETDAY" ]]; then
print -u 2 "It's not $TARGETDAY yet, it's $SERVERDAY"
exit 2
fi
# Include contact information per requst from @topaz
USER_AGENT="https://github.com/listba/advent-of-code by aoc@trevorstone.org"
COOKIE=session=$(<${BASE_DIR}/.cookie)
URL="https://adventofcode.com/$YEAR/day/$DAY/input"
if [ $DAY -lt 10 ]; then
DAY="0${DAY}"
fi
OUT_DIR="${BASE_DIR}/${YEAR}/resources/day-${DAY}"
OUT_FILE="${OUT_DIR}/input.txt"
if [[ -s "$OUT_FILE" ]]; then
print -u 2 "input for $YEAR $DAY already exists. Delete $OUT_FILE and try again."
exit 1
fi
mkdir -p "$OUT_DIR"
touch "${OUT_DIR}/sample.txt"
print -u 2 "Fetching $URL to $OUT_FILE"
curl -f -b "$COOKIE" -A "$USER_AGENT" "$URL" > "${OUT_FILE}"