fetch-nyt.sh (1392B)
1 #!/usr/bin/env bash 2 # 3 # Fetch a NYT crossword puzzle's raw v6 JSON for inspection. 4 # 5 # Usage: 6 # ./Scripts/fetch-nyt.sh [YYYY-MM-DD] [output-path] 7 # 8 # Defaults: today (America/New_York), nyt-<date>.json in the cwd. 9 # The NYT-S cookie is read from cookies.txt at the repo root (gitignored). 10 # Get the value from a signed-in browser session 11 # (DevTools → Application → Cookies → https://www.nytimes.com). 12 # Override by setting NYT_S in the environment. 13 14 set -euo pipefail 15 16 repo_root="$(cd "$(dirname "$0")/.." && pwd)" 17 cookie_file="${repo_root}/cookies.txt" 18 19 if [[ -z "${NYT_S:-}" ]]; then 20 if [[ -f "$cookie_file" ]]; then 21 NYT_S="$(tr -d '[:space:]' < "$cookie_file")" 22 fi 23 fi 24 25 if [[ -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 29 fi 30 31 date_arg="${1:-$(TZ=America/New_York date +%Y-%m-%d)}" 32 out_path="${2:-nyt-${date_arg}.json}" 33 34 url="https://www.nytimes.com/svc/crosswords/v6/puzzle/daily/${date_arg}.json" 35 36 http_code=$(curl -sS \ 37 -H "Cookie: NYT-S=${NYT_S}" \ 38 -o "${out_path}" \ 39 -w "%{http_code}" \ 40 "${url}") 41 42 if [[ "${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 48 fi 49 50 echo "saved ${out_path} ($(wc -c < "${out_path}") bytes)"