-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_input.py
executable file
·63 lines (48 loc) · 1.56 KB
/
get_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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
"""
Fetch AoC input as files.
By Asger Hautop Drewsen: https://github.com/Tyilo
"""
import sys
from pathlib import Path
from typing import Optional
import requests
YEAR = 2023
URL_PREFIX = f"https://adventofcode.com/{YEAR}"
def validate_session(session):
test_url = f"{URL_PREFIX}/settings"
r = session.get(test_url)
return r.status_code == 200 and r.url == test_url
session_cookie: Optional[str]
try:
with open(".session", "r") as f:
session_cookie = f.read().strip()
except FileNotFoundError:
session_cookie = None
while True:
if not session_cookie:
session_cookie = input("Session cookie value: ").strip()
with open(".session", "w") as f:
f.write(session_cookie)
session = requests.Session()
session.headers["User-Agent"] = "get_input.py @ github.com/BarrensZeppelin/adventofcode2023"
session.cookies.set("session", session_cookie, domain=".adventofcode.com", path="/")
if validate_session(session):
break
print("That session cookie doesn't seem to work. Try again.")
session_cookie = None
for i in range(1, 26):
path = Path(f"{i:02}.in")
if path.exists():
continue
r = session.get(f"{URL_PREFIX}/day/{i}/input")
if r.ok:
with path.open("wb") as fb:
fb.write(r.content)
print(f"Downloaded {path.name}")
else:
if r.status_code == 404:
print(f"Day {i} not released yet")
break
else:
sys.exit(f"Got unknown status code: {r.status_code}\n{r.text.strip()}")