-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc.py
46 lines (35 loc) · 1.2 KB
/
aoc.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
35
36
37
38
39
40
41
42
43
44
45
46
import requests
import sys
import os
import subprocess
from datetime import datetime
from pathlib import Path
from shutil import copyfile
aoc_session = os.environ.get("AOC_SESSION")
if not aoc_session:
sys.stderr.write(
"""You must export a value for AOC_SESSION in your environment for
this script to work. See https://github.com/seemaullal/advent-of-code
for further instructions."""
)
exit(1)
if len(sys.argv) < 2:
raise Exception("Pass in a day number so we know what day to download.")
day_number = sys.argv[1].zfill(2)
if len(sys.argv) > 2:
year = sys.argv[2]
else:
year = datetime.now().year
sys.stdout.write(f"Downloading day {day_number} for {year}\n")
day_directory = f"{Path.cwd()}/{year}/{day_number}"
Path(f"{day_directory}/inputs").mkdir(exist_ok=True, parents=True)
# URL does not have leading zeros
aoc_url = f"https://adventofcode.com/{year}/day/{int(day_number)}"
input_contents = requests.get(
f"{aoc_url}/input",
cookies={"session": os.environ.get("AOC_SESSION")},
).text
with open(f"{day_directory}/inputs/input.txt", "w") as file:
file.write(input_contents)
copyfile("template.py", f"{day_directory}/solution.py")
subprocess.run(["open", aoc_url])