-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_next_day.py
64 lines (46 loc) · 1.55 KB
/
make_next_day.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
64
import os
import shutil
from datetime import date
from pathlib import Path
from typing import Optional
import requests
from dotenv import load_dotenv
load_dotenv()
SESSION_ID = os.environ["SESSION_ID"]
PROJECT_ROOT = Path(__file__).parent
TEMPLATE_DIR = PROJECT_ROOT / "template"
def make_path(year: int, day: Optional[int] = None) -> Path:
path = Path(f"y{year}")
if day is not None:
path /= f"day{day:02d}"
return path
def clone_template(day: date) -> None:
destination = make_path(day.year, day.day)
destination.mkdir(exist_ok=True, parents=True)
for file in TEMPLATE_DIR.glob("*.py"):
if not (destination / file.name).exists():
shutil.copy2(file, destination)
def get_day() -> date:
today = date.today()
max_day = max(
int(path.name.replace("day", "")) for path in make_path(today.year).glob("day*")
)
max_day = min(max_day, today.day) + 1
return date(today.year, today.month, max_day)
def download_data(date_obj: date) -> None:
destination = make_path(date_obj.year, date_obj.day) / "input.txt"
if destination.exists():
raise FileExistsError(f"File {destination} exists, skipping download")
r = requests.get(
f"https://adventofcode.com/{date_obj.year}/day/{date_obj.day}/input",
cookies={"session": SESSION_ID},
)
r.raise_for_status()
with destination.open("w") as fp:
fp.write(r.text)
def main() -> None:
date_obj = get_day()
clone_template(date_obj)
download_data(date_obj)
if __name__ == "__main__":
main()