File size: 2,761 Bytes
5069c88
6ab9a37
 
 
 
 
 
 
 
 
 
5069c88
 
6ab9a37
 
5069c88
6ab9a37
 
 
 
 
5069c88
6ab9a37
 
 
 
 
 
 
 
5069c88
6ab9a37
 
 
5069c88
6ab9a37
 
 
 
 
 
5069c88
 
 
 
 
 
6ab9a37
 
 
5069c88
6ab9a37
 
5069c88
6ab9a37
 
5069c88
 
 
6ab9a37
5069c88
 
6ab9a37
 
 
 
 
 
 
 
 
 
 
 
 
5069c88
6ab9a37
5069c88
 
6ab9a37
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
#!/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()