Repository navigation
Expand file tree
/
Copy pathtools.py
More file actions
71 lines (54 loc) · 2.03 KB
/
Copy pathtools.py
File metadata and controls
71 lines (54 loc) · 2.03 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
from dotenv import load_dotenv
load_dotenv()
import os
import httpx
from pathlib import Path
# Set up headers with the session cookie
headers = {
"Cookie": f"session={os.environ["SESSION_COOKIE"]}",
}
ROOT_DIR = Path(__file__).parent
def get_local_input(path):
with open(path, "r") as f:
data = f.read()
return data
def dump_input(destination: str, data: str) -> str:
destination.parent.mkdir(parents=True, exist_ok=True)
with open(destination, "w") as f:
f.write(data)
return data
# Fetch the input
def fetch_input(url: str) -> str | None:
url_path = httpx.URL(url).path
destination = ROOT_DIR / f"./{url_path}.txt"
if destination.exists():
print("file exists in local storage")
return get_local_input(destination)
try:
with httpx.Client() as client:
print(f"Fetching data from {url}")
response = client.get(url, headers=headers)
response.raise_for_status() # Raise an error for bad responses (e.g., 404)
return dump_input(destination, response.text.strip()) # Return the input as a string
except httpx.HTTPStatusError as exc:
print(f"Error fetching input: {exc}")
return None
def dump_assignment(url_path: str, data: str) -> None:
# ['', '2024', 'day', '2']
_, _, day, num = url_path.split("/")
fname = f"{day}_{int(num):02d}.html"
with open(fname, "w") as f:
f.write(data)
def fetch_assignment(url: str):
url_path = httpx.URL(url).path
try:
with httpx.Client() as client:
print(f"Fetching assignment data from {url}")
response = client.get(url, headers=headers)
response.raise_for_status() # Raise an error for bad responses (e.g., 404)
# print(response.text)
dump_assignment(url_path, response.text)
# return dump_input(url_path, response.text.strip()) # Return the input as a string
except httpx.HTTPStatusError as exc:
print(f"Error fetching input: {exc}")
return None