-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimals_web_generator.py
More file actions
175 lines (139 loc) · 5.14 KB
/
animals_web_generator.py
File metadata and controls
175 lines (139 loc) · 5.14 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# animals_web_generator.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Zootopia with API — Website Generator
- Fragt den Tiernamen ab
- Holt die Daten via data_fetcher.fetch_data()
- Rendert Cards oder eine leere-State-Meldung ins Template
"""
from __future__ import annotations
import html
import sys
from pathlib import Path
from typing import Any, Dict, Iterable, List, Optional
import data_fetcher # eigenes Modul, das die API abruft
PLACEHOLDER = "__REPLACE_ANIMALS_INFO__"
# ---------- IO ----------
def read_text(path: str | Path) -> str:
return Path(path).read_text(encoding="utf-8")
def write_text(path: str | Path, content: str) -> None:
Path(path).write_text(content, encoding="utf-8")
# ---------- helpers ----------
def get_ci(d: Dict[str, Any], *keys: str) -> Optional[Any]:
"""Case-insensitive Getter: liefert den Wert zum ersten passenden Key."""
for k in keys:
if k in d:
return d[k]
lower_map = {k.lower(): v for k, v in d.items()}
for k in keys:
v = lower_map.get(k.lower())
if v is not None:
return v
return None
def get_field(animal: Dict[str, Any], *keys: str) -> Optional[Any]:
"""
Sucht ein Feld zuerst auf Top-Level, dann in 'characteristics'.
Leere Strings -> None.
"""
v = get_ci(animal, *keys)
if v is None:
ch = get_ci(animal, "characteristics")
if isinstance(ch, dict):
v = get_ci(ch, *keys)
if isinstance(v, str):
v = v.strip()
if not v:
return None
return v
def format_value(value: Any) -> str:
"""Listen als kommagetrennten Text rendern und HTML escapen."""
if isinstance(value, (list, tuple)):
value = ", ".join(str(x).strip() for x in value if str(x).strip())
return html.escape(str(value))
# ---------- rendering ----------
def serialize_animal(animal: Dict[str, Any]) -> str:
"""
<li class="cards__item">
<div class="card__title">Name</div>
<div class="card__text">
<ul class="card__facts">
<li class="card__fact"><span class="label">Diet:</span> ...</li>
...
</ul>
</div>
</li>
"""
name = get_field(animal, "name")
title_html = (
f' <div class="card__title">{html.escape(str(name))}</div>\n' if name else ""
)
facts: List[tuple[str, Any]] = []
for label, keys in [
("Diet", ("diet",)),
("Location", ("locations", "location")),
("Type", ("type",)),
("Skin type", ("skin_type", "skin type", "skintype")),
("Scientific name", ("scientific_name", "latin_name")),
("Family", ("family",)),
("Order", ("order",)),
("Class", ("class", "class_name")),
("Habitat", ("habitat",)),
("Geo range", ("geo_range", "native_region", "range")),
("Conservation status", ("conservation_status", "status")),
("Description", ("description",)),
]:
val = get_field(animal, *keys)
# Für Locations nur den ersten Eintrag zeigen (wie bisher)
if label == "Location":
if isinstance(val, list) and val:
val = val[0]
if val is not None:
facts.append((label, val))
if not (title_html or facts):
return ""
facts_html = "\n".join(
f' <li class="card__fact"><span class="label">{html.escape(label)}:</span> {format_value(val)}</li>'
for label, val in facts
)
item = ' <li class="cards__item">\n'
if title_html:
item += title_html
item += ' <div class="card__text">\n'
item += ' <ul class="card__facts">\n' + facts_html + "\n"
item += " </ul>\n </div>\n </li>\n"
return item
def build_cards(animals: Iterable[Dict[str, Any]]) -> str:
return "".join(serialize_animal(a) for a in animals if isinstance(a, dict))
def render_empty(query: str, details: str | None = None) -> str:
"""Leerer Zustand als hübscher Block."""
q = html.escape(query)
det = f'<p class="empty__hint">{html.escape(details)}</p>' if details else ""
return (
f'<section class="empty">'
f'<h2>The animal "{q}" doesn\'t exist.</h2>'
f'<p>Try a different name (e.g., "Fox", "Bear", "Eagle").</p>'
f"{det}</section>"
)
# ---------- main ----------
def main() -> None:
template_path = sys.argv[1] if len(sys.argv) > 1 else "animals_template.html"
out_path = sys.argv[2] if len(sys.argv) > 2 else "animals.html"
animal_name = ""
while not animal_name:
animal_name = input("Please enter an animal: ").strip()
template_html = read_text(template_path)
try:
data = data_fetcher.fetch_data(animal_name)
except Exception as e:
html_out = template_html.replace(
PLACEHOLDER, render_empty(animal_name, f"Error: {e}")
)
write_text(out_path, html_out)
print("Website was generated with an error message.")
return
html_block = build_cards(data) if data else render_empty(animal_name)
write_text(out_path, template_html.replace(PLACEHOLDER, html_block))
print("Website was successfully generated to the file animals.html.")
if __name__ == "__main__":
main()