Last active
November 27, 2019 13:44
-
-
Save tyilo/3cc06b33b4cb990473ee40bfb464a15b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
from pathlib import Path | |
import requests | |
YEAR = 2019 | |
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 | |
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.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 f: | |
f.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()}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment