-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc_input.py
40 lines (29 loc) · 871 Bytes
/
aoc_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
35
36
37
38
39
40
import json
from pathlib import Path
class UnexpectedResponseError(RuntimeError):
pass
def get_input(year, day):
thisdir = Path(__file__).parent / f"y{year}"
try:
with open(Path(thisdir, "input.json"), "r") as f:
data = {int(k): v for k, v in json.load(f).items()}
except OSError:
data = {}
if day not in data:
import requests
with open(Path(thisdir, "cookie.txt"), "r") as f:
SESSCOOKIE = f.read().strip()
resp = requests.get(
f"https://adventofcode.com/{year}/day/{day}/input",
cookies = {
"session": SESSCOOKIE,
}
)
if resp.status_code != 200:
raise UnexpectedResponseError(f"Expected 200, got {resp.status_code}")
data[day] = resp.content.decode("utf-8")
with open(Path(thisdir, "input.json"), "w") as f:
json.dump(data, f, indent = 4)
return data[day]
if __name__ == "__main__":
print(get_input(2020, 1))