V.AISTUDIO / app /run_seo_fix.py
bep40's picture
Upload app/run_seo_fix.py
6ab9a37 verified
Raw
History Blame
2.76 kB
#!/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'''<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex,follow">
<title>{title}</title>
<meta name="description" content="{desc}">
<link rel="canonical" href="{BASE_URL}/san-pham/{slug}/">
<meta property="og:type" content="product">
<meta property="og:title" content="{title}">
<meta property="og:description" content="{desc}">
<meta property="og:image" content="{img}">
<meta property="og:url" content="{BASE_URL}/san-pham/{slug}/">
<meta property="og:site_name" content="V.AISTUDIO">
<meta name="twitter:card" content="summary_large_image">
<script>window.location.replace("/?product={slug}");</script>
</head>
<body><h1><a href="/?product={slug}">{title}</a></h1></body>
</html>'''
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()