#!/usr/bin/env python3 import os, sys, json, re, time, urllib.request from huggingface_hub import HfApi SPACE_ID = "bep40/V.AISTUDIO" BASE_URL = "https://bep40-v-aistudio.static.hf.space" def slugify(name): s = ''.join(c for c in (name or '').lower() if c.isalnum() or c in '- ') s = re.sub(r'-+', '-', s).strip('-') return s or 'product' def main(): print("=== SEO Stub Generator ===") api = HfApi() # Download index.html print("[1/3] Downloading index.html...") with urllib.request.urlopen(f"https://huggingface.co/spaces/{SPACE_ID}/resolve/main/index.html") as r: html = r.read().decode('utf-8') print(f" Size: {len(html):,} bytes") # Extract products print("[2/3] Extracting products...") m = re.search(r'var\s+products\s*=\s*(\[[\s\S]*?\]);', html) if not m: print("ERROR: Cannot find products array") return products = json.loads(m.group(1)) print(f" Found {len(products)} products") # Upload stubs print("[3/3] Uploading stubs...") uploaded = 0 for p in products: slug = p.get('slug') or slugify(p.get('name', 'p')) name = str(p.get('name', 'Product'))[:200] brand = str(p.get('brand', 'V.AISTUDIO')) img = p.get('image') or f"{BASE_URL}/logo/logo_600.png" desc = re.sub(r'<[^>]+>', '', str(p.get('description', '')))[:160] title = f"{name} | {brand}" stub = f''' {title}

{title}

''' try: api.upload_file( path_or_fileobj=stub.encode('utf-8'), path_in_repo=f"san-pham/{slug}/index.html", repo_id=SPACE_ID, repo_type="space", commit_message=f"SEO: {name[:40]}" ) uploaded += 1 if uploaded % 100 == 0: print(f" {uploaded} stubs uploaded...") except Exception as e: pass print(f"\nDONE! {uploaded} stubs uploaded to {SPACE_ID}") if __name__ == "__main__": main()