Scripts/fetch-nyt.sh (1392B)
1
2
3
4
5
6
7
8
9
10
11
12
13
14set -euo pipefail
15
16repo_root="$(cd "$(dirname "$0")/.." && pwd)"
17cookie_file="${repo_root}/cookies.txt"
18
19if [[ -z "${NYT_S:-}" ]]; then
20 if [[ -f "$cookie_file" ]]; then
21 NYT_S="$(tr -d '[:space:]' < "$cookie_file")"
22 fi
23fi
24
25if [[ -z "${NYT_S:-}" ]]; then
26 echo "error: NYT-S cookie not found" >&2
27 echo " put the cookie value in ${cookie_file}, or set NYT_S in the environment" >&2
28 exit 1
29fi
30
31date_arg="${1:-$(TZ=America/New_York date +%Y-%m-%d)}"
32out_path="${2:-nyt-${date_arg}.json}"
33
34url="https://www.nytimes.com/svc/crosswords/v6/puzzle/daily/${date_arg}.json"
35
36http_code=$(curl -sS \
37 -H "Cookie: NYT-S=${NYT_S}" \
38 -o "${out_path}" \
39 -w "%{http_code}" \
40 "${url}")
41
42if [[ "${http_code}" != "200" ]]; then
43 echo "error: HTTP ${http_code}" >&2
44 echo "body:" >&2
45 cat "${out_path}" >&2
46 rm -f "${out_path}"
47 exit 1
48fi
49
50echo "saved ${out_path} ($(wc -c < "${out_path}") bytes)"