-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.py
More file actions
90 lines (70 loc) · 2.4 KB
/
fetch.py
File metadata and controls
90 lines (70 loc) · 2.4 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import requests
import sys
from pathlib import Path
from datetime import date
from dotenv import load_dotenv
from rich.console import Console
console = Console()
# Load session cookie from .env
load_dotenv()
SESSION = os.getenv("AOC_SESSION")
YEAR = os.getenv("YEAR") or date.today().year
BASE_URL = "https://adventofcode.com"
TEMPLATE = """def part1(data):
return None
def part2(data):
return None
if __name__ == "__main__":
with open("solutions/day_{:02d}/input.txt") as f:
data = f.read().strip()
print("Part 1:", part1(data))
print("Part 2:", part2(data))
"""
TEST_TEMPLATE = """import unittest
from solutions.day_{:02d}.solution import part1, part2
class TestDay01(unittest.TestCase):
sample_input = \"""
...
\"""
def test_part1(self):
self.assertEqual(part1(self.sample_input), None)
def test_part2(self):
self.assertEqual(part2(self.sample_input), None)
"""
def fetch_input(day):
if not SESSION:
raise ValueError("AOC_SESSION not set in .env")
# Fetch input
url = f"{BASE_URL}/{YEAR}/day/{day}/input"
headers = {"Cookie": f"session={SESSION}"}
response = requests.get(url, headers=headers)
if response.status_code != 200:
raise Exception(f"Failed to fetch input for day {day}: {response.status_code}")
# Save input file
input_path = Path(f"solutions/day_{day:02d}/input.txt")
input_path.parent.mkdir(exist_ok=True)
input_path.write_text(response.text.strip())
console.print(f"Input saved to {input_path}")
# Create solution file
solution_path = Path(f"solutions/day_{day:02d}/solution.py")
if not solution_path.exists():
solution_path.write_text(TEMPLATE.format(day))
console.print(f"Solution template created: {solution_path}")
# Create test file
test_path = Path(f"solutions/day_{day:02d}/test_solution.py")
if not test_path.exists():
test_path.write_text(TEST_TEMPLATE.format(day))
console.print(f"Test template created: {test_path}")
# Create init file to treat it as module
test_path = Path(f"solutions/day_{day:02d}/__init__.py")
if not test_path.exists():
test_path.write_text("")
if __name__ == "__main__":
try:
day = int(sys.argv[1])
fetch_input(day)
except IndexError:
console.print(
"[red]Oopla, you forgot to specify a day to fetch\n(e.g. ./fetch.py 1)[/red] "
)