[run.py] add get_input_file

This commit is contained in:
2025-07-28 09:56:51 +02:00
parent 87871aed4d
commit 3186a86291

38
run.py
View File

@@ -1,9 +1,32 @@
import urllib.request
import getpass
import sys import sys
import time import time
import subprocess import subprocess
import os
from pathlib import Path from pathlib import Path
_auth = None
def get_auth():
global _auth
if _auth is None:
if "AUTH" in os.environ:
_auth = os.environ["AUTH"]
else:
_auth = getpass.getpass(prompt="Cookie: ")
def get_input_file(year, day):
url = f"https://adventofcode.com/{year}/day/{day}/input"
r = urllib.request.Request(url)
r.add_header("Cookie", f"session={_auth}")
res = urllib.request.urlopen(r)
return res
def main(year, day): def main(year, day):
if day is not None: if day is not None:
path = Path(f"{year}/day{day}") path = Path(f"{year}/day{day}")
@@ -13,8 +36,12 @@ def main(year, day):
print(f"Invalid day {day}", file=sys.stderr) print(f"Invalid day {day}", file=sys.stderr)
exit(1) exit(1)
if not input_path.exists(): if not input_path.exists():
print(f"Could not find input file {input_path}", file=sys.stderr) print(f"Downloading input file {input_path}")
exit(1) get_auth()
with open(input_path, "wb") as f:
res = get_input_file(year, day)
f.write(res.read())
run_day(script_path, input_path) run_day(script_path, input_path)
else: else:
for day in range(1, 26): for day in range(1, 26):
@@ -23,8 +50,11 @@ def main(year, day):
input_path = path / Path("input.txt") input_path = path / Path("input.txt")
if script_path.exists(): if script_path.exists():
if not input_path.exists(): if not input_path.exists():
print(f"Could not find input file {input_path}, skipped day {day}.", file=sys.stderr) print(f"Downloading input file {input_path}")
continue get_auth()
with open(input_path, "wb") as f:
res = get_input_file(year, day)
f.write(res.read())
run_day(script_path, input_path) run_day(script_path, input_path)