File size: 3,671 Bytes
201164a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
SEO Stub Generator - Phiên bản đơn giản cho Static Space
Tạo file HTML stub cho mỗi sản phẩm để xử lý URL /san-pham/{slug}/

CÁCH CHẠY:
1. Trên UI Space: Settings → Build → Trigger Rebuild
   (Build sẽ tự động chạy build.sh → python3 app/run_seo_generation.py)
2. Hoặc manually: python3 app/run_seo_generation.py

YÊU CẦU:
- huggingface_hub (được cài trong build.sh)
"""
import json
import re
import unicodedata
import urllib.request

try:
    from huggingface_hub import HfApi
except ImportError:
    import subprocess
    subprocess.run(["pip", "install", "huggingface_hub", "-q"])
    from huggingface_hub import HfApi

SPACE_ID = "bep40/V.AISTUDIO"
BASE_URL = "https://bep40-v-aistudio.static.hf.space"

def slugify(name):
    s = unicodedata.normalize('NFKD', name or '').lower()
    s = re.sub(r'[^a-z0-9]+', '-', s).strip('-')
    return s or str(hash(name) % 100000)

def create_stub(p):
    slug = p.get('slug') or slugify(p.get('name', 'product'))
    name = str(p.get('name', 'Product'))[:200]
    brand = str(p.get('brand', ''))
    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} - V.AISTUDIO" if brand else f"{name} - V.AISTUDIO"
    
    return f'''<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{name} | {brand} - V.AISTUDIO</title>
<meta name="description" content="{desc}">
<meta name="robots" content="noindex,follow">
<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>'''

def main():
    # Tải index.html
    url = f"https://huggingface.co/spaces/{SPACE_ID}/resolve/main/index.html"
    try:
        with urllib.request.urlopen(url, timeout=30) as r:
            html = r.read().decode('utf-8')
    except Exception as e:
        print(f"LỖI tải index: {e}")
        return
    
    # Trích xuất products
    match = re.search(r'var\s+products\s*=\s*(\[[\s\S]*?\]);', html)
    if not match:
        print("KHÔNG TÌM THẤY products array!")
        return
    
    products = json.loads(match.group(1))
    print(f"Tìm thấy {len(products)} sản phẩm")
    
    api = HfApi()
    uploaded = 0
    errors = 0
    
    for i, p in enumerate(products):
        slug = p.get('slug') or slugify(p.get('name', 'product'))
        stub = create_stub(p)
        
        try:
            api.upload_file(
                path_or_fileobj=stub.encode(),
                path_in_repo=f"san-pham/{slug}/index.html",
                repo_id=SPACE_ID,
                repo_type="space",
                commit_message=f"SEO stub: {slug}"
            )
            uploaded += 1
            if uploaded % 100 == 0:
                print(f"✓ Đã tạo {uploaded}/{len(products)} stubs...")
        except Exception as e:
            errors += 1
            if errors < 5:
                print(f"Lỗi {slug}: {e}")
    
    print(f"\n=== HOÀN THÀNH ===")
    print(f"Thành công: {uploaded}")
    print(f"Lỗi: {errors}")

if __name__ == "__main__":
    main()