-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_input.py
34 lines (25 loc) · 918 Bytes
/
fetch_input.py
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
import json
from datetime import date
from pathlib import Path
import requests
def fetch_input(year, day, session):
input_path = Path(f"{year}/{day:02d}/input.txt")
if not input_path.exists():
input_path.parent.mkdir(parents=True, exist_ok=True)
url = f"https://adventofcode.com/{year}/day/{day}/input"
resp = requests.get(url, cookies={"session": session})
resp.raise_for_status()
with input_path.open("wb") as f:
f.write(resp.content)
return input_path
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("year", type=int, default=date.today().year, nargs="?")
args = parser.parse_args()
with open(".adventofcode.json") as f:
session = json.load(f)["session"]
for day in range(1, 26):
fetch_input(args.year, day, session).read_text()
if __name__ == "__main__":
main()