-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcachepuzzle
More file actions
executable file
·53 lines (51 loc) · 1.53 KB
/
cachepuzzle
File metadata and controls
executable file
·53 lines (51 loc) · 1.53 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
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/zsh
# Copyright 2025 Trevor Stone
#
# 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 HTML for an Advent of Code day if the time has come.
# Requires a copy of the session cookie in a .cookie-jar file.
# Does nothing if the puzzle file is newer than the last change to
# input.actual.expected
set -e
VERBOSE=''
if [[ $1 == '-v' ]]; then
VERBOSE='-v'
shift
fi
if [ $# -lt 2 ]; then
print -u 2 "Usage: cachepuzzle year day"
exit 1
fi
YEAR=${1//[^0-9]}
DAY=${2//[^0-9]}
BASEDIR=${0:h}
if [[ -z $DAY ]]; then
print -u 2 "Non-numeric year/day $1/$2"
exit 1
fi
EXPFILE=$BASEDIR/input/$YEAR/$DAY/input.actual.expected
PUZZLE=$BASEDIR/$YEAR/day$DAY/puzzle/puzzle.html
if [[ $PUZZLE -nt $EXPFILE ]]; then
print -u 2 "$PUZZLE is newer than $EXPFILE, not fetching"
exit 0
fi
# Include contact information per request from @topaz
USER_AGENT="https://github.com/flwyd/adventofcode by aoc@trevorstone.org"
COOKIES=$BASEDIR/.cookie-jar
if [[ ! -s $COOKIES ]]; then
print -u 2 "Who stole the cookies from ${COOKIES}?"
exit 1
fi
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
mkdir -p ${PUZZLE:h}
URL="https://adventofcode.com/$YEAR/day/$DAY"
print -u 2 "Saving $URL to $PUZZLE"
curl $VERBOSE -f -b $COOKIES -A $USER_AGENT -o $PUZZLE $URL
sed -i -e 's#href="/#href="https://adventofcode.com/#g' $PUZZLE