|
10 | 10 | import httpx |
11 | 11 | import json |
12 | 12 | import pathlib |
13 | | -import shutil |
14 | | -import yaml |
| 13 | +import os |
15 | 14 |
|
16 | 15 | ROOT = pathlib.Path(__file__).parents[1] |
17 | 16 |
|
|
21 | 20 | SCHEDULE_DATA = "https://programapi24.europython.eu/2024/schedule.json" |
22 | 21 |
|
23 | 22 |
|
24 | | -def write_mdx(data: dict[str, Any], output_dir: pathlib.Path, content_key: str) -> None: |
25 | | - if output_dir.exists(): |
26 | | - shutil.rmtree(output_dir) |
27 | | - |
28 | | - output_dir.mkdir(parents=True, exist_ok=True) |
29 | | - |
30 | | - for key, value in data.items(): |
31 | | - filename = f"{key}.mdx" |
32 | | - path = output_dir / filename |
33 | | - |
34 | | - content = value.pop(content_key) or "" |
35 | | - |
36 | | - content = content.replace("<3", "❤️") |
37 | | - |
38 | | - frontmatter = yaml.dump(value, sort_keys=True) |
39 | | - |
40 | | - with path.open("w", encoding="utf-8") as f: |
41 | | - f.write(f"---\n{frontmatter}---\n\n{content}") |
42 | 23 |
|
43 | 24 |
|
44 | 25 | def download_data(url: str) -> dict[str, Any]: |
45 | | - with httpx.Client() as client: |
| 26 | + #TODO: fix SSL cert and force verification |
| 27 | + with httpx.Client(verify=False) as client: |
46 | 28 | response = client.get(url) |
47 | 29 | response.raise_for_status() |
48 | 30 | data = response.json() |
49 | 31 |
|
50 | 32 | return data |
51 | 33 |
|
52 | 34 |
|
| 35 | +def save_json_data(filename, data, directory="src/data"): |
| 36 | + os.makedirs(directory, exist_ok=True) # Ensure the directory exists |
| 37 | + file_path = os.path.join(directory, f"{filename}.json") |
| 38 | + |
| 39 | + with open(file_path, "w", encoding="utf-8") as file: |
| 40 | + json.dump(data, file, indent=4, ensure_ascii=False) |
| 41 | + |
| 42 | + print(f"Saved {filename}.json in {directory}") |
| 43 | + |
| 44 | + |
| 45 | + |
53 | 46 | def download() -> None: |
54 | 47 | speakers = download_data(SPEAKERS_URL) |
55 | 48 | sessions = download_data(SESSIONS_URL) |
56 | 49 | schedule = download_data(SCHEDULE_DATA) |
57 | 50 |
|
58 | | - for session in sessions.values(): |
59 | | - session["speakers"] = [ |
60 | | - speakers[speaker_id]["slug"] for speaker_id in session.get("speakers", []) |
61 | | - ] |
62 | | - |
63 | | - for speaker in speakers.values(): |
64 | | - speaker["submissions"] = [ |
65 | | - sessions[session_id]["slug"] |
66 | | - for session_id in speaker.get("submissions", []) |
67 | | - if session_id in sessions |
68 | | - ] |
69 | | - |
70 | | - write_mdx(sessions, ROOT / "src/content/sessions", "abstract") |
71 | | - write_mdx(speakers, ROOT / "src/content/speakers", "biography") |
72 | | - |
73 | | - for day, data in schedule["days"].items(): |
74 | | - path = ROOT / f"src/content/days/{day}.json" |
75 | | - with path.open("w", encoding="utf-8") as f: |
76 | | - json.dump(data, f, indent=2) |
| 51 | + save_json_data("speakers", speakers) |
| 52 | + save_json_data("sessions", sessions) |
| 53 | + save_json_data("schedule", schedule) |
77 | 54 |
|
78 | 55 |
|
79 | | -download() |
| 56 | +if __name__ == "__main__": |
| 57 | + download() |
0 commit comments