#!/usr/bin/env python3 """Generate SEO stubs for ALL products in index.html - run during build.sh""" import json, re, unicodedata, os, time, urllib.request from huggingface_hub import HfApi SPACE_ID = "bep40/V.AISTUDIO" BASE_URL = "https://bep40-v-aistudio.static.hf.space" def download_index(): print("Downloading index.html...") api = HfApi() path = api.hf_hub_download(SPACE_ID, "index.html", repo_type="space") with open(path, 'r') as f: return f.read() def extract_products(html): print("Extracting products...") idx = html.find('var products = ') if idx == -1: idx = html.find('products = [') if idx == -1: raise ValueError("Products not found") start = html.find('[', idx) # Parse JSON array bracket = 0 end = start in_str = False for i in range(start, len(html)): c = html[i] if c == '"' and html[i-1] != '\\': in_str = not in_str elif not in_str: if c == '[': bracket += 1 elif c == ']': bracket -= 1 if bracket == 0: end = i + 1 break products = json.loads(html[start:end]) print(f"Found {len(products)} products") return products def slugify(name): s = unicodedata.normalize('NFKD', name).lower() s = re.sub(r'[^a-z0-9]+', '-', s).strip('-') s = re.sub(r'-+', '-', s) return s def create_stub(p): slug = p.get('slug') or slugify(p.get('name', 'p')) name = p.get('name', 'Product')[:200] brand = p.get('brand', '') price = p.get('price', '') img = p.get('image', BASE_URL + '/logo/logo_600.png') desc = re.sub(r'<[^>]+>', '', str(p.get('description', '')))[:160] title = f"{name} | {brand} - V.AISTUDIO" if brand else f"{name} - V.AISTUDIO" return f'''