aoc

Advent of Code
git clone https://git.pngu.org/aoc
Log | Files | Refs | README

commit 88699d89e09b5c56fe291a0c6e21e621665e08e9
parent 977e727c19989a3d26589455ccf4358e88567379
Author: Jack Sangdahl <jack@pngu.org>
Date:   Thu,  4 Dec 2025 09:32:53 +0100

chore: Improvements to data fetching script

Diffstat:
Mdata.py | 56++++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 44 insertions(+), 12 deletions(-)

diff --git a/data.py b/data.py @@ -1,18 +1,50 @@ -import os import subprocess -import time +from datetime import datetime +from os import getenv from pathlib import Path +from zoneinfo import ZoneInfo import dotenv dotenv.load_dotenv() -id = os.getenv("SESSION_ID") -year = time.strftime("%Y") -hour = time.strftime("%H") -day = int(time.strftime("%d")) -cmd = f'curl https://adventofcode.com/{year}/day/{day}/input --cookie "session={id}"' -output = subprocess.check_output(cmd, shell=True) -output = output.decode("utf-8") -with open(f"data/{day}_{year}.txt", "w") as f: - f.write(output) -Path(f"{day}_{year}.py").touch() + +editor = getenv("EDITOR", "vi") +session_id = getenv("SESSION_ID") +if not session_id: + raise ValueError("SESSION_ID not found") + +aoc_tz = ZoneInfo("America/New_York") +aoc_time = datetime.now(aoc_tz) +year = aoc_time.year +day = aoc_time.day + +data_dir = Path("data") +data_dir.mkdir(exist_ok=True) +data_file = data_dir / f"{day}_{year}.txt" + +cmd = [ + "curl", + f"https://adventofcode.com/{year}/day/{day}/input", + "--cookie", + f"session={session_id}", + "-f", + "-s", +] + +try: + output = subprocess.check_output(cmd, stderr=subprocess.PIPE) + output = output.decode("utf-8") + if "log in" in output: + raise ValueError("failed to fetch input; check SESSION_ID") + with open(data_file, "w") as f: + f.write(output) + +except subprocess.CalledProcessError as e: + print(f"error fetching input: {e.stderr.decode('utf-8')}") + exit(1) + +source_file = Path(f"{day}_{year}.py") +if not source_file.exists(): + source_file.touch() + +subprocess.run([editor, str(source_file), str(data_file)])