-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
389 lines (345 loc) · 14.9 KB
/
generate.py
File metadata and controls
389 lines (345 loc) · 14.9 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import json
import os
import sys
import datetime
import urllib.request
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
from urllib.parse import urlparse, parse_qs, unquote
# Config - Use relative paths
BASE_DIR = Path(__file__).resolve().parent
DATA_FILE = BASE_DIR / 'data' / 'shows.json'
TEMPLATE_DIR = BASE_DIR / 'templates'
OUTPUT_DIR = BASE_DIR
SHOWS_DIR = OUTPUT_DIR / 'shows'
BASE_URL = 'https://willbearfruits.github.io/kloom-radio'
def load_data():
"""Load show data from JSON file with error handling."""
try:
with open(DATA_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
print(f"ERROR: Data file not found at {DATA_FILE}")
sys.exit(1)
except json.JSONDecodeError as e:
print(f"ERROR: Invalid JSON in data file: {e}")
sys.exit(1)
except Exception as e:
print(f"ERROR: Could not load data: {e}")
sys.exit(1)
def save_data(data):
"""Save show data to JSON file with error handling."""
try:
with open(DATA_FILE, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=4, ensure_ascii=False)
except IOError as e:
print(f"ERROR: Could not save data: {e}")
sys.exit(1)
def extract_feed_path(embed_url):
parsed = urlparse(embed_url)
query = parse_qs(parsed.query)
if 'feed' in query:
return unquote(query['feed'][0])
return None
def fetch_mixcloud_metadata(feed_path):
if not feed_path.endswith('/'):
feed_path += '/'
api_url = f"https://api.mixcloud.com{feed_path}"
print(f"Fetching metadata from: {api_url}")
try:
with urllib.request.urlopen(api_url) as response:
if response.status == 200:
return json.loads(response.read().decode())
except Exception as e:
print(f"Error fetching {api_url}: {e}")
return None
def update_show_data(shows):
updated = False
for show in shows:
if show['type'] == 'embed' and 'mixcloud' in show['embed_url']:
if 'image_url' not in show or not show['image_url']:
feed_path = extract_feed_path(show['embed_url'])
if feed_path:
meta = fetch_mixcloud_metadata(feed_path)
if meta:
show['image_url'] = meta.get('pictures', {}).get('extra_large')
if not show.get('tags'):
show['tags'] = [t['name'] for t in meta.get('tags', [])]
if not show.get('description'):
show['description'] = meta.get('description', '')
show['play_count'] = meta.get('play_count', 0)
updated = True
if updated:
save_data(shows)
print("Updated shows.json with new metadata.")
return shows
def generate_og_image(show):
"""Generate a per-show OG image (1200x630 PNG)."""
try:
from PIL import Image, ImageDraw, ImageFont
except ImportError:
print("WARNING: Pillow not installed — skipping OG image generation")
return
W, H = 1200, 630
BLUE, YELLOW, MAGENTA, GREEN, BLACK, WHITE = (
(0,0,255), (255,255,0), (255,0,255), (0,255,0), (0,0,0), (255,255,255)
)
def load_font(size):
# Try Hebrew-supporting fonts first (FreeSans has excellent Hebrew support)
for p in ["/usr/share/fonts/truetype/freefont/FreeSansBold.ttf",
"/usr/share/fonts/truetype/noto/NotoSansHebrew-Bold.ttf",
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"]:
if os.path.exists(p):
return ImageFont.truetype(p, size)
return ImageFont.load_default()
def load_mono(size):
# Try Hebrew-supporting fonts first (FreeSans has excellent Hebrew support)
for p in ["/usr/share/fonts/truetype/freefont/FreeSans.ttf",
"/usr/share/fonts/truetype/noto/NotoSansHebrew-Regular.ttf",
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"]:
if os.path.exists(p):
return ImageFont.truetype(p, size)
return ImageFont.load_default()
img = Image.new("RGB", (W, H), BLUE)
d = ImageDraw.Draw(img)
# scanlines
for y in range(0, H, 4):
d.line([(0, y), (W, y)], fill=(0, 0, 12), width=2)
# border bars
d.rectangle([0, 0, W, 8], fill=BLACK)
d.rectangle([0, H-8, W, H], fill=BLACK)
d.rectangle([0, 0, 12, H], fill=MAGENTA)
d.rectangle([W-12, 0, W, H], fill=MAGENTA)
# series label top-left
d.rectangle([60, 60, 580, 108], fill=BLACK)
d.text((80, 68), f"// {show.get('series','').upper()} // {show.get('date','')}", fill=YELLOW, font=load_mono(22))
# main magenta box (shadow + fill + border)
bx, by, bw, bh = 60, 140, 1080, 300
d.rectangle([bx+6, by+6, bx+bw+6, by+bh+6], fill=BLACK)
d.rectangle([bx, by, bx+bw, by+bh], fill=MAGENTA)
d.rectangle([bx, by, bx+bw, by+bh], outline=BLACK, width=6)
# --- title: wrap long titles across up to 3 lines ---
title = show.get('title', 'UNTITLED')
font_big = load_font(72)
font_med = load_font(52)
font_sm = load_font(40)
# measure and pick the right size / line-split
def measure(txt, fnt):
return d.textlength(txt, font=fnt)
if measure(title, font_big) <= bw - 80:
# fits on one line at big size
d.text((600, 290), title, fill=BLACK, font=font_big, anchor="mm")
elif measure(title, font_med) <= bw - 80:
d.text((600, 290), title, fill=BLACK, font=font_med, anchor="mm")
else:
# split into words and wrap at med size
words = title.split()
lines = []
current = ""
for w in words:
test = (current + " " + w).strip()
if measure(test, font_med) <= bw - 80:
current = test
else:
if current:
lines.append(current)
current = w
if current:
lines.append(current)
# if still too many chars per line drop to small font
fnt = font_med
if any(measure(l, font_med) > bw - 80 for l in lines):
fnt = font_sm
# re-wrap at small size
lines = []
current = ""
for w in words:
test = (current + " " + w).strip()
if measure(test, fnt) <= bw - 80:
current = test
else:
if current:
lines.append(current)
current = w
if current:
lines.append(current)
line_h = fnt.size if hasattr(fnt, 'size') else 60
total_h = line_h * len(lines)
start_y = by + bh // 2 - total_h // 2 + line_h // 2
for i, line in enumerate(lines[:3]):
d.text((600, start_y + i * line_h), line, fill=BLACK, font=fnt, anchor="mm")
# tags row
tags = show.get('tags', [])[:4]
tx = 100
for tag in tags:
tw = int(measure(f"#{tag}", load_mono(18))) + 24
d.rectangle([tx, 478, tx+tw, 510], fill=WHITE, outline=BLACK, width=3)
d.text((tx+12, 482), f"#{tag}", fill=BLACK, font=load_mono(18))
tx += tw + 12
# glitch bars top-right
for i, (off, w, col) in enumerate([(0,240,YELLOW),(20,200,GREEN),(40,180,MAGENTA),(0,160,YELLOW)]):
d.rectangle([900+off, 65+i*12, 900+off+w, 71+i*12], fill=col)
# bottom status bar
d.rectangle([0, 540, W, H-8], fill=BLACK)
d.text((80, 558), "KLOOM LO KADOSH // NOTHING IS HOLY", fill=GREEN, font=load_mono(18))
d.text((80, 585), "THE SIGNAL IS THE MESSAGE.", fill=YELLOW, font=load_mono(15))
# guest badge bottom-right if present
guest = show.get('guest', '')
if guest:
d.rectangle([820, 548, 1130, 614], fill=YELLOW, outline=GREEN)
d.text((975, 562), "GUEST", fill=BLACK, font=load_mono(16), anchor="mm")
d.text((975, 590), guest, fill=BLACK, font=load_mono(20), anchor="mm")
og_dir = BASE_DIR / 'assets' / 'og'
og_dir.mkdir(parents=True, exist_ok=True)
out = og_dir / f"{show['id']}.png"
img.save(str(out), "PNG")
print(f"Generated OG: assets/og/{show['id']}.png")
def tojson_filter(x):
"""Serialize to JSON, safe for HTML attributes (escapes < > & ')."""
rv = json.dumps(x, ensure_ascii=False)
rv = rv.replace('&', '\\u0026').replace('<', '\\u003c').replace('>', '\\u003e').replace("'", '\\u0027')
return rv
def generate_search_index(shows):
"""Write client-side search index."""
index = [{'id': s['id'], 'title': s.get('title',''),
'description': s.get('description',''), 'series': s.get('series',''),
'guest': s.get('guest',''), 'tags': s.get('tags',[])} for s in shows]
with open(OUTPUT_DIR / 'search-index.json', 'w', encoding='utf-8') as f:
json.dump(index, f, ensure_ascii=False)
print("Generated: search-index.json")
def generate_rss_feed(shows):
"""Write RSS 2.0 feed."""
now = datetime.datetime.now(datetime.timezone.utc).strftime('%a, %d %b %Y %H:%M:%S %z')
items = []
for s in shows:
try:
dt = datetime.datetime.strptime(s['date'], '%Y-%m-%d')
pub_date = dt.strftime('%a, %d %b %Y 00:00:00 +0000')
except (ValueError, KeyError):
pub_date = now
desc = (s.get('description') or '').replace('&','&').replace('<','<').replace('>','>')
title_esc = (s.get('title') or 'Untitled').replace('&','&').replace('<','<').replace('>','>')
series_esc = (s.get('series') or '').replace('&','&').replace('<','<').replace('>','>')
items.append(
f' <item>\n'
f' <title>{title_esc}</title>\n'
f' <link>{BASE_URL}/shows/{s["id"]}.html</link>\n'
f' <description>{desc}</description>\n'
f' <pubDate>{pub_date}</pubDate>\n'
f' <category>{series_esc}</category>\n'
f' <guid isPermaLink="true">{BASE_URL}/shows/{s["id"]}.html</guid>\n'
f' </item>'
)
channel_items = '\n'.join(items)
rss = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<rss version="2.0" xmlns:atom="http://www.w3.org/Atom">\n'
' <channel>\n'
' <title>KLOOM LO KADOSH</title>\n'
f' <link>{BASE_URL}/</link>\n'
' <description>Nothing Is Holy. Experimental radio archive.</description>\n'
f' <lastBuildDate>{now}</lastBuildDate>\n'
f' <atom:link href="{BASE_URL}/feed.xml" rel="self" type="application/rss+xml"/>\n'
+ channel_items + '\n'
' </channel>\n'
'</rss>\n'
)
with open(OUTPUT_DIR / 'feed.xml', 'w', encoding='utf-8') as f:
f.write(rss)
print("Generated: feed.xml")
def generate_sitemap(shows):
"""Write sitemap.xml."""
urls = [
f' <url><loc>{BASE_URL}/</loc></url>',
f' <url><loc>{BASE_URL}/about.html</loc></url>',
f' <url><loc>{BASE_URL}/contact.html</loc></url>',
]
for s in shows:
urls.append(f' <url><loc>{BASE_URL}/shows/{s["id"]}.html</loc><lastmod>{s["date"]}</lastmod></url>')
sitemap = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
+ '\n'.join(urls) + '\n'
'</urlset>\n'
)
with open(OUTPUT_DIR / 'sitemap.xml', 'w', encoding='utf-8') as f:
f.write(sitemap)
print("Generated: sitemap.xml")
def generate_robots_txt():
"""Write robots.txt with sitemap pointer."""
with open(OUTPUT_DIR / 'robots.txt', 'w', encoding='utf-8') as f:
f.write(f'User-agent: *\nDisallow:\n\nSitemap: {BASE_URL}/sitemap.xml\n')
print("Generated: robots.txt")
def generate_site():
"""Generate static site from show data."""
shows = load_data()
shows = update_show_data(shows)
shows.sort(key=lambda x: x['date'], reverse=True)
# Setup Jinja Environment (Required for 'include')
try:
env = Environment(loader=FileSystemLoader(str(TEMPLATE_DIR)))
except Exception as e:
print(f"ERROR: Could not load templates from {TEMPLATE_DIR}: {e}")
sys.exit(1)
# Register tojson filter (HTML-attribute-safe JSON)
env.filters['tojson'] = tojson_filter
# Compute absolute URLs so the persistent player works across pages
for show in shows:
if show.get('src'):
show['audio_url'] = BASE_URL + '/' + show['src'].replace('./', '')
show['show_url'] = BASE_URL + '/shows/' + show['id'] + '.html'
# 1. Generate Individual Show Pages
try:
master_template = env.get_template('master_glitch.html')
except Exception as e:
print(f"ERROR: Could not load master template: {e}")
sys.exit(1)
# Create shows directory if it doesn't exist
try:
SHOWS_DIR.mkdir(parents=True, exist_ok=True)
except OSError as e:
print(f"ERROR: Could not create shows directory: {e}")
sys.exit(1)
for show in shows:
try:
# Generate OG image for this show
generate_og_image(show)
context = show.copy()
context['show'] = show # full dict for tojson in templates
context['BASE_URL'] = BASE_URL
context['generated_at'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
output = master_template.render(context)
filename = f"{show['id']}.html"
filepath = SHOWS_DIR / filename
with open(filepath, 'w', encoding='utf-8') as f:
f.write(output)
print(f"Generated Page: {filename}")
except Exception as e:
print(f"WARNING: Could not generate page for {show.get('id', 'unknown')}: {e}")
# 2. Generate Index Page (List Layout)
try:
index_template = env.get_template('index_list_glitch.html')
index_output = index_template.render(shows=shows, BASE_URL=BASE_URL, generated_at=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
with open(OUTPUT_DIR / 'index.html', 'w', encoding='utf-8') as f:
f.write(index_output)
print("Generated Index: index.html")
except Exception as e:
print(f"ERROR: Could not generate index page: {e}")
sys.exit(1)
# 3. Generate support files
generate_search_index(shows)
generate_rss_feed(shows)
generate_sitemap(shows)
generate_robots_txt()
# 4. Generate static pages (about, contact)
for page_name in ['about', 'contact']:
try:
tmpl = env.get_template(f'{page_name}.html')
output = tmpl.render(BASE_URL=BASE_URL, generated_at=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
with open(OUTPUT_DIR / f'{page_name}.html', 'w', encoding='utf-8') as f:
f.write(output)
print(f"Generated: {page_name}.html")
except Exception as e:
print(f"WARNING: Could not generate {page_name}.html: {e}")
if __name__ == "__main__":
generate_site()