bep40 commited on
Commit
f7d7f1f
·
verified ·
1 Parent(s): b38fff7

Upload generate_seo_stubs.py

Browse files
Files changed (1) hide show
  1. generate_seo_stubs.py +120 -0
generate_seo_stubs.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ SEO STUB GENERATOR cho V.AISTUDIO Static Space
4
+ Tạo 1979 file HTML cho mỗi sản phẩm để fix 404 và hỗ tr�n SEO URL sharing.
5
+
6
+ Cài đặt: pip install huggingface_hub requests
7
+ Chạy: python3 generate_seo_stubs.py
8
+
9
+ Yêu cầu: BIẾT_TOKEN trong environment hoặc ~/.huggingface/token
10
+ """
11
+ import json
12
+ import re
13
+ import unicodedata
14
+ import time
15
+ import urllib.request
16
+
17
+ try:
18
+ from huggingface_hub import HfApi
19
+ except ImportError:
20
+ import subprocess
21
+ subprocess.check_call(["pip", "install", "-q", "huggingface_hub"])
22
+ from huggingface_hub import HfApi
23
+
24
+ SPACE_ID = "bep40/V.AISTUDIO"
25
+ BASE = "https://bep40-v-aistudio.static.hf.space"
26
+
27
+ def slugify(name):
28
+ s = unicodedata.normalize('NFKD', name)
29
+ s = s.encode('ascii', 'ignore').decode('ascii').lower()
30
+ s = re.sub(r'[^a-z0-9]+', '-', s).strip('-')
31
+ s = re.sub(r'-+', '-', s)
32
+ return s
33
+
34
+ def html_stub(slug, name, brand, price, img, desc):
35
+ title = f"{name} | {brand} - V.AISTUDIO" if brand else f"{name} - V.AISTUDIO"
36
+ desc = re.sub(r'<[^>]+>', '', str(desc))[:160]
37
+ img = img or f"{BASE}/logo/logo_600.png"
38
+ return f'''<!DOCTYPE html>
39
+ <html lang="vi">
40
+ <head>
41
+ <meta charset="UTF-8">
42
+ <title>{title}</title>
43
+ <meta name="description" content="{desc}">
44
+ <meta name="robots" content="noindex,follow">
45
+ <link rel="canonical" href="{BASE}/san-pham/{slug}/">
46
+ <meta property="og:type" content="product">
47
+ <meta property="og:title" content="{title}">
48
+ <meta property="og:description" content="{desc}">
49
+ <meta property="og:image" content="{img}">
50
+ <meta property="og:url" content="{BASE}/san-pham/{slug}/">
51
+ <meta property="og:site_name" content="V.AISTUDIO">
52
+ <meta name="twitter:card" content="summary_large_image">
53
+ <script>window.location.replace("/?product={slug}");</script>
54
+ </head>
55
+ <body><h1><a href="/?product={slug}">{title}</a></h1></body>
56
+ </html>'''
57
+
58
+ def main():
59
+ api = HfApi()
60
+
61
+ # Download index.html
62
+ print("Downloading index.html...")
63
+ with urllib.request.urlopen(f"https://huggingface.co/spaces/{SPACE_ID}/resolve/main/index.html") as r:
64
+ html = r.read().decode()
65
+
66
+ # Extract products JSON
67
+ m = re.search(r'var products\s*=\s*(\[[\s\S]*?\]);', html)
68
+ if not m:
69
+ print("ERROR: Khong tim thay products array")
70
+ return
71
+
72
+ # Parse
73
+ products_json = m.group(1)
74
+ products_json = re.sub(r'\\x([0-9a-f]{2})', lambda x: chr(int(x.group(1),16)), products_json)
75
+ products_json = re.sub(r',\s*([}\]])', r'\1', products_json)
76
+ products = json.loads(products_json)
77
+ print(f"Found {len(products)} products")
78
+
79
+ # Upload stubs
80
+ uploaded = 0
81
+ seen = {}
82
+ for p in products:
83
+ name = p.get('name', '')
84
+ slug = p.get('slug') or slugify(name)
85
+ if slug in seen:
86
+ seen[slug] += 1
87
+ slug = f"{slug}-{seen[slug]}"
88
+ seen[slug] = 0
89
+
90
+ stub = html_stub(
91
+ slug,
92
+ name[:200],
93
+ p.get('brand', ''),
94
+ p.get('price', ''),
95
+ p.get('image', ''),
96
+ p.get('description', '')
97
+ )
98
+
99
+ try:
100
+ api.upload_file(
101
+ path_or_fileobj=stub.encode(),
102
+ path_in_repo=f"san-pham/{slug}/index.html",
103
+ repo_id=SPACE_ID,
104
+ repo_type="space",
105
+ commit_message=f"SEO stub: {name[:40]}"
106
+ )
107
+ uploaded += 1
108
+ except Exception as e:
109
+ print(f" Error: {e}")
110
+
111
+ if uploaded % 100 == 0:
112
+ print(f" {uploaded}...")
113
+ time.sleep(0.5)
114
+
115
+ print(f"Done! {uploaded} stubs uploaded to {SPACE_ID}")
116
+ # Xoa file nay sau khi chay xong
117
+ print("\nKiem tra: curl -sI https://bep40-v-aistudio.static.hf.space/san-pham/<slug>/")
118
+
119
+ if __name__ == "__main__":
120
+ main()