-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-sample.sh
More file actions
executable file
·124 lines (98 loc) · 2.41 KB
/
get-sample.sh
File metadata and controls
executable file
·124 lines (98 loc) · 2.41 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
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
116
117
118
119
120
121
122
123
124
#!/bin/bash
# Common functions
function real_base_name () {
target=$1
(
while true; do
cd "$(dirname "$target")"
target=$(basename "$target")
link=$(readlink "$target")
test "$link" || break
target=$link
done
echo "$(pwd -P)"
)
}
function output () {
echo "$@"
}
function output_error () {
>&2 echo "$@"
}
function fail () {
>&2 echo "$@"
exit 1
}
function usage {
output_error "Usage: $BASH_SOURCE [--year <YEAR> --day <DAY> --token <TOKEN>]"
output_error "--year: Year"
output_error "--day: Day"
output_error "--token: Token"
exit 1
}
YEAR="$(date +"%Y")"
DAY="$(date +"%-d")"
TOKEN="$AOC_SESSION_TOKEN"
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--day)
shift
DAY="$1"
;;
--year)
shift
YEAR="$1"
;;
--token)
shift
TOKEN="$1"
;;
*)
output_error "Unknown option: $key"
usage
;;
esac
shift # past argument or value
done
SCRIPT_DIR=$(real_base_name "$0")
cd "$SCRIPT_DIR"
mkdir -p "./src/main/resources/$YEAR" || fail "Failed to create directory"
HTML=$(curl -f --cookie "session=$TOKEN" -s -L -S --compressed -H "User-Agent: aoc-example-fetcher" "https://adventofcode.com/$YEAR/day/$DAY") || fail "Failed to get example"
INDEX="1"
while [[ true ]]; do
echo "$HTML" | python3 -c '
import sys, re, html
# Which block to pick (1-based index)
n = int(sys.argv[1]) if len(sys.argv) > 1 else 1
data = sys.stdin.read()
# More robust regex:
# - (?i) case-insensitive
# - (?s) dot matches newlines
# - allow attributes on <code>, allow whitespace before </pre>
pattern = re.compile(r"(?is)<pre><code[^>]*>(.*?)</code>\s*</pre>")
blocks = pattern.findall(data)
if not blocks:
sys.stderr.write("No <pre><code> blocks found in HTML.\n")
# Optional: uncomment to debug first 500 chars of HTML
# sys.stderr.write(data[:500] + "\n")
sys.exit(1)
if not (1 <= n <= len(blocks)):
sys.exit(1)
block = blocks[n - 1]
# Decode HTML entities (<, >, &, etc.)
block = html.unescape(block)
# Print the block as-is (keeping internal newlines)
sys.stdout.write(block.rstrip("\n") + "\n")
' "$INDEX" > /tmp/sample.txt
OUTPUT="$(cat /tmp/sample.txt)"
if [ -z "$OUTPUT" ]; then
break
fi
mv /tmp/sample.txt "./src/main/resources/$YEAR/day${DAY}-sample${INDEX}.txt"
output "Found sample $INDEX:"
output "$OUTPUT"
output ""
INDEX=$(( $INDEX + 1 ))
done