bep40 commited on
Commit
709596c
·
verified ·
1 Parent(s): aa042d1

Restore to commit 60e310a

Browse files
.gitattributes CHANGED
@@ -1,16 +1,35 @@
1
- *.png filter=lfs diff=lfs merge=lfs -text
2
- *.jpg filter=lfs diff=lfs merge=lfs -text
3
- *.jpeg filter=lfs diff=lfs merge=lfs -text
4
- *.gif filter=lfs diff=lfs merge=lfs -text
5
- *.webp filter=lfs diff=lfs merge=lfs -text
6
- *.mp4 filter=lfs diff=lfs merge=lfs -text
7
- *.mp3 filter=lfs diff=lfs merge=lfs -text
8
- *.wav filter=lfs diff=lfs merge=lfs -text
9
- *.flac filter=lfs diff=lfs merge=lfs -text
10
- *.ogg filter=lfs diff=lfs merge=lfs -text
11
- *.ttf filter=lfs diff=lfs merge=lfs -text
12
- *.otf filter=lfs diff=lfs merge=lfs -text
13
- *.woff filter=lfs diff=lfs merge=lfs -text
14
- *.woff2 filter=lfs diff=lfs merge=lfs -text
15
- *.eot filter=lfs diff=lfs merge=lfs -text
16
- *.svg filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
Dockerfile CHANGED
@@ -12,4 +12,4 @@ RUN pip install --no-cache-dir -r requirements.txt || true
12
  COPY . .
13
  EXPOSE 7860
14
 
15
- CMD ["uvicorn", "_run:app", "--host", "0.0.0.0", "--port", "7860", "--reload"]
 
12
  COPY . .
13
  EXPOSE 7860
14
 
15
+ CMD ["uvicorn", "_run:app", "--host", "0.0.0.0", "--port", "7860", "--reload"]
TRIGGER_REBUILD_V6.md CHANGED
@@ -1 +1 @@
1
- # Rebuild trigger v6 - 1780971280.8544343
 
1
+ # Rebuild trigger v6 - 1780971280.8544343
_run.py CHANGED
@@ -1,19 +1 @@
1
- import os, sys
2
-
3
- # Auto-restore from commit 60e310a if needed
4
- _restore_marker = '/app/.restored_60e310a'
5
- if not os.path.exists(_restore_marker):
6
- print("[RESTORE] First run - restoring from commit 60e310a...", flush=True)
7
- try:
8
- import subprocess
9
- result = subprocess.run([sys.executable, '/app/restore.py'], capture_output=True, text=True, timeout=120)
10
- print(f"[RESTORE] stdout: {result.stdout[-500:]}", flush=True)
11
- print(f"[RESTORE] stderr: {result.stderr[-500:]}", flush=True)
12
- if result.returncode != 0:
13
- print(f"[RESTORE] Failed with code {result.returncode}", flush=True)
14
- except Exception as e:
15
- print(f"[RESTORE] Error: {e}", flush=True)
16
- # Mark as restored even if failed (avoid retry loop)
17
- open(_restore_marker, 'w').write('restored')
18
-
19
- from app_v2_entry import app # v5-stable inline bongda proxy
 
1
+ from app_v2_entry import app # v5-stable inline bongda proxy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
_static_build_trigger.txt CHANGED
@@ -1 +1 @@
1
- # Force rebuild - match_detail debug v2
 
1
+ # Force rebuild - match_detail debug v2
ai_fix2.py ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, re, subprocess, html as html_lib, json
2
+ from urllib.parse import quote_plus, urlparse, parse_qs, unquote
3
+ import requests
4
+ import ai_patch as prev
5
+ from ai_patch import app
6
+ from fastapi import Request
7
+ from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
8
+
9
+ base = prev.base
10
+
11
+
12
+ def clean(s):
13
+ return re.sub(r"\s+", " ", html_lib.unescape(s or "")).strip()
14
+
15
+
16
+ def _is_real_article_text(raw):
17
+ raw = clean(raw)
18
+ if len(raw) < 500:
19
+ return False
20
+ # Reject search-result/title-only pages: need several real sentences.
21
+ sentences = re.split(r"(?<=[\.\!\?])\s+", raw)
22
+ long_sentences = [s for s in sentences if len(s) > 45]
23
+ return len(long_sentences) >= 5
24
+
25
+
26
+ def _extract_ddg_url(href):
27
+ if not href:
28
+ return ""
29
+ if href.startswith("//"):
30
+ href = "https:" + href
31
+ if "duckduckgo.com/l/" in href:
32
+ try:
33
+ qs = parse_qs(urlparse(href).query)
34
+ if qs.get("uddg"):
35
+ return unquote(qs["uddg"][0])
36
+ except Exception:
37
+ pass
38
+ return href
39
+
40
+
41
+ def _ddg_article_urls(topic, limit=12):
42
+ urls = []
43
+ try:
44
+ q = quote_plus(topic + " tin tức bài viết phân tích")
45
+ r = requests.get("https://html.duckduckgo.com/html/?q=" + q, headers=base.HEADERS, timeout=18)
46
+ r.encoding = "utf-8"
47
+ from bs4 import BeautifulSoup
48
+ soup = BeautifulSoup(r.text, "lxml")
49
+ for a in soup.select("a.result__a"):
50
+ u = _extract_ddg_url(a.get("href", ""))
51
+ if not u.startswith("http"):
52
+ continue
53
+ if any(bad in u for bad in ["google.com", "youtube.com", "facebook.com", "x.com", "twitter.com"]):
54
+ continue
55
+ if u not in urls:
56
+ urls.append(u)
57
+ if len(urls) >= limit:
58
+ break
59
+ except Exception:
60
+ pass
61
+ return urls
62
+
63
+
64
+ def _rss_article_urls(topic, limit=10):
65
+ out = []
66
+ try:
67
+ url = "https://news.google.com/rss/search?q=" + quote_plus(topic) + "&hl=vi&gl=VN&ceid=VN:vi"
68
+ r = requests.get(url, headers=base.HEADERS, timeout=15)
69
+ r.encoding = "utf-8"
70
+ from bs4 import BeautifulSoup
71
+ soup = BeautifulSoup(r.text, "xml")
72
+ for it in soup.find_all("item")[:limit]:
73
+ title = it.find("title").get_text(" ", strip=True) if it.find("title") else ""
74
+ link = it.find("link").get_text(strip=True) if it.find("link") else ""
75
+ src = it.find("source").get_text(" ", strip=True) if it.find("source") else base._domain(link)
76
+ if title and link:
77
+ out.append({"title": title, "url": link, "via": src, "excerpt": title})
78
+ except Exception:
79
+ pass
80
+ return out
81
+
82
+
83
+ def _topic_source_articles(topic, limit=5):
84
+ """Scrape actual article bodies. Do not accept title-only sources."""
85
+ candidates = []
86
+ seen = set()
87
+
88
+ # 1) DuckDuckGo actual result URLs are usually more directly scrapable.
89
+ for u in _ddg_article_urls(topic, limit=14):
90
+ if u not in seen:
91
+ seen.add(u)
92
+ candidates.append({"url": u, "title": "", "via": base._domain(u)})
93
+
94
+ # 2) Add base web_context sources.
95
+ try:
96
+ _ctx, srcs = base.web_context(topic, limit=8)
97
+ for s in srcs or []:
98
+ u = s.get("url") or ""
99
+ if u.startswith("http") and u not in seen:
100
+ seen.add(u)
101
+ candidates.append(s)
102
+ except Exception:
103
+ pass
104
+
105
+ # 3) Google News RSS fallback last.
106
+ for s in _rss_article_urls(topic, limit=10):
107
+ u = s.get("url") or ""
108
+ if u.startswith("http") and u not in seen:
109
+ seen.add(u)
110
+ candidates.append(s)
111
+
112
+ out = []
113
+ for s in candidates[:24]:
114
+ url = s.get("url") or ""
115
+ try:
116
+ page = base.scrape_any_url(url)
117
+ raw = (page.get("summary", "") + "\n" + page.get("text", "")).strip()
118
+ if not _is_real_article_text(raw):
119
+ continue
120
+ title = page.get("title") or s.get("title") or url
121
+ via = page.get("via") or s.get("via") or base._domain(url)
122
+ out.append({
123
+ "title": title,
124
+ "url": url,
125
+ "raw": raw,
126
+ "image": page.get("image") or "",
127
+ "via": via,
128
+ "source": {"title": title, "url": url, "excerpt": raw[:700], "via": via}
129
+ })
130
+ if len(out) >= limit:
131
+ break
132
+ except Exception:
133
+ continue
134
+ return out[:limit]
135
+
136
+
137
+ def sentence_split(text):
138
+ text = re.sub(r"^[•\-\*]\s*", "", text or "", flags=re.M)
139
+ text = re.sub(r"\n+", ". ", text)
140
+ parts = []
141
+ for s in re.split(r"(?<=[\.\!\?])\s+", text):
142
+ s = clean(s)
143
+ if len(s) >= 8:
144
+ parts.append(s)
145
+ return parts
146
+
147
+
148
+ def srt_time(sec):
149
+ ms = int((sec - int(sec)) * 1000)
150
+ sec = int(sec)
151
+ return f"{sec//3600:02d}:{(sec%3600)//60:02d}:{sec%60:02d},{ms:03d}"
152
+
153
+
154
+ def parse_timecode(t):
155
+ # 00:00:01.234 or 00:00:01,234
156
+ t = t.replace(',', '.')
157
+ parts = t.split(':')
158
+ if len(parts) == 3:
159
+ return int(parts[0])*3600 + int(parts[1])*60 + float(parts[2])
160
+ if len(parts) == 2:
161
+ return int(parts[0])*60 + float(parts[1])
162
+ return float(parts[0])
163
+
164
+
165
+ def convert_vtt_to_scaled_srt(vtt_path, srt_path, speed=1.2):
166
+ try:
167
+ txt = open(vtt_path, 'r', encoding='utf-8').read().splitlines()
168
+ cues = []
169
+ i = 0
170
+ while i < len(txt):
171
+ line = txt[i].strip()
172
+ if '-->' in line:
173
+ a, b = [x.strip().split()[0] for x in line.split('-->')[:2]]
174
+ start = parse_timecode(a) / speed
175
+ end = parse_timecode(b) / speed
176
+ i += 1
177
+ texts = []
178
+ while i < len(txt) and txt[i].strip():
179
+ texts.append(txt[i].strip())
180
+ i += 1
181
+ s = clean(' '.join(texts))
182
+ if s:
183
+ cues.append((start, end, s))
184
+ i += 1
185
+ if not cues:
186
+ return False
187
+ with open(srt_path, 'w', encoding='utf-8') as f:
188
+ for idx, (st, en, s) in enumerate(cues, 1):
189
+ if en <= st:
190
+ en = st + 1.2
191
+ f.write(f"{idx}\n{srt_time(st)} --> {srt_time(en)}\n{s}\n\n")
192
+ return True
193
+ except Exception:
194
+ return False
195
+
196
+
197
+ def write_weighted_srt(script, path, total_duration):
198
+ subs = sentence_split(script)
199
+ if not subs:
200
+ subs = [clean(script)[:140] or "VNEWS"]
201
+ total_chars = max(1, sum(len(x) for x in subs))
202
+ usable = max(2.0, float(total_duration) - 1.0)
203
+ cur = 0.5
204
+ with open(path, "w", encoding="utf-8") as f:
205
+ for i, s in enumerate(subs, 1):
206
+ dur = max(1.8, min(7.0, usable * len(s) / total_chars))
207
+ start = cur
208
+ end = min(total_duration - 0.15, cur + dur)
209
+ cur = end + 0.18
210
+ f.write(f"{i}\n{srt_time(start)} --> {srt_time(end)}\n{s}\n\n")
211
+ if cur >= total_duration - 0.2:
212
+ break
213
+
214
+
215
+ def tts_script_full(post, emotion):
216
+ title = clean(post.get("title", ""))
217
+ text = clean(post.get("text", ""))
218
+ text = re.sub(r"Nguồn tham khảo:.*", "", text, flags=re.S).strip()
219
+ prefix = {
220
+ "urgent": "Tin nhanh.",
221
+ "warm": "Câu chuyện đáng chú ý.",
222
+ "serious": "Bản tin nghiêm túc.",
223
+ "energetic": "Cập nhật nổi bật.",
224
+ }.get(emotion, "")
225
+ script = f"{prefix} {title}. {text}".strip()
226
+ # Keep complete wall summary. Only trim pathological payloads, on sentence boundary.
227
+ if len(script) > 3600:
228
+ tmp = script[:3600]
229
+ cut = max(tmp.rfind("."), tmp.rfind("!"), tmp.rfind("?"))
230
+ script = tmp[:cut + 1] if cut > 1600 else tmp
231
+ script = re.sub(r"([\.\!\?])\s*", r"\1\n", script)
232
+ script = re.sub(r"\n{2,}", "\n", script).strip()
233
+ return script
234
+
235
+
236
+ _PATCH = {('/api/topic_post','POST'),('/api/ai/short/{post_id}','POST'),('/api/ai/short-file/{file_id}','GET'),('/','GET')}
237
+ app.router.routes = [r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
238
+
239
+
240
+ @app.post('/api/topic_post')
241
+ async def topic_post_aggregate(request: Request):
242
+ body = await request.json()
243
+ topic = base._clean_text(body.get('topic',''))
244
+ if not topic:
245
+ return JSONResponse({'error':'missing topic'}, status_code=400)
246
+ articles = _topic_source_articles(topic, limit=5)
247
+ if not articles:
248
+ return JSONResponse({'error':'Không scrape được nội dung bài viết thật cho chủ đề này. Hãy thử chủ đề cụ thể hơn hoặc dán URL trực tiếp.'}, status_code=422)
249
+ source_blocks = []
250
+ sources = []
251
+ image = ""
252
+ for i, art in enumerate(articles, 1):
253
+ raw = art.get('raw','')
254
+ source_blocks.append(f"[Nguồn {i}] {art.get('title','')} ({art.get('via','')})\n{raw[:3000]}")
255
+ sources.append(art.get('source') or {'title': art.get('title'), 'url': art.get('url'), 'via': art.get('via'), 'excerpt': raw[:600]})
256
+ if not image and art.get('image'):
257
+ image = art.get('image')
258
+ ctx = "\n\n".join(source_blocks)
259
+ prompt = f"""Bạn là biên tập viên tổng hợp tin tức tiếng Việt.
260
+
261
+ Chủ đề: {topic}
262
+
263
+ NHIỆM VỤ:
264
+ - Đọc nội dung của TẤT CẢ các bài nguồn bên dưới.
265
+ - Tổng hợp thành 1 bản tóm tắt chung duy nhất, giống cách tóm tắt qua URL.
266
+ - Không tạo mỗi tiêu đề thành một bài riêng.
267
+ - Không chỉ liệt kê tiêu đề; phải dựa vào nội dung trong từng bài.
268
+ - Không lặp ý giữa các nguồn.
269
+ - Tối đa 6 gạch đầu dòng, mỗi dòng 1 câu rõ ràng.
270
+ - Nếu các nguồn có góc nhìn khác nhau, gộp lại thành ý tổng hợp.
271
+ - Cuối cùng thêm dòng: Nguồn tham khảo: tên website.
272
+
273
+ Nội dung nguồn:
274
+ {ctx[:16000]}"""
275
+ text = await prev.base.qwen_generate(prompt, image_url=image or None, max_tokens=1100)
276
+ text = prev._postprocess_ai_text(text, max_units=7)
277
+ if 'Nguồn tham khảo:' not in text:
278
+ text += '\n\n' + prev._source_line(sources)
279
+ post = base.make_post('Tổng hợp: ' + topic, text, image or base.pollinations_image_url(topic), '', 'topic_aggregate', sources=sources[:5])
280
+ posts = base._load_ai_wall(); posts.insert(0, post); base._save_ai_wall(posts)
281
+ return JSONResponse({'post': post, 'count_sources': len(sources)})
282
+
283
+
284
+ @app.post('/api/ai/short/{post_id}')
285
+ async def ai_short_full(post_id: str, request: Request):
286
+ try:
287
+ body = await request.json()
288
+ except Exception:
289
+ body = {}
290
+ voice = str(body.get('voice','nu')).lower().strip()
291
+ emotion = str(body.get('emotion','neutral')).lower().strip()
292
+ speed = max(0.85, min(1.35, float(body.get('speed', 1.2) or 1.2)))
293
+ posts = base._load_ai_wall()
294
+ post = next((p for p in posts if str(p.get('id')) == str(post_id)), None)
295
+ if not post:
296
+ return JSONResponse({'error':'post not found'}, status_code=404)
297
+ os.makedirs(base.SHORTS_DIR, exist_ok=True)
298
+ suffix = f"_{voice}_{emotion}_{str(speed).replace('.', 'p')}_fullv2"
299
+ out_mp4 = os.path.join(base.SHORTS_DIR, base._safe_name(post_id + suffix) + '.mp4')
300
+ if os.path.exists(out_mp4):
301
+ post['video'] = '/api/ai/short-file/' + post_id + suffix
302
+ base._save_ai_wall(posts)
303
+ return JSONResponse({'video': post['video'], 'speed': speed, 'subtitles': True})
304
+ work = os.path.join(base.SHORTS_DIR, base._safe_name(post_id + suffix)); os.makedirs(work, exist_ok=True)
305
+ img = os.path.join(work,'image.jpg'); frame = os.path.join(work,'frame.jpg'); audio = os.path.join(work,'voice.mp3'); audio_fast=os.path.join(work,'voice_fast.mp3'); srt=os.path.join(work,'subtitles.srt'); vtt=os.path.join(work,'subtitles.vtt')
306
+ try:
307
+ base._download_image(post.get('img'), post.get('title','AI news'), img)
308
+ prev._make_short_frame_full(post, img, frame)
309
+ script = tts_script_full(post, emotion)
310
+ edge_voice = {'nam':'vi-VN-NamMinhNeural','male':'vi-VN-NamMinhNeural','nu':'vi-VN-HoaiMyNeural','female':'vi-VN-HoaiMyNeural','mien-nam':'vi-VN-HoaiMyNeural'}.get(voice,'vi-VN-HoaiMyNeural')
311
+ used_edge = False
312
+ try:
313
+ subprocess.run(['python','-m','edge_tts','--voice',edge_voice,'--text',script,'--write-media',audio,'--write-subtitles',vtt], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=260)
314
+ used_edge = True
315
+ except Exception:
316
+ tld = 'com.vn' if voice in ('nu','female','mien-nam') else 'com'
317
+ try:
318
+ base.gTTS(script, lang='vi', tld=tld, slow=False).save(audio)
319
+ except TypeError:
320
+ base.gTTS(script, lang='vi', slow=False).save(audio)
321
+ subprocess.run(['ffmpeg','-y','-i',audio,'-filter:a',f'atempo={speed}','-vn',audio_fast], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=220)
322
+ duration = 45.0
323
+ try:
324
+ pr = subprocess.run(['ffprobe','-v','error','-show_entries','format=duration','-of','default=noprint_wrappers=1:nokey=1',audio_fast], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20)
325
+ duration = float((pr.stdout or b'45').decode().strip() or 45)
326
+ except Exception:
327
+ pass
328
+ if used_edge and os.path.exists(vtt):
329
+ ok = convert_vtt_to_scaled_srt(vtt, srt, speed=speed)
330
+ if not ok:
331
+ write_weighted_srt(script, srt, duration)
332
+ else:
333
+ write_weighted_srt(script, srt, duration)
334
+ vf = "scale=1080:1920,subtitles='{}':force_style='FontName=DejaVu Sans,FontSize=16,PrimaryColour=&H00FFFFFF,OutlineColour=&HAA000000,BorderStyle=1,Outline=1.5,Shadow=0,Alignment=2,MarginV=42'".format(srt.replace("'", "\\'"))
335
+ cmd = ['ffmpeg','-y','-loop','1','-i',frame,'-i',audio_fast,'-shortest','-c:v','libx264','-tune','stillimage','-pix_fmt','yuv420p','-c:a','aac','-b:a','128k','-vf',vf,out_mp4]
336
+ subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=420)
337
+ post['video'] = '/api/ai/short-file/' + post_id + suffix
338
+ post['short_voice'] = voice; post['short_emotion'] = emotion; post['short_speed'] = speed; post['short_subtitles'] = True
339
+ base._save_ai_wall(posts)
340
+ return JSONResponse({'video': post['video'], 'voice': voice, 'emotion': emotion, 'speed': speed, 'subtitles': True, 'duration': duration})
341
+ except Exception as e:
342
+ return JSONResponse({'error':'Không tạo được shorts: '+str(e)[:180]}, status_code=500)
343
+
344
+
345
+ @app.get('/api/ai/short-file/{file_id}')
346
+ def ai_short_file_full(file_id: str):
347
+ path = os.path.join(base.SHORTS_DIR, base._safe_name(file_id) + '.mp4')
348
+ if not os.path.exists(path):
349
+ return JSONResponse({'error':'not found'}, status_code=404)
350
+ return FileResponse(path, media_type='video/mp4', filename=f'vnews-ai-{file_id}.mp4')
351
+
352
+
353
+ app.router.routes = [r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
354
+
355
+ @app.get('/')
356
+ async def index_fix2():
357
+ with open('/app/static/index.html','r',encoding='utf-8') as f:
358
+ html = f.read()
359
+ inject = prev.PATCH_INJECT + r'''
360
+ <script>
361
+ (function(){
362
+ window.createTopicPost=function(){let inp=document.getElementById('ai-topic-input');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&j.post){window.location.reload();alert('Đã tổng hợp NỘI DUNG các bài nguồn thành 1 bản tóm tắt trên Tường AI');}else alert(j.error||'Lỗi tạo bài')}).catch(e=>alert(e.message||'Lỗi tạo bài'));};
363
+ })();
364
+ </script>
365
+ '''
366
+ return HTMLResponse(html.replace('</body>', inject+'\n</body>'))
ai_patch.py ADDED
@@ -0,0 +1,751 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import time
4
+ import random
5
+ import json
6
+ import html as html_lib
7
+ import subprocess
8
+ import requests
9
+ import ai_ext as base
10
+ from ai_ext import app
11
+ from fastapi import Request
12
+ from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
13
+ from bs4 import BeautifulSoup
14
+ from urllib.parse import quote_plus
15
+
16
+ try:
17
+ from PIL import Image, ImageDraw, ImageFont
18
+ except Exception:
19
+ Image = ImageDraw = ImageFont = None
20
+
21
+
22
+ def _clean(s):
23
+ s = html_lib.unescape(s or "")
24
+ s = re.sub(r"[ \t]+", " ", s)
25
+ s = re.sub(r"\n{3,}", "\n\n", s)
26
+ return s.strip()
27
+
28
+
29
+ def _norm(s):
30
+ s = s.lower()
31
+ s = re.sub(r"[^\wÀ-ỹ\s]", " ", s)
32
+ s = re.sub(r"\s+", " ", s).strip()
33
+ return s
34
+
35
+
36
+ def _similar(a, b):
37
+ ta = set(_norm(a).split())
38
+ tb = set(_norm(b).split())
39
+ if not ta or not tb:
40
+ return False
41
+ return len(ta & tb) / max(1, min(len(ta), len(tb))) >= 0.72
42
+
43
+
44
+ def _dedupe_units(units, max_units=7):
45
+ out, seen = [], set()
46
+ for u in units:
47
+ u = _clean(re.sub(r"^[-•*\d\.\)\s]+", "", u))
48
+ if len(u) < 18:
49
+ continue
50
+ nu = _norm(u)
51
+ if nu in seen:
52
+ continue
53
+ if any(_similar(u, old) for old in out):
54
+ continue
55
+ seen.add(nu)
56
+ out.append(u)
57
+ if len(out) >= max_units:
58
+ break
59
+ return out
60
+
61
+
62
+ def _postprocess_ai_text(text, max_units=7):
63
+ text = _clean(text)
64
+ if not text:
65
+ return text
66
+ drop_prefixes = (
67
+ "dưới đây", "sau đây", "bài viết", "tôi sẽ", "mình sẽ",
68
+ "tóm tắt bài", "tiêu đề:", "sapo:", "nội dung:", "kết luận:"
69
+ )
70
+ raw_lines = []
71
+ for line in re.split(r"\n+", text):
72
+ line = _clean(line)
73
+ if not line:
74
+ continue
75
+ low = line.lower().strip()
76
+ if any(low.startswith(p) and len(line) < 80 for p in drop_prefixes):
77
+ continue
78
+ raw_lines.append(line)
79
+ units = []
80
+ for line in raw_lines:
81
+ if len(line) > 260:
82
+ units.extend(re.split(r"(?<=[\.\!\?])\s+(?=[A-ZÀ-Ỹ0-9])", line))
83
+ else:
84
+ units.append(line)
85
+ units = _dedupe_units(units, max_units=max_units)
86
+ if not units:
87
+ return text[:900]
88
+ title = ""
89
+ if raw_lines and len(raw_lines[0]) <= 90 and not raw_lines[0].startswith(("-", "•", "*")):
90
+ title = raw_lines[0]
91
+ units = [u for u in units if not _similar(u, title)]
92
+ body = "\n".join("• " + u for u in units[:max_units])
93
+ return (title + "\n\n" + body).strip() if title else body
94
+
95
+
96
+ def _fallback_summary_from_prompt(prompt, max_units=6):
97
+ text = prompt or ""
98
+ for marker in ["Nội dung nguồn:", "Nội dung bài:", "Nội dung gốc:", "Nội dung:", "Nguồn/bối cảnh internet:"]:
99
+ if marker in text:
100
+ text = text.split(marker, 1)[1]
101
+ break
102
+ text = re.sub(r"https?://\S+", "", text)
103
+ text = re.sub(r"\s+", " ", text).strip()
104
+ sentences = re.split(r"(?<=[\.\!\?])\s+(?=[A-ZÀ-Ỹ0-9])", text)
105
+ candidates = []
106
+ for s in sentences:
107
+ s = _clean(s)
108
+ if 45 <= len(s) <= 260:
109
+ candidates.append(s)
110
+ units = _dedupe_units(candidates, max_units=max_units)
111
+ if units:
112
+ return "\n".join("• " + u for u in units)
113
+ if text:
114
+ return "• " + text[:700].rsplit(" ", 1)[0]
115
+ return "• Không có đủ nội dung nguồn để tóm tắt."
116
+
117
+
118
+ def _source_line(sources):
119
+ names = []
120
+ for s in (sources or [])[:5]:
121
+ via = s.get("via") or base._domain(s.get("url", "")) or s.get("title", "")
122
+ if via and via not in names:
123
+ names.append(via)
124
+ return "Nguồn tham khảo: " + ", ".join(names[:5]) if names else "Nguồn tham khảo: tổng hợp internet"
125
+
126
+
127
+ def _make_summary_prompt(title, raw, source_hint=""):
128
+ return f"""Bạn là biên tập viên tóm tắt tin tức tiếng Việt.
129
+
130
+ NHIỆM VỤ BẮT BUỘC:
131
+ - Chỉ TÓM TẮT nội dung chính, KHÔNG viết lại toàn bộ bài.
132
+ - Không lặp lại cùng một ý, cùng một câu, cùng một chi tiết.
133
+ - Không thêm thông tin ngoài nguồn.
134
+ - Tối đa 5 gạch đầu dòng, mỗi gạch đầu dòng 1 câu ngắn.
135
+ - Nếu bài có số liệu/nhân vật/thời điểm quan trọng thì giữ lại.
136
+ - Không viết phần mở bài dài, không viết văn kể lại.
137
+
138
+ Tiêu đề nguồn: {title}
139
+ Nguồn: {source_hint}
140
+
141
+ Nội dung nguồn:
142
+ {raw[:14000]}
143
+ """
144
+
145
+
146
+ def _direct_news_rss(topic, limit=10):
147
+ out = []
148
+ try:
149
+ url = "https://news.google.com/rss/search?q=" + quote_plus(topic) + "&hl=vi&gl=VN&ceid=VN:vi"
150
+ r = requests.get(url, headers=base.HEADERS, timeout=15)
151
+ r.encoding = "utf-8"
152
+ soup = BeautifulSoup(r.text, "xml")
153
+ for it in soup.find_all("item")[:limit]:
154
+ title = it.find("title").get_text(" ", strip=True) if it.find("title") else ""
155
+ link = it.find("link").get_text(strip=True) if it.find("link") else ""
156
+ src = it.find("source").get_text(" ", strip=True) if it.find("source") else base._domain(link)
157
+ if title and link:
158
+ out.append({"title": title, "url": link, "via": src, "excerpt": title})
159
+ except Exception:
160
+ pass
161
+ return out
162
+
163
+
164
+ def _topic_source_articles(topic, limit=5):
165
+ """Return actual scraped article bodies for a topic. Each source becomes one Wall AI post."""
166
+ try:
167
+ _ctx, sources = base.web_context(topic, limit=limit)
168
+ except Exception:
169
+ sources = []
170
+ if not sources:
171
+ sources = _direct_news_rss(topic, limit=10)
172
+ out, seen = [], set()
173
+ for s in (sources or [])[:limit * 3]:
174
+ url = s.get("url") or ""
175
+ if not url.startswith("http") or url in seen:
176
+ continue
177
+ seen.add(url)
178
+ try:
179
+ page = base.scrape_any_url(url)
180
+ raw = (page.get("summary", "") + "\n" + page.get("text", "")).strip()
181
+ if len(raw) < 180:
182
+ continue
183
+ title = page.get("title") or s.get("title") or url
184
+ via = page.get("via") or s.get("via") or base._domain(url)
185
+ out.append({
186
+ "title": title,
187
+ "url": url,
188
+ "raw": raw,
189
+ "image": page.get("image") or "",
190
+ "via": via,
191
+ "source": {"title": title, "url": url, "excerpt": raw[:700], "via": via}
192
+ })
193
+ if len(out) >= limit:
194
+ break
195
+ except Exception:
196
+ continue
197
+ if not out:
198
+ for s in (sources or _direct_news_rss(topic, 6))[:limit]:
199
+ title = s.get("title") or topic
200
+ excerpt = s.get("excerpt") or s.get("description") or s.get("content") or title
201
+ url = s.get("url", "")
202
+ via = s.get("via") or base._domain(url)
203
+ out.append({
204
+ "title": title,
205
+ "url": url,
206
+ "raw": excerpt,
207
+ "image": base.pollinations_image_url(title),
208
+ "via": via,
209
+ "source": {"title": title, "url": url, "excerpt": excerpt[:700], "via": via}
210
+ })
211
+ return out[:limit]
212
+
213
+
214
+ async def qwen_generate_resilient(prompt: str, image_url=None, max_tokens: int = 1200):
215
+ errors = []
216
+ token = base._hf_token()
217
+ try:
218
+ original = getattr(base, "_original_qwen_generate", None)
219
+ if original:
220
+ txt = await original(prompt, image_url=image_url, max_tokens=max_tokens)
221
+ if txt:
222
+ base.LAST_QWEN_ERROR = ""
223
+ return txt
224
+ if getattr(base, "LAST_QWEN_ERROR", ""):
225
+ errors.append("sdk: " + str(base.LAST_QWEN_ERROR)[:260])
226
+ except Exception as e:
227
+ errors.append(f"sdk: {type(e).__name__}: {str(e)[:260]}")
228
+ if token:
229
+ models = []
230
+ for m in [
231
+ os.getenv("QWEN_VL_MODEL", ""),
232
+ "Qwen/Qwen2.5-VL-7B-Instruct",
233
+ "Qwen/Qwen2.5-VL-3B-Instruct",
234
+ "Qwen/Qwen2.5-7B-Instruct",
235
+ "Qwen/Qwen2.5-3B-Instruct",
236
+ "Qwen/Qwen2.5-1.5B-Instruct",
237
+ ]:
238
+ if m and m not in models:
239
+ models.append(m)
240
+ headers = {"Authorization": "Bearer " + token, "Content-Type": "application/json"}
241
+ for model in models:
242
+ try:
243
+ is_vl = "VL" in model and bool(image_url)
244
+ user_content = ([{"type": "image_url", "image_url": {"url": image_url}}, {"type": "text", "text": prompt}] if is_vl else prompt)
245
+ payload = {
246
+ "model": model,
247
+ "messages": [
248
+ {"role": "system", "content": "Bạn là biên tập viên AI tiếng Việt. Chỉ tóm tắt súc tích nội dung nguồn, không viết lại toàn bài, không lặp ý, không bịa chi tiết."},
249
+ {"role": "user", "content": user_content},
250
+ ],
251
+ "max_tokens": min(int(max_tokens or 900), 1400),
252
+ "temperature": 0.35,
253
+ "top_p": 0.85,
254
+ }
255
+ r = requests.post("https://router.huggingface.co/v1/chat/completions", headers=headers, json=payload, timeout=95)
256
+ if r.status_code >= 300:
257
+ errors.append(f"{model}: HTTP {r.status_code} {r.text[:180]}")
258
+ continue
259
+ j = r.json()
260
+ txt = (j.get("choices", [{}])[0].get("message", {}).get("content") or "").strip()
261
+ if txt:
262
+ base.LAST_QWEN_ERROR = ""
263
+ return txt
264
+ errors.append(f"{model}: empty response")
265
+ except Exception as e:
266
+ errors.append(f"{model}: {type(e).__name__}: {str(e)[:220]}")
267
+ else:
268
+ errors.append("missing HF_TOKEN")
269
+ base.LAST_QWEN_ERROR = " | ".join(errors[-6:]) or "Qwen unavailable; used extractive fallback"
270
+ print("[qwen resilient fallback]", base.LAST_QWEN_ERROR)
271
+ return _fallback_summary_from_prompt(prompt, max_units=6)
272
+
273
+
274
+ if not hasattr(base, "_original_qwen_generate"):
275
+ base._original_qwen_generate = base.qwen_generate
276
+ base.qwen_generate = qwen_generate_resilient
277
+
278
+
279
+ @app.get('/api/wall')
280
+ def compat_wall():
281
+ return JSONResponse({'posts': base._load_ai_wall()[:80]})
282
+
283
+
284
+ _PATCHED_PATHS = {
285
+ ('/api/topic_post', 'POST'),
286
+ ('/api/url_wall', 'POST'),
287
+ ('/api/rewrite_share', 'POST'),
288
+ ('/api/ai/short/{post_id}', 'POST'),
289
+ }
290
+ app.router.routes = [
291
+ r for r in app.router.routes
292
+ if not any(getattr(r, 'path', None) == p and m in getattr(r, 'methods', set()) for p, m in _PATCHED_PATHS)
293
+ ]
294
+
295
+
296
+ @app.post('/api/topic_post')
297
+ async def compat_topic_post(request: Request):
298
+ body = await request.json()
299
+ topic = base._clean_text(body.get('topic', ''))
300
+ if not topic:
301
+ return JSONResponse({'error': 'missing topic'}, status_code=400)
302
+ articles = _topic_source_articles(topic, limit=4)
303
+ if not articles:
304
+ return JSONResponse({'error': 'Không lấy được bài viết nguồn cho chủ đề này.'}, status_code=422)
305
+ new_posts = []
306
+ posts = base._load_ai_wall()
307
+ for art in articles:
308
+ prompt = f"""Tóm tắt RIÊNG bài viết nguồn sau để đăng Tường AI.
309
+
310
+ Chủ đề lọc: {topic}
311
+ Tiêu đề bài nguồn: {art['title']}
312
+ Nguồn: {art['via']}
313
+
314
+ Yêu cầu bắt buộc:
315
+ - Tóm tắt nội dung trong BÀI VIẾT này, không chỉ tiêu đề.
316
+ - Không trộn với bài khác.
317
+ - Không viết lại toàn bộ bài.
318
+ - Không lặp ý.
319
+ - 4-6 gạch đầu dòng, mỗi dòng 1 câu rõ ràng.
320
+ - Giữ số liệu/nhân vật/thời điểm quan trọng nếu có.
321
+
322
+ Nội dung bài:
323
+ {art['raw'][:14000]}"""
324
+ text = await base.qwen_generate(prompt, image_url=art.get('image') or None, max_tokens=900)
325
+ text = _postprocess_ai_text(text, max_units=6)
326
+ src = [art['source']]
327
+ if 'Nguồn tham khảo:' not in text:
328
+ text += "\n\n" + _source_line(src)
329
+ post = base.make_post(art['title'], text, art.get('image') or base.pollinations_image_url(art['title']), art.get('url') or '', 'topic_article', sources=src)
330
+ new_posts.append(post)
331
+ posts = new_posts + posts
332
+ base._save_ai_wall(posts)
333
+ return JSONResponse({'post': new_posts[0], 'posts': new_posts, 'count': len(new_posts)})
334
+
335
+
336
+ @app.post('/api/url_wall')
337
+ async def compat_url_wall(request: Request):
338
+ body = await request.json()
339
+ url = base._clean_text(body.get('url', ''))
340
+ if not url.startswith('http'):
341
+ return JSONResponse({'error': 'missing url'}, status_code=400)
342
+ try:
343
+ data = base.scrape_any_url(url)
344
+ except Exception as e:
345
+ return JSONResponse({'error': 'Không scrape được URL: ' + str(e)[:180]}, status_code=422)
346
+ raw = (data.get('summary', '') + '\n' + data.get('text', '')).strip()
347
+ if len(raw) < 120:
348
+ return JSONResponse({'error': 'URL không có đủ nội dung để tóm tắt'}, status_code=422)
349
+ prompt = _make_summary_prompt(data.get('title', ''), raw, data.get('via', '') or base._domain(url))
350
+ text = await base.qwen_generate(prompt, image_url=data.get('image') or None, max_tokens=850)
351
+ text = _postprocess_ai_text(text, max_units=6)
352
+ src = [{'title': data.get('title'), 'url': url, 'excerpt': raw[:500], 'via': data.get('via') or base._domain(url)}]
353
+ if 'Nguồn tham khảo:' not in text:
354
+ text += "\n\n" + _source_line(src)
355
+ post = base.make_post(data.get('title') or 'Bài viết', text, data.get('image') or '', url, 'url', sources=src)
356
+ posts = base._load_ai_wall(); posts.insert(0, post); base._save_ai_wall(posts)
357
+ return JSONResponse({'post': post})
358
+
359
+
360
+ @app.post('/api/rewrite_share')
361
+ async def compat_rewrite_share(request: Request):
362
+ body = await request.json()
363
+ url = base._clean_text(body.get('url', ''))
364
+ if not url.startswith('http'):
365
+ return JSONResponse({'error': 'missing url'}, status_code=400)
366
+ try:
367
+ data = base.scrape_any_url(url)
368
+ except Exception as e:
369
+ return JSONResponse({'error': 'Không đọc được bài viết: ' + str(e)[:180]}, status_code=422)
370
+ raw = (data.get('summary', '') + '\n' + data.get('text', '')).strip()
371
+ if len(raw) < 120:
372
+ return JSONResponse({'error': 'Bài viết không đủ nội dung để tóm tắt'}, status_code=422)
373
+ prompt = _make_summary_prompt(data.get('title', ''), raw, data.get('via', '') or base._domain(url))
374
+ text = await base.qwen_generate(prompt, image_url=data.get('image') or None, max_tokens=850)
375
+ text = _postprocess_ai_text(text, max_units=6)
376
+ src = [{'title': data.get('title'), 'url': url, 'excerpt': raw[:500], 'via': data.get('via') or base._domain(url)}]
377
+ if 'Nguồn tham khảo:' not in text:
378
+ text += "\n\n" + _source_line(src)
379
+ post = base.make_post(data.get('title') or 'Bài viết', text, data.get('image') or '', url, 'summary', sources=src)
380
+ posts = base._load_ai_wall(); posts.insert(0, post); base._save_ai_wall(posts)
381
+ return JSONResponse({'post': post})
382
+
383
+
384
+ def _emotion_script(text, emotion):
385
+ text = _clean(text)
386
+ if emotion == 'urgent':
387
+ return 'Tin nhanh. ' + text
388
+ if emotion == 'warm':
389
+ return 'Câu chuyện đáng chú ý. ' + text
390
+ if emotion == 'serious':
391
+ return 'Bản tin nghiêm túc. ' + text
392
+ if emotion == 'energetic':
393
+ return 'Cập nhật nổi bật. ' + text
394
+ return text
395
+
396
+
397
+ def _tts_script_smart(post, emotion):
398
+ raw = base._short_script(post)
399
+ raw = re.sub(r"^[•\-\*]\s*", "", raw, flags=re.M)
400
+ raw = re.sub(r"\s*\n\s*", ". ", raw)
401
+ raw = re.sub(r"([\.\!\?])\s*", r"\1\n", raw)
402
+ raw = re.sub(r"\n{2,}", "\n", raw).strip()
403
+ raw = _emotion_script(raw, emotion)
404
+ if len(raw) > 1000:
405
+ raw = raw[:1000]
406
+ cut = max(raw.rfind("."), raw.rfind("!"), raw.rfind("?"))
407
+ if cut > 350:
408
+ raw = raw[:cut + 1]
409
+ return raw
410
+
411
+
412
+ def _split_subtitle_sentences(script):
413
+ parts = []
414
+ for line in script.splitlines():
415
+ line = _clean(line)
416
+ if not line:
417
+ continue
418
+ for s in re.split(r"(?<=[\.\!\?])\s+", line):
419
+ s = _clean(s)
420
+ if 8 <= len(s) <= 140:
421
+ parts.append(s)
422
+ return parts[:12]
423
+
424
+
425
+ def _srt_time(sec):
426
+ ms = int((sec - int(sec)) * 1000)
427
+ sec = int(sec)
428
+ h = sec // 3600
429
+ m = (sec % 3600) // 60
430
+ s = sec % 60
431
+ return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
432
+
433
+
434
+ def _write_srt(script, path, total_duration=30):
435
+ subs = _split_subtitle_sentences(script)
436
+ if not subs:
437
+ subs = [script[:120]]
438
+ dur = max(2.2, min(5.0, total_duration / max(1, len(subs))))
439
+ cur = 0.3
440
+ with open(path, 'w', encoding='utf-8') as f:
441
+ for i, s in enumerate(subs, 1):
442
+ start = cur
443
+ end = cur + dur
444
+ cur = end + 0.15
445
+ f.write(f"{i}\n{_srt_time(start)} --> {_srt_time(end)}\n{s}\n\n")
446
+
447
+
448
+ def _wrap_text_px(draw, text, font, max_width, max_lines):
449
+ words = _clean(text).split()
450
+ lines, cur = [], ""
451
+ for w in words:
452
+ test = (cur + " " + w).strip()
453
+ try:
454
+ width = draw.textbbox((0, 0), test, font=font)[2]
455
+ except Exception:
456
+ width = len(test) * 20
457
+ if width <= max_width:
458
+ cur = test
459
+ else:
460
+ if cur:
461
+ lines.append(cur)
462
+ cur = w
463
+ if len(lines) >= max_lines:
464
+ break
465
+ if cur and len(lines) < max_lines:
466
+ lines.append(cur)
467
+ return lines
468
+
469
+
470
+ def _make_short_frame_full(post, img_path, out_path):
471
+ if Image is None:
472
+ return base._make_short_frame(post, img_path, out_path)
473
+ W, H = 1080, 1920
474
+ bg = Image.new("RGB", (W, H), (14, 14, 14))
475
+ try:
476
+ im = Image.open(img_path).convert("RGB")
477
+ target = (1080, 760)
478
+ im_ratio = im.width / im.height
479
+ target_ratio = target[0] / target[1]
480
+ if im_ratio > target_ratio:
481
+ new_h = target[1]
482
+ new_w = int(new_h * im_ratio)
483
+ else:
484
+ new_w = target[0]
485
+ new_h = int(new_w / im_ratio)
486
+ im = im.resize((new_w, new_h))
487
+ left = (new_w - target[0]) // 2
488
+ top = (new_h - target[1]) // 2
489
+ im = im.crop((left, top, left + target[0], top + target[1]))
490
+ bg.paste(im, (0, 0))
491
+ except Exception:
492
+ pass
493
+ draw = ImageDraw.Draw(bg)
494
+ try:
495
+ font_title = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 54)
496
+ font_body = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 38)
497
+ font_label = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 30)
498
+ except Exception:
499
+ font_title = font_body = font_label = None
500
+ draw.rectangle((0, 720, W, H), fill=(14, 14, 14))
501
+ margin = 48
502
+ maxw = W - margin * 2
503
+ draw.text((margin, 770), "VNEWS · Tường AI", fill=(92, 184, 122), font=font_label)
504
+ y = 830
505
+ for ln in _wrap_text_px(draw, post.get("title", ""), font_title, maxw, 4):
506
+ draw.text((margin, y), ln, fill=(255, 255, 255), font=font_title)
507
+ y += 66
508
+ y += 18
509
+ text = post.get("text", "")
510
+ text = re.sub(r"Nguồn tham khảo:.*", "", text, flags=re.S).strip()
511
+ body_lines = _wrap_text_px(draw, text, font_body, maxw, 14)
512
+ for ln in body_lines:
513
+ draw.text((margin, y), ln, fill=(220, 220, 220), font=font_body)
514
+ y += 50
515
+ if y > 1640:
516
+ break
517
+ bg.save(out_path, quality=92)
518
+
519
+
520
+
521
+
522
+ def _summary_segments_from_post(post, max_segments=7):
523
+ raw = _clean(post.get('text') or post.get('title') or '')
524
+ raw = re.sub(r'^Bản tin AI viết lại:\s*', '', raw, flags=re.I)
525
+ raw = re.sub(r'Nguồn tham khảo:.*$', '', raw, flags=re.I|re.S).strip()
526
+ lines=[]
527
+ for ln in raw.splitlines():
528
+ ln=_clean(re.sub(r'^[•\-\*\d\.\)\s]+','',ln))
529
+ if not ln: continue
530
+ low=ln.lower()
531
+ if low.startswith(('điểm chính','tiêu đề','sapo','nguồn tham khảo')): continue
532
+ if len(ln)>=18: lines.append(ln)
533
+ if len(lines)<2:
534
+ lines=[]
535
+ for s in re.split(r'(?<=[\.\!\?])\s+', raw):
536
+ s=_clean(s)
537
+ if len(s)>=25: lines.append(s)
538
+ segs=_dedupe_units(lines, max_units=max_segments)
539
+ return segs[:max_segments] if segs else [post.get('title','Bản tin VNEWS')]
540
+
541
+
542
+ def _make_scene_frame(post, segment, idx, total, img_path, out_path, emotion='neutral'):
543
+ if Image is None:
544
+ return _make_short_frame_full(post, img_path, out_path)
545
+ W,H=1080,1920
546
+ bg=Image.new('RGB',(W,H),(10,10,10))
547
+ try:
548
+ im=Image.open(img_path).convert('RGB')
549
+ ratio=im.width/max(1,im.height); target=W/H
550
+ if ratio>target:
551
+ nh=H; nw=int(nh*ratio)
552
+ else:
553
+ nw=W; nh=int(nw/ratio)
554
+ cover=im.resize((nw,nh)); left=(nw-W)//2; top=(nh-H)//2
555
+ cover=cover.crop((left,top,left+W,top+H))
556
+ bg.paste(cover,(0,0))
557
+ bg=Image.blend(bg, Image.new('RGB',(W,H),(0,0,0)), 0.50)
558
+ hero_h=720; target=W/hero_h
559
+ if ratio>target:
560
+ nh=hero_h; nw=int(nh*ratio)
561
+ else:
562
+ nw=W; nh=int(nw/ratio)
563
+ hero=im.resize((nw,nh)); left=(nw-W)//2; top=(nh-hero_h)//2
564
+ hero=hero.crop((left,top,left+W,top+hero_h))
565
+ bg.paste(hero,(0,0))
566
+ except Exception:
567
+ pass
568
+ draw=ImageDraw.Draw(bg)
569
+ try:
570
+ font_brand=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',34)
571
+ font_small=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',28)
572
+ font_seg=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',58)
573
+ font_title=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',34)
574
+ except Exception:
575
+ font_brand=font_small=font_seg=font_title=None
576
+ draw.rectangle((0,680,W,H), fill=(12,12,12))
577
+ dot_x=48; dot_y=742
578
+ for i in range(total):
579
+ fill=(92,184,122) if i==idx else (70,70,70)
580
+ draw.rounded_rectangle((dot_x+i*38,dot_y,dot_x+i*38+24,dot_y+10), radius=5, fill=fill)
581
+ draw.text((48,780),'VNEWS AI SHORT',fill=(110,231,143),font=font_brand)
582
+ draw.rounded_rectangle((48,834,260,880), radius=20, fill=(28,70,45))
583
+ draw.text((66,842),f'Đoạn {idx+1}/{total}',fill=(235,235,235),font=font_small)
584
+ y=940; maxw=W-96
585
+ for ln in _wrap_text_px(draw, segment, font_seg, maxw, 8):
586
+ draw.text((48,y),ln,fill=(255,255,255),font=font_seg)
587
+ y+=74
588
+ if y>1500: break
589
+ y2=1640
590
+ draw.line((48,y2-22,W-48,y2-22),fill=(70,70,70),width=2)
591
+ for ln in _wrap_text_px(draw, post.get('title',''), font_title, maxw, 3):
592
+ draw.text((48,y2),ln,fill=(220,220,220),font=font_title)
593
+ y2+=46
594
+ bg.save(out_path, quality=92)
595
+
596
+
597
+ def _estimate_audio_duration(path, fallback=4.0):
598
+ try:
599
+ pr=subprocess.run(['ffprobe','-v','error','-show_entries','format=duration','-of','default=noprint_wrappers=1:nokey=1',path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=20)
600
+ return max(1.5, float((pr.stdout or b'').decode().strip() or fallback))
601
+ except Exception:
602
+ return fallback
603
+
604
+
605
+ @app.post('/api/ai/short/{post_id}')
606
+ async def patched_ai_short(post_id: str, request: Request):
607
+ try:
608
+ body = await request.json()
609
+ except Exception:
610
+ body = {}
611
+ voice = str(body.get('voice', 'nu')).strip().lower()
612
+ emotion = str(body.get('emotion', 'neutral')).strip().lower()
613
+ speed = float(body.get('speed', 1.2) or 1.2)
614
+ speed = max(0.85, min(1.35, speed))
615
+
616
+ posts = base._load_ai_wall()
617
+ post = next((p for p in posts if str(p.get('id')) == str(post_id)), None)
618
+ if not post:
619
+ return JSONResponse({'error': 'post not found'}, status_code=404)
620
+
621
+ segments = _summary_segments_from_post(post, max_segments=7)
622
+ seg_hash = hashlib.md5(('|'.join(segments)+voice+emotion+str(speed)).encode('utf-8')).hexdigest()[:8]
623
+ os.makedirs(base.SHORTS_DIR, exist_ok=True)
624
+ suffix = f"_{voice}_{emotion}_{str(speed).replace('.', 'p')}_{seg_hash}_scenes_nosub"
625
+ out_mp4 = os.path.join(base.SHORTS_DIR, base._safe_name(post_id + suffix) + '.mp4')
626
+ if os.path.exists(out_mp4):
627
+ post['video'] = '/api/ai/short-file/' + post_id + suffix
628
+ post['short_voice'] = voice
629
+ post['short_emotion'] = emotion
630
+ post['short_speed'] = speed
631
+ post['short_segments'] = segments
632
+ post['short_subtitles'] = False
633
+ base._save_ai_wall(posts)
634
+ return JSONResponse({'video': post['video'], 'voice': voice, 'emotion': emotion, 'speed': speed, 'subtitles': False, 'segments': segments})
635
+ if base.gTTS is None:
636
+ return JSONResponse({'error': 'gTTS chưa sẵn sàng'}, status_code=503)
637
+
638
+ work = os.path.join(base.SHORTS_DIR, base._safe_name(post_id + suffix))
639
+ os.makedirs(work, exist_ok=True)
640
+ img = os.path.join(work, 'image.jpg')
641
+ try:
642
+ base._download_image(post.get('img'), post.get('title', 'AI news'), img)
643
+ edge_voice = {
644
+ 'nam': 'vi-VN-NamMinhNeural',
645
+ 'male': 'vi-VN-NamMinhNeural',
646
+ 'nu': 'vi-VN-HoaiMyNeural',
647
+ 'female': 'vi-VN-HoaiMyNeural',
648
+ 'mien-nam': 'vi-VN-HoaiMyNeural',
649
+ }.get(voice, 'vi-VN-HoaiMyNeural')
650
+ part_files=[]
651
+ for idx, seg in enumerate(segments):
652
+ frame=os.path.join(work,f'frame_{idx:02d}.jpg')
653
+ aud=os.path.join(work,f'voice_{idx:02d}.mp3')
654
+ aud_fast=os.path.join(work,f'voice_{idx:02d}_fast.mp3')
655
+ part=os.path.join(work,f'part_{idx:02d}.mp4')
656
+ _make_scene_frame(post, seg, idx, len(segments), img, frame, emotion=emotion)
657
+ spoken=_emotion_script(seg, emotion)
658
+ try:
659
+ subprocess.run(['python','-m','edge_tts','--voice',edge_voice,'--text',spoken,'--write-media',aud], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=120)
660
+ except Exception:
661
+ tld='com.vn' if voice in ('nu','female','mien-nam') else 'com'
662
+ try:
663
+ base.gTTS(spoken, lang='vi', tld=tld, slow=False).save(aud)
664
+ except TypeError:
665
+ base.gTTS(spoken, lang='vi', slow=False).save(aud)
666
+ subprocess.run(['ffmpeg','-y','-i',aud,'-filter:a',f'atempo={speed}','-vn',aud_fast], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=90)
667
+ dur=_estimate_audio_duration(aud_fast, fallback=4.0)+0.35
668
+ subprocess.run(['ffmpeg','-y','-loop','1','-t',str(dur),'-i',frame,'-i',aud_fast,'-shortest','-c:v','libx264','-tune','stillimage','-pix_fmt','yuv420p','-c:a','aac','-b:a','128k',part], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=150)
669
+ part_files.append(part)
670
+ concat=os.path.join(work,'concat.txt')
671
+ with open(concat,'w',encoding='utf-8') as f:
672
+ for p in part_files:
673
+ f.write("file '" + p.replace("'", "'\\''") + "'\n")
674
+ subprocess.run(['ffmpeg','-y','-f','concat','-safe','0','-i',concat,'-c','copy',out_mp4], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=180)
675
+ post['video'] = '/api/ai/short-file/' + post_id + suffix
676
+ post['short_voice'] = voice
677
+ post['short_emotion'] = emotion
678
+ post['short_speed'] = speed
679
+ post['short_segments'] = segments
680
+ post['short_subtitles'] = False
681
+ base._save_ai_wall(posts)
682
+ return JSONResponse({'video': post['video'], 'voice': voice, 'emotion': emotion, 'speed': speed, 'subtitles': False, 'segments': segments})
683
+ except Exception as e:
684
+ return JSONResponse({'error': 'Không tạo được shorts: ' + str(e)[:220]}, status_code=500)
685
+
686
+
687
+ @app.get('/api/ai/short-file/{file_id}')
688
+ def patched_ai_short_file(file_id: str):
689
+ path = os.path.join(base.SHORTS_DIR, base._safe_name(file_id) + '.mp4')
690
+ if not os.path.exists(path):
691
+ return JSONResponse({'error': 'not found'}, status_code=404)
692
+ return FileResponse(path, media_type='video/mp4', filename=f'vnews-ai-{file_id}.mp4')
693
+
694
+
695
+ @app.get('/api/ai_shorts')
696
+ def api_ai_shorts():
697
+ posts = [p for p in base._load_ai_wall() if p.get('video')]
698
+ return JSONResponse({'posts': posts[:80]})
699
+
700
+
701
+ app.router.routes = [r for r in app.router.routes if not (getattr(r, 'path', None) == '/' and 'GET' in getattr(r, 'methods', set()))]
702
+
703
+ PATCH_INJECT = r'''
704
+ <style>
705
+ .ai-wall-patched{margin:6px 4px;background:#1a1a1a;border:1px solid #2a2a2a;border-radius:8px;overflow:hidden}
706
+ .ai-wall-card{flex:0 0 250px;background:#141414;border:1px solid #2b2b2b;border-radius:10px;padding:8px}
707
+ .ai-wall-img{width:100%;aspect-ratio:16/9;background:#222;border-radius:8px;overflow:hidden;margin-bottom:6px}
708
+ .ai-wall-img img{width:100%;height:100%;object-fit:cover}
709
+ .ai-wall-title{font-size:12px;color:#5cb87a;font-weight:800;line-height:1.3;margin-bottom:4px}
710
+ .ai-wall-text{font-size:11px;color:#bbb;line-height:1.45;white-space:pre-wrap;display:-webkit-box;-webkit-line-clamp:5;-webkit-box-orient:vertical;overflow:hidden}
711
+ .ai-wall-actions{display:flex;gap:6px;margin-top:8px}
712
+ .ai-wall-actions button,.ai-wall-actions select{flex:1;border:1px solid #333;background:#222;color:#ddd;border-radius:14px;padding:6px 8px;font-size:10px;min-width:0}
713
+ .ai-wall-actions button.primary{background:#2d8659;border-color:#2d8659;color:#fff}
714
+ .ai-short-card{flex:0 0 145px}
715
+ .ai-short-video{width:100%;aspect-ratio:9/16;background:#000;border-radius:8px;overflow:hidden}
716
+ .ai-short-video video{width:100%;height:100%;object-fit:cover}
717
+ .ai-short-progress{position:fixed;inset:0;background:rgba(0,0,0,.78);z-index:99999;display:none;align-items:center;justify-content:center;padding:20px}
718
+ .ai-short-progress.active{display:flex}
719
+ .ai-short-box{max-width:420px;width:100%;background:#141414;border:2px solid #2d8659;border-radius:14px;padding:18px;color:#eee;box-shadow:0 0 30px rgba(45,134,89,.35)}
720
+ .ai-short-box h3{color:#5cb87a;margin-bottom:10px}
721
+ .ai-short-step{font-size:13px;line-height:1.55;color:#ccc}
722
+ .ai-short-spinner{width:34px;height:34px;border:4px solid #333;border-top-color:#5cb87a;border-radius:50%;animation:spin 1s linear infinite;margin:10px auto}
723
+ @keyframes spin{to{transform:rotate(360deg)}}
724
+ </style>
725
+ <div id="ai-short-progress" class="ai-short-progress"><div class="ai-short-box"><h3>🎬 Đang tạo Short AI</h3><div class="ai-short-spinner"></div><div class="ai-short-step" id="ai-short-step">Đang chuẩn bị...</div></div></div>
726
+ <script>
727
+ (function(){
728
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
729
+ let patchedWall=[];let aiShorts=[];
730
+ function showProgress(msg){let box=document.getElementById('ai-short-progress');let st=document.getElementById('ai-short-step');if(st)st.innerHTML=msg;if(box)box.classList.add('active');}
731
+ function hideProgress(){document.getElementById('ai-short-progress')?.classList.remove('active');}
732
+ function updateAiLabels(){document.querySelectorAll('.ai-compose-title').forEach(e=>e.textContent='🤖 Tường AI: lọc từng bài theo chủ đề, tóm tắt nội dung bài');document.querySelectorAll('button').forEach(b=>{if((b.textContent||'').includes('AI viết lại'))b.textContent='🤖 Tóm tắt AI & đăng tường';});}
733
+ async function loadPatchedWall(){try{const r=await fetch('/api/ai_wall');const j=await r.json();patchedWall=j.posts||[];renderPatchedWall();updateAiLabels();}catch(e){}try{const r2=await fetch('/api/ai_shorts');const j2=await r2.json();aiShorts=j2.posts||[];renderAiShorts();}catch(e){}}
734
+ function renderAiShorts(){const home=document.getElementById('view-home');if(!home)return;document.getElementById('ai-shorts-patched')?.remove();if(!aiShorts.length)return;let wrap=document.createElement('div');wrap.id='ai-shorts-patched';wrap.className='ai-wall-patched';let h='<div class="slider-header"><span class="slider-label">🎬 Short AI</span><span class="slider-note">Video đã tạo</span></div><div class="slider-track">';aiShorts.slice(0,30).forEach((p,i)=>{h+=`<div class="ai-short-card" onclick="aiReadShortPatched(${i})"><div class="ai-short-video"><video src="${p.video}" muted playsinline preload="metadata"></video></div><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div>';wrap.innerHTML=h;let wall=document.getElementById('ai-wall-patched');if(wall)wall.after(wrap);else home.prepend(wrap);}
735
+ function renderPatchedWall(){const home=document.getElementById('view-home');if(!home)return;document.getElementById('ai-wall-patched')?.remove();if(!patchedWall.length)return;let wrap=document.createElement('div');wrap.id='ai-wall-patched';wrap.className='ai-wall-patched';let h='<div class="slider-header"><span class="slider-label">🧱 Tường AI</span><span class="slider-note">Mỗi nguồn = một bài tóm tắt</span></div><div class="slider-track">';patchedWall.slice(0,30).forEach((p,i)=>{h+=`<div class="ai-wall-card"><div class="ai-wall-img">${p.img?`<img src="${p.img}">`:''}</div><div class="ai-wall-title">${esc(p.title)}</div><div class="ai-wall-text">${esc(p.text)}</div><div class="ai-wall-actions"><button onclick="aiReadWallPatched(${i})">Xem</button><button class="primary" onclick="aiMakeShortPatched(${i})">Shorts</button></div></div>`});h+='</div>';wrap.innerHTML=h;let after=document.querySelector('.ai-compose');if(after)after.after(wrap);else home.prepend(wrap);}
736
+ window.aiReadShortPatched=function(i){const p=aiShorts[i];if(!p)return;showView('view-article');let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">Short AI</span><h1 class="article-title">${esc(p.title)}</h1><video class="article-img" src="${p.video}" controls playsinline autoplay></video><p class="article-p" style="white-space:pre-wrap">${esc(p.text||'')}</p><div class="article-actions"><button onclick="window.open('${p.video}','_blank')">⬇ Mở video</button>${p.url?`<button onclick="window.open('${p.url}','_blank')">🔗 Nguồn</button>`:''}</div></div>`;document.getElementById('view-article').innerHTML=h;window.scrollTo(0,0)};
737
+ window.aiReadWallPatched=function(i){const p=patchedWall[i];if(!p)return;showView('view-article');let sources='';if(p.sources&&p.sources.length){sources='<div class="article-summary"><b>Nguồn tham khảo:</b><br>'+p.sources.slice(0,5).map(s=>`• ${esc(s.title||s.url||'Nguồn')} ${s.url?`(${esc(new URL(s.url).hostname.replace('www.',''))})`:''}`).join('<br>')+'</div>'}let voiceBox=`<div class="article-actions"><select id="ai-short-voice"><option value="nu">Giọng nữ Việt</option><option value="nam">Giọng nam Việt</option><option value="mien-nam">Giọng miền Nam</option></select><select id="ai-short-emotion"><option value="neutral">Trung tính</option><option value="urgent">Tin nhanh</option><option value="warm">Ấm áp</option><option value="serious">Nghiêm túc</option><option value="energetic">Sôi nổi</option></select><button onclick="aiMakeShortPatched(${i})">🎬 Tạo video shorts</button></div>`;let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${p.img?`<img class="article-img" src="${p.img}">`:''}${sources}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>${p.video?`<video class="article-img" src="${p.video}" controls playsinline></video>`:''}<div class="article-actions">${p.url?`<button onclick="window.open('${p.url}','_blank')">🔗 Nguồn</button>`:''}</div>${voiceBox}</div>`;document.getElementById('view-article').innerHTML=h;window.scrollTo(0,0)};
738
+ window.aiMakeShortPatched=async function(i){const p=patchedWall[i];if(!p)return;let voice=document.getElementById('ai-short-voice')?.value||'nu';let emotion=document.getElementById('ai-short-emotion')?.value||'neutral';let voiceName={nu:'Giọng nữ Việt',nam:'Giọng nam Việt','mien-nam':'Giọng miền Nam'}[voice]||voice;let emotionName={neutral:'Trung tính',urgent:'Tin nhanh',warm:'Ấm áp',serious:'Nghiêm túc',energetic:'Sôi nổi'}[emotion]||emotion;let ok=confirm(`Quy trình tạo short AI:\n\n1) Dùng ảnh đại diện của bài hoặc tạo ảnh minh họa nếu thiếu.\n2) Rút gọn nội dung tóm tắt thành kịch bản đọc ngắn.\n3) Tự ngắt câu theo dấu câu và xuống dòng hợp lý.\n4) Tạo giọng đọc tiếng Việt: ${voiceName}.\n5) Áp dụng cảm xúc/kịch bản: ${emotionName}.\n6) Tăng tốc giọng đọc 1.2 lần.\n7) Mỗi đoạn tóm tắt sẽ là một cảnh riêng theo thời lượng đọc.\n8) Không thêm phụ đề; video chỉ có chữ cảnh và giọng đọc.\n9) Sau khi xong, video xuất hiện ở slide "Short AI".\n\nQuá trình có thể mất 1-3 phút. Bạn muốn bắt đầu?`);if(!ok)return;try{showProgress(`Bước 1/5: Chuẩn bị ảnh và căn chữ full width...<br>Bước 2/5: Tạo kịch bản, tự ngắt câu/xuống dòng...<br>Bước 3/5: Tạo giọng đọc ${voiceName}, cảm xúc ${emotionName}.<br>Bước 4/5: Tăng tốc 1.2x và ghép từng cảnh riêng, không phụ đề.<br>Bước 5/5: Lưu vào slide "Short AI".`);const r=await fetch('/api/ai/short/'+p.id,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice,emotion,speed:1.2})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');p.video=j.video;hideProgress();alert('Hoàn tất: video shorts đã được tạo và thêm vào slide "Short AI".');aiReadWallPatched(i);loadPatchedWall();}catch(e){hideProgress();alert('Không tạo được shorts: '+e.message)}};
739
+ window.createTopicPost=function(){let inp=document.getElementById('ai-topic-input');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&(j.posts||j.post)){let arr=j.posts||[j.post];patchedWall=arr.concat(patchedWall.filter(x=>!arr.find(y=>y.id===x.id)));renderPatchedWall();if(inp)inp.value='';alert(`Đã lọc và tóm tắt ${arr.length} bài viết theo chủ đề lên Tường AI`);}else alert(j.error||'Lỗi tạo bài')}).catch(e=>alert(e.message||'Lỗi tạo bài'));};
740
+ window.createUrlPost=function(){let inp=document.getElementById('ai-url-input');let url=(inp&&inp.value||'').trim();if(!url)return alert('Dán URL trước');if(!/^https?:\/\//i.test(url))return alert('URL cần bắt đầu bằng http:// hoặc https://');fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&j.post){patchedWall=[j.post].concat(patchedWall.filter(x=>x.id!==j.post.id));renderPatchedWall();if(inp)inp.value='';alert('Đã tóm tắt URL và đăng lên Tường AI');}else alert(j.error||'Lỗi URL')}).catch(e=>alert(e.message||'Lỗi URL'));};
741
+ window.rewriteCurrentArticle=function(){if(!window._currentArticle&&typeof _currentArticle!=='undefined')window._currentArticle=_currentArticle;let cur=window._currentArticle||_currentArticle;if(!cur)return;let btn=document.querySelector('.article-actions button.primary');if(btn){btn.textContent='Đang tóm tắt...';btn.disabled=true}fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:cur.url})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&j.post){document.getElementById('rewrite-result').innerHTML=`<div class="rewrite-box"><div class="rewrite-title">Đã tóm tắt và đăng Tường AI</div><div class="rewrite-text">${esc(j.post.text||'')}</div></div>`;patchedWall=[j.post].concat(patchedWall.filter(x=>x.id!==j.post.id));renderPatchedWall();alert('Đã tóm tắt lên Tường AI');}else alert(j.error||'Không tóm tắt được')}).catch(e=>alert(e.message||'Lỗi tóm tắt')).finally(()=>{if(btn){btn.textContent='🤖 Tóm tắt AI & đăng tường';btn.disabled=false}})};
742
+ setTimeout(loadPatchedWall,1500);setInterval(updateAiLabels,2000);
743
+ })();
744
+ </script>
745
+ '''
746
+
747
+ @app.get('/')
748
+ async def index_patched():
749
+ with open('/app/static/index.html','r',encoding='utf-8') as f:
750
+ html=f.read()
751
+ return HTMLResponse(html.replace('</body>', PATCH_INJECT+'\n</body>'))
ai_runtime.py ADDED
@@ -0,0 +1,357 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, re, subprocess, json, time, hashlib
2
+ import ai_patch as old
3
+ from ai_patch import app
4
+ import ai_ext as base
5
+ from fastapi import Request
6
+ from fastapi.responses import JSONResponse, HTMLResponse, FileResponse
7
+ try:
8
+ from PIL import Image, ImageDraw, ImageFont
9
+ except Exception:
10
+ Image = ImageDraw = ImageFont = None
11
+
12
+
13
+ def clean(s):
14
+ import html as html_lib
15
+ return re.sub(r"\s+", " ", html_lib.unescape(s or "")).strip()
16
+
17
+
18
+ def _domain(url):
19
+ try:
20
+ from urllib.parse import urlparse
21
+ return urlparse(url or '').netloc.replace('www.','')
22
+ except Exception:
23
+ return ''
24
+
25
+
26
+ def _strip_bullet_prefix(s):
27
+ # remove bullets, numbered prefixes, leading dots commonly produced by AI summaries
28
+ return clean(re.sub(r'^[\s•\-\*·▪▫●○\d\.\)\(]+', '', s or ''))
29
+
30
+
31
+ def source_line(sources):
32
+ names=[]
33
+ for s in (sources or [])[:5]:
34
+ via=s.get('via') or _domain(s.get('url','')) or s.get('title','')
35
+ if via and via not in names:names.append(via)
36
+ return 'Nguồn tham khảo: '+', '.join(names[:5]) if names else 'Nguồn tham khảo: tổng hợp internet'
37
+
38
+
39
+ def _source_badge(post):
40
+ sources=post.get('sources') or []
41
+ for s in sources:
42
+ via=s.get('via') or _domain(s.get('url',''))
43
+ if via:return via
44
+ return _domain(post.get('url','')) or post.get('source') or 'VNEWS'
45
+
46
+
47
+ def _collect_all_images(data):
48
+ imgs=[]
49
+ def add(u):
50
+ u=(u or '').strip()
51
+ if not u or u.startswith('data:') or 'base64' in u:return
52
+ if u.startswith('//'):u='https:'+u
53
+ if u not in imgs:imgs.append(u)
54
+ add(data.get('image') or data.get('og_image') or data.get('img'))
55
+ for u in data.get('images') or []:add(u)
56
+ for b in data.get('body') or []:
57
+ if isinstance(b,dict) and b.get('type')=='img':add(b.get('src'))
58
+ return imgs[:20]
59
+
60
+
61
+ def _scrape_url_with_images(url):
62
+ data=base.scrape_any_url(url)
63
+ # extra pass: collect every useful image from original HTML, because some readers only return one image
64
+ try:
65
+ import requests
66
+ from bs4 import BeautifulSoup
67
+ r=requests.get(url,headers=base.HEADERS,timeout=18);r.encoding='utf-8'
68
+ soup=BeautifulSoup(r.text,'lxml')
69
+ extra=[]
70
+ for im in soup.find_all('img'):
71
+ src=im.get('data-src') or im.get('data-original') or im.get('data-lazy-src') or im.get('src') or ''
72
+ if src.startswith('//'):src='https:'+src
73
+ if src and 'base64' not in src and src not in extra:
74
+ # skip tiny icons/logos as much as possible
75
+ low=src.lower()
76
+ if any(x in low for x in ['logo','icon','avatar','sprite']):
77
+ continue
78
+ extra.append(src)
79
+ if len(extra)>=20:break
80
+ data['images']=_collect_all_images(data)+[u for u in extra if u not in _collect_all_images(data)]
81
+ except Exception:
82
+ data['images']=_collect_all_images(data)
83
+ data['images']=_collect_all_images(data)
84
+ if data['images'] and not data.get('image'):
85
+ data['image']=data['images'][0]
86
+ return data
87
+
88
+
89
+ def rich_context(topic, limit=5):
90
+ try: ctx,sources=base.web_context(topic, limit=limit)
91
+ except Exception: ctx,sources='',[]
92
+ rich=[];rs=[];seen=set()
93
+ for s in (sources or [])[:limit*2]:
94
+ url=s.get('url') or ''
95
+ if not url.startswith('http') or url in seen:continue
96
+ seen.add(url)
97
+ try:
98
+ data=base.scrape_any_url(url)
99
+ raw=(data.get('summary','')+'\n'+data.get('text','')).strip()
100
+ if len(raw)<180:continue
101
+ title=data.get('title') or s.get('title') or url
102
+ via=data.get('via') or s.get('via') or _domain(url)
103
+ rich.append(f"### {title} ({via})\n{raw[:2600]}")
104
+ rs.append({'title':title,'url':url,'excerpt':raw[:700],'via':via})
105
+ if len(rich)>=limit:break
106
+ except Exception:continue
107
+ if rich:return '\n\n'.join(rich),rs
108
+ return ctx or f'Chủ đề: {topic}', sources or []
109
+
110
+
111
+ def postprocess(text):
112
+ if hasattr(old,'_postprocess_ai_text'):
113
+ out=old._postprocess_ai_text(text, max_units=7)
114
+ else:
115
+ out=clean(text)
116
+ # keep wall text readable, but ensure short generation later won't show bullets
117
+ return out
118
+
119
+
120
+ # Remove old routes we must override.
121
+ _PATCH={('/api/topic_post','POST'),('/api/url_wall','POST'),('/api/rewrite_share','POST'),('/api/ai/url','POST'),('/api/ai/short/{post_id}','POST'),('/api/ai/short-file/{file_id}','GET'),('/','GET')}
122
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
123
+
124
+
125
+ @app.post('/api/url_wall')
126
+ async def url_wall_only(request:Request):
127
+ body=await request.json();url=base._clean_text(body.get('url',''))
128
+ if not url.startswith('http'):return JSONResponse({'error':'missing url'},status_code=400)
129
+ try:data=_scrape_url_with_images(url)
130
+ except Exception as e:return JSONResponse({'error':'Không scrape được URL: '+str(e)[:180]},status_code=422)
131
+ raw=(data.get('summary','')+'\n'+data.get('text','')).strip()
132
+ if len(raw)<120:return JSONResponse({'error':'URL không có đủ nội dung để tóm tắt'},status_code=422)
133
+ prompt=f"""Tóm tắt bài viết nguồn dưới đây để đăng lên Tường AI VNEWS.
134
+
135
+ Yêu cầu bắt buộc:
136
+ - Chỉ tóm tắt nội dung chính, không viết lại toàn bộ bài.
137
+ - Ngắn gọn, cụ thể, dễ hiểu.
138
+ - Không lặp lại ý và không thêm chi tiết ngoài nguồn.
139
+ - Tối đa 5 ý chính hoặc 2 đoạn ngắn.
140
+ - Tránh dùng dấu đầu dòng nếu không thật cần thiết.
141
+
142
+ Tiêu đề gốc: {data.get('title','')}
143
+ Nguồn: {data.get('via','') or _domain(url)}
144
+ Nội dung gốc:
145
+ {raw[:16000]}"""
146
+ text=await base.qwen_generate(prompt,image_url=(data.get('image') or None),max_tokens=900)
147
+ if not text:text=old._fallback_summary_from_prompt(prompt,max_units=5) if hasattr(old,'_fallback_summary_from_prompt') else raw[:900]
148
+ text=postprocess(text)
149
+ src=[{'title':data.get('title'), 'url':url, 'excerpt':raw[:500], 'via':data.get('via') or _domain(url)}]
150
+ if 'Nguồn tham khảo:' not in text:text+='\n\n'+source_line(src)
151
+ images=_collect_all_images(data)
152
+ post=base.make_post(data.get('title') or 'Bài viết',text,images[0] if images else (data.get('image') or ''),url,'url',sources=src)
153
+ post['images']=images
154
+ posts=base._load_ai_wall();posts.insert(0,post);base._save_ai_wall(posts)
155
+ return JSONResponse({'post':post})
156
+
157
+
158
+ @app.post('/api/rewrite_share')
159
+ async def rewrite_share_url_only(request:Request):
160
+ return await url_wall_only(request)
161
+
162
+
163
+ @app.post('/api/ai/url')
164
+ async def ai_url_compat(request:Request):
165
+ return await url_wall_only(request)
166
+
167
+
168
+ @app.post('/api/topic_post')
169
+ async def topic_disabled(request:Request):
170
+ return JSONResponse({'error':'Đã tắt tạo bài theo chủ đề. Vui lòng dán URL bài viết để AI tóm tắt.'},status_code=410)
171
+
172
+
173
+ def split_segments(post,max_segments=8):
174
+ text=clean(post.get('text') or post.get('title') or '')
175
+ text=re.sub(r'Nguồn tham khảo:.*$','',text,flags=re.I|re.S).strip()
176
+ lines=[]
177
+ for ln in text.splitlines():
178
+ ln=_strip_bullet_prefix(ln)
179
+ if len(ln)>=18:lines.append(ln)
180
+ if len(lines)<2:
181
+ lines=[_strip_bullet_prefix(s) for s in re.split(r'(?<=[\.\!\?])\s+',text) if len(_strip_bullet_prefix(s))>=25]
182
+ segs=[];cur=''
183
+ for ln in lines:
184
+ ln=_strip_bullet_prefix(ln)
185
+ if not ln:continue
186
+ if len(cur)+len(ln)<180:cur=(cur+' '+ln).strip()
187
+ else:
188
+ if cur:segs.append(_strip_bullet_prefix(cur))
189
+ cur=ln
190
+ if cur:segs.append(_strip_bullet_prefix(cur))
191
+ return segs[:max_segments] or [_strip_bullet_prefix(post.get('title','VNEWS'))]
192
+
193
+
194
+ def wrap_text(draw,text,font,maxw,max_lines):
195
+ words=clean(text).split();lines=[];cur=''
196
+ for w in words:
197
+ test=(cur+' '+w).strip()
198
+ try:width=draw.textbbox((0,0),test,font=font)[2]
199
+ except Exception:width=len(test)*20
200
+ if width<=maxw:cur=test
201
+ else:
202
+ if cur:lines.append(cur)
203
+ cur=w
204
+ if len(lines)>=max_lines:break
205
+ if cur and len(lines)<max_lines:lines.append(cur)
206
+ return lines
207
+
208
+
209
+ def _draw_center(draw, lines, font, y, fill, W, line_h):
210
+ for ln in lines:
211
+ try:
212
+ box=draw.textbbox((0,0),ln,font=font);tw=box[2]-box[0]
213
+ except Exception:
214
+ tw=len(ln)*24
215
+ x=max(30,(W-tw)//2)
216
+ draw.text((x,y),ln,fill=fill,font=font)
217
+ y+=line_h
218
+ return y
219
+
220
+
221
+ def make_frame(post,seg,idx,total,img_path,out_path):
222
+ if Image is None:raise RuntimeError('Pillow not ready')
223
+ W,H=1080,1920;bg=Image.new('RGB',(W,H),(12,12,12))
224
+ hero_h=760
225
+ try:
226
+ im=Image.open(img_path).convert('RGB');ratio=im.width/max(1,im.height)
227
+ target=(W,hero_h);tr=target[0]/target[1]
228
+ if ratio>tr:nh=target[1];nw=int(nh*ratio)
229
+ else:nw=target[0];nh=int(nw/ratio)
230
+ im=im.resize((nw,nh));left=(nw-target[0])//2;top=(nh-target[1])//2
231
+ bg.paste(im.crop((left,top,left+target[0],top+target[1])),(0,0))
232
+ except Exception:pass
233
+ draw=ImageDraw.Draw(bg)
234
+ try:
235
+ fb=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',58)
236
+ ft=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',38)
237
+ fs=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',30)
238
+ fsmall=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',28)
239
+ except Exception:fb=ft=fs=fsmall=None
240
+ # source badge on top image corner
241
+ badge='Nguồn: '+_source_badge(post)
242
+ try:
243
+ b=draw.textbbox((0,0),badge,font=fsmall);bw=b[2]-b[0];bh=b[3]-b[1]
244
+ except Exception:
245
+ bw=len(badge)*16;bh=34
246
+ bx=W-bw-42;by=24
247
+ draw.rounded_rectangle((bx-16,by-8,W-24,by+bh+14),radius=18,fill=(0,0,0,170))
248
+ draw.text((bx,by),badge,fill=(255,255,255),font=fsmall)
249
+ # bottom text area
250
+ draw.rectangle((0,hero_h-20,W,H),fill=(12,12,12))
251
+ # progress bars centered
252
+ total_w=total*38-14;start=(W-total_w)//2
253
+ for i in range(total):
254
+ fill=(92,184,122) if i==idx else (70,70,70)
255
+ draw.rounded_rectangle((start+i*38,820,start+i*38+24,832),radius=6,fill=fill)
256
+ brand='VNEWS AI SHORT'
257
+ try:
258
+ bb=draw.textbbox((0,0),brand,font=ft);tx=(W-(bb[2]-bb[0]))//2
259
+ except Exception:tx=360
260
+ draw.text((tx,870),brand,fill=(110,231,143),font=ft)
261
+ clean_seg=_strip_bullet_prefix(seg)
262
+ lines=wrap_text(draw,clean_seg,fb,W-120,8)
263
+ block_h=len(lines)*74
264
+ y=max(980, 1250-block_h//2)
265
+ _draw_center(draw,lines,fb,y,(255,255,255),W,74)
266
+ # small title centered near bottom
267
+ title_lines=wrap_text(draw,_strip_bullet_prefix(post.get('title','')),fs,W-120,3)
268
+ y2=1640
269
+ draw.line((80,y2-26,W-80,y2-26),fill=(70,70,70),width=2)
270
+ _draw_center(draw,title_lines,fs,y2,(220,220,220),W,42)
271
+ bg.save(out_path,quality=92)
272
+
273
+
274
+ def make_tts(text,voice,out_path):
275
+ v={'nam':'vi-VN-NamMinhNeural','male':'vi-VN-NamMinhNeural','nu':'vi-VN-HoaiMyNeural','female':'vi-VN-HoaiMyNeural','mien-nam':'vi-VN-HoaiMyNeural'}.get(voice,'vi-VN-HoaiMyNeural')
276
+ text=_strip_bullet_prefix(text)
277
+ try:subprocess.run(['python','-m','edge_tts','--voice',v,'--text',text,'--write-media',out_path],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=160)
278
+ except Exception:
279
+ tld='com.vn' if voice in ('nu','female','mien-nam') else 'com'
280
+ try:base.gTTS(text,lang='vi',tld=tld,slow=False).save(out_path)
281
+ except TypeError:base.gTTS(text,lang='vi',slow=False).save(out_path)
282
+
283
+
284
+ @app.post('/api/ai/short/{post_id}')
285
+ async def short_segments(post_id:str,request:Request):
286
+ try:body=await request.json()
287
+ except Exception:body={}
288
+ voice=str(body.get('voice','nu')).lower().strip();emotion=str(body.get('emotion','neutral')).lower().strip();speed=max(0.85,min(1.35,float(body.get('speed',1.2) or 1.2)))
289
+ posts=base._load_ai_wall();post=next((p for p in posts if str(p.get('id'))==str(post_id)),None)
290
+ if not post:return JSONResponse({'error':'post not found'},status_code=404)
291
+ segs=split_segments(post,8)
292
+ os.makedirs(base.SHORTS_DIR,exist_ok=True);suffix=f'_{voice}_{emotion}_{str(speed).replace(".","p")}_centered_source_nobullet'
293
+ out=os.path.join(base.SHORTS_DIR,base._safe_name(post_id+suffix)+'.mp4')
294
+ if os.path.exists(out):post['video']='/api/ai/short-file/'+post_id+suffix;base._save_ai_wall(posts);return JSONResponse({'video':post['video'],'segments':len(segs),'subtitles':False})
295
+ work=os.path.join(base.SHORTS_DIR,base._safe_name(post_id+suffix));os.makedirs(work,exist_ok=True)
296
+ img=os.path.join(work,'image.jpg');base._download_image(post.get('img'),post.get('title','AI news'),img)
297
+ clips=[]
298
+ try:
299
+ for i,seg in enumerate(segs):
300
+ frame=os.path.join(work,f'f{i}.jpg');aud=os.path.join(work,f'a{i}.mp3');aud2=os.path.join(work,f'a{i}_fast.mp3');clip=os.path.join(work,f'c{i}.mp4')
301
+ seg=_strip_bullet_prefix(seg)
302
+ make_frame(post,seg,i,len(segs),img,frame)
303
+ prefix={'urgent':'Tin nhanh.','warm':'Câu chuyện đáng chú ý.','serious':'Bản tin nghiêm túc.','energetic':'Cập nhật nổi bật.'}.get(emotion,'')
304
+ spoken=(prefix+' '+seg).strip() if i==0 and prefix else seg
305
+ make_tts(spoken,voice,aud)
306
+ subprocess.run(['ffmpeg','-y','-i',aud,'-filter:a',f'atempo={speed}','-vn',aud2],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=120)
307
+ subprocess.run(['ffmpeg','-y','-loop','1','-i',frame,'-i',aud2,'-shortest','-c:v','libx264','-tune','stillimage','-pix_fmt','yuv420p','-c:a','aac','-b:a','128k','-vf','scale=1080:1920',clip],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=180)
308
+ clips.append(clip)
309
+ lf=os.path.join(work,'list.txt')
310
+ with open(lf,'w',encoding='utf-8') as f:
311
+ for c in clips:f.write("file '{}".format(c.replace("'","'\\''"))+"'\n")
312
+ subprocess.run(['ffmpeg','-y','-f','concat','-safe','0','-i',lf,'-c','copy',out],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=240)
313
+ post['video']='/api/ai/short-file/'+post_id+suffix;post['short_subtitles']=False;post['short_segments']=segs;post['short_speed']=speed;base._save_ai_wall(posts)
314
+ return JSONResponse({'video':post['video'],'segments':len(segs),'subtitles':False})
315
+ except Exception as e:return JSONResponse({'error':'Không tạo được shorts: '+str(e)[:200]},status_code=500)
316
+
317
+
318
+ @app.get('/api/ai/short-file/{file_id}')
319
+ def short_file(file_id:str):
320
+ path=os.path.join(base.SHORTS_DIR,base._safe_name(file_id)+'.mp4')
321
+ if not os.path.exists(path):return JSONResponse({'error':'not found'},status_code=404)
322
+ return FileResponse(path,media_type='video/mp4',filename=f'vnews-ai-{file_id}.mp4')
323
+
324
+
325
+ # Rebuild / with old UI injection plus final UI overrides.
326
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
327
+ @app.get('/')
328
+ async def index_runtime():
329
+ with open('/app/static/index.html','r',encoding='utf-8') as f:html=f.read()
330
+ inject=getattr(old,'PATCH_INJECT','')+r'''
331
+ <style>
332
+ /* Hide old topic UI, keep URL input only */
333
+ #ai-topic-input{display:none!important}
334
+ #ai-topic-input,*[onclick*="createTopicPost"]{display:none!important}
335
+ .ai-topic-row,.topic-row,.ai-compose-topic{display:none!important}
336
+ .ai-wall-gallery{display:grid;grid-template-columns:repeat(2,1fr);gap:6px;margin:10px 0}.ai-wall-gallery img{width:100%;aspect-ratio:16/9;object-fit:cover;border-radius:8px;background:#222}.ai-wall-gallery img:first-child{grid-column:1/-1}.ai-url-only-note{font-size:11px;color:#888;margin:5px 0 8px}
337
+ </style>
338
+ <script>
339
+ (function(){
340
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
341
+ function hideTopicControls(){
342
+ document.querySelectorAll('#ai-topic-input').forEach(e=>{let p=e.closest('.ai-compose,.ai-compose-topic,.topic-row,div'); if(p&&p.querySelector('#ai-url-input')) e.style.display='none'; else if(p) p.style.display='none';});
343
+ document.querySelectorAll('button').forEach(b=>{let t=(b.textContent||'').toLowerCase();let oc=b.getAttribute('onclick')||'';if(oc.includes('createTopicPost')||t.includes('chủ đề'))b.style.display='none';});
344
+ let url=document.getElementById('ai-url-input'); if(url&&!document.getElementById('ai-url-only-note')){let n=document.createElement('div');n.id='ai-url-only-note';n.className='ai-url-only-note';n.textContent='Dán URL bài viết để AI tóm tắt và lấy ảnh từ bài.';url.insertAdjacentElement('afterend',n);}
345
+ }
346
+ window.createTopicPost=function(){alert('Đã tắt ô nhập chủ đề. Vui lòng dán URL bài viết.');};
347
+ window.createUrlPost=function(){let inp=document.getElementById('ai-url-input');let url=(inp&&inp.value||'').trim();if(!url)return alert('Dán URL trước');if(!/^https?:\/\//i.test(url))return alert('URL cần bắt đầu bằng http:// hoặc https://');fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})}).then(r=>r.json().then(j=>({ok:r.ok,j}))).then(({ok,j})=>{if(ok&&j.post){if(typeof prependWallPost==='function')prependWallPost(j.post);if(window.patchedWall)window.patchedWall=[j.post].concat(window.patchedWall||[]);if(inp)inp.value='';alert('Đã tóm tắt URL, lấy ảnh trong bài và đăng lên Tường AI');location.reload();}else alert(j.error||'Lỗi URL')}).catch(e=>alert(e.message||'Lỗi URL'));};
348
+ function galleryHtml(p){let imgs=(p.images||[]).filter(Boolean);if(!imgs.length&&p.img)imgs=[p.img];if(!imgs.length)return '';return '<div class="ai-wall-gallery">'+imgs.slice(0,12).map(u=>`<img src="${esc(u)}" loading="lazy">`).join('')+'</div>';}
349
+ function patchReaders(){
350
+ let oldRead=window.aiReadWallPatched||window.aiReadWall;
351
+ window.aiReadWallPatched=window.aiReadWall=function(i){let arr=window.patchedWall||window.aiWall||[];let p=arr[i];if(!p&&oldRead)return oldRead(i);if(!p)return;showView('view-article');let sources='';if(p.sources&&p.sources.length){sources='<div class="article-summary"><b>Nguồn tham khảo:</b><br>'+p.sources.slice(0,5).map(s=>`• ${esc(s.title||s.url||'Nguồn')} ${s.url?`(${esc(new URL(s.url).hostname.replace('www.',''))})`:''}`).join('<br>')+'</div>'}let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${galleryHtml(p)}${sources}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>${p.video?`<video class="article-img" src="${p.video}" controls playsinline></video>`:''}<div class="article-actions">${p.url?`<button onclick="window.open('${p.url}','_blank')">🔗 Nguồn</button>`:''}<button onclick="aiMakeShortPatched?aiMakeShortPatched(${i}):aiMakeShort(${i})">🎬 Tạo video shorts</button></div></div>`;document.getElementById('view-article').innerHTML=h;window.scrollTo(0,0);};
352
+ }
353
+ setInterval(hideTopicControls,1000);setTimeout(hideTopicControls,300);setTimeout(patchReaders,1600);
354
+ })();
355
+ </script>
356
+ '''
357
+ return HTMLResponse(html.replace('</body>',inject+'\n</body>') if '</body>' in html else html+inject)
ai_runtime_final.py ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final runtime overrides for VNEWS AI UI, article-only images, shareable AI wall, and robust Vietnamese shorts."""
2
+ import os, re, requests, subprocess, time
3
+ from urllib.parse import urlparse, quote
4
+ import ai_runtime as rt
5
+ from ai_runtime import app
6
+ import ai_ext as base
7
+ from fastapi import Request, Query
8
+ from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
9
+ try:
10
+ from PIL import Image, ImageDraw, ImageFont
11
+ except Exception:
12
+ Image = ImageDraw = ImageFont = None
13
+
14
+ RESTORE_INDEX_URL = "https://huggingface.co/spaces/bep40/vnews/raw/restore-33c3dda/static/index.html"
15
+ SPACE_URL = "https://bep40-vnews.hf.space"
16
+ DEFAULT_IMG = "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
17
+
18
+ # Only voices that support Vietnamese reliably. Extra labels map to these Vietnamese neural voices.
19
+ VN_VOICES = {
20
+ "nu": "vi-VN-HoaiMyNeural", "female": "vi-VN-HoaiMyNeural", "hoaimy": "vi-VN-HoaiMyNeural",
21
+ "nu-tre": "vi-VN-HoaiMyNeural", "nu-truyen-cam": "vi-VN-HoaiMyNeural", "nu-tin-nhanh": "vi-VN-HoaiMyNeural",
22
+ "nam": "vi-VN-NamMinhNeural", "male": "vi-VN-NamMinhNeural", "namminh": "vi-VN-NamMinhNeural",
23
+ "nam-tram": "vi-VN-NamMinhNeural", "nam-ban-tin": "vi-VN-NamMinhNeural", "nam-nang-dong": "vi-VN-NamMinhNeural",
24
+ }
25
+
26
+
27
+ def clean(s):
28
+ import html as html_lib
29
+ return re.sub(r"\s+", " ", html_lib.unescape(s or "")).strip()
30
+
31
+
32
+ def _domain(url):
33
+ try:return urlparse(url or '').netloc.replace('www.','')
34
+ except Exception:return ''
35
+
36
+
37
+ def _strip_bullet_prefix(s):
38
+ return clean(re.sub(r'^[\s•\-\*·▪▫●○\d\.\)\(]+', '', s or ''))
39
+
40
+
41
+ def _source_badge_url_first(post):
42
+ d=_domain(post.get('url',''))
43
+ if d:return d
44
+ for s in post.get('sources') or []:
45
+ d=_domain(s.get('url',''))
46
+ if d:return d
47
+ return 'VNEWS'
48
+
49
+
50
+ def _abs_url(src, base_url):
51
+ if not src:return ''
52
+ src=src.strip()
53
+ if src.startswith('//'):return 'https:'+src
54
+ if src.startswith('/'):
55
+ try:
56
+ p=urlparse(base_url);return f'{p.scheme}://{p.netloc}{src}'
57
+ except Exception:return src
58
+ return src
59
+
60
+
61
+ def _article_content_block(soup):
62
+ for tag in soup.find_all(['script','style','nav','footer','aside','form','noscript','iframe']):tag.decompose()
63
+ # Aggressively remove related/ad/recommend containers before image collection.
64
+ bad_re=re.compile(r'(related|relate|recommend|suggest|sidebar|ads|advert|popular|more|xem-them|xemthem|tin-lien-quan|tinlienquan|doc-them|docthem|other-news|news-other|article-related|box-tin|box_related|story-related|recommend-news|same-category|cate-list|news-list|most-view|banner|qc|quang-cao|sponsor)',re.I)
65
+ for el in list(soup.find_all(True)):
66
+ cls=' '.join(el.get('class',[])); eid=el.get('id',''); role=el.get('role','')
67
+ if bad_re.search(cls) or bad_re.search(eid) or bad_re.search(role):
68
+ el.decompose()
69
+ selectors=['article','main article','.article-content','.article__body','.article-body','.article-detail','.detail-content','.content-detail','.singular-content','.news-content','.post-content','.entry-content','.knc-content','.fck_detail','.cms-body','.story-body','[class*=article-content]','[class*=detail-content]','[class*=singular-content]']
70
+ for sel in selectors:
71
+ el=soup.select_one(sel)
72
+ if el and (len(el.find_all('p'))>=2 or len(el.find_all(['figure','picture','img']))>=1):return el
73
+ best=None;score=0
74
+ for el in soup.find_all(['article','main','section','div']):
75
+ ps=el.find_all('p');imgs=el.find_all('img');txt=' '.join(p.get_text(' ',strip=True) for p in ps)
76
+ sc=len(ps)*120+len(imgs)*10+min(len(txt),4500)
77
+ cls=' '.join(el.get('class',[])).lower()
78
+ if any(k in cls for k in ['article','content','detail','post','entry','story']):sc+=800
79
+ if sc>score:best=el;score=sc
80
+ return best or soup
81
+
82
+
83
+ def _image_is_likely_article(im, src):
84
+ low=(src or '').lower()
85
+ if not src or src.startswith('data:') or 'base64' in low:return False
86
+ if any(x in low for x in ['logo','icon','avatar','sprite','banner','ads','advert','tracking','pixel','social','share','author','thumb-related']):return False
87
+ alt=(im.get('alt') or im.get('title') or '').lower()
88
+ if any(x in alt for x in ['logo','avatar','quảng cáo','advertisement','banner']):return False
89
+ try:
90
+ w=int(re.sub(r'\D','',str(im.get('width') or '0')) or 0);h=int(re.sub(r'\D','',str(im.get('height') or '0')) or 0)
91
+ if (w and w<220) or (h and h<140):return False
92
+ except Exception:pass
93
+ return True
94
+
95
+
96
+ def _article_only_images(url):
97
+ """Collect images only inside main article content. If uncertain, return fewer/no images rather than related/ad images."""
98
+ imgs=[]
99
+ try:
100
+ from bs4 import BeautifulSoup
101
+ r=requests.get(url,headers=getattr(base,'HEADERS',{}),timeout=18);r.encoding='utf-8'
102
+ soup=BeautifulSoup(r.text,'lxml')
103
+ block=_article_content_block(soup)
104
+ candidates=[]
105
+ # Prefer figure/picture under article body; then direct img in body.
106
+ for el in block.find_all(['figure','picture'],recursive=True):
107
+ im=el.find('img')
108
+ if im:candidates.append(im)
109
+ for im in block.find_all('img',recursive=True):
110
+ if im not in candidates:candidates.append(im)
111
+ seen=set()
112
+ for im in candidates:
113
+ src=(im.get('data-src') or im.get('data-original') or im.get('data-lazy-src') or im.get('data-srcset') or im.get('srcset') or im.get('src') or '')
114
+ if ',' in src:src=src.split(',')[0].strip().split(' ')[0]
115
+ else:src=src.strip().split(' ')[0]
116
+ src=_abs_url(src,url)
117
+ if src in seen or not _image_is_likely_article(im,src):continue
118
+ # parent text guard: skip images from any remaining related block
119
+ parent_txt=' '.join((im.parent.get('class',[]) if im.parent else []))+' '+(im.parent.get('id','') if im.parent else '')
120
+ if re.search(r'(related|recommend|tin-lien-quan|doc-them|xem-them|popular|ads|banner)',parent_txt,re.I):continue
121
+ seen.add(src);imgs.append(src)
122
+ if len(imgs)>=20:break
123
+ # Use og:image ONLY as article main image fallback when no body image found.
124
+ if not imgs:
125
+ og=soup.find('meta',property='og:image') or soup.find('meta',attrs={'name':'twitter:image'})
126
+ if og:
127
+ src=_abs_url(og.get('content',''),url)
128
+ if src and 'logo' not in src.lower() and 'banner' not in src.lower():imgs.append(src)
129
+ except Exception:pass
130
+ return imgs[:20]
131
+
132
+
133
+ def _scrape_url_article_only(url):
134
+ data=base.scrape_any_url(url)
135
+ imgs=_article_only_images(url)
136
+ data['images']=imgs
137
+ if imgs:data['image']=imgs[0]
138
+ else:data['image']=''
139
+ return data
140
+
141
+
142
+ def _blank_image(path, title='VNEWS'):
143
+ if Image is None:return None
144
+ im=Image.new('RGB',(1080,760),(24,48,36));draw=ImageDraw.Draw(im)
145
+ try:f=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',48)
146
+ except Exception:f=None
147
+ draw.text((60,330),clean(title)[:40] or 'VNEWS',fill=(255,255,255),font=f)
148
+ im.save(path,quality=90);return path
149
+
150
+
151
+ def _download_image_safe(url, fallback_title, out_path):
152
+ if url:
153
+ try:
154
+ r=requests.get(url,headers=getattr(base,'HEADERS',{}),timeout=18)
155
+ if r.status_code==200 and len(r.content)>1200:
156
+ with open(out_path,'wb') as f:f.write(r.content)
157
+ # verify PIL opens it
158
+ if Image:
159
+ Image.open(out_path).verify()
160
+ return out_path
161
+ except Exception:pass
162
+ try:
163
+ return base._download_image('',fallback_title,out_path)
164
+ except Exception:
165
+ return _blank_image(out_path,fallback_title)
166
+
167
+
168
+ def final_make_tts(text,voice,out_path):
169
+ text=_strip_bullet_prefix(text) or 'Bản tin VNEWS.'
170
+ # Only Vietnamese voices. Unknown choices fall back to Vietnamese female.
171
+ edge_voice=VN_VOICES.get(str(voice or '').lower().strip(), 'vi-VN-HoaiMyNeural')
172
+ for ev in [edge_voice, 'vi-VN-HoaiMyNeural', 'vi-VN-NamMinhNeural']:
173
+ try:
174
+ subprocess.run(['python','-m','edge_tts','--voice',ev,'--text',text,'--write-media',out_path],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=180)
175
+ if os.path.exists(out_path) and os.path.getsize(out_path)>1000:return out_path
176
+ except Exception:pass
177
+ try:
178
+ base.gTTS(text,lang='vi',tld='com.vn',slow=False).save(out_path)
179
+ if os.path.exists(out_path) and os.path.getsize(out_path)>1000:return out_path
180
+ except Exception:pass
181
+ # Last-resort silent audio guarantees short generation succeeds.
182
+ subprocess.run(['ffmpeg','-y','-f','lavfi','-i','anullsrc=channel_layout=stereo:sample_rate=44100','-t','3','-q:a','9','-acodec','libmp3lame',out_path],stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=30)
183
+ return out_path
184
+
185
+
186
+ def _draw_center(draw, lines, font, y, fill, W, line_h):
187
+ for ln in lines:
188
+ try:box=draw.textbbox((0,0),ln,font=font);tw=box[2]-box[0]
189
+ except Exception:tw=len(ln)*24
190
+ draw.text((max(30,(W-tw)//2),y),ln,fill=fill,font=font);y+=line_h
191
+ return y
192
+
193
+
194
+ def final_make_frame(post,seg,idx,total,img_path,out_path):
195
+ if Image is None:return rt.make_frame(post,seg,idx,total,img_path,out_path)
196
+ W,H=1080,1920;hero_h=760;bg=Image.new('RGB',(W,H),(12,12,12))
197
+ try:
198
+ im=Image.open(img_path).convert('RGB');ratio=im.width/max(1,im.height);tr=W/hero_h
199
+ if ratio>tr:nh=hero_h;nw=int(nh*ratio)
200
+ else:nw=W;nh=int(nw/ratio)
201
+ im=im.resize((nw,nh));left=(nw-W)//2;top=(nh-hero_h)//2;bg.paste(im.crop((left,top,left+W,top+hero_h)),(0,0))
202
+ except Exception:pass
203
+ draw=ImageDraw.Draw(bg)
204
+ try:
205
+ fb=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',58);ft=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',38);fs=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',30);fsmall=ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf',28)
206
+ except Exception:fb=ft=fs=fsmall=None
207
+ badge='Nguồn: '+_source_badge_url_first(post)
208
+ try:b=draw.textbbox((0,0),badge,font=fsmall);bw=b[2]-b[0];bh=b[3]-b[1]
209
+ except Exception:bw=len(badge)*16;bh=34
210
+ bx=W-bw-42;by=24;draw.rounded_rectangle((bx-16,by-8,W-24,by+bh+14),radius=18,fill=(0,0,0));draw.text((bx,by),badge,fill=(255,255,255),font=fsmall)
211
+ draw.rectangle((0,hero_h-20,W,H),fill=(12,12,12))
212
+ total=max(1,total);total_w=total*38-14;start=(W-total_w)//2
213
+ for i in range(total):draw.rounded_rectangle((start+i*38,820,start+i*38+24,832),radius=6,fill=(92,184,122) if i==idx else (70,70,70))
214
+ brand='VNEWS AI SHORT'
215
+ try:bb=draw.textbbox((0,0),brand,font=ft);tx=(W-(bb[2]-bb[0]))//2
216
+ except Exception:tx=360
217
+ draw.text((tx,870),brand,fill=(110,231,143),font=ft)
218
+ seg=_strip_bullet_prefix(seg);lines=rt.wrap_text(draw,seg,fb,W-120,8);y=max(980,1250-(len(lines)*74)//2);_draw_center(draw,lines,fb,y,(255,255,255),W,74)
219
+ title_lines=rt.wrap_text(draw,_strip_bullet_prefix(post.get('title','')),fs,W-120,3);y2=1640;draw.line((80,y2-26,W-80,y2-26),fill=(70,70,70),width=2);_draw_center(draw,title_lines,fs,y2,(220,220,220),W,42)
220
+ bg.save(out_path,quality=92)
221
+
222
+ # Monkey patches for old functions.
223
+ rt.make_frame=final_make_frame;rt.make_tts=final_make_tts;rt._source_badge=_source_badge_url_first
224
+
225
+ # Override endpoints.
226
+ _PATCH={('/api/url_wall','POST'),('/api/rewrite_share','POST'),('/api/ai/url','POST'),('/api/ai/short/{post_id}','POST'),('/','GET'),('/aw','GET')}
227
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
228
+
229
+ @app.post('/api/url_wall')
230
+ async def final_url_wall(request:Request):
231
+ body=await request.json();url=base._clean_text(body.get('url',''))
232
+ if not url.startswith('http'):return JSONResponse({'error':'missing url'},status_code=400)
233
+ try:data=_scrape_url_article_only(url)
234
+ except Exception as e:return JSONResponse({'error':'Không scrape được URL: '+str(e)[:180]},status_code=422)
235
+ raw=(data.get('summary','')+'\n'+data.get('text','')).strip()
236
+ if len(raw)<120:return JSONResponse({'error':'URL không có đủ nội dung để tóm tắt'},status_code=422)
237
+ prompt=f"""Tóm tắt bài viết nguồn dưới đây để đăng lên Tường AI VNEWS.
238
+
239
+ Yêu cầu:
240
+ - Chỉ tóm tắt nội dung chính, không viết lại toàn bộ bài.
241
+ - Ngắn gọn, cụ thể, dễ hiểu.
242
+ - Không lặp ý, không thêm chi tiết ngoài nguồn.
243
+ - Tối đa 5 ý chính hoặc 2 đoạn ngắn.
244
+ - Hạn chế dùng dấu đầu dòng.
245
+
246
+ Tiêu đề gốc: {data.get('title','')}
247
+ Nguồn: {_domain(url)}
248
+ Nội dung gốc:
249
+ {raw[:16000]}"""
250
+ text=await base.qwen_generate(prompt,image_url=(data.get('image') or None),max_tokens=900)
251
+ if not text:text=rt.old._fallback_summary_from_prompt(prompt,max_units=5) if hasattr(rt.old,'_fallback_summary_from_prompt') else raw[:900]
252
+ text=rt.postprocess(text) if hasattr(rt,'postprocess') else text
253
+ src=[{'title':data.get('title'), 'url':url, 'excerpt':raw[:500], 'via':_domain(url)}]
254
+ if 'Nguồn tham khảo:' not in text:text+='\n\n'+rt.source_line(src)
255
+ imgs=data.get('images') or []
256
+ post=base.make_post(data.get('title') or 'Bài viết',text,imgs[0] if imgs else '',url,'url',sources=src)
257
+ post['images']=imgs
258
+ posts=base._load_ai_wall();posts.insert(0,post);base._save_ai_wall(posts)
259
+ return JSONResponse({'post':post})
260
+
261
+ @app.post('/api/rewrite_share')
262
+ async def final_rewrite_share(request:Request):return await final_url_wall(request)
263
+ @app.post('/api/ai/url')
264
+ async def final_ai_url(request:Request):return await final_url_wall(request)
265
+
266
+ @app.post('/api/ai/short/{post_id}')
267
+ async def final_short(post_id:str,request:Request):
268
+ try:body=await request.json()
269
+ except Exception:body={}
270
+ voice=str(body.get('voice','nu')).lower().strip();emotion=str(body.get('emotion','neutral')).lower().strip();speed=max(0.85,min(1.35,float(body.get('speed',1.2) or 1.2)))
271
+ posts=base._load_ai_wall();post=next((p for p in posts if str(p.get('id'))==str(post_id)),None)
272
+ if not post:return JSONResponse({'error':'post not found'},status_code=404)
273
+ segs=rt.split_segments(post,8) if hasattr(rt,'split_segments') else [_strip_bullet_prefix(post.get('text') or post.get('title') or 'VNEWS')]
274
+ imgs=[u for u in (post.get('images') or []) if u] or ([post.get('img')] if post.get('img') else [])
275
+ os.makedirs(base.SHORTS_DIR,exist_ok=True);suffix=f'_{voice}_{emotion}_{str(speed).replace(".","p")}_articleimgs_vivoice'
276
+ out=os.path.join(base.SHORTS_DIR,base._safe_name(post_id+suffix)+'.mp4')
277
+ if os.path.exists(out):
278
+ post['video']='/api/ai/short-file/'+post_id+suffix;base._save_ai_wall(posts);return JSONResponse({'video':post['video'],'segments':len(segs),'subtitles':False})
279
+ work=os.path.join(base.SHORTS_DIR,base._safe_name(post_id+suffix));os.makedirs(work,exist_ok=True)
280
+ clips=[]
281
+ try:
282
+ for i,seg in enumerate(segs):
283
+ img_url=imgs[i % len(imgs)] if imgs else ''
284
+ img=os.path.join(work,f'image_{i}.jpg');frame=os.path.join(work,f'f{i}.jpg');aud=os.path.join(work,f'a{i}.mp3');aud2=os.path.join(work,f'a{i}_fast.mp3');clip=os.path.join(work,f'c{i}.mp4')
285
+ _download_image_safe(img_url,post.get('title','AI news'),img)
286
+ seg=_strip_bullet_prefix(seg);final_make_frame(post,seg,i,len(segs),img,frame)
287
+ prefix={'urgent':'Tin nhanh.','warm':'Câu chuyện đáng chú ý.','serious':'Bản tin nghiêm túc.','energetic':'Cập nhật nổi bật.'}.get(emotion,'')
288
+ spoken=(prefix+' '+seg).strip() if i==0 and prefix else seg
289
+ final_make_tts(spoken,voice,aud)
290
+ try:subprocess.run(['ffmpeg','-y','-i',aud,'-filter:a',f'atempo={speed}','-vn',aud2],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=120)
291
+ except Exception:aud2=aud
292
+ try:
293
+ subprocess.run(['ffmpeg','-y','-loop','1','-i',frame,'-i',aud2,'-shortest','-c:v','libx264','-tune','stillimage','-pix_fmt','yuv420p','-c:a','aac','-b:a','128k','-vf','scale=1080:1920',clip],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=180)
294
+ except Exception:
295
+ # last-resort visual-only 4s clip
296
+ subprocess.run(['ffmpeg','-y','-loop','1','-t','4','-i',frame,'-f','lavfi','-i','anullsrc=channel_layout=stereo:sample_rate=44100','-shortest','-c:v','libx264','-pix_fmt','yuv420p','-c:a','aac','-vf','scale=1080:1920',clip],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=120)
297
+ clips.append(clip)
298
+ lf=os.path.join(work,'list.txt')
299
+ with open(lf,'w',encoding='utf-8') as f:
300
+ for c in clips:f.write("file '"+c.replace("","'\\''"))+"'\n")
301
+ subprocess.run(['ffmpeg','-y','-f','concat','-safe','0','-i',lf,'-c','copy',out],check=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=240)
302
+ post['video']='/api/ai/short-file/'+post_id+suffix;post['short_subtitles']=False;post['short_segments']=segs;post['short_speed']=speed;base._save_ai_wall(posts)
303
+ return JSONResponse({'video':post['video'],'segments':len(segs),'subtitles':False})
304
+ except Exception as e:return JSONResponse({'error':'Không tạo được shorts: '+str(e)[:220]},status_code=500)
305
+
306
+ @app.get('/aw')
307
+ def ai_wall_share(post:str=Query(default=''), short:int=Query(default=0)):
308
+ posts=base._load_ai_wall();p=next((x for x in posts if str(x.get('id'))==str(post)),None)
309
+ if not p:return HTMLResponse(f'<script>location.href="{SPACE_URL}"</script>')
310
+ title=p.get('title') or 'VNEWS AI';img=p.get('img') or DEFAULT_IMG
311
+ desc=(p.get('text') or '')[:220]
312
+ return HTMLResponse(f'<!doctype html><html><head><meta charset="utf-8"><title>{title}</title><meta property="og:title" content="{title}"><meta property="og:description" content="{desc}"><meta property="og:image" content="{img}"><meta property="og:type" content="article"><meta name="twitter:card" content="summary_large_image"></head><body><script>localStorage.setItem('pending_ai_post','{post}');location.href='{SPACE_URL}'</script></body></html>')
313
+
314
+ FINAL_INJECT = r'''
315
+ <style>
ai_runtime_final2.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final2: improve article-image detection without over-filtering real article images."""
2
+ import re, requests
3
+ from urllib.parse import urlparse
4
+ import ai_runtime_final as f1
5
+ from ai_runtime_final import app, base, rt, HTMLResponse, JSONResponse, Request, Query
6
+
7
+
8
+ def _domain(url):
9
+ try:return urlparse(url or '').netloc.replace('www.','')
10
+ except Exception:return ''
11
+
12
+
13
+ def _abs_url(src, base_url):
14
+ if not src:return ''
15
+ src=src.strip()
16
+ if src.startswith('//'):return 'https:'+src
17
+ if src.startswith('/'):
18
+ try:
19
+ p=urlparse(base_url);return f'{p.scheme}://{p.netloc}{src}'
20
+ except Exception:return src
21
+ return src
22
+
23
+ BAD_RE=re.compile(r'(related|relate|recommend|suggest|sidebar|ads|advert|popular|xem-them|xemthem|tin-lien-quan|tinlienquan|doc-them|docthem|other-news|news-other|article-related|box-tin|box_related|story-related|recommend-news|same-category|cate-list|most-view|banner|qc|quang-cao|sponsor|social|share|comment|author|newsletter)',re.I)
24
+ GOOD_RE=re.compile(r'(article|content|detail|body|post|entry|story|fck|cms|singular|main|news)',re.I)
25
+ IMG_EXT_RE=re.compile(r'\.(jpg|jpeg|png|webp|avif)(\?|$)',re.I)
26
+ ARTICLE_LINK_RE=re.compile(r'\.(html|htm|shtml|tpo|chn)(\?|$)|/\d{4}/|post\d+|article',re.I)
27
+
28
+
29
+ def _clean_soup(soup):
30
+ for tag in soup.find_all(['script','style','nav','footer','aside','form','noscript','iframe']):
31
+ tag.decompose()
32
+
33
+
34
+ def _find_article_block(soup):
35
+ """Find the article body first; do not delete suspected related blocks before finding it."""
36
+ selectors=[
37
+ 'article', 'main article',
38
+ '.article-content','.article__content','.article__body','.article-body','.article-detail','.article__detail',
39
+ '.detail-content','.content-detail','.singular-content','.news-content','.post-content','.entry-content',
40
+ '.knc-content','.fck_detail','.cms-body','.story-body','.maincontent','.main-content',
41
+ '[class*=article-content]','[class*=article__content]','[class*=detail-content]','[class*=singular-content]',
42
+ '[class*=cms-body]','[class*=story-body]'
43
+ ]
44
+ for sel in selectors:
45
+ el=soup.select_one(sel)
46
+ if el and (len(el.find_all('p'))>=2 or len(el.find_all(['figure','picture','img']))>=1):
47
+ return el
48
+ best=None;best_score=0
49
+ for el in soup.find_all(['article','main','section','div']):
50
+ cls=' '.join(el.get('class',[]));eid=el.get('id','')
51
+ if BAD_RE.search(cls+' '+eid) and not GOOD_RE.search(cls+' '+eid):
52
+ continue
53
+ ps=el.find_all('p');imgs=el.find_all('img')
54
+ text=' '.join(p.get_text(' ',strip=True) for p in ps)
55
+ long_ps=sum(1 for p in ps if len(p.get_text(' ',strip=True))>40)
56
+ score=long_ps*180+len(ps)*40+min(len(text),5000)+len(imgs)*25
57
+ if GOOD_RE.search(cls+' '+eid):score+=800
58
+ if score>best_score:
59
+ best=el;best_score=score
60
+ return best or soup
61
+
62
+
63
+ def _ancestor_bad(im, block):
64
+ node=im
65
+ while node and node is not block:
66
+ if getattr(node,'name',None) in ['aside','nav','footer']:
67
+ return True
68
+ cls=' '.join(node.get('class',[])) if hasattr(node,'get') else ''
69
+ eid=node.get('id','') if hasattr(node,'get') else ''
70
+ if BAD_RE.search(cls+' '+eid):
71
+ return True
72
+ node=getattr(node,'parent',None)
73
+ return False
74
+
75
+
76
+ def _image_anchor_penalty(im, page_url):
77
+ a=im.find_parent('a')
78
+ if not a:return 0
79
+ href=_abs_url(a.get('href',''),page_url)
80
+ if not href:return 0
81
+ # If anchor opens the image itself, do not penalize.
82
+ if IMG_EXT_RE.search(href):return 0
83
+ # If anchor points to another article, it is probably related content.
84
+ try:
85
+ p1=urlparse(page_url);p2=urlparse(href)
86
+ if href!=page_url and ARTICLE_LINK_RE.search(href) and (p2.path!=p1.path):
87
+ return -100
88
+ except Exception:pass
89
+ return -10
90
+
91
+
92
+ def _near_article_text_score(im):
93
+ score=0
94
+ # caption/figcaption is strong sign of article image
95
+ fig=im.find_parent('figure')
96
+ if fig:
97
+ score+=5
98
+ cap=fig.find('figcaption')
99
+ if cap and len(cap.get_text(' ',strip=True))>10:score+=4
100
+ if im.find_parent('picture'):score+=2
101
+ # paragraph around image
102
+ parent=im.parent
103
+ for node in [parent, getattr(parent,'parent',None) if parent else None, fig]:
104
+ if not node:continue
105
+ ps=node.find_all('p') if hasattr(node,'find_all') else []
106
+ if any(len(p.get_text(' ',strip=True))>40 for p in ps):score+=3;break
107
+ # sibling paragraph near figure/image
108
+ holder=fig or parent
109
+ if holder:
110
+ for sib in [holder.find_previous_sibling(), holder.find_next_sibling()]:
111
+ if sib and len(sib.get_text(' ',strip=True))>40:
112
+ score+=2
113
+ break
114
+ return score
115
+
116
+
117
+ def _image_score(im, src, block, page_url):
118
+ low=(src or '').lower()
119
+ if not src or src.startswith('data:') or 'base64' in low:return -999
120
+ if any(x in low for x in ['logo','icon','avatar','sprite','tracking','pixel','social','share','author']):return -999
121
+ if _ancestor_bad(im,block):return -999
122
+ score=0
123
+ # Explicit dimensions: only reject truly tiny images; if missing dimensions, allow.
124
+ try:
125
+ w=int(re.sub(r'\D','',str(im.get('width') or '0')) or 0);h=int(re.sub(r'\D','',str(im.get('height') or '0')) or 0)
126
+ if (w and w<120) or (h and h<90):return -999
127
+ if w>=500 or h>=300:score+=3
128
+ except Exception:pass
129
+ alt=(im.get('alt') or im.get('title') or '').lower()
130
+ if any(x in alt for x in ['logo','avatar','quảng cáo','advertisement','banner']):return -999
131
+ cls=' '.join(im.get('class',[]));eid=im.get('id','')
132
+ if BAD_RE.search(cls+' '+eid):return -999
133
+ if GOOD_RE.search(cls+' '+eid):score+=2
134
+ score+=_near_article_text_score(im)
135
+ score+=_image_anchor_penalty(im,page_url)
136
+ if any(x in low for x in ['cdn','photo','image','media','upload','thumb','avatar']):score+=1
137
+ # Tienphong and many VN papers use lazy/data src without figure; still accept if inside article block.
138
+ if im.find_parent(['article','main']) or GOOD_RE.search(' '.join(block.get('class',[]))+' '+block.get('id','')):score+=3
139
+ return score
140
+
141
+
142
+ def _extract_img_src(im, page_url):
143
+ src=(im.get('data-src') or im.get('data-original') or im.get('data-lazy-src') or im.get('data-srcset') or im.get('srcset') or im.get('src') or '')
144
+ if ',' in src:src=src.split(',')[0].strip().split(' ')[0]
145
+ else:src=src.strip().split(' ')[0]
146
+ return _abs_url(src,page_url)
147
+
148
+
149
+ def _article_only_images(url):
150
+ imgs=[]
151
+ try:
152
+ from bs4 import BeautifulSoup
153
+ r=requests.get(url,headers=getattr(base,'HEADERS',{}),timeout=18);r.encoding='utf-8'
154
+ soup=BeautifulSoup(r.text,'lxml')
155
+ _clean_soup(soup)
156
+ block=_find_article_block(soup)
157
+ candidates=[]
158
+ for el in block.find_all(['figure','picture'],recursive=True):
159
+ im=el.find('img')
160
+ if im and im not in candidates:candidates.append(im)
161
+ for im in block.find_all('img',recursive=True):
162
+ if im not in candidates:candidates.append(im)
163
+ scored=[];seen=set()
164
+ for im in candidates:
165
+ src=_extract_img_src(im,url)
166
+ if not src or src in seen:continue
167
+ seen.add(src)
168
+ sc=_image_score(im,src,block,url)
169
+ if sc>=2:
170
+ scored.append((sc,src))
171
+ # Keep original article order but only for scored images, filtering duplicate URLs.
172
+ good=set(src for sc,src in sorted(scored,reverse=True) if sc>=2)
173
+ for im in candidates:
174
+ src=_extract_img_src(im,url)
175
+ if src in good and src not in imgs:imgs.append(src)
176
+ if len(imgs)>=20:break
177
+ # Fallback: og:image is usually article main image, and better than no image.
178
+ if not imgs:
179
+ og=soup.find('meta',property='og:image') or soup.find('meta',attrs={'name':'twitter:image'})
180
+ if og:
181
+ src=_abs_url(og.get('content',''),url)
182
+ if src and not any(x in src.lower() for x in ['logo','icon','avatar','sprite']):imgs.append(src)
183
+ except Exception:pass
184
+ return imgs[:20]
185
+
186
+
187
+ def _scrape_url_article_only(url):
188
+ data=base.scrape_any_url(url)
189
+ imgs=_article_only_images(url)
190
+ data['images']=imgs
191
+ data['image']=imgs[0] if imgs else ''
192
+ return data
193
+
194
+ # Override the functions used by inherited endpoints.
195
+ f1._article_only_images=_article_only_images
196
+ f1._scrape_url_article_only=_scrape_url_article_only
197
+
198
+ # Replace URL endpoints to use improved extraction.
199
+ _PATCH={('/api/url_wall','POST'),('/api/rewrite_share','POST'),('/api/ai/url','POST'),('/','GET')}
200
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
201
+
202
+ @app.post('/api/url_wall')
203
+ async def final2_url_wall(request:Request):
204
+ body=await request.json();url=base._clean_text(body.get('url',''))
205
+ if not url.startswith('http'):return JSONResponse({'error':'missing url'},status_code=400)
206
+ try:data=_scrape_url_article_only(url)
207
+ except Exception as e:return JSONResponse({'error':'Không scrape được URL: '+str(e)[:180]},status_code=422)
208
+ raw=(data.get('summary','')+'\n'+data.get('text','')).strip()
209
+ if len(raw)<120:return JSONResponse({'error':'URL không có đủ nội dung để tóm tắt'},status_code=422)
210
+ prompt=f"""Tóm tắt bài viết nguồn dưới đây để đăng lên Tường AI VNEWS.
211
+
212
+ Yêu cầu:
213
+ - Chỉ tóm tắt nội dung chính, không viết lại toàn bộ bài.
214
+ - Ngắn gọn, cụ thể, dễ hiểu.
215
+ - Không lặp ý, không thêm chi tiết ngoài nguồn.
216
+ - Tối đa 5 ý chính hoặc 2 đoạn ngắn.
217
+ - Hạn chế dùng dấu đầu dòng.
218
+
219
+ Tiêu đề gốc: {data.get('title','')}
220
+ Nguồn: {_domain(url)}
221
+ Nội dung gốc:
222
+ {raw[:16000]}"""
223
+ text=await base.qwen_generate(prompt,image_url=(data.get('image') or None),max_tokens=900)
224
+ if not text:text=rt.old._fallback_summary_from_prompt(prompt,max_units=5) if hasattr(rt.old,'_fallback_summary_from_prompt') else raw[:900]
225
+ text=rt.postprocess(text) if hasattr(rt,'postprocess') else text
226
+ src=[{'title':data.get('title'), 'url':url, 'excerpt':raw[:500], 'via':_domain(url)}]
227
+ if 'Nguồn tham khảo:' not in text:text+='\n\n'+rt.source_line(src)
228
+ imgs=data.get('images') or []
229
+ post=base.make_post(data.get('title') or 'Bài viết',text,imgs[0] if imgs else '',url,'url',sources=src)
230
+ post['images']=imgs
231
+ posts=base._load_ai_wall();posts.insert(0,post);base._save_ai_wall(posts)
232
+ return JSONResponse({'post':post})
233
+
234
+ @app.post('/api/rewrite_share')
235
+ async def final2_rewrite_share(request:Request):return await final2_url_wall(request)
236
+ @app.post('/api/ai/url')
237
+ async def final2_ai_url(request:Request):return await final2_url_wall(request)
238
+
239
+ @app.get('/')
240
+ async def index_final2():
241
+ html=f1._load_index_html();body=getattr(rt.old,'PATCH_INJECT','') + f1.FINAL_INJECT
242
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
ai_runtime_final3.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final3 runtime: Qwen topic posts, robust YouTube shorts, TikTok-style actions for Shorts and Short AI."""
2
+ import os, re, time, json, hashlib, requests
3
+ from urllib.parse import quote, urlparse
4
+ import ai_runtime_final2 as f2
5
+ from ai_runtime_final2 import app, base, rt, HTMLResponse, JSONResponse, Request, Query
6
+
7
+ SPACE_URL="https://bep40-vnews.hf.space"
8
+ SHORT_CHANNELS=["baodantri7941","baosuckhoedoisongboyte"]
9
+ _SHORTS_CACHE={"t":0,"d":[]}
10
+ AI_INTERACTIONS_FILE="/data/ai_interactions.json" if os.path.isdir('/data') else "/app/data/ai_interactions.json"
11
+
12
+
13
+ def clean(s):
14
+ import html as html_lib
15
+ return re.sub(r"\s+"," ",html_lib.unescape(s or "")).strip()
16
+
17
+
18
+ def _domain(u):
19
+ try:return urlparse(u or '').netloc.replace('www.','')
20
+ except Exception:return ''
21
+
22
+
23
+ def _load_json(path,default):
24
+ try:
25
+ if os.path.exists(path):
26
+ with open(path,'r',encoding='utf-8') as f:return json.load(f)
27
+ except Exception:pass
28
+ return default
29
+
30
+
31
+ def _save_json(path,data):
32
+ try:
33
+ os.makedirs(os.path.dirname(path),exist_ok=True);tmp=path+'.tmp'
34
+ with open(tmp,'w',encoding='utf-8') as f:json.dump(data,f,ensure_ascii=False)
35
+ os.replace(tmp,path)
36
+ except Exception:pass
37
+
38
+
39
+ def _youtube_shorts_ytdlp(handle,count=20):
40
+ try:
41
+ import yt_dlp
42
+ url=f"https://www.youtube.com/@{handle}/shorts"
43
+ opts={'quiet':True,'extract_flat':True,'skip_download':True,'playlistend':count,'ignoreerrors':True,'no_warnings':True}
44
+ with yt_dlp.YoutubeDL(opts) as ydl:
45
+ info=ydl.extract_info(url,download=False)
46
+ out=[]
47
+ for e in (info or {}).get('entries') or []:
48
+ vid=e.get('id') or ''
49
+ if not re.match(r'^[A-Za-z0-9_-]{11}$',vid):continue
50
+ title=e.get('title') or 'YouTube Short'
51
+ out.append({'title':title,'link':f'https://www.youtube.com/watch?v={vid}','img':f'https://i.ytimg.com/vi/{vid}/hqdefault.jpg','source':'yt','id':vid,'channel':handle})
52
+ return out
53
+ except Exception:return []
54
+
55
+
56
+ def _youtube_shorts_html(handle,count=20):
57
+ try:
58
+ html=requests.get(f"https://www.youtube.com/@{handle}/shorts",headers=getattr(base,'HEADERS',{}),timeout=15).text
59
+ ids=[];out=[]
60
+ for m in re.finditer(r'"videoId":"([A-Za-z0-9_-]{11})"',html):
61
+ vid=m.group(1)
62
+ if vid in ids:continue
63
+ ids.append(vid)
64
+ snip=html[max(0,m.start()-1000):m.start()+1800]
65
+ title='YouTube Short'
66
+ mt=re.search(r'"title":\{"runs":\[\{"text":"([^"]+)"',snip) or re.search(r'"accessibilityText":"([^"]+)"',snip)
67
+ if mt:title=clean(mt.group(1).replace('\\n',' '))
68
+ out.append({'title':title,'link':f'https://www.youtube.com/watch?v={vid}','img':f'https://i.ytimg.com/vi/{vid}/hqdefault.jpg','source':'yt','id':vid,'channel':handle})
69
+ if len(out)>=count:break
70
+ return out
71
+ except Exception:return []
72
+
73
+
74
+ def _fresh_shorts():
75
+ items=[];seen=set()
76
+ for ch in SHORT_CHANNELS:
77
+ got=_youtube_shorts_ytdlp(ch,24) or _youtube_shorts_html(ch,24)
78
+ for v in got:
79
+ if v['id'] not in seen:
80
+ seen.add(v['id']);items.append(v)
81
+ # fallback from main if live scrape fails
82
+ try:
83
+ for v in getattr(rt.old.base if hasattr(rt.old,'base') else rt,'SHORTS_FALLBACK',[]) or []:
84
+ vid=v.get('id')
85
+ if vid and vid not in seen:
86
+ seen.add(vid);items.append(v)
87
+ except Exception:pass
88
+ return items[:50]
89
+
90
+
91
+ def _topic_image(topic):
92
+ try:return base.pollinations_image_url(topic)
93
+ except Exception:return "https://image.pollinations.ai/prompt/"+quote("Vietnamese news editorial illustration "+topic)+"?width=1024&height=576&nologo=true"
94
+
95
+ # Remove old endpoints/root to override.
96
+ _PATCH={('/api/shorts','GET'),('/api/topic_post','POST'),('/api/ai/interact','POST'),('/','GET')}
97
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
98
+
99
+ @app.get('/api/shorts')
100
+ def api_shorts_final3(refresh:int=Query(default=0)):
101
+ now=time.time()
102
+ if not refresh and _SHORTS_CACHE['d'] and now-_SHORTS_CACHE['t']<900:
103
+ return JSONResponse(_SHORTS_CACHE['d'])
104
+ data=_fresh_shorts()
105
+ _SHORTS_CACHE.update({'t':now,'d':data})
106
+ return JSONResponse(data)
107
+
108
+ @app.post('/api/topic_post')
109
+ async def topic_post_qwen(request:Request):
110
+ body=await request.json();topic=clean(body.get('topic',''))
111
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
112
+ img=_topic_image(topic)
113
+ prompt=f"""Bạn là biên tập viên VNEWS. Dựa trên kiến thức tổng quát của bạn, hãy tạo một bài đăng Tường AI bằng tiếng Việt về chủ đề: {topic}
114
+
115
+ Yêu cầu:
116
+ - Viết như một bài tin/tạp chí ngắn, có tiêu đề hấp dẫn.
117
+ - 1 đoạn mở đầu 2 câu.
118
+ - 4-6 ý chính rõ ràng, không lan man.
119
+ - Nếu chủ đề là thể thao/c��ng nghệ/xã hội, hãy viết có bối cảnh và nhận định.
120
+ - Không khẳng định số liệu thời sự mới nếu không chắc; dùng cách diễn đạt thận trọng.
121
+ - Cuối bài thêm dòng: Nguồn tham khảo: Qwen2.5-VL / kiến thức tổng hợp.
122
+ """
123
+ text=await base.qwen_generate(prompt,image_url=img,max_tokens=1100)
124
+ if not text:
125
+ text=f"{topic}\n\nĐây là bài gợi ý do AI tạo dựa trên kiến thức tổng hợp. Nội dung cung cấp bối cảnh, các điểm đáng chú ý và góc nhìn tham khảo về chủ đề này.\n\nNguồn tham khảo: Qwen2.5-VL / kiến thức tổng hợp."
126
+ post=base.make_post(topic,text,img,'','topic_qwen',sources=[{'title':'Qwen2.5-VL / kiến thức tổng hợp','url':'','via':'Qwen2.5-VL'}])
127
+ post['images']=[img]
128
+ posts=base._load_ai_wall();posts.insert(0,post);base._save_ai_wall(posts)
129
+ return JSONResponse({'post':post})
130
+
131
+ @app.post('/api/ai/interact')
132
+ async def ai_interact(request:Request):
133
+ body=await request.json();pid=str(body.get('id','')).strip();kind=str(body.get('kind','wall')).strip();action=str(body.get('action','')).strip();text=clean(body.get('text',''))
134
+ if not pid:return JSONResponse({'error':'missing id'},status_code=400)
135
+ db=_load_json(AI_INTERACTIONS_FILE,{})
136
+ key=kind+':'+pid
137
+ st=db.get(key) or {'views':0,'likes':0,'comments':[],'asks':[]}
138
+ if action=='view':st['views']=int(st.get('views',0))+1
139
+ elif action=='like':st['likes']=int(st.get('likes',0))+1
140
+ elif action=='comment' and text:
141
+ st.setdefault('comments',[]).insert(0,{'text':text[:240],'ts':int(time.time())});st['comments']=st['comments'][:80]
142
+ elif action=='ask' and text:
143
+ posts=base._load_ai_wall();p=next((x for x in posts if str(x.get('id'))==pid),{})
144
+ prompt=f"""Trả lời ngắn bằng tiếng Việt cho câu hỏi của người xem về nội dung này.
145
+ Tiêu đề: {p.get('title','')}
146
+ Nội dung: {(p.get('text') or '')[:4000]}
147
+ Câu hỏi: {text}
148
+ """
149
+ ans=await base.qwen_generate(prompt,max_tokens=500)
150
+ if not ans:ans='AI chưa trả lời được lúc này. Bạn thử hỏi lại ngắn gọn hơn.'
151
+ st.setdefault('asks',[]).insert(0,{'q':text[:240],'a':ans[:1000],'ts':int(time.time())});st['asks']=st['asks'][:50]
152
+ db[key]=st;_save_json(AI_INTERACTIONS_FILE,db)
153
+ return JSONResponse({'stats':st})
154
+
155
+ FINAL3_INJECT = r'''
156
+ <style>
157
+ .ai-compose-row.topic-final3{display:flex!important;flex-direction:column!important;gap:8px!important;width:100%!important}.ai-compose-row.topic-final3 input,.ai-compose-row.topic-final3 button{width:100%!important;box-sizing:border-box!important}.short-action-panel{position:absolute;right:8px;bottom:92px;display:flex;flex-direction:column;gap:12px;z-index:20}.short-action-btn{background:none;border:0;color:#fff;text-align:center;font-size:10px}.short-action-btn .ico{width:44px;height:44px;border-radius:50%;background:rgba(0,0,0,.45);display:flex;align-items:center;justify-content:center;font-size:21px;margin:auto}.short-modal{position:fixed;inset:auto 0 0 0;max-height:60vh;background:#181818;border-radius:16px 16px 0 0;z-index:99999;padding:14px;display:none;overflow:auto}.short-modal.active{display:block}.short-modal textarea,.short-modal input{width:100%;background:#222;border:1px solid #444;color:#eee;border-radius:10px;padding:9px;margin:6px 0}.short-modal button{background:#2d8659;border:0;color:#fff;border-radius:10px;padding:8px 12px;margin:4px}.ai-short-home{margin:6px 4px;background:#1a1a1a;border:1px solid #2a2a2a;border-radius:8px;overflow:hidden}.ai-short-card-final{flex:0 0 120px}.ai-short-card-final video{width:100%;aspect-ratio:9/16;object-fit:cover;background:#000;border-radius:8px}
158
+ </style>
159
+ <div id="short-modal" class="short-modal"></div>
160
+ <script>
161
+ (function(){
162
+ let finalWall3=[];let currentShortCtx=null;
163
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
164
+ function ensureTopicBox(){let comp=document.querySelector('.ai-compose');if(!comp)return;if(!document.getElementById('ai-topic-input-final3')){let row=document.createElement('div');row.className='ai-compose-row topic-final3';row.innerHTML='<input id="ai-topic-input-final3" placeholder="Nhập chủ đề để Qwen2.5VL gợi ý bài đăng lên Tường AI..."><button onclick="createTopicPostFinal3()">✨ Tạo bài theo chủ đề bằng Qwen</button>';comp.insertBefore(row,comp.firstChild.nextSibling);} }
165
+ window.createTopicPostFinal3=async function(){let inp=document.getElementById('ai-topic-input-final3');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');let btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tạo...'}try{let r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');finalWall3.unshift(j.post);if(window.finalWall)window.finalWall.unshift(j.post);if(inp)inp.value='';renderAIShortHome();if(window.renderWall)window.renderWall();alert('Đã tạo bài chủ đề và đăng lên Tường AI, không reload.');}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='✨ Tạo bài theo chủ đề bằng Qwen'}}};
166
+ async function refreshFinalWall3(){try{finalWall3=(await (await fetch('/api/ai_wall')).json()).posts||[];renderAIShortHome();}catch(e){}}
167
+ function renderAIShortHome(){let home=document.getElementById('view-home');if(!home)return;document.getElementById('ai-short-home')?.remove();let vids=finalWall3.filter(p=>p.video);if(!vids.length)return;let wrap=document.createElement('div');wrap.id='ai-short-home';wrap.className='ai-short-home';let h='<div class="slider-header"><span class="slider-label">🎬 Short AI</span><span class="slider-note">Video đã tạo</span></div><div class="slider-track">';vids.slice(0,30).forEach((p,i)=>{h+=`<div class="ai-short-card-final" onclick="openAIShortFeed(${i})"><video src="${p.video}" muted playsinline preload="metadata"></video><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div>';wrap.innerHTML=h;let after=document.getElementById('ai-wall-final')||document.querySelector('.ai-compose');if(after)after.after(wrap);else home.prepend(wrap);}
168
+ window.openAIShortFeed=function(start){let vids=finalWall3.filter(p=>p.video);if(!vids.length)return;showView('view-tiktok');let h='<button class="back-btn" onclick="switchCat(\'home\')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';let ordered=start>0?vids.slice(start).concat(vids.slice(0,start)):vids;ordered.forEach((p,i)=>{h+=`<div class="tiktok-slide" data-kind="ai" data-id="${p.id}"><video src="${p.video}" playsinline controls loop></video><div class="tiktok-bottom"><span class="badge badge-ai">AI</span><p class="tiktok-title">${esc(p.title)}</p></div>${actionPanel('ai',p.id,i)}<span class="tiktok-counter">${i+1}/${ordered.length}</span></div>`});h+='</div></div>';document.getElementById('view-tiktok').innerHTML=h;initActionFeed();}
169
+ function actionPanel(kind,id,i){return `<div class="short-action-panel"><button class="short-action-btn" onclick="shortAct('${kind}','${id}','view')"><div class="ico">👁</div><span id="v-${kind}-${id}">0</span></button><button class="short-action-btn" onclick="shortAct('${kind}','${id}','like')"><div class="ico">❤️</div><span id="l-${kind}-${id}">0</span></button><button class="short-action-btn" onclick="openCommentBox('${kind}','${id}')"><div class="ico">💬</div><span>BL</span></button><button class="short-action-btn" onclick="openAskBox('${kind}','${id}')"><div class="ico">🤖</div><span>Hỏi</span></button><button class="short-action-btn" onclick="shareShortCtx('${kind}','${id}')"><div class="ico">📤</div><span>Share</span></button></div>`}
170
+ window.shortAct=async function(kind,id,action,text=''){let url=kind==='yt'?'/api/short-action':'/api/ai/interact';let body=kind==='yt'?{id,action,text}:{id,kind:'short',action,text};let r=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});let j=await r.json();let st=j.stats||j;let v=document.getElementById(`v-${kind}-${id}`),l=document.getElementById(`l-${kind}-${id}`);if(v&&st.views!=null)v.textContent=st.views;if(l&&st.likes!=null)l.textContent=st.likes;return st;}
171
+ window.openCommentBox=function(kind,id){let m=document.getElementById('short-modal');m.innerHTML=`<h3>💬 Bình luận</h3><textarea id="short-comment-text" placeholder="Nhập bình luận..."></textarea><button onclick="submitShortComment('${kind}','${id}')">Gửi</button><button onclick="closeShortModal()">Đóng</button>`;m.classList.add('active')}
172
+ window.submitShortComment=async function(kind,id){let t=document.getElementById('short-comment-text').value.trim();if(!t)return;await shortAct(kind,id,'comment',t);alert('Đã gửi bình luận');closeShortModal()}
173
+ window.openAskBox=function(kind,id){let m=document.getElementById('short-modal');m.innerHTML=`<h3>🤖 Hỏi AI</h3><input id="short-ask-text" placeholder="Bạn muốn hỏi gì về nội dung này?"><div id="short-answer"></div><button onclick="submitShortAsk('${kind}','${id}')">Hỏi</button><button onclick="closeShortModal()">Đóng</button>`;m.classList.add('active')}
174
+ window.submitShortAsk=async function(kind,id){let t=document.getElementById('short-ask-text').value.trim();if(!t)return;if(kind==='yt'){document.getElementById('short-answer').innerHTML='AI chỉ hỗ trợ trả lời sâu cho Short AI/Tường AI.';return}let st=await shortAct(kind,id,'ask',t);let a=(st.asks&&st.asks[0]&&st.asks[0].a)||'Chưa có trả lời';document.getElementById('short-answer').innerHTML='<p style="white-space:pre-wrap;color:#ccc">'+esc(a)+'</p>'}
175
+ window.closeShortModal=function(){document.getElementById('short-modal').classList.remove('active')}
176
+ window.shareShortCtx=function(kind,id){if(kind==='ai'){let p=finalWall3.find(x=>x.id===id);if(p){let url=location.origin+'/aw?post='+encodeURIComponent(id)+'&short=1';if(navigator.share)navigator.share({title:'🎬 Short AI: '+p.title,url}).catch(()=>{});else navigator.clipboard.writeText(url).then(()=>alert('Đã sao chép link!'));}}else{let url='https://www.youtube.com/watch?v='+id;if(navigator.share)navigator.share({title:'Shorts VNEWS',url}).catch(()=>{});else navigator.clipboard.writeText(url).then(()=>alert('Đã sao chép link!'));}}
177
+ function initActionFeed(){let feed=document.getElementById('tiktok-feed');if(!feed)return;let slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{let v=sl.querySelector('video');let fr=sl.querySelector('iframe');if(idx===i){if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;let kind=sl.dataset.kind,id=sl.dataset.id;if(kind&&id)shortAct(kind,id,'view').catch(()=>{})}else{if(v)v.pause();if(fr&&fr.src)fr.src=''}});cur=i}let t;feed.addEventListener('scroll',()=>{clearTimeout(t);t=setTimeout(()=>{let rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2,b=-1,d=1e9;slides.forEach((sl,i)=>{let dd=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(dd<d){d=dd;b=i}});if(b>=0)act(b)},150)});setTimeout(()=>act(0),300)}
178
+ // Override openTikTok for regular YouTube shorts with same action layout.
179
+ window.openTikTok=async function(type,startIdx){showView('view-tiktok');let arts= type==='shorts'? await fetch('/api/shorts?refresh=1').then(r=>r.json()).catch(()=>[]) : await fetch(type==='highlights'?'/api/highlights':'/api/bdp_videos').then(r=>r.json()).catch(()=>[]);if(type!=='shorts'&&window.buildTikTokPlayer)return window.buildTikTokPlayer(arts,startIdx,type);let ordered=startIdx>0?arts.slice(startIdx).concat(arts.slice(0,startIdx)):arts;let h='<button class="back-btn" onclick="switchCat(\'home\')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';ordered.forEach((v,i)=>{let id=v.id||((v.link||'').match(/v=([A-Za-z0-9_-]{11})/)||[])[1]||String(i);let src='https://www.youtube.com/embed/'+id+'?autoplay=1&rel=0&playsinline=1';h+=`<div class="tiktok-slide" data-kind="yt" data-id="${id}"><iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe><div class="tiktok-bottom"><span class="badge badge-fpt">YT</span><p class="tiktok-title">${esc(v.title)}</p></div>${actionPanel('yt',id,i)}<span class="tiktok-counter">${i+1}/${ordered.length}</span></div>`});h+='</div></div>';document.getElementById('view-tiktok').innerHTML=h;initActionFeed();}
180
+ // Patch make short: update home Short AI slide without reload.
181
+ let oldMake=window.makeFinalShort||window.aiMakeShortPatched;
182
+ window.makeFinalShort=window.aiMakeShortPatched=async function(i){let arr=finalWall3.length?finalWall3:(window.finalWall||[]);let p=arr[i];if(!p&&oldMake)return oldMake(i);if(!p)return;let voice=document.getElementById('ai-short-voice')?.value||'nu';let emotion=document.getElementById('ai-short-emotion')?.value||'neutral';let btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tạo...'}try{let r=await fetch('/api/ai/short/'+p.id,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice,emotion,speed:1.2})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi tạo short');p.video=j.video;let idx=finalWall3.findIndex(x=>x.id===p.id);if(idx<0)finalWall3.unshift(p);renderAIShortHome();if(window.renderWall)window.renderWall();alert('Đã tạo short và thêm vào slide Short AI, không reload.');}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='🎬 Tạo short'}}}
183
+ setTimeout(()=>{ensureTopicBox();refreshFinalWall3();},700);setInterval(ensureTopicBox,1500);
184
+ })();
185
+ </script>
186
+ '''
187
+
188
+ @app.get('/')
189
+ async def index_final3():
190
+ html=f2.f1._load_index_html();body=getattr(rt.old,'PATCH_INJECT','') + f2.f1.FINAL_INJECT + FINAL3_INJECT
191
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
ai_runtime_final4.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final4 runtime: fix topic button visibility, shorts home feed, AI asking for videos/articles."""
2
+ import re, time, json, os, requests
3
+ from urllib.parse import urlparse
4
+ import ai_runtime_final3 as f3
5
+ from ai_runtime_final3 import app, base, rt, HTMLResponse, JSONResponse, Request, Query
6
+ try:
7
+ import main as main_mod
8
+ except Exception:
9
+ main_mod=None
10
+
11
+ AI_INTERACTIONS_FILE=f3.AI_INTERACTIONS_FILE
12
+ _SHORTS_CACHE={"t":0,"d":[]}
13
+ SHORT_CHANNELS=f3.SHORT_CHANNELS
14
+
15
+
16
+ def clean(s):
17
+ import html as html_lib
18
+ return re.sub(r"\s+"," ",html_lib.unescape(s or "")).strip()
19
+
20
+
21
+ def _domain(u):
22
+ try:return urlparse(u or '').netloc.replace('www.','')
23
+ except Exception:return ''
24
+
25
+
26
+ def _load_json(path,default):
27
+ try:
28
+ if os.path.exists(path):
29
+ with open(path,'r',encoding='utf-8') as f:return json.load(f)
30
+ except Exception:pass
31
+ return default
32
+
33
+
34
+ def _save_json(path,data):
35
+ try:
36
+ os.makedirs(os.path.dirname(path),exist_ok=True);tmp=path+'.tmp'
37
+ with open(tmp,'w',encoding='utf-8') as f:json.dump(data,f,ensure_ascii=False)
38
+ os.replace(tmp,path)
39
+ except Exception:pass
40
+
41
+
42
+ def _fallback_shorts():
43
+ out=[];seen=set()
44
+ candidates=[]
45
+ try:candidates+=(getattr(main_mod,'SHORTS_FALLBACK',[]) or [])
46
+ except Exception:pass
47
+ try:candidates+=(getattr(rt,'SHORTS_FALLBACK',[]) or [])
48
+ except Exception:pass
49
+ # hard fallback if imports fail
50
+ hard=[('Lu_iCQ5YwNM','Công an lập hồ sơ xử lý người phụ nữ chửi bới, tát tài xế ô tô | Dân trí','baodantri7941'),('CwWvijF8BOA','Chú rể bật khóc nhận món quà bí mật người cha quá cố gửi 26 năm trước | Dân trí','baodantri7941'),('7Pd6vZ2Lz1M','Hành động ấm lòng trong tìm kiếm học sinh tử vong ở sông Lô | SKĐS','baosuckhoedoisongboyte'),('SlHLt_ZyPiE','Xử phạt người đàn ông xóa số điện thoại cứu hộ trên cao tốc Bắc - Nam | SKĐS','baosuckhoedoisongboyte')]
51
+ for vid,title,ch in hard:
52
+ candidates.append({'id':vid,'title':title,'channel':ch,'link':'https://www.youtube.com/watch?v='+vid,'img':'https://i.ytimg.com/vi/'+vid+'/hqdefault.jpg','source':'yt'})
53
+ for v in candidates:
54
+ vid=v.get('id') or ''
55
+ if vid and vid not in seen:
56
+ seen.add(vid)
57
+ if not v.get('link'):v['link']='https://www.youtube.com/watch?v='+vid
58
+ if not v.get('img'):v['img']='https://i.ytimg.com/vi/'+vid+'/hqdefault.jpg'
59
+ v['source']='yt';out.append(v)
60
+ return out
61
+
62
+
63
+ def _fresh_shorts():
64
+ items=[];seen=set()
65
+ for ch in SHORT_CHANNELS:
66
+ got=f3._youtube_shorts_ytdlp(ch,24) or f3._youtube_shorts_html(ch,24)
67
+ for v in got:
68
+ vid=v.get('id')
69
+ if vid and vid not in seen:
70
+ seen.add(vid);items.append(v)
71
+ for v in _fallback_shorts():
72
+ vid=v.get('id')
73
+ if vid and vid not in seen:
74
+ seen.add(vid);items.append(v)
75
+ return items[:60]
76
+
77
+ # Remove endpoints/root to override.
78
+ _PATCH={('/api/shorts','GET'),('/api/ai/interact','POST'),('/api/article/ask','POST'),('/','GET')}
79
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
80
+
81
+ @app.get('/api/shorts')
82
+ def api_shorts_final4(refresh:int=Query(default=0)):
83
+ now=time.time()
84
+ if not refresh and _SHORTS_CACHE['d'] and now-_SHORTS_CACHE['t']<900:return JSONResponse(_SHORTS_CACHE['d'])
85
+ data=_fresh_shorts()
86
+ _SHORTS_CACHE.update({'t':now,'d':data})
87
+ return JSONResponse(data)
88
+
89
+ @app.post('/api/ai/interact')
90
+ async def ai_interact_final4(request:Request):
91
+ body=await request.json();pid=str(body.get('id','')).strip();kind=str(body.get('kind','wall')).strip();action=str(body.get('action','')).strip();text=clean(body.get('text',''));context=clean(body.get('context',''));title=clean(body.get('title',''))
92
+ if not pid:return JSONResponse({'error':'missing id'},status_code=400)
93
+ db=_load_json(AI_INTERACTIONS_FILE,{})
94
+ key=kind+':'+pid
95
+ st=db.get(key) or {'views':0,'likes':0,'comments':[],'asks':[]}
96
+ if action=='view':st['views']=int(st.get('views',0))+1
97
+ elif action=='like':st['likes']=int(st.get('likes',0))+1
98
+ elif action=='comment' and text:
99
+ st.setdefault('comments',[]).insert(0,{'text':text[:240],'ts':int(time.time())});st['comments']=st['comments'][:80]
100
+ elif action=='ask' and text:
101
+ if kind in ('ai','short','wall'):
102
+ posts=base._load_ai_wall();p=next((x for x in posts if str(x.get('id'))==pid),{})
103
+ title=title or p.get('title','');context=context or (p.get('text') or '')
104
+ # For YouTube shorts, frontend sends title/context because AI cannot watch video.
105
+ if not context:context=title or pid
106
+ prompt=f"""Bạn là trợ lý VNEWS. Trả lời chi tiết bằng tiếng Việt dựa trên thông tin có sẵn về video/bài viết.
107
+
108
+ Tiêu đề/ngữ cảnh: {title}
109
+ Nội dung mô tả: {context[:5000]}
110
+
111
+ Câu hỏi người dùng: {text}
112
+
113
+ Yêu cầu:
114
+ - Nếu là video YouTube/Shorts và chỉ có tiêu đề, hãy nói rõ rằng bạn suy luận từ tiêu đề/mô tả, không khẳng định đã xem video.
115
+ - Trả lời cụ thể, có giải thích, không quá ngắn.
116
+ """
117
+ ans=await base.qwen_generate(prompt,max_tokens=900)
118
+ if not ans:ans='AI chưa trả lời được lúc này. Bạn thử hỏi lại cụ thể hơn.'
119
+ st.setdefault('asks',[]).insert(0,{'q':text[:240],'a':ans[:1500],'ts':int(time.time())});st['asks']=st['asks'][:50]
120
+ db[key]=st;_save_json(AI_INTERACTIONS_FILE,db)
121
+ return JSONResponse({'stats':st})
122
+
123
+ @app.post('/api/article/ask')
124
+ async def article_ask(request:Request):
125
+ body=await request.json();url=clean(body.get('url',''));question=clean(body.get('question',''))
126
+ if not question:return JSONResponse({'error':'missing question'},status_code=400)
127
+ title='';raw=''
128
+ try:
129
+ data=None
130
+ if url and hasattr(f3.f2.f1,'_scrape_url_article_only'):
131
+ data=f3.f2.f1._scrape_url_article_only(url)
132
+ if not data and url:data=base.scrape_any_url(url)
133
+ if data:
134
+ title=data.get('title','');raw=(data.get('summary','')+'\n'+data.get('text','')).strip()
135
+ except Exception:pass
136
+ context=raw[:12000] if raw else clean(body.get('context',''))[:12000]
137
+ prompt=f"""Bạn là trợ lý đọc hiểu bài viết của VNEWS. Hãy trả lời chi tiết câu hỏi của người dùng dựa trên bài viết.
138
+
139
+ Tiêu đề bài: {title}
140
+ Nội dung bài:
141
+ {context}
142
+
143
+ Câu hỏi: {question}
144
+
145
+ Yêu cầu:
146
+ - Trả lời bằng tiếng Việt.
147
+ - Dựa sát nội dung bài, nếu bài không có thông tin thì nói rõ.
148
+ - Giải thích chi tiết, có gạch đầu dòng khi hữu ích.
149
+ """
150
+ ans=await base.qwen_generate(prompt,max_tokens=1200)
151
+ if not ans:ans='AI chưa trả lời được lúc này. Bạn thử hỏi lại hoặc rút gọn câu hỏi.'
152
+ return JSONResponse({'answer':ans,'title':title})
153
+
154
+ FINAL4_INJECT = r'''
155
+ <style>
156
+ /* Ensure topic Qwen button is visible; earlier patches hide any button containing “chủ đề”. */
157
+ .topic-final4{display:flex!important;flex-direction:column!important;gap:8px!important;width:100%!important;margin-top:6px}.topic-final4 input,.topic-final4 button{display:block!important;width:100%!important;box-sizing:border-box!important}.topic-final4 button{background:#2d8659!important;color:#fff!important;border:0!important;border-radius:18px!important;padding:9px 12px!important;font-size:11px!important;font-weight:700!important}.article-ai-ask{margin-top:12px;background:#141414;border:1px solid #2a2a2a;border-radius:10px;padding:10px}.article-ai-ask textarea{width:100%;min-height:70px;background:#222;border:1px solid #444;color:#eee;border-radius:10px;padding:9px}.article-ai-ask button{background:#2d8659;border:0;color:#fff;border-radius:10px;padding:8px 12px;margin-top:6px}.article-ai-answer{white-space:pre-wrap;color:#ccc;font-size:13px;line-height:1.55;margin-top:8px}.ai-compose-row:has(#ai-url-input){display:flex!important;flex-direction:column!important}.ai-compose-row:has(#ai-url-input) input,.ai-compose-row:has(#ai-url-input) button{width:100%!important}
158
+ </style>
159
+ <script>
160
+ (function(){
161
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
162
+ let shortsMap={};
163
+ function ensureTopicButtonFinal4(){let comp=document.querySelector('.ai-compose');if(!comp)return;if(!document.getElementById('ai-topic-input-final4')){let row=document.createElement('div');row.className='topic-final4';row.innerHTML='<input id="ai-topic-input-final4" placeholder="Nhập chủ đề để Qwen2.5VL tạo bài lên Tường AI..."><button id="ai-topic-btn-final4" onclick="createTopicPostFinal4()">✨ Tạo bài bằng Qwen</button>';comp.insertBefore(row,comp.firstChild.nextSibling);}let b=document.getElementById('ai-topic-btn-final4');if(b){b.style.display='block';b.textContent='✨ Tạo bài bằng Qwen';}}
164
+ window.createTopicPostFinal4=async function(){let inp=document.getElementById('ai-topic-input-final4');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');let btn=document.getElementById('ai-topic-btn-final4');if(btn){btn.disabled=true;btn.textContent='Đang tạo...'}try{let r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');if(window.finalWall)window.finalWall.unshift(j.post);if(window.finalWall3)window.finalWall3.unshift(j.post);if(inp)inp.value='';if(window.renderWall)window.renderWall();alert('Đã tạo bài bằng Qwen và đăng lên Tường AI.');}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='✨ Tạo bài bằng Qwen'}}};
165
+ // Guarantee Shorts slide appears on home even if previous loadHome missed it.
166
+ async function ensureShortsHome(){let home=document.getElementById('view-home');if(!home||document.getElementById('shorts-final4'))return;let sh=await fetch('/api/shorts?refresh=1').then(r=>r.json()).catch(()=>[]);if(!sh.length)return;let wrap=document.createElement('div');wrap.id='shorts-final4';wrap.className='slider-wrap';let h='<div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS</span><span class="slider-note">Cập nhật YouTube</span></div><div class="slider-track">';sh.slice(0,30).forEach((a,i)=>{shortsMap[a.id]=a;h+=`<div class="slider-item shorts-item" onclick="openTikTok('shorts',${i})"><div class="slider-thumb shorts-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div>';wrap.innerHTML=h;let after=document.querySelector('.ai-compose')||home.firstChild;if(after)after.after(wrap);else home.prepend(wrap);}
167
+ // Patch ask for YouTube shorts: AI receives title/context.
168
+ let oldShortAct=window.shortAct;
169
+ window.shortAct=async function(kind,id,action,text=''){let meta=shortsMap[id]||{};let url='/api/ai/interact';let body={id,kind:kind==='yt'?'yt':kind,action,text,title:meta.title||'',context:meta.title?('Video Shorts YouTube từ kênh '+(meta.channel||'')+'. Tiêu đề: '+meta.title):''};let r=await fetch(url,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});let j=await r.json();let st=j.stats||j;let v=document.getElementById(`v-${kind}-${id}`),l=document.getElementById(`l-${kind}-${id}`);if(v&&st.views!=null)v.textContent=st.views;if(l&&st.likes!=null)l.textContent=st.likes;return st;};
170
+ window.submitShortAsk=async function(kind,id){let t=document.getElementById('short-ask-text').value.trim();if(!t)return;let st=await shortAct(kind,id,'ask',t);let a=(st.asks&&st.asks[0]&&st.asks[0].a)||'Chưa có trả lời';document.getElementById('short-answer').innerHTML='<p style="white-space:pre-wrap;color:#ccc">'+esc(a)+'</p>';};
171
+ // Patch openTikTok to populate shortsMap.
172
+ let oldOpenTikTok=window.openTikTok;
173
+ window.openTikTok=async function(type,startIdx){if(type==='shorts'){let arts=await fetch('/api/shorts?refresh=1').then(r=>r.json()).catch(()=>[]);arts.forEach(a=>{if(a.id)shortsMap[a.id]=a});}return oldOpenTikTok?oldOpenTikTok(type,startIdx):null;};
174
+ function addArticleAskBox(){let view=document.getElementById('view-article');if(!view||document.getElementById('article-ai-ask'))return;let art=view.querySelector('.article-view');if(!art)return;let box=document.createElement('div');box.id='article-ai-ask';box.className='article-ai-ask';box.innerHTML='<h3 style="font-size:14px;color:#5cb87a;margin-bottom:6px">🤖 Hỏi AI về bài viết</h3><textarea id="article-ai-question" placeholder="Nhập câu hỏi cần AI trả lời chi tiết về bài viết..."></textarea><button onclick="askArticleAI()">Hỏi AI</button><div id="article-ai-answer" class="article-ai-answer"></div>';art.appendChild(box);}
175
+ window.askArticleAI=async function(){let q=document.getElementById('article-ai-question')?.value.trim();if(!q)return alert('Nhập câu hỏi trước');let ans=document.getElementById('article-ai-answer');ans.textContent='Đang hỏi AI...';let url=(window._currentArticle&&window._currentArticle.url)||((typeof _currentArticle!=='undefined'&&_currentArticle.url)||'');let context=document.querySelector('.article-view')?.innerText||'';try{let r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,question:q,context})});let j=await r.json();ans.textContent=j.answer||j.error||'Không có trả lời';}catch(e){ans.textContent='Lỗi hỏi AI: '+e.message}}
176
+ let oldReadArticle=window.readArticle;if(oldReadArticle){window.readArticle=async function(){let ret=await oldReadArticle.apply(this,arguments);setTimeout(addArticleAskBox,700);return ret;}}
177
+ setTimeout(()=>{ensureTopicButtonFinal4();ensureShortsHome();},1000);setInterval(()=>{ensureTopicButtonFinal4();if(document.getElementById('view-home')?.classList.contains('active'))ensureShortsHome();addArticleAskBox();},2000);
178
+ })();
179
+ </script>
180
+ '''
181
+
182
+ @app.get('/')
183
+ async def index_final4():
184
+ html=f3.f2.f1._load_index_html();body=getattr(rt.old,'PATCH_INJECT','')+f3.f2.f1.FINAL_INJECT+f3.FINAL3_INJECT+FINAL4_INJECT
185
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
ai_runtime_final5.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final5 runtime: remove duplicate topic box, improve Qwen topic knowledge output, fix Shorts direct playback."""
2
+ import re, time
3
+ from urllib.parse import quote
4
+ import ai_runtime_final4 as f4
5
+ from ai_runtime_final4 import app, base, rt, HTMLResponse, JSONResponse, Request, Query
6
+
7
+ # Remove topic/root endpoints to override.
8
+ _PATCH={('/api/topic_post','POST'),('/','GET')}
9
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
10
+
11
+ def clean(s):
12
+ import html as html_lib
13
+ return re.sub(r"\s+"," ",html_lib.unescape(s or "")).strip()
14
+
15
+ def _topic_image(topic):
16
+ try:return base.pollinations_image_url(topic)
17
+ except Exception:return "https://image.pollinations.ai/prompt/"+quote("Vietnamese educational editorial illustration "+topic)+"?width=1024&height=576&nologo=true"
18
+
19
+ @app.post('/api/topic_post')
20
+ async def topic_post_knowledge(request:Request):
21
+ body=await request.json();topic=clean(body.get('topic',''))
22
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
23
+ img=_topic_image(topic)
24
+ prompt=f"""Người dùng muốn đăng một bài trên Tường AI về chủ đề: "{topic}".
25
+
26
+ Hãy viết NGAY nội dung kiến thức/thông tin hữu ích về chủ đề đó, không lập dàn ý chung chung, không nói "có thể viết", không hướng dẫn cách viết.
27
+
28
+ Yêu cầu đầu ra:
29
+ - Tiêu đề hấp dẫn, cụ thể.
30
+ - 1 đoạn mở đầu giải thích trực tiếp chủ đề là gì/vì sao đáng chú ý.
31
+ - 5-7 đoạn hoặc ý chính cung cấp kiến thức thực chất, ví dụ, bối cảnh, tác động, hiểu lầm thường gặp, điểm cần lưu ý.
32
+ - Nếu chủ đề là thể thao, hãy nói về bối cảnh, nhân vật/đội bóng, ý nghĩa chiến thuật hoặc lịch sử liên quan.
33
+ - Nếu chủ đề là công nghệ/khoa học/xã hội, hãy giải thích khái niệm, ứng dụng, rủi ro/lợi ích, ví dụ thực tế.
34
+ - Không bịa số liệu thời sự mới; nếu không chắc, dùng cách nói thận trọng.
35
+ - Viết như bài đăng hoàn chỉnh để đọc được ngay.
36
+ - Cuối bài thêm: Nguồn tham khảo: Qwen2.5-VL / kiến thức tổng hợp.
37
+ """
38
+ text=await base.qwen_generate(prompt,image_url=img,max_tokens=1400)
39
+ if not text:
40
+ text=f"{topic}\n\n{topic} là một chủ đề có nhiều khía cạnh cần nhìn từ bối cảnh, ý nghĩa thực tế và tác động đối với người quan tâm. Bài viết này tóm lược các điểm quan trọng nhất để người đọc hiểu nhanh vấn đề, thay vì chỉ liệt kê tiêu đề hoặc dàn ý.\n\nNguồn tham khảo: Qwen2.5-VL / kiến thức tổng hợp."
41
+ post=base.make_post(topic,text,img,'','topic_qwen',sources=[{'title':'Qwen2.5-VL / kiến thức tổng hợp','url':'','via':'Qwen2.5-VL'}])
42
+ post['images']=[img]
43
+ posts=base._load_ai_wall();posts.insert(0,post);base._save_ai_wall(posts)
44
+ return JSONResponse({'post':post})
45
+
46
+ FINAL5_INJECT=r'''
47
+ <style>
48
+ /* Keep exactly one topic input */
49
+ #ai-topic-input-final3,.ai-compose-row.topic-final3,#ai-topic-input-final4,.topic-final4{display:none!important}.topic-final5{display:flex!important;flex-direction:column!important;gap:8px!important;width:100%!important;margin-top:6px}.topic-final5 input,.topic-final5 button{display:block!important;width:100%!important;box-sizing:border-box!important}.topic-final5 button{background:#2d8659!important;color:#fff!important;border:0!important;border-radius:18px!important;padding:9px 12px!important;font-size:11px!important;font-weight:700!important}
50
+ </style>
51
+ <script>
52
+ (function(){
53
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
54
+ let shortsFinal5=[];
55
+ function removeDuplicateTopicBoxes(){document.querySelectorAll('#ai-topic-input-final3,.topic-final3,#ai-topic-input-final4,.topic-final4').forEach(e=>{let row=e.closest('.topic-final3,.topic-final4,.ai-compose-row')||e;e.remove?row.remove():row.style.display='none'});let comp=document.querySelector('.ai-compose');if(!comp)return;if(!document.getElementById('ai-topic-input-final5')){let row=document.createElement('div');row.className='topic-final5';row.innerHTML='<input id="ai-topic-input-final5" placeholder="Bạn muốn AI viết kiến thức về chủ đề gì? Ví dụ: thần đồng Arsenal, AI trong giáo dục, biến đổi khí hậu..."><button id="ai-topic-btn-final5" onclick="createTopicPostFinal5()">✨ Tạo bài kiến thức bằng Qwen</button>';comp.insertBefore(row,comp.firstChild.nextSibling);} }
56
+ window.createTopicPostFinal5=async function(){let inp=document.getElementById('ai-topic-input-final5');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');let btn=document.getElementById('ai-topic-btn-final5');if(btn){btn.disabled=true;btn.textContent='Đang tạo bài...'}try{let r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');if(window.finalWall)window.finalWall.unshift(j.post);if(window.finalWall3)window.finalWall3.unshift(j.post);if(inp)inp.value='';if(window.renderWall)window.renderWall();if(window.renderAIShortHome)window.renderAIShortHome();alert('Đã tạo bài kiến thức và đăng lên Tường AI.');}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='✨ Tạo bài kiến thức bằng Qwen'}}};
57
+ async function loadShortsFinal5(){shortsFinal5=await fetch('/api/shorts?refresh=1').then(r=>r.json()).catch(()=>[]);return shortsFinal5;}
58
+ function actionPanel(kind,id){return `<div class="short-action-panel"><button class="short-action-btn" onclick="shortAct('${kind}','${id}','view')"><div class="ico">👁</div><span id="v-${kind}-${id}">0</span></button><button class="short-action-btn" onclick="shortAct('${kind}','${id}','like')"><div class="ico">❤️</div><span id="l-${kind}-${id}">0</span></button><button class="short-action-btn" onclick="openCommentBox('${kind}','${id}')"><div class="ico">💬</div><span>BL</span></button><button class="short-action-btn" onclick="openAskBox('${kind}','${id}')"><div class="ico">🤖</div><span>Hỏi</span></button><button class="short-action-btn" onclick="shareShortCtx('${kind}','${id}')"><div class="ico">📤</div><span>Share</span></button></div>`}
59
+ window.openShortsFinal5=async function(startIdx){let arts=shortsFinal5.length?shortsFinal5:await loadShortsFinal5();if(!arts.length)return alert('Chưa tải được Shorts');let ordered=startIdx>0?arts.slice(startIdx).concat(arts.slice(0,startIdx)):arts;showView('view-tiktok');let h='<button class="back-btn" onclick="switchCat(\'home\')">← Shorts Dân trí & SKĐS</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';ordered.forEach((v,i)=>{let id=v.id||((v.link||'').match(/v=([A-Za-z0-9_-]{11})/)||[])[1]||String(i);let src='https://www.youtube.com/embed/'+id+'?autoplay=1&rel=0&playsinline=1';h+=`<div class="tiktok-slide" data-kind="yt" data-id="${id}" data-title="${esc(v.title)}" data-channel="${esc(v.channel||'')}"><iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe><div class="tiktok-bottom"><span class="badge badge-fpt">YT</span><p class="tiktok-title">${esc(v.title)}</p></div>${actionPanel('yt',id)}<span class="tiktok-counter">${i+1}/${ordered.length}</span></div>`});h+='</div></div>';document.getElementById('view-tiktok').innerHTML=h;initShortsFeedFinal5();}
60
+ function initShortsFeedFinal5(){let feed=document.getElementById('tiktok-feed');if(!feed)return;let slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{let fr=sl.querySelector('iframe');let v=sl.querySelector('video');if(idx===i){if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;if(v)v.play().catch(()=>{});shortAct(sl.dataset.kind,sl.dataset.id,'view').catch(()=>{})}else{if(fr&&fr.src)fr.src='';if(v)v.pause();}});cur=i}let t;feed.addEventListener('scroll',()=>{clearTimeout(t);t=setTimeout(()=>{let rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2,b=-1,d=1e9;slides.forEach((sl,i)=>{let dd=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(dd<d){d=dd;b=i}});if(b>=0)act(b)},130)});setTimeout(()=>act(0),250)}
61
+ function patchShortsHomeClick(){let home=document.getElementById('view-home');if(!home)return;document.querySelectorAll('#shorts-final4 .slider-item').forEach((el,i)=>{el.setAttribute('onclick',`openShortsFinal5(${i})`)});document.querySelectorAll('.slider-wrap .slider-label').forEach(label=>{if((label.textContent||'').includes('Shorts')){let wrap=label.closest('.slider-wrap');wrap?.querySelectorAll('.slider-item').forEach((el,i)=>el.setAttribute('onclick',`openShortsFinal5(${i})`));}})}
62
+ let oldOpen=window.openTikTok;window.openTikTok=function(type,startIdx){if(type==='shorts')return openShortsFinal5(startIdx||0);return oldOpen?oldOpen(type,startIdx):null;};
63
+ // Make YouTube ask AI receive title/channel from slide dataset.
64
+ let oldShortAct=window.shortAct;window.shortAct=async function(kind,id,action,text=''){let slide=document.querySelector(`.tiktok-slide[data-id="${id}"]`);let title=slide?.dataset.title||'';let channel=slide?.dataset.channel||'';let body={id,kind:kind==='yt'?'yt':kind,action,text,title,context:title?('Video Shorts YouTube từ kênh '+channel+'. Tiêu đề: '+title):''};let r=await fetch('/api/ai/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});let j=await r.json();let st=j.stats||j;let v=document.getElementById(`v-${kind}-${id}`),l=document.getElementById(`l-${kind}-${id}`);if(v&&st.views!=null)v.textContent=st.views;if(l&&st.likes!=null)l.textContent=st.likes;return st;};
65
+ setTimeout(async()=>{removeDuplicateTopicBoxes();await loadShortsFinal5();patchShortsHomeClick();},900);setInterval(()=>{removeDuplicateTopicBoxes();patchShortsHomeClick();},1800);
66
+ })();
67
+ </script>
68
+ '''
69
+
70
+ @app.get('/')
71
+ async def index_final5():
72
+ html=f4.f3.f2.f1._load_index_html();body=getattr(rt.old,'PATCH_INJECT','')+f4.f3.f2.f1.FINAL_INJECT+f4.f3.FINAL3_INJECT+f4.FINAL4_INJECT+FINAL5_INJECT
73
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
ai_runtime_final6.py ADDED
@@ -0,0 +1,1325 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final6: robust topic synthesis, stable shorts, hot topic hashtags.
2
+
3
+ This runtime intentionally overrides only the topic/shorts/root endpoints from the restored app.
4
+ """
5
+ import re, time, json, os, threading, html as html_lib
6
+ from urllib.parse import quote, urlparse, parse_qs, unquote
7
+ import requests
8
+ from bs4 import BeautifulSoup
9
+ import ai_runtime_final5 as f5
10
+ from ai_runtime_final5 import app, rt, HTMLResponse, JSONResponse, Request, Query
11
+
12
+ _PATCH={('/api/topic_post','POST'),('/api/shorts','GET'),('/api/hot_topics','GET'),('/api/topic_sources','GET'),('/','GET')}
13
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in _PATCH)]
14
+
15
+ _TOPIC_CACHE={}
16
+ _HOT_CACHE={"t":0,"d":[]}
17
+ _SHORTS_CACHE_FINAL6={"t":0,"d":[]}
18
+ _TRANSLATE_CACHE_PATH="/data/title_vi_cache.json" if os.path.isdir('/data') else "/app/data/title_vi_cache.json"
19
+ _translate_lock=threading.Lock()
20
+ YOUTUBE_HANDLES=["baodantri7941","baosuckhoedoisongboyte"]
21
+ UA={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36","Accept-Language":"vi,en;q=0.8"}
22
+ STOP_WORDS=set('và của các những một được trong với cho tại sau trước khi không người việt nam hôm nay mới nhất nóng tin tức cập nhật'.split())
23
+ TRUSTED_SITES=['vnexpress.net','dantri.com.vn','vietnamnet.vn','tuoitre.vn','thanhnien.vn','laodong.vn','vov.vn','vtv.vn','genk.vn','cafef.vn','thethaovanhoa.vn']
24
+
25
+ def clean(s):return re.sub(r"\s+"," ",html_lib.unescape(str(s or ""))).strip()
26
+ def _domain(u):
27
+ try:return urlparse(u or '').netloc.replace('www.','')
28
+ except Exception:return ''
29
+
30
+ def _load_title_cache():
31
+ try:
32
+ if os.path.exists(_TRANSLATE_CACHE_PATH):
33
+ with open(_TRANSLATE_CACHE_PATH,'r',encoding='utf-8') as f:return json.load(f)
34
+ except Exception:pass
35
+ return {}
36
+ def _save_title_cache(db):
37
+ try:
38
+ os.makedirs(os.path.dirname(_TRANSLATE_CACHE_PATH),exist_ok=True);tmp=_TRANSLATE_CACHE_PATH+'.tmp'
39
+ with open(tmp,'w',encoding='utf-8') as f:json.dump(db,f,ensure_ascii=False)
40
+ os.replace(tmp,_TRANSLATE_CACHE_PATH)
41
+ except Exception:pass
42
+
43
+ def _looks_vietnamese(s):
44
+ s=s or ''
45
+ if re.search(r'[àáạảãâầấậẩẫăằắặẳẵèéẹẻẽêềếệểễìíịỉĩòóọỏõôồốộổỗơờớợởỡùúụủũưừứựửữỳýỵỷỹđ]',s,re.I):return True
46
+ low=' '+s.lower()+' '
47
+ return any(w in low for w in [' và ',' của ',' người ',' tại ',' trong ',' với ',' không ',' được ',' công an ',' bệnh viện ',' học sinh ',' tài xế ',' bóng đá ',' tin tức ',' sức khỏe '])
48
+ def _translate_title_vi(title):
49
+ title=clean(title)
50
+ if not title or _looks_vietnamese(title):return title
51
+ with _translate_lock:
52
+ db=_load_title_cache()
53
+ if title in db:return db[title]
54
+ vi=title
55
+ try:
56
+ r=requests.get('https://translate.googleapis.com/translate_a/single',params={'client':'gtx','sl':'auto','tl':'vi','dt':'t','q':title},headers=UA,timeout=8)
57
+ if r.status_code==200:
58
+ data=r.json();vi=''.join(part[0] for part in data[0] if part and part[0]).strip() or title
59
+ except Exception:pass
60
+ vi=clean(vi)
61
+ with _translate_lock:
62
+ db=_load_title_cache();db[title]=vi;_save_title_cache(db)
63
+ return vi
64
+
65
+ # ===== Hot topics / hashtags =====
66
+ def _keywords_from_title(title):
67
+ title=clean(re.sub(r'\s+-\s+.*$','',title))
68
+ words=[w for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',title) if len(w)>2 and w.lower() not in STOP_WORDS]
69
+ phrases=[]
70
+ for n in (4,3,2):
71
+ for i in range(0,max(0,len(words)-n+1)):
72
+ ph=' '.join(words[i:i+n]).strip()
73
+ if len(ph)>=8:phrases.append(ph)
74
+ if words:phrases.append(' '.join(words[:5]))
75
+ return phrases[:4]
76
+
77
+ def _hot_topics():
78
+ now=time.time()
79
+ if _HOT_CACHE['d'] and now-_HOT_CACHE['t']<900:return _HOT_CACHE['d']
80
+ topics=[];seen=set()
81
+ feeds=[
82
+ 'https://news.google.com/rss?hl=vi&gl=VN&ceid=VN:vi',
83
+ 'https://news.google.com/rss/headlines/section/topic/NATION?hl=vi&gl=VN&ceid=VN:vi',
84
+ 'https://news.google.com/rss/headlines/section/topic/BUSINESS?hl=vi&gl=VN&ceid=VN:vi',
85
+ 'https://news.google.com/rss/headlines/section/topic/SPORTS?hl=vi&gl=VN&ceid=VN:vi',
86
+ 'https://news.google.com/rss/headlines/section/topic/TECHNOLOGY?hl=vi&gl=VN&ceid=VN:vi'
87
+ ]
88
+ for feed in feeds:
89
+ try:
90
+ r=requests.get(feed,headers=UA,timeout=10);r.encoding='utf-8'
91
+ soup=BeautifulSoup(r.text,'xml')
92
+ for it in soup.find_all('item')[:15]:
93
+ title=clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
94
+ for kw in _keywords_from_title(title):
95
+ key=kw.lower()
96
+ if key not in seen and len(kw)<=60:
97
+ seen.add(key);topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw})
98
+ if len(topics)>=24:break
99
+ if len(topics)>=24:break
100
+ except Exception:pass
101
+ if len(topics)>=24:break
102
+ for kw in ['AI trong giáo dục','World Cup 2026','kinh tế Việt Nam','biến đổi khí hậu','giá vàng','bóng đá Việt Nam','an ninh mạng','xe điện','sức khỏe tinh thần','thị trường chứng khoán']:
103
+ if kw.lower() not in seen:topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw})
104
+ _HOT_CACHE.update({'t':now,'d':topics[:24]})
105
+ return _HOT_CACHE['d']
106
+ @app.get('/api/hot_topics')
107
+ def api_hot_topics():return JSONResponse({'topics':_hot_topics()})
108
+
109
+ # ===== Topic web research =====
110
+ def _unwrap_ddg_href(href):
111
+ if not href:return ''
112
+ if href.startswith('//duckduckgo.com/l/?') or 'duckduckgo.com/l/?' in href:
113
+ qs=parse_qs(urlparse('https:'+href if href.startswith('//') else href).query)
114
+ return unquote(qs.get('uddg',[''])[0])
115
+ return href
116
+
117
+ def _ddg_search(query, limit=10):
118
+ items=[];seen=set()
119
+ try:
120
+ url='https://html.duckduckgo.com/html/?q='+quote(query)
121
+ r=requests.get(url,headers=UA,timeout=14);r.encoding='utf-8'
122
+ soup=BeautifulSoup(r.text,'lxml')
123
+ for res in soup.select('.result'):
124
+ a=res.select_one('.result__title a') or res.find('a',href=True)
125
+ if not a:continue
126
+ link=_unwrap_ddg_href(a.get('href',''));title=clean(a.get_text(' ',strip=True));snippet=clean((res.select_one('.result__snippet') or res).get_text(' ',strip=True))
127
+ if not link.startswith('http') or link in seen:continue
128
+ if any(bad in link for bad in ['duckduckgo.com','youtube.com','facebook.com','tiktok.com','twitter.com','x.com']):continue
129
+ seen.add(link);items.append({'title':title,'url':link,'source':_domain(link),'snippet':snippet})
130
+ if len(items)>=limit:break
131
+ except Exception:pass
132
+ return items
133
+
134
+ def _google_news_items(topic, limit=8):
135
+ items=[];seen=set()
136
+ try:
137
+ rss='https://news.google.com/rss/search?q='+quote(topic)+'&hl=vi&gl=VN&ceid=VN:vi'
138
+ r=requests.get(rss,headers=UA,timeout=12);r.encoding='utf-8'
139
+ soup=BeautifulSoup(r.text,'xml')
140
+ for it in soup.find_all('item')[:limit*2]:
141
+ title=clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
142
+ link=clean(it.find('link').get_text(strip=True) if it.find('link') else '')
143
+ src=clean(it.find('source').get_text(' ',strip=True) if it.find('source') else _domain(link))
144
+ if title and link and link not in seen:
145
+ seen.add(link);items.append({'title':title,'url':link,'source':src,'snippet':''})
146
+ if len(items)>=limit:break
147
+ except Exception:pass
148
+ return items
149
+
150
+ def _candidate_urls(topic):
151
+ seen=set();items=[]
152
+ queries=[topic+' tin tức Việt Nam', topic+' phân tích bối cảnh', topic+' site:vnexpress.net OR site:dantri.com.vn OR site:vietnamnet.vn']
153
+ for q in queries:
154
+ for it in _ddg_search(q,8):
155
+ if it['url'] not in seen:
156
+ seen.add(it['url']);items.append(it)
157
+ if len(items)>=12:break
158
+ for site in TRUSTED_SITES[:8]:
159
+ for it in _ddg_search(f'{topic} site:{site}',3):
160
+ if it['url'] not in seen:
161
+ seen.add(it['url']);items.append(it)
162
+ for it in _google_news_items(topic,8):
163
+ if it['url'] not in seen:
164
+ seen.add(it['url']);items.append(it)
165
+ return items[:24]
166
+
167
+ def _extract_article_text_bs(url, max_chars=9000):
168
+ try:
169
+ r=requests.get(url,headers=UA,timeout=16,allow_redirects=True)
170
+ if r.status_code>=400:return ''
171
+ r.encoding='utf-8';soup=BeautifulSoup(r.text,'lxml')
172
+ for tag in soup.find_all(['script','style','nav','footer','aside','form','noscript','iframe','svg']):tag.decompose()
173
+ candidates=[]
174
+ for sel in ['article','main','.article-content','.detail-content','.singular-content','.fck_detail','.content-detail','.entry-content','.story-body','.knc-content']:
175
+ el=soup.select_one(sel)
176
+ if el:candidates.append(el)
177
+ if not candidates:candidates=[soup.body or soup]
178
+ best=max(candidates,key=lambda el:len(el.find_all('p')) if el else 0)
179
+ ps=[]
180
+ for el in best.find_all(['p','h2','h3'],recursive=True):
181
+ t=clean(el.get_text(' ',strip=True))
182
+ if len(t)>45 and not any(x in t.lower() for x in ['đăng ký nhận tin','theo dõi chúng tôi','chuyên mục','xem thêm','tin liên quan','advertisement']):ps.append(t)
183
+ if sum(len(x) for x in ps)>max_chars:break
184
+ return '\n'.join(ps)[:max_chars]
185
+ except Exception:return ''
186
+
187
+ def _jina_read_text(url, max_chars=9000):
188
+ try:
189
+ ju='https://r.jina.ai/http://'+url
190
+ r=requests.get(ju,headers=UA,timeout=28);r.encoding='utf-8'
191
+ if r.status_code!=200 or not r.text:return ''
192
+ lines=[]
193
+ for ln in r.text.splitlines():
194
+ t=clean(ln)
195
+ if not t or t.startswith(('Title:','URL Source:','Published Time:','Markdown Content:','Image:','Description:')):continue
196
+ if len(t)>45:lines.append(t)
197
+ if sum(len(x) for x in lines)>max_chars:break
198
+ return '\n'.join(lines)[:max_chars]
199
+ except Exception:return ''
200
+
201
+ def _scrape_article_text(url, max_chars=9000):
202
+ text=_extract_article_text_bs(url,max_chars)
203
+ if len(text)<350:text=_jina_read_text(url,max_chars)
204
+ return text
205
+
206
+ def _score_relevance(topic, title, text, snippet=''):
207
+ keys=[w.lower() for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',topic) if len(w)>2 and w.lower() not in STOP_WORDS]
208
+ hay=(title+' '+snippet+' '+text[:2500]).lower()
209
+ if not keys:return 1
210
+ return sum(1 for k in keys if k in hay)
211
+
212
+ def _web_research_context(topic):
213
+ now=time.time();key=topic.lower().strip()
214
+ if key in _TOPIC_CACHE and now-_TOPIC_CACHE[key]['t']<900:return _TOPIC_CACHE[key]['d']
215
+ items=_candidate_urls(topic)
216
+ crawled=[]
217
+ for it in items:
218
+ text=_scrape_article_text(it['url'],9000)
219
+ rel=_score_relevance(topic,it.get('title',''),text,it.get('snippet',''))
220
+ if text and len(text)>300 and rel>0:
221
+ crawled.append({**it,'text':text,'rel':rel})
222
+ elif it.get('snippet') and rel>0:
223
+ crawled.append({**it,'text':it['snippet'],'rel':rel,'snippet_only':True})
224
+ crawled=sorted(crawled,key=lambda x:(x.get('rel',0),len(x.get('text',''))),reverse=True)[:6]
225
+ blocks=[];sources=[]
226
+ for it in crawled:
227
+ label='ĐOẠN MÔ TẢ TỪ KẾT QUẢ TÌM KIẾM' if it.get('snippet_only') else 'NỘI DUNG BÀI VIẾT ĐÃ CRAWL'
228
+ blocks.append(f"NGUỒN: {it['source']}\nTIÊU ĐỀ: {it['title']}\n{label}:\n{it['text'][:8500]}")
229
+ sources.append({'title':it['title'],'url':it['url'],'via':it['source']})
230
+ data={'context':'\n\n---\n\n'.join(blocks),'sources':sources[:8],'count':len(blocks)}
231
+ _TOPIC_CACHE[key]={'t':now,'d':data}
232
+ return data
233
+
234
+ def _topic_image(topic):
235
+ try:return f5.base.pollinations_image_url(topic)
236
+ except Exception:return 'https://image.pollinations.ai/prompt/'+quote('Vietnamese editorial illustration, '+topic)+'?width=1024&height=576&nologo=true'
237
+
238
+ @app.get('/api/topic_sources')
239
+ def api_topic_sources(topic:str=Query(...)):
240
+ data=_web_research_context(clean(topic))
241
+ return JSONResponse({'count':data.get('count',0),'sources':data.get('sources',[]),'has_context':bool(data.get('context'))})
242
+
243
+ @app.post('/api/topic_post')
244
+ async def topic_post_synthesis(request:Request):
245
+ body=await request.json();topic=clean(body.get('topic',''))
246
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
247
+ img=_topic_image(topic);research=_web_research_context(topic);context=research.get('context','');sources=research.get('sources',[])
248
+ if not context or research.get('count',0)==0:
249
+ return JSONResponse({'error':'Không tìm/crawl được đủ nội dung về chủ đề này. Hãy thử chủ đề cụ thể hơn hoặc dùng hashtag gợi ý.'},status_code=422)
250
+ prompt=f"""Bạn là biên tập viên VNEWS. Người dùng chọn chủ đề: "{topic}".
251
+
252
+ Dưới đây là NỘI DUNG các bài viết/đoạn mô tả đã crawl từ internet. Hãy đọc hiểu và TỔNG HỢP thành MỘT BÀI VIẾT HOÀN CHỈNH. Tuyệt đối không bê nguyên văn, không xếp danh sách tiêu đề thành bài viết, không viết kiểu trả lời chat.
253
+
254
+ DỮ LIỆU CRAWL:
255
+ {context[:30000]}
256
+
257
+ Yêu cầu bắt buộc:
258
+ - Viết bằng tiếng Việt, văn phong báo điện tử/tạp chí.
259
+ - Tiêu đề mới, rõ, hấp dẫn.
260
+ - Sapo 2-3 câu nêu vấn đề chính.
261
+ - 5-8 đoạn nội dung tổng hợp: bối cảnh, diễn biến/khái niệm, phân tích, tác động, điểm cần lưu ý.
262
+ - Dùng thông tin từ nội dung đã crawl để tổng hợp ý; nếu chỉ có mô tả tìm kiếm thì viết thận trọng.
263
+ - KHÔNG liệt kê các tiêu đề nguồn. KHÔNG mở đầu bằng "Dưới đây là" hay "Tôi sẽ".
264
+ - Cuối bài thêm mục "Nguồn tham khảo" gồm tên nguồn ngắn gọn.
265
+ """
266
+ text=await f5.base.qwen_generate(prompt,image_url=img,max_tokens=2800)
267
+ if not text or len(text)<500:
268
+ parts=[]
269
+ for block in context.split('---'):
270
+ body=block.split('NỘI DUNG BÀI VIẾT ĐÃ CRAWL:')[-1].split('ĐOẠN MÔ TẢ TỪ KẾT QUẢ TÌM KIẾM:')[-1].strip()
271
+ if len(body)>120:parts.append(body)
272
+ joined='\n\n'.join(parts)[:8500]
273
+ text=(f"{topic}: những điểm chính cần biết\n\n{topic} đang thu hút sự chú ý vì liên quan đến nhiều khía cạnh thực tế. Tổng hợp từ các nội dung thu thập được, có thể nhìn vấn đề qua bối cảnh, tác động và những điểm cần theo dõi.\n\n"+joined+"\n\nNguồn tham khảo: "+', '.join(sorted({s.get('via','') for s in sources if s.get('via')})))
274
+ post=f5.base.make_post(topic,text,img,'','topic_web_synthesis',sources=[s for s in sources if s.get('url')]);post['images']=[img]
275
+ posts=f5.base._load_ai_wall();posts.insert(0,post);f5.base._save_ai_wall(posts)
276
+ return JSONResponse({'post':post})
277
+
278
+ # ===== Stable newest Dantri/SKDS Shorts =====
279
+ def _yt_ytdlp(handle,count=30):
280
+ try:
281
+ import yt_dlp
282
+ urls=[f'https://www.youtube.com/@{handle}/shorts',f'https://www.youtube.com/@{handle}/videos']
283
+ out=[];seen=set();opts={'quiet':True,'extract_flat':True,'skip_download':True,'playlistend':count,'ignoreerrors':True,'no_warnings':True,'extractor_args':{'youtube':{'player_client':['web']}}}
284
+ for url in urls:
285
+ with yt_dlp.YoutubeDL(opts) as ydl:info=ydl.extract_info(url,download=False)
286
+ for e in (info or {}).get('entries') or []:
287
+ vid=e.get('id') or ''
288
+ if not re.match(r'^[A-Za-z0-9_-]{11}$',vid) or vid in seen:continue
289
+ title=e.get('title') or 'YouTube Short'
290
+ if url.endswith('/videos') and '#short' not in title.lower() and 'shorts' not in title.lower():continue
291
+ seen.add(vid);out.append({'title':title,'link':'https://www.youtube.com/watch?v='+vid,'img':'https://i.ytimg.com/vi/'+vid+'/hqdefault.jpg','source':'yt','id':vid,'channel':handle})
292
+ if len(out)>=count:break
293
+ if len(out)>=count:break
294
+ return out
295
+ except Exception:return []
296
+ def _yt_html(handle,count=30):
297
+ out=[];seen=set()
298
+ for suffix in ['shorts','videos']:
299
+ try:
300
+ r=requests.get(f'https://www.youtube.com/@{handle}/{suffix}',headers=UA,timeout=15);html=r.text
301
+ for m in re.finditer(r'"videoId":"([A-Za-z0-9_-]{11})"',html):
302
+ vid=m.group(1)
303
+ if vid in seen:continue
304
+ snip=html[max(0,m.start()-1200):m.start()+2200];title='YouTube Short'
305
+ mt=re.search(r'"title":\{"runs":\[\{"text":"([^"]+)"',snip) or re.search(r'"accessibilityText":"([^"]+)"',snip)
306
+ if mt:title=clean(mt.group(1).replace('\\n',' '))
307
+ if suffix=='videos' and '#short' not in title.lower() and 'shorts' not in title.lower():continue
308
+ seen.add(vid);out.append({'title':title,'link':'https://www.youtube.com/watch?v='+vid,'img':'https://i.ytimg.com/vi/'+vid+'/hqdefault.jpg','source':'yt','id':vid,'channel':handle})
309
+ if len(out)>=count:break
310
+ except Exception:pass
311
+ if len(out)>=count:break
312
+ return out[:count]
313
+ def _fallback_shorts():
314
+ try:return f5._fallback_shorts()
315
+ except Exception:return []
316
+ @app.get('/api/shorts')
317
+ def api_shorts_final6(refresh:int=Query(default=0)):
318
+ now=time.time()
319
+ if not refresh and _SHORTS_CACHE_FINAL6['d'] and now-_SHORTS_CACHE_FINAL6['t']<600:return JSONResponse(_SHORTS_CACHE_FINAL6['d'])
320
+ raw=[]
321
+ for h in YOUTUBE_HANDLES:raw.extend(_yt_ytdlp(h,30) or _yt_html(h,30))
322
+ raw.extend(_fallback_shorts())
323
+ seen=set();out=[]
324
+ for v in raw:
325
+ vid=v.get('id') or ''
326
+ if not vid:
327
+ m=re.search(r'(?:v=|shorts/|youtu\.be/)([A-Za-z0-9_-]{11})',v.get('link',''));vid=m.group(1) if m else ''
328
+ title=_translate_title_vi(v.get('title') or 'YouTube Short');key=vid or re.sub(r'\W+','',title.lower())[:80]
329
+ if not key or key in seen:continue
330
+ seen.add(key);item=dict(v);item['id']=vid;item['title']=title
331
+ if vid:item['link']='https://www.youtube.com/watch?v='+vid;item['img']='https://i.ytimg.com/vi/'+vid+'/hqdefault.jpg'
332
+ item['source']='yt';out.append(item)
333
+ if len(out)>=40:break
334
+ _SHORTS_CACHE_FINAL6.update({'t':now,'d':out})
335
+ return JSONResponse(out)
336
+
337
+ FINAL6_INJECT=r'''
338
+ <style>
339
+ #ai-topic-input-final3,.topic-final3,#ai-topic-input-final4,.topic-final4{display:none!important}.topic-final5{display:flex!important}.ai-wall-topic-live{margin:6px 4px;background:#1a1a1a;border:1px solid #2a2a2a;border-radius:8px;overflow:hidden}.hot-topic-row{display:flex;gap:6px;overflow-x:auto;padding:4px 0}.hot-chip{flex:0 0 auto;background:#222;border:1px solid #333;color:#ddd;border-radius:16px;padding:5px 10px;font-size:11px;cursor:pointer}.hot-chip:active{transform:scale(.96)}.topic-source-note{font-size:10px;color:#777;margin-top:4px;line-height:1.3}
340
+ </style>
341
+ <script>
342
+ (function(){
343
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
344
+ let liveTopicWall=[];
345
+ async function ensureHotTopics(){let inp=document.getElementById('ai-topic-input-final5');if(!inp||document.getElementById('hot-topic-row-final6'))return;let row=document.createElement('div');row.id='hot-topic-row-final6';row.className='hot-topic-row';row.innerHTML='<span style="color:#777;font-size:11px;padding:5px 0">Đang tải từ khóa nóng...</span>';inp.insertAdjacentElement('afterend',row);let note=document.createElement('div');note.id='topic-source-note';note.className='topic-source-note';note.textContent='AI sẽ tìm nhiều nguồn, crawl nội dung bài viết rồi tổng hợp thành bài mới.';row.insertAdjacentElement('afterend',note);let j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));let topics=j.topics||[];row.innerHTML=topics.slice(0,18).map(t=>`<button class="hot-chip" onclick="document.getElementById('ai-topic-input-final5').value='${esc(t.topic).replace(/'/g,'\\\'')}';document.getElementById('ai-topic-input-final5').focus();">${esc(t.label)}</button>`).join('')||'';}
346
+ async function ensureNewsShortsHome(){if(!document.getElementById('view-home')?.classList.contains('active'))return;let labels=[...document.querySelectorAll('.slider-wrap .slider-label')];let wraps=labels.filter(l=>/shorts|short /i.test(l.textContent||'')&&!/short ai/i.test(l.textContent||'')).map(l=>l.closest('.slider-wrap')).filter(Boolean);wraps.forEach((w,i)=>{if(i>0)w.remove();});let w=wraps[0];if(w){let seen=new Set();[...w.querySelectorAll('.slider-item')].forEach(it=>{let img=it.querySelector('img')?.src||'';let tt=(it.querySelector('.slider-title')?.textContent||'').trim().toLowerCase();let k=img||tt;if(k&&seen.has(k))it.remove();else if(k)seen.add(k);});if(w.querySelectorAll('.slider-item').length>=6)return;w.remove();}let sh=await fetch('/api/shorts?refresh=1').then(r=>r.json()).catch(()=>[]);if(!sh.length)return;let wrap=document.createElement('div');wrap.className='slider-wrap';wrap.id='shorts-final6-stable';let h='<div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS</span><span class="slider-note">Mới nhất</span></div><div class="slider-track">';sh.slice(0,30).forEach((a,i)=>{h+=`<div class="slider-item shorts-item" onclick="openTikTok('shorts',${i})"><div class="slider-thumb shorts-thumb">${a.img?`<img src="${esc(a.img)}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div>';wrap.innerHTML=h;let comp=document.querySelector('.ai-compose')||document.getElementById('view-home').firstChild;if(comp)comp.after(wrap);else document.getElementById('view-home').prepend(wrap);}
347
+ function renderLiveTopicWall(){let home=document.getElementById('view-home');if(!home||!liveTopicWall.length)return;document.getElementById('ai-wall-topic-live')?.remove();let wrap=document.createElement('div');wrap.id='ai-wall-topic-live';wrap.className='ai-wall-topic-live';let h='<div class="slider-header"><span class="slider-label">🧱 Tường AI mới</span><span class="slider-note">Tổng hợp từ web</span></div><div class="slider-track">';liveTopicWall.slice(0,20).forEach((p,i)=>{h+=`<div class="wall-item"><div class="wall-thumb">${p.img?`<img src="${esc(p.img)}">`:''}</div><div class="wall-title">${esc(p.title)}</div><div class="wall-text">${esc(p.text)}</div><div class="wall-actions"><button class="primary" onclick="readLiveTopicWall(${i})">Xem</button></div></div>`});h+='</div>';wrap.innerHTML=h;let comp=document.querySelector('.ai-compose');if(comp)comp.after(wrap);else home.prepend(wrap);}
348
+ window.readLiveTopicWall=function(i){let p=liveTopicWall[i];if(!p)return;showView('view-article');let imgs=(p.images||[]).filter(Boolean);let gal=imgs.length?'<div class="ai-wall-gallery">'+imgs.slice(0,12).map(u=>`<img src="${esc(u)}" loading="lazy">`).join('')+'</div>':(p.img?`<img class="article-img" src="${esc(p.img)}">`:'');document.getElementById('view-article').innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${gal}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p><div class="article-actions"><button onclick="shareAI?shareAI(${JSON.stringify(p).replace(/"/g,'&quot;')},false):navigator.clipboard.writeText(location.href)">📤 Chia sẻ</button></div></div>`;window.scrollTo(0,0)};
349
+ window.createTopicPostFinal5=async function(){let inp=document.getElementById('ai-topic-input-final5');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');let btn=document.getElementById('ai-topic-btn-final5');if(btn){btn.disabled=true;btn.textContent='Đang tìm nguồn...'}try{let src=await fetch('/api/topic_sources?topic='+encodeURIComponent(topic)).then(r=>r.json()).catch(()=>null);if(btn&&src)btn.textContent='Đã tìm '+(src.count||0)+' nguồn, đang tổng hợp...';let r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');liveTopicWall.unshift(j.post);if(inp)inp.value='';renderLiveTopicWall();readLiveTopicWall(0);alert('Đã tạo bài tổng hợp từ nội dung web và đăng lên Tường AI.');}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='✨ Tạo bài tổng hợp từ web bằng Qwen'}}};
350
+ setInterval(()=>{document.querySelectorAll('#ai-topic-input-final3,.topic-final3,#ai-topic-input-final4,.topic-final4').forEach(e=>(e.closest('.topic-final3,.topic-final4,.ai-compose-row')||e).remove());let b=document.getElementById('ai-topic-btn-final5');if(b){b.style.display='block';b.textContent='✨ Tạo b��i tổng hợp từ web bằng Qwen';}ensureHotTopics();ensureNewsShortsHome();},1200);setTimeout(()=>{ensureHotTopics();ensureNewsShortsHome();},1200);
351
+ })();
352
+ </script>
353
+ '''
354
+
355
+ @app.get('/')
356
+ async def index_final6():
357
+ html=f5.f4.f3.f2.f1._load_index_html()
358
+ body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT+FINAL6_INJECT
359
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
360
+
361
+
362
+ # ===== FINAL6B: Vietnam hot hashtags + reliable VN RSS/source retrieval =====
363
+ VN_RSS_FEEDS = [
364
+ ('VnExpress Thời sự','https://vnexpress.net/rss/thoi-su.rss'),
365
+ ('VnExpress Thế giới','https://vnexpress.net/rss/the-gioi.rss'),
366
+ ('VnExpress Kinh doanh','https://vnexpress.net/rss/kinh-doanh.rss'),
367
+ ('VnExpress Công nghệ','https://vnexpress.net/rss/so-hoa.rss'),
368
+ ('VnExpress Thể thao','https://vnexpress.net/rss/the-thao.rss'),
369
+ ('VnExpress Giải trí','https://vnexpress.net/rss/giai-tri.rss'),
370
+ ('VnExpress Sức khỏe','https://vnexpress.net/rss/suc-khoe.rss'),
371
+ ('VnExpress Giáo dục','https://vnexpress.net/rss/giao-duc.rss'),
372
+ ('Dân trí Xã hội','https://dantri.com.vn/rss/xa-hoi.rss'),
373
+ ('Dân trí Thế giới','https://dantri.com.vn/rss/the-gioi.rss'),
374
+ ('Dân trí Kinh doanh','https://dantri.com.vn/rss/kinh-doanh.rss'),
375
+ ('Dân trí Sức khỏe','https://dantri.com.vn/rss/suc-khoe.rss'),
376
+ ('Dân trí Thể thao','https://dantri.com.vn/rss/the-thao.rss'),
377
+ ('Dân trí Công nghệ','https://dantri.com.vn/rss/suc-manh-so.rss'),
378
+ ('Vietnamnet Thời sự','https://vietnamnet.vn/thoi-su.rss'),
379
+ ('Vietnamnet Kinh doanh','https://vietnamnet.vn/kinh-doanh.rss'),
380
+ ('Vietnamnet Công nghệ','https://vietnamnet.vn/cong-nghe.rss'),
381
+ ('Vietnamnet Thể thao','https://vietnamnet.vn/the-thao.rss'),
382
+ ]
383
+
384
+ def _fetch_rss_items(feed_name, feed_url, max_items=15):
385
+ items=[]
386
+ try:
387
+ r=requests.get(feed_url,headers=UA,timeout=10);r.encoding='utf-8'
388
+ soup=BeautifulSoup(r.text,'xml')
389
+ for it in soup.find_all('item')[:max_items]:
390
+ title=clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
391
+ link=clean(it.find('link').get_text(strip=True) if it.find('link') else '')
392
+ desc=it.find('description').get_text(' ',strip=True) if it.find('description') else ''
393
+ desc_txt=clean(BeautifulSoup(desc,'lxml').get_text(' ',strip=True))
394
+ if title and link:
395
+ items.append({'title':title,'url':link,'source':feed_name,'snippet':desc_txt})
396
+ except Exception:pass
397
+ return items
398
+
399
+ def _vn_rss_pool():
400
+ now=time.time();key='vn_rss_pool'
401
+ if key in _TOPIC_CACHE and now-_TOPIC_CACHE[key]['t']<600:return _TOPIC_CACHE[key]['d']
402
+ pool=[];seen=set()
403
+ for name,url in VN_RSS_FEEDS:
404
+ for it in _fetch_rss_items(name,url,12):
405
+ if it['url'] not in seen:
406
+ seen.add(it['url']);pool.append(it)
407
+ _TOPIC_CACHE[key]={'t':now,'d':pool}
408
+ return pool
409
+
410
+ def _topic_tokens(topic):
411
+ toks=[w.lower() for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',topic or '') if len(w)>1]
412
+ return [t for t in toks if t not in STOP_WORDS]
413
+
414
+ def _score_topic_item(topic,item):
415
+ toks=_topic_tokens(topic)
416
+ hay=(item.get('title','')+' '+item.get('snippet','')+' '+item.get('source','')).lower()
417
+ if not toks:return 0
418
+ score=0
419
+ for t in toks:
420
+ if t in hay:score+=2 if len(t)>3 else 1
421
+ phrase=topic.lower().strip()
422
+ if phrase and phrase in hay:score+=8
423
+ return score
424
+
425
+ # Override: hashtags must be Việt Nam-focused, using VN news RSS directly.
426
+ def _hot_topics():
427
+ now=time.time()
428
+ if _HOT_CACHE['d'] and now-_HOT_CACHE['t']<600:return _HOT_CACHE['d']
429
+ pool=_vn_rss_pool()
430
+ freq={};display={}
431
+ for it in pool[:180]:
432
+ title=re.sub(r'\s+-\s+.*$','',it.get('title',''))
433
+ # Extract compact Vietnamese hot phrases from current VN headlines.
434
+ kws=[]
435
+ # quoted/name phrases first
436
+ for m in re.findall(r'([A-ZĐÀ-Ỹ][A-Za-zÀ-ỹ0-9]+(?:\s+[A-ZĐÀ-ỸA-Za-zÀ-ỹ0-9][A-Za-zÀ-ỹ0-9]+){1,4})',title):
437
+ if len(m)>=6:kws.append(m)
438
+ kws += _keywords_from_title(title)
439
+ for kw in kws[:5]:
440
+ kw=clean(kw)
441
+ words=[w for w in kw.split() if w.lower() not in STOP_WORDS]
442
+ if len(words)<2:continue
443
+ kw=' '.join(words[:5])
444
+ if len(kw)<6 or len(kw)>55:continue
445
+ key=kw.lower()
446
+ freq[key]=freq.get(key,0)+1
447
+ display[key]=kw
448
+ ranked=sorted(freq.items(),key=lambda x:x[1],reverse=True)
449
+ topics=[];seen=set()
450
+ for key,_ in ranked:
451
+ kw=display[key]
452
+ if key in seen:continue
453
+ seen.add(key)
454
+ label='#'+re.sub(r'\s+','',kw.title())
455
+ topics.append({'label':label,'topic':kw})
456
+ if len(topics)>=24:break
457
+ # VN fallback, not generic global.
458
+ for kw in ['Giá vàng trong nước','Bão và mưa lũ','Bóng đá Việt Nam','Kinh tế Việt Nam','AI tại Việt Nam','Giá xăng dầu','Thị trường chứng khoán Việt Nam','Tuyển Việt Nam','Sức khỏe cộng đồng','An ninh mạng Việt Nam']:
459
+ if kw.lower() not in seen:topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw})
460
+ _HOT_CACHE.update({'t':now,'d':topics[:24]})
461
+ return _HOT_CACHE['d']
462
+
463
+ def _candidate_urls(topic):
464
+ seen=set();items=[]
465
+ # 1) VN RSS pool relevance is most reliable and has direct URLs.
466
+ scored=[]
467
+ for it in _vn_rss_pool():
468
+ sc=_score_topic_item(topic,it)
469
+ if sc>0:scored.append((sc,it))
470
+ for sc,it in sorted(scored,key=lambda x:x[0],reverse=True)[:12]:
471
+ if it['url'] not in seen:
472
+ seen.add(it['url']);items.append(it)
473
+ # 2) Search trusted web if RSS not enough.
474
+ queries=[topic+' Việt Nam tin tức',topic+' phân tích Việt Nam',topic+' mới nhất']
475
+ for q in queries:
476
+ for it in _ddg_search(q,8):
477
+ if it['url'] not in seen:
478
+ seen.add(it['url']);items.append(it)
479
+ if len(items)>=14:break
480
+ # 3) Google News as supplemental titles/direct links.
481
+ for it in _google_news_items(topic,10):
482
+ if it['url'] not in seen:
483
+ seen.add(it['url']);items.append(it)
484
+ return items[:24]
485
+
486
+ def _web_research_context(topic):
487
+ now=time.time();key='ctx2:'+topic.lower().strip()
488
+ if key in _TOPIC_CACHE and now-_TOPIC_CACHE[key]['t']<900:return _TOPIC_CACHE[key]['d']
489
+ items=_candidate_urls(topic)
490
+ crawled=[]
491
+ for it in items:
492
+ text=_scrape_article_text(it['url'],9000)
493
+ rel=_score_relevance(topic,it.get('title',''),text,it.get('snippet','')) or _score_topic_item(topic,it)
494
+ # If RSS item has good snippet, keep it even when full text blocks.
495
+ if text and len(text)>300 and rel>0:
496
+ crawled.append({**it,'text':text,'rel':rel})
497
+ elif it.get('snippet') and len(it['snippet'])>120 and rel>0:
498
+ crawled.append({**it,'text':it['snippet'],'rel':rel,'snippet_only':True})
499
+ crawled=sorted(crawled,key=lambda x:(x.get('rel',0),len(x.get('text',''))),reverse=True)[:7]
500
+ blocks=[];sources=[]
501
+ for it in crawled:
502
+ label='ĐOẠN MÔ TẢ TỪ RSS/TÌM KIẾM' if it.get('snippet_only') else 'NỘI DUNG BÀI VIẾT ĐÃ CRAWL'
503
+ blocks.append(f"NGUỒN: {it['source']}\nTIÊU ĐỀ: {it['title']}\n{label}:\n{it['text'][:8500]}")
504
+ sources.append({'title':it['title'],'url':it['url'],'via':it['source']})
505
+ data={'context':'\n\n---\n\n'.join(blocks),'sources':sources[:8],'count':len(blocks)}
506
+ _TOPIC_CACHE[key]={'t':now,'d':data}
507
+ return data
508
+
509
+
510
+ # ===== FINAL6C: FAST topic generation (RSS cache first, no slow full-page crawling) =====
511
+ import asyncio
512
+ _FAST_TOPIC_CACHE={}
513
+ FAST_RSS_FEEDS=[
514
+ ('VnExpress','https://vnexpress.net/rss/tin-moi-nhat.rss'),
515
+ ('VnExpress Thời sự','https://vnexpress.net/rss/thoi-su.rss'),
516
+ ('VnExpress Thế giới','https://vnexpress.net/rss/the-gioi.rss'),
517
+ ('VnExpress Kinh doanh','https://vnexpress.net/rss/kinh-doanh.rss'),
518
+ ('VnExpress Công nghệ','https://vnexpress.net/rss/so-hoa.rss'),
519
+ ('VnExpress Thể thao','https://vnexpress.net/rss/the-thao.rss'),
520
+ ('Dân trí','https://dantri.com.vn/rss/home.rss'),
521
+ ('Dân trí Xã hội','https://dantri.com.vn/rss/xa-hoi.rss'),
522
+ ('Dân trí Kinh doanh','https://dantri.com.vn/rss/kinh-doanh.rss'),
523
+ ('Dân trí Thể thao','https://dantri.com.vn/rss/the-thao.rss'),
524
+ ('Dân trí Công nghệ','https://dantri.com.vn/rss/suc-manh-so.rss'),
525
+ ('Vietnamnet','https://vietnamnet.vn/rss/tin-moi-nhat.rss'),
526
+ ('Vietnamnet Thời sự','https://vietnamnet.vn/thoi-su.rss'),
527
+ ('Vietnamnet Kinh doanh','https://vietnamnet.vn/kinh-doanh.rss'),
528
+ ('Vietnamnet Công nghệ','https://vietnamnet.vn/cong-nghe.rss'),
529
+ ('Vietnamnet Thể thao','https://vietnamnet.vn/the-thao.rss'),
530
+ ]
531
+
532
+ def _fast_fetch_rss(feed_name, feed_url, max_items=20):
533
+ items=[]
534
+ try:
535
+ r=requests.get(feed_url,headers=UA,timeout=6);r.encoding='utf-8'
536
+ soup=BeautifulSoup(r.text,'xml')
537
+ for it in soup.find_all('item')[:max_items]:
538
+ title=clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
539
+ link=clean(it.find('link').get_text(strip=True) if it.find('link') else '')
540
+ desc_raw=it.find('description').get_text(' ',strip=True) if it.find('description') else ''
541
+ desc=clean(BeautifulSoup(desc_raw,'lxml').get_text(' ',strip=True))
542
+ if title and link:
543
+ items.append({'title':title,'url':link,'source':feed_name,'snippet':desc})
544
+ except Exception:pass
545
+ return items
546
+
547
+ def _fast_rss_pool():
548
+ now=time.time();key='fast_rss_pool'
549
+ if key in _FAST_TOPIC_CACHE and now-_FAST_TOPIC_CACHE[key]['t']<600:return _FAST_TOPIC_CACHE[key]['d']
550
+ pool=[];seen=set()
551
+ # Sequential with short timeouts is predictable; RSS is small.
552
+ for name,url in FAST_RSS_FEEDS:
553
+ for it in _fast_fetch_rss(name,url,16):
554
+ if it['url'] not in seen:
555
+ seen.add(it['url']);pool.append(it)
556
+ _FAST_TOPIC_CACHE[key]={'t':now,'d':pool}
557
+ return pool
558
+
559
+ def _fast_topic_tokens(topic):
560
+ toks=[w.lower() for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',topic or '') if len(w)>1]
561
+ return [t for t in toks if t not in STOP_WORDS]
562
+
563
+ def _fast_score(topic,item):
564
+ toks=_fast_topic_tokens(topic)
565
+ hay=(item.get('title','')+' '+item.get('snippet','')+' '+item.get('source','')).lower()
566
+ if not toks:return 0
567
+ score=0
568
+ for t in toks:
569
+ if t in hay:score+=3 if len(t)>3 else 1
570
+ phrase=topic.lower().strip()
571
+ if phrase and phrase in hay:score+=12
572
+ return score
573
+
574
+ def _fast_sources(topic, limit=8):
575
+ pool=_fast_rss_pool()
576
+ scored=[]
577
+ for it in pool:
578
+ sc=_fast_score(topic,it)
579
+ if sc>0:scored.append((sc,it))
580
+ scored=sorted(scored,key=lambda x:(x[0],len(x[1].get('snippet',''))),reverse=True)
581
+ out=[];seen=set()
582
+ for sc,it in scored:
583
+ if it['url'] in seen:continue
584
+ seen.add(it['url']);out.append({**it,'score':sc})
585
+ if len(out)>=limit:break
586
+ # If topic too narrow and no match, use top latest from VN RSS as weak context instead of slow crawling.
587
+ if not out:
588
+ out=pool[:min(limit,8)]
589
+ return out
590
+
591
+ def _fast_context(topic):
592
+ now=time.time();key='fast_ctx:'+topic.lower().strip()
593
+ if key in _FAST_TOPIC_CACHE and now-_FAST_TOPIC_CACHE[key]['t']<600:return _FAST_TOPIC_CACHE[key]['d']
594
+ sources=_fast_sources(topic,8)
595
+ blocks=[];src=[]
596
+ for it in sources:
597
+ text=(it.get('snippet') or '').strip()
598
+ # Use title + RSS description only: fast and reliable.
599
+ blocks.append(f"NGUỒN: {it.get('source','')}\nTIÊU ĐỀ: {it.get('title','')}\nTÓM TẮT RSS:\n{text}")
600
+ src.append({'title':it.get('title',''),'url':it.get('url',''),'via':it.get('source','')})
601
+ data={'context':'\n\n---\n\n'.join(blocks),'sources':src,'count':len(blocks)}
602
+ _FAST_TOPIC_CACHE[key]={'t':now,'d':data}
603
+ return data
604
+
605
+ def _fallback_fast_article(topic, sources):
606
+ lines=[]
607
+ for s in sources[:7]:
608
+ title=s.get('title','')
609
+ if title:lines.append(title)
610
+ body='\n'.join('• '+x for x in lines[:7])
611
+ vias=', '.join(sorted({s.get('via','') for s in sources if s.get('via')}))
612
+ return (f"{topic}: những điểm đáng chú ý\n\n"
613
+ f"{topic} đang là chủ đề được quan tâm trong dòng tin tức hiện nay. Dựa trên các nguồn tin mới nhất, có thể tổng hợp nhanh một số điểm nổi bật để người đọc nắm bối cảnh và theo dõi tiếp diễn biến.\n\n"
614
+ f"Các nguồn tin liên quan cho thấy chủ đề này gắn với những diễn biến sau:\n{body}\n\n"
615
+ f"Nhìn chung, đây là vấn đề cần được theo dõi theo nhiều góc độ: bối cảnh, tác động thực tế, phản ứng của các bên liên quan và những thông tin cập nhật tiếp theo. Người đọc nên đối chiếu thêm các nguồn chính thống khi cần quyết định hoặc đánh giá chi tiết.\n\n"
616
+ f"Nguồn tham khảo: {vias}")
617
+
618
+ # Remove previous slow topic routes and register fast versions last.
619
+ app.router.routes=[r for r in app.router.routes if not any(getattr(r,'path',None)==p and m in getattr(r,'methods',set()) for p,m in {('/api/topic_post','POST'),('/api/topic_sources','GET')})]
620
+
621
+ @app.get('/api/topic_sources')
622
+ def api_topic_sources_fast(topic:str=Query(...)):
623
+ data=_fast_context(clean(topic))
624
+ return JSONResponse({'count':data.get('count',0),'sources':data.get('sources',[]),'has_context':bool(data.get('context')),'mode':'fast_rss'})
625
+
626
+ @app.post('/api/topic_post')
627
+ async def topic_post_fast(request:Request):
628
+ body=await request.json();topic=clean(body.get('topic',''))
629
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
630
+ img=_topic_image(topic)
631
+ research=_fast_context(topic);context=research.get('context','');sources=research.get('sources',[])
632
+ prompt=f"""Bạn là biên tập viên VNEWS. Hãy viết MỘT BÀI VIẾT HOÀN CHỈNH bằng tiếng Việt về chủ đề: {topic}
633
+
634
+ Dữ liệu nhanh từ RSS nguồn Việt Nam:
635
+ {context[:12000]}
636
+
637
+ Yêu cầu:
638
+ - Không liệt kê tiêu đề nguồn thành bài viết.
639
+ - Tổng hợp thành bài báo/tạp chí hoàn chỉnh.
640
+ - Có tiêu đề mới, sapo 2-3 câu, 4-6 đoạn phân tích/bối cảnh/tác động.
641
+ - Diễn đạt lại, không sao chép nguyên văn.
642
+ - Nếu dữ liệu ít, viết thận trọng và nêu các điểm cần theo dõi.
643
+ - Cuối bài có mục Nguồn tham khảo.
644
+ """
645
+ text=None
646
+ try:
647
+ text=await asyncio.wait_for(f5.base.qwen_generate(prompt,image_url=img,max_tokens=1300),timeout=28)
648
+ except Exception:
649
+ text=None
650
+ if not text or len(text)<350:
651
+ text=_fallback_fast_article(topic,sources)
652
+ post=f5.base.make_post(topic,text,img,'','topic_fast_rss',sources=[s for s in sources if s.get('url')])
653
+ post['images']=[img]
654
+ posts=f5.base._load_ai_wall();posts.insert(0,post);f5.base._save_ai_wall(posts)
655
+ return JSONResponse({'post':post,'mode':'fast_rss','sources_count':len(sources)})
656
+
657
+
658
+ # ===== FINAL6D: FAST HOME LOAD =====
659
+ _FAST_HOME_CACHE={"t":0,"d":[]}
660
+ _FAST_DT_CACHE={"t":0,"d":[]}
661
+ _FAST_VNEGO_CACHE={"t":0,"d":[]}
662
+ _FAST_HL_CACHE={"t":0,"d":[]}
663
+
664
+ def _rss_articles_fast(feed_url, group, source='vne', limit=6):
665
+ out=[]
666
+ try:
667
+ r=requests.get(feed_url,headers=UA,timeout=4);r.encoding='utf-8'
668
+ soup=BeautifulSoup(r.text,'xml')
669
+ for it in soup.find_all('item')[:limit*2]:
670
+ title=clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
671
+ link=clean(it.find('link').get_text(strip=True) if it.find('link') else '')
672
+ desc_raw=it.find('description').get_text(' ',strip=True) if it.find('description') else ''
673
+ ds=BeautifulSoup(desc_raw,'lxml')
674
+ im=ds.find('img'); img=im.get('src','') if im else ''
675
+ desc=clean(ds.get_text(' ',strip=True))[:160]
676
+ if title and link:
677
+ out.append({'title':title,'link':link,'img':img,'summary':desc,'source':source,'group':group})
678
+ if len(out)>=limit:break
679
+ except Exception:pass
680
+ return out
681
+
682
+ def _fast_homepage():
683
+ now=time.time()
684
+ if _FAST_HOME_CACHE['d'] and now-_FAST_HOME_CACHE['t']<600:return _FAST_HOME_CACHE['d']
685
+ feeds=[('Thời Sự','https://vnexpress.net/rss/thoi-su.rss'),('Thế Giới','https://vnexpress.net/rss/the-gioi.rss'),('Kinh Doanh','https://vnexpress.net/rss/kinh-doanh.rss'),('Công Nghệ','https://vnexpress.net/rss/so-hoa.rss'),('Thể Thao','https://vnexpress.net/rss/the-thao.rss'),('Giải Trí','https://vnexpress.net/rss/giai-tri.rss'),('Sức Khỏe','https://vnexpress.net/rss/suc-khoe.rss'),('Giáo Dục','https://vnexpress.net/rss/giao-duc.rss'),('Pháp Luật','https://vnexpress.net/rss/phap-luat.rss'),('Du Lịch','https://vnexpress.net/rss/du-lich.rss')]
686
+ arts=[]
687
+ try:
688
+ from concurrent.futures import ThreadPoolExecutor, as_completed
689
+ with ThreadPoolExecutor(max_workers=6) as ex:
690
+ futs=[ex.submit(_rss_articles_fast,u,g,'vne',6) for g,u in feeds]
691
+ for f in as_completed(futs,timeout=7):
692
+ try:arts.extend(f.result() or [])
693
+ except Exception:pass
694
+ except Exception:
695
+ for g,u in feeds[:5]:arts.extend(_rss_articles_fast(u,g,'vne',4))
696
+ if arts:_FAST_HOME_CACHE.update({'t':now,'d':arts})
697
+ return _FAST_HOME_CACHE['d'] or arts
698
+
699
+ def _fast_dantri_hot():
700
+ now=time.time()
701
+ if _FAST_DT_CACHE['d'] and now-_FAST_DT_CACHE['t']<900:return _FAST_DT_CACHE['d']
702
+ data=_rss_articles_fast('https://dantri.com.vn/rss/home.rss','Tin Nổi Bật','dantri',12)
703
+ if data:_FAST_DT_CACHE.update({'t':now,'d':data})
704
+ return data
705
+
706
+ def _fast_vnego():
707
+ now=time.time()
708
+ if _FAST_VNEGO_CACHE['d'] and now-_FAST_VNEGO_CACHE['t']<900:return _FAST_VNEGO_CACHE['d']
709
+ out=[]
710
+ try:
711
+ r=requests.get('https://vnexpress.net/vne-go',headers=UA,timeout=4);r.encoding='utf-8'
712
+ soup=BeautifulSoup(r.text,'lxml');seen=set()
713
+ for a in soup.find_all('a',href=True):
714
+ href=a.get('href','');title=clean(a.get('title','') or a.get_text(' ',strip=True))
715
+ if not title or len(title)<8 or not href.startswith('http') or href in seen:continue
716
+ if '/vne-go' not in href and '/video/' not in href:continue
717
+ seen.add(href);img='';im=a.find('img') or (a.parent.find('img') if a.parent else None)
718
+ if im:img=im.get('data-src') or im.get('src','')
719
+ out.append({'title':title,'link':href,'img':img,'source':'vne-video'})
720
+ if len(out)>=10:break
721
+ except Exception:pass
722
+ _FAST_VNEGO_CACHE.update({'t':now,'d':out})
723
+ return out
724
+
725
+ def _fast_highlights():
726
+ now=time.time()
727
+ if _FAST_HL_CACHE['d'] and now-_FAST_HL_CACHE['t']<900:return _FAST_HL_CACHE['d']
728
+ _FAST_HL_CACHE.update({'t':now,'d':[]})
729
+ return []
730
+
731
+ for _p in ['/api/homepage','/api/dantri_hot','/api/vne_video','/api/highlights']:
732
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)==_p and 'GET' in getattr(r,'methods',set()))]
733
+ @app.get('/api/homepage')
734
+ def api_homepage_fast():return JSONResponse(_fast_homepage())
735
+ @app.get('/api/dantri_hot')
736
+ def api_dantri_hot_fast():return JSONResponse(_fast_dantri_hot())
737
+ @app.get('/api/vne_video')
738
+ def api_vne_video_fast():return JSONResponse(_fast_vnego())
739
+ @app.get('/api/highlights')
740
+ def api_highlights_fast():return JSONResponse(_fast_highlights())
741
+
742
+ FINAL6_FAST_HOME_INJECT = """
743
+ <script>
744
+ (function(){
745
+ const oldFetch=window.fetch;
746
+ window.__allowShortRefresh=false;
747
+ window.fetch=function(url,opts){try{let u=String(url||'');if(u.includes('/api/shorts?refresh=1')&&!window.__allowShortRefresh)url='/api/shorts';}catch(e){}return oldFetch.call(this,url,opts)};
748
+ setTimeout(()=>{window.__allowShortRefresh=true;},7000);
749
+ })();
750
+ </script>
751
+ """
752
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
753
+ @app.get('/')
754
+ async def index_final6_fast_home():
755
+ html=f5.f4.f3.f2.f1._load_index_html()
756
+ body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT+FINAL6_INJECT+FINAL6_FAST_HOME_INJECT
757
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
758
+
759
+
760
+ # ===== FINAL6E: SHOW SOURCE CONTENTS IN TOPIC ARTICLE =====
761
+ def _extract_source_details_from_context(context, sources):
762
+ details=[]
763
+ # Map source urls by title for URL/via enrichment
764
+ src_by_title={clean(s.get('title','')):s for s in (sources or [])}
765
+ for block in (context or '').split('---'):
766
+ block=block.strip()
767
+ if not block:continue
768
+ via='';title='';content=''
769
+ m=re.search(r'NGUỒN:\s*(.*)',block)
770
+ if m:via=clean(m.group(1))
771
+ m=re.search(r'TIÊU ĐỀ:\s*(.*)',block)
772
+ if m:title=clean(m.group(1))
773
+ if 'NỘI DUNG BÀI VIẾT ĐÃ CRAWL:' in block:
774
+ content=block.split('NỘI DUNG BÀI VIẾT ĐÃ CRAWL:',1)[1]
775
+ elif 'TÓM TẮT RSS:' in block:
776
+ content=block.split('TÓM TẮT RSS:',1)[1]
777
+ elif 'ĐOẠN MÔ TẢ' in block:
778
+ content=re.split(r'ĐOẠN MÔ TẢ[^:]*:',block,1)[-1]
779
+ content=clean(content)
780
+ if not title and not content:continue
781
+ s=src_by_title.get(title,{})
782
+ details.append({'title':title or s.get('title','Nguồn tham khảo'),'url':s.get('url',''),'via':via or s.get('via',''),'content':content[:1800]})
783
+ if len(details)>=8:break
784
+ return details
785
+
786
+ # Remove prior topic endpoint and register one that stores source_details in post.
787
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/api/topic_post' and 'POST' in getattr(r,'methods',set()))]
788
+
789
+ @app.post('/api/topic_post')
790
+ async def topic_post_with_source_contents(request:Request):
791
+ body=await request.json();topic=clean(body.get('topic',''))
792
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
793
+ img=_topic_image(topic)
794
+ research=_fast_context(topic) if '_fast_context' in globals() else _web_research_context(topic)
795
+ context=research.get('context','');sources=research.get('sources',[])
796
+ details=_extract_source_details_from_context(context,sources)
797
+ if not context or not details:
798
+ return JSONResponse({'error':'Không tìm/crawl được đủ nội dung về chủ đề này. Hãy thử chủ đề cụ thể hơn hoặc dùng hashtag gợi ý.'},status_code=422)
799
+ source_brief='\n\n'.join([f"[{i+1}] {d.get('title','')} ({d.get('via','')})\n{d.get('content','')[:1400]}" for i,d in enumerate(details)])
800
+ prompt=f"""Bạn là biên tập viên VNEWS. Hãy viết MỘT BÀI VIẾT HOÀN CHỈNH bằng tiếng Việt về chủ đề: {topic}
801
+
802
+ Dưới đây là nội dung từng nguồn đã thu thập. Hãy tổng hợp ý chính, không sao chép nguyên văn, không biến các tiêu đề thành danh sách.
803
+
804
+ NỘI DUNG NGUỒN:
805
+ {source_brief[:18000]}
806
+
807
+ Yêu cầu:
808
+ - Tiêu đề mới, rõ, hấp dẫn.
809
+ - Sapo 2-3 câu.
810
+ - 5-8 đoạn phân tích/bối cảnh/tác động/điểm cần lưu ý.
811
+ - Không dùng câu "Dưới đây là" hoặc "Tôi sẽ".
812
+ - Cuối bài có mục "Nguồn tham khảo" nêu tên nguồn.
813
+ """
814
+ text=None
815
+ try:
816
+ import asyncio
817
+ text=await asyncio.wait_for(f5.base.qwen_generate(prompt,image_url=img,max_tokens=1700),timeout=35)
818
+ except Exception:
819
+ text=None
820
+ if not text or len(text)<350:
821
+ bullets='\n'.join([f"• {d['title']}: {d.get('content','')[:320]}" for d in details[:6]])
822
+ vias=', '.join(sorted({d.get('via','') for d in details if d.get('via')}))
823
+ text=(f"{topic}: tổng hợp những điểm đáng chú ý\n\n"
824
+ f"{topic} đang được nhiều nguồn tin đề cập với các góc nhìn khác nhau. Dưới đây là phần tổng hợp nhanh từ những nội dung đã thu thập được.\n\n"
825
+ f"{bullets}\n\n"
826
+ f"Nhìn chung, chủ đề này cần được theo dõi thêm ở các khía cạnh: bối cảnh, tác động thực tế, phản ứng của các bên liên quan và các diễn biến mới trong thời gian tới.\n\n"
827
+ f"Nguồn tham khảo: {vias}")
828
+ post=f5.base.make_post(topic,text,img,'','topic_fast_rss_with_sources',sources=[s for s in sources if s.get('url')])
829
+ post['images']=[img]
830
+ post['source_details']=details
831
+ posts=f5.base._load_ai_wall();posts.insert(0,post);f5.base._save_ai_wall(posts)
832
+ return JSONResponse({'post':post,'mode':'fast_rss_with_source_details','sources_count':len(details)})
833
+
834
+ FINAL6E_INJECT = """
835
+ <style>
836
+ .source-detail-box{margin-top:14px;background:#151515;border:1px solid #2b2b2b;border-radius:10px;padding:10px}.source-detail-box h3{font-size:14px;color:#5cb87a;margin-bottom:8px}.source-detail-item{background:#202020;border-radius:8px;padding:9px;margin:7px 0}.source-detail-title{font-size:12px;font-weight:700;color:#eee;line-height:1.35}.source-detail-meta{font-size:10px;color:#888;margin:3px 0}.source-detail-content{font-size:12px;color:#bbb;line-height:1.5;white-space:pre-wrap;max-height:220px;overflow:auto}.source-detail-item a{color:#5cb87a;font-size:11px;text-decoration:none}
837
+ </style>
838
+ <script>
839
+ (function(){
840
+ function escE(s){return String(s||'').replace(/[&<>\"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','\"':'&quot;',"'":'&#39;'}[m]));}
841
+ window.__topicWallE=[];
842
+ function sourceDetailsHtml(p){let arr=p.source_details||[];if(!arr.length)return '';let h='<div class="source-detail-box"><h3>📚 Nội dung từng nguồn đã dùng</h3>';arr.forEach((s,i)=>{h+=`<div class="source-detail-item"><div class="source-detail-title">${i+1}. ${escE(s.title)}</div><div class="source-detail-meta">${escE(s.via||'Nguồn')}</div><div class="source-detail-content">${escE(s.content||'')}</div>${s.url?`<a href="${escE(s.url)}" target="_blank">Mở nguồn gốc</a>`:''}</div>`});h+='</div>';return h;}
843
+ function renderTopicWallE(){let home=document.getElementById('view-home');if(!home||!window.__topicWallE.length)return;document.getElementById('ai-wall-topic-live')?.remove();let wrap=document.createElement('div');wrap.id='ai-wall-topic-live';wrap.className='ai-wall-topic-live';let h='<div class="slider-header"><span class="slider-label">🧱 Tường AI mới</span><span class="slider-note">Có nội dung nguồn</span></div><div class="slider-track">';window.__topicWallE.slice(0,20).forEach((p,i)=>{h+=`<div class="wall-item"><div class="wall-thumb">${p.img?`<img src="${escE(p.img)}">`:''}</div><div class="wall-title">${escE(p.title)}</div><div class="wall-text">${escE(p.text)}</div><div class="wall-actions"><button class="primary" onclick="readTopicWallE(${i})">Xem</button></div></div>`});h+='</div>';wrap.innerHTML=h;let comp=document.querySelector('.ai-compose');if(comp)comp.after(wrap);else home.prepend(wrap);}
844
+ window.readTopicWallE=function(i){let p=window.__topicWallE[i];if(!p)return;showView('view-article');let imgs=(p.images||[]).filter(Boolean);let gal=imgs.length?'<div class="ai-wall-gallery">'+imgs.slice(0,12).map(u=>`<img src="${escE(u)}" loading="lazy">`).join('')+'</div>':(p.img?`<img class="article-img" src="${escE(p.img)}">`:'');document.getElementById('view-article').innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${escE(p.title)}</h1>${gal}<p class="article-p" style="white-space:pre-wrap">${escE(p.text)}</p>${sourceDetailsHtml(p)}<div class="article-actions"><button onclick="shareAI?shareAI(${JSON.stringify(p).replace(/\"/g,'&quot;')},false):navigator.clipboard.writeText(location.href)">📤 Chia sẻ</button></div></div>`;window.scrollTo(0,0)};
845
+ window.createTopicPostFinal5=async function(){let inp=document.getElementById('ai-topic-input-final5');let topic=(inp&&inp.value||'').trim();if(!topic)return alert('Nhập chủ đề trước');let btn=document.getElementById('ai-topic-btn-final5');if(btn){btn.disabled=true;btn.textContent='Đang lấy nguồn...'}try{let src=await fetch('/api/topic_sources?topic='+encodeURIComponent(topic)).then(r=>r.json()).catch(()=>null);if(btn&&src)btn.textContent='Đã tìm '+(src.count||0)+' nguồn, đang viết...';let r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');window.__topicWallE.unshift(j.post);if(inp)inp.value='';renderTopicWallE();readTopicWallE(0);}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='✨ Tạo bài tổng hợp từ web bằng Qwen'}}};
846
+ })();
847
+ </script>
848
+ """
849
+
850
+ # Override root one last time to append source-details UI.
851
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
852
+ @app.get('/')
853
+ async def index_final6_source_details():
854
+ html=f5.f4.f3.f2.f1._load_index_html()
855
+ body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT+FINAL6_INJECT+globals().get('FINAL6_FAST_HOME_INJECT','')+FINAL6E_INJECT
856
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
857
+
858
+
859
+ # ===== FINAL6F: CLEAN TOPIC OUTPUT + IN-APP SOURCE READER =====
860
+ def _clean_generated_article(text, topic=''):
861
+ """Remove prompt/instruction leakage from generated topic articles."""
862
+ text=str(text or '').strip()
863
+ bad_patterns=[
864
+ r'^\s*[•\-]*\s*Hãy viết .*',
865
+ r'^\s*[•\-]*\s*Dưới đây là .*',
866
+ r'^\s*[•\-]*\s*Dữ liệu .*',
867
+ r'^\s*[•\-]*\s*NỘI DUNG NGUỒN.*',
868
+ r'^\s*[•\-]*\s*Yêu cầu\s*:.*',
869
+ r'^\s*[•\-]*\s*Tiêu đề mới.*',
870
+ r'^\s*[•\-]*\s*Sapo\s*2.*',
871
+ r'^\s*[•\-]*\s*5\s*[-–]\s*8\s*đoạn.*',
872
+ r'^\s*[•\-]*\s*Không dùng câu.*',
873
+ r'^\s*[•\-]*\s*Cuối bài.*',
874
+ r'^\s*[•\-]*\s*Không liệt kê.*',
875
+ r'^\s*[•\-]*\s*Tổng hợp thành.*',
876
+ r'^\s*[•\-]*\s*Diễn đạt lại.*',
877
+ r'^\s*[•\-]*\s*Tuyệt đối.*',
878
+ ]
879
+ out=[]
880
+ for ln in text.splitlines():
881
+ s=ln.strip()
882
+ if not s:
883
+ out.append(ln);continue
884
+ if any(re.search(p,s,re.I) for p in bad_patterns):
885
+ continue
886
+ out.append(ln)
887
+ cleaned='\n'.join(out).strip()
888
+ # If model returned a markdown code/prompt-like block, keep content after first plausible title line.
889
+ cleaned=re.sub(r'^(?:Bài viết|Nội dung bài viết)\s*[::]\s*','',cleaned,flags=re.I).strip()
890
+ # Remove duplicated leading topic instruction if it appears inline.
891
+ cleaned=re.sub(r'Hãy viết MỘT BÀI VIẾT HOÀN CHỈNH[^\n\.]*[\.\n]*','',cleaned,flags=re.I).strip()
892
+ return cleaned or text
893
+
894
+ def _source_article_data(url):
895
+ try:
896
+ r=requests.get(url,headers=UA,timeout=14);r.encoding='utf-8'
897
+ soup=BeautifulSoup(r.text,'lxml')
898
+ h1=soup.find('h1')
899
+ ogt=soup.find('meta',property='og:title')
900
+ ogd=soup.find('meta',property='og:description')
901
+ ogi=soup.find('meta',property='og:image')
902
+ title=clean(h1.get_text(' ',strip=True) if h1 else (ogt.get('content','') if ogt else ''))
903
+ summary=clean(ogd.get('content','') if ogd else '')
904
+ img=ogi.get('content','') if ogi else ''
905
+ except Exception:
906
+ title='';summary='';img=''
907
+ text=_scrape_article_text(url,12000)
908
+ body=[]
909
+ for para in re.split(r'\n+',text or ''):
910
+ para=clean(para)
911
+ if len(para)>35:
912
+ body.append({'type':'p','text':para})
913
+ if len(body)>=80:break
914
+ if not title:title=url
915
+ if not body and summary:body=[{'type':'p','text':summary}]
916
+ return {'title':title,'summary':summary,'og_image':img,'body':body,'source':'topic-source','url':url}
917
+
918
+ @app.get('/api/topic_source_article')
919
+ def api_topic_source_article(url:str=Query(...)):
920
+ if not url.startswith('http'):
921
+ return JSONResponse({'error':'bad url'},status_code=400)
922
+ return JSONResponse(_source_article_data(url))
923
+
924
+ # Override topic generation one last time with output cleaning and source details.
925
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/api/topic_post' and 'POST' in getattr(r,'methods',set()))]
926
+
927
+ @app.post('/api/topic_post')
928
+ async def topic_post_clean_final(request:Request):
929
+ body=await request.json();topic=clean(body.get('topic',''))
930
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
931
+ img=_topic_image(topic)
932
+ research=_fast_context(topic) if '_fast_context' in globals() else _web_research_context(topic)
933
+ context=research.get('context','');sources=research.get('sources',[])
934
+ details=_extract_source_details_from_context(context,sources) if '_extract_source_details_from_context' in globals() else []
935
+ if not details:
936
+ # Build details directly from sources/snippets if helper unavailable or empty.
937
+ for s in sources[:8]:
938
+ details.append({'title':s.get('title',''),'url':s.get('url',''),'via':s.get('via',''),'content':s.get('excerpt','') or s.get('snippet','') or ''})
939
+ if not context and not details:
940
+ return JSONResponse({'error':'Không tìm/crawl được đủ nội dung về chủ đề này. Hãy thử chủ đề cụ thể hơn hoặc dùng hashtag gợi ý.'},status_code=422)
941
+ source_brief='\n\n'.join([f"[{i+1}] {d.get('title','')} ({d.get('via','')})\n{d.get('content','')[:1400]}" for i,d in enumerate(details[:8])])
942
+ prompt=f"""Vai trò: biên tập viên VNEWS.
943
+ Nhiệm vụ: viết một bài báo tiếng Việt hoàn chỉnh về chủ đề "{topic}" dựa trên các nguồn bên dưới.
944
+
945
+ Nguồn thu thập:
946
+ {source_brief[:18000]}
947
+
948
+ Quy tắc biên tập:
949
+ 1. Chỉ xuất bản bài viết cuối cùng, không nhắc lại yêu cầu, không liệt kê chỉ dẫn.
950
+ 2. Không sao chép nguyên văn; hãy tổng hợp và diễn đạt lại.
951
+ 3. Bài có tiêu đề, sapo, các đoạn phân tích/bối cảnh/tác động, và mục Nguồn tham khảo ngắn.
952
+ 4. Không dùng các câu như "Dưới đây là", "Tôi sẽ", "Yêu cầu".
953
+ """
954
+ text=None
955
+ try:
956
+ import asyncio
957
+ text=await asyncio.wait_for(f5.base.qwen_generate(prompt,image_url=img,max_tokens=1700),timeout=35)
958
+ except Exception:
959
+ text=None
960
+ if not text or len(text)<350:
961
+ bullets='\n'.join([f"• {d.get('title','')}: {d.get('content','')[:320]}" for d in details[:6]])
962
+ vias=', '.join(sorted({d.get('via','') for d in details if d.get('via')}))
963
+ text=(f"{topic}: tổng hợp những điểm đáng chú ý\n\n"
964
+ f"{topic} đang được nhiều nguồn tin đề cập với các góc nhìn khác nhau. Dựa trên nội dung đã thu thập, có thể rút ra một số điểm chính để người đọc nắm nhanh bối cảnh.\n\n"
965
+ f"{bullets}\n\n"
966
+ f"Nhìn chung, chủ đề này cần được theo dõi thêm ở các khía cạnh: bối cảnh, tác động thực tế, phản ứng của các bên liên quan và các diễn biến mới trong thời gian tới.\n\n"
967
+ f"Nguồn tham khảo: {vias}")
968
+ text=_clean_generated_article(text,topic)
969
+ post=f5.base.make_post(topic,text,img,'','topic_clean_with_sources',sources=[s for s in sources if s.get('url')])
970
+ post['images']=[img]
971
+ post['source_details']=details[:8]
972
+ posts=f5.base._load_ai_wall();posts.insert(0,post);f5.base._save_ai_wall(posts)
973
+ return JSONResponse({'post':post,'mode':'clean_with_source_details','sources_count':len(details)})
974
+
975
+ FINAL6F_INJECT = """
976
+ <script>
977
+ (function(){
978
+ function escF(s){return String(s||'').replace(/[&<>\\"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','\\"':'&quot;',"'":'&#39;'}[m]));}
979
+ window.readTopicSourceE=async function(url){
980
+ showView('view-article');
981
+ const el=document.getElementById('view-article');
982
+ el.innerHTML='<div class="loading">Đang tải nguồn...</div>';
983
+ try{
984
+ let data=await fetch('/api/topic_source_article?url='+encodeURIComponent(url)).then(r=>r.json());
985
+ if(!data||data.error||!data.body||!data.body.length){throw new Error('Không đọc được nguồn')}
986
+ let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><h1 class="article-title">${escF(data.title)}</h1>`;
987
+ if(data.summary)h+=`<div class="article-summary">${escF(data.summary)}</div>`;
988
+ if(data.og_image)h+=`<img class="article-img" src="${escF(data.og_image)}">`;
989
+ data.body.forEach(b=>{if(b.type==='p')h+=`<p class="article-p">${escF(b.text)}</p>`;else if(b.type==='heading')h+=`<h2 class="article-h2">${escF(b.text)}</h2>`;});
990
+ h+=`<div class="article-actions"><button onclick="window.open('${escF(url)}','_blank')">🔗 Mở gốc</button></div></div>`;
991
+ el.innerHTML=h;window.scrollTo(0,0);
992
+ }catch(e){el.innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="loading">Không đọc được nguồn.<br><a href="${escF(url)}" target="_blank" style="color:#5cb87a">Mở link gốc</a></div>`}
993
+ };
994
+ // Upgrade existing source detail boxes: replace external open behavior by in-app button.
995
+ function enhanceSourceButtons(){document.querySelectorAll('.source-detail-item a[href]').forEach(a=>{let u=a.getAttribute('href');if(!u||a.dataset.vnews)return;a.dataset.vnews='1';a.textContent='Xem trực tiếp trên VNEWS';a.setAttribute('href','javascript:void(0)');a.onclick=function(){readTopicSourceE(u);return false;};});}
996
+ setInterval(enhanceSourceButtons,1000);setTimeout(enhanceSourceButtons,500);
997
+ })();
998
+ </script>
999
+ """
1000
+
1001
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
1002
+ @app.get('/')
1003
+ async def index_final6_clean_links():
1004
+ html=f5.f4.f3.f2.f1._load_index_html()
1005
+ body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT+FINAL6_INJECT+globals().get('FINAL6_FAST_HOME_INJECT','')+globals().get('FINAL6E_INJECT','')+FINAL6F_INJECT
1006
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
1007
+
1008
+
1009
+ # ===== FINAL6G: SELECTED FAST SOURCES =====
1010
+ # Restrict hot hashtags + topic articles to the requested sources only:
1011
+ # Thethaovanhoa, Dantri, VTV, VnExpress, Vatvostudio, GenK, VNReview.
1012
+ SELECTED_SOURCE_FEEDS=[
1013
+ ('VnExpress','https://vnexpress.net/rss/tin-moi-nhat.rss'),
1014
+ ('VnExpress Thời sự','https://vnexpress.net/rss/thoi-su.rss'),
1015
+ ('VnExpress Thế giới','https://vnexpress.net/rss/the-gioi.rss'),
1016
+ ('VnExpress Kinh doanh','https://vnexpress.net/rss/kinh-doanh.rss'),
1017
+ ('VnExpress Công nghệ','https://vnexpress.net/rss/so-hoa.rss'),
1018
+ ('VnExpress Thể thao','https://vnexpress.net/rss/the-thao.rss'),
1019
+ ('Dân trí','https://dantri.com.vn/rss/home.rss'),
1020
+ ('Dân trí Xã hội','https://dantri.com.vn/rss/xa-hoi.rss'),
1021
+ ('Dân trí Kinh doanh','https://dantri.com.vn/rss/kinh-doanh.rss'),
1022
+ ('Dân trí Thể thao','https://dantri.com.vn/rss/the-thao.rss'),
1023
+ ('Dân trí Công nghệ','https://dantri.com.vn/rss/suc-manh-so.rss'),
1024
+ ('VTV','https://vtv.vn/rss/trang-chu.rss'),
1025
+ ('VTV Thời sự','https://vtv.vn/rss/thoi-su.rss'),
1026
+ ('VTV Công nghệ','https://vtv.vn/rss/cong-nghe.rss'),
1027
+ ('Thể thao văn hóa','https://thethaovanhoa.vn/rss/home.rss'),
1028
+ ('Thể thao văn hóa Bóng đá','https://thethaovanhoa.vn/rss/bong-da.rss'),
1029
+ ('GenK','https://genk.vn/home.rss'),
1030
+ ('GenK AI','https://genk.vn/ai.rss'),
1031
+ ('VNReview','https://vnreview.vn/rss/home.rss'),
1032
+ ('VNReview Công nghệ','https://vnreview.vn/rss/cong-nghe.rss'),
1033
+ ]
1034
+ SELECTED_HOMEPAGES=[
1035
+ ('VTV','https://vtv.vn/'),
1036
+ ('Thể thao văn hóa','https://thethaovanhoa.vn/'),
1037
+ ('GenK','https://genk.vn/'),
1038
+ ('VNReview','https://vnreview.vn/'),
1039
+ ('Vatvostudio','https://vatvostudio.vn/'),
1040
+ ]
1041
+ SELECTED_DOMAINS=['vnexpress.net','dantri.com.vn','vtv.vn','thethaovanhoa.vn','genk.vn','vnreview.vn','vatvostudio.vn']
1042
+
1043
+ def _selected_fetch_rss(feed_name, feed_url, max_items=10):
1044
+ items=[]
1045
+ try:
1046
+ r=requests.get(feed_url,headers=UA,timeout=4);r.encoding='utf-8'
1047
+ if r.status_code>=400:return []
1048
+ soup=BeautifulSoup(r.text,'xml')
1049
+ for it in soup.find_all('item')[:max_items]:
1050
+ title=clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
1051
+ link=clean(it.find('link').get_text(strip=True) if it.find('link') else '')
1052
+ desc_raw=it.find('description').get_text(' ',strip=True) if it.find('description') else ''
1053
+ ds=BeautifulSoup(desc_raw,'lxml')
1054
+ desc=clean(ds.get_text(' ',strip=True))
1055
+ if title and link:
1056
+ items.append({'title':title,'url':link,'source':feed_name,'snippet':desc})
1057
+ except Exception:pass
1058
+ return items
1059
+
1060
+ def _selected_scrape_homepage(name, url, max_items=10):
1061
+ items=[];seen=set()
1062
+ try:
1063
+ r=requests.get(url,headers=UA,timeout=4);r.encoding='utf-8'
1064
+ if r.status_code>=400:return []
1065
+ soup=BeautifulSoup(r.text,'lxml')
1066
+ base=url.rstrip('/')
1067
+ for a in soup.find_all('a',href=True):
1068
+ href=a.get('href','').strip();title=clean(a.get('title','') or a.get_text(' ',strip=True))
1069
+ if not href or not title or len(title)<18:continue
1070
+ if href.startswith('/'):
1071
+ p=urlparse(url); href=f'{p.scheme}://{p.netloc}{href}'
1072
+ if not href.startswith('http') or href in seen:continue
1073
+ dom=_domain(href)
1074
+ if not any(d in dom for d in SELECTED_DOMAINS):continue
1075
+ if any(x in href.lower() for x in ['#','javascript:','facebook','youtube','tiktok']):continue
1076
+ seen.add(href)
1077
+ items.append({'title':title,'url':href,'source':name,'snippet':''})
1078
+ if len(items)>=max_items:break
1079
+ except Exception:pass
1080
+ return items
1081
+
1082
+ def _fast_rss_pool():
1083
+ now=time.time();key='selected_fast_pool'
1084
+ if key in _FAST_TOPIC_CACHE and now-_FAST_TOPIC_CACHE[key]['t']<600:return _FAST_TOPIC_CACHE[key]['d']
1085
+ pool=[];seen=set()
1086
+ # RSS first: fast and reliable.
1087
+ for name,url in SELECTED_SOURCE_FEEDS:
1088
+ for it in _selected_fetch_rss(name,url,10):
1089
+ if it['url'] not in seen:
1090
+ seen.add(it['url']);pool.append(it)
1091
+ # Homepage fallback for sources with weak/no RSS, especially Vatvostudio.
1092
+ for name,url in SELECTED_HOMEPAGES:
1093
+ for it in _selected_scrape_homepage(name,url,10):
1094
+ if it['url'] not in seen:
1095
+ seen.add(it['url']);pool.append(it)
1096
+ _FAST_TOPIC_CACHE[key]={'t':now,'d':pool}
1097
+ return pool
1098
+
1099
+ def _hot_topics():
1100
+ now=time.time()
1101
+ if _HOT_CACHE['d'] and now-_HOT_CACHE['t']<600:return _HOT_CACHE['d']
1102
+ pool=_fast_rss_pool()
1103
+ freq={};display={}
1104
+ for it in pool[:220]:
1105
+ title=re.sub(r'\s+-\s+.*$','',it.get('title',''))
1106
+ kws=[]
1107
+ for m in re.findall(r'([A-ZĐÀ-Ỹ][A-Za-zÀ-ỹ0-9]+(?:\s+[A-ZĐÀ-ỸA-Za-zÀ-ỹ0-9][A-Za-zÀ-ỹ0-9]+){1,4})',title):
1108
+ if len(m)>=6:kws.append(m)
1109
+ kws+=_keywords_from_title(title)
1110
+ for kw in kws[:5]:
1111
+ words=[w for w in clean(kw).split() if w.lower() not in STOP_WORDS]
1112
+ if len(words)<2:continue
1113
+ kw=' '.join(words[:5])
1114
+ if len(kw)<6 or len(kw)>55:continue
1115
+ key=kw.lower();freq[key]=freq.get(key,0)+1;display[key]=kw
1116
+ topics=[];seen=set()
1117
+ for key,_ in sorted(freq.items(),key=lambda x:x[1],reverse=True):
1118
+ kw=display[key]
1119
+ if key in seen:continue
1120
+ seen.add(key);topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw})
1121
+ if len(topics)>=24:break
1122
+ for kw in ['AI tại Việt Nam','Công nghệ Việt Nam','VTV thời sự','VnExpress kinh doanh','Dân trí xã hội','GenK AI','VNReview công nghệ','Vatvostudio smartphone','Thể thao văn hóa World Cup']:
1123
+ if kw.lower() not in seen:topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw})
1124
+ _HOT_CACHE.update({'t':now,'d':topics[:24]})
1125
+ return _HOT_CACHE['d']
1126
+
1127
+ def _candidate_urls(topic):
1128
+ seen=set();items=[]
1129
+ scored=[]
1130
+ for it in _fast_rss_pool():
1131
+ sc=_fast_score(topic,it)
1132
+ if sc>0:scored.append((sc,it))
1133
+ for sc,it in sorted(scored,key=lambda x:(x[0],len(x[1].get('snippet',''))),reverse=True)[:14]:
1134
+ if it['url'] not in seen:
1135
+ seen.add(it['url']);items.append(it)
1136
+ # Search only selected sources when RSS lacks matches.
1137
+ if len(items)<6:
1138
+ for dom in SELECTED_DOMAINS:
1139
+ for it in _ddg_search(f'{topic} site:{dom}',4):
1140
+ if it['url'] not in seen:
1141
+ seen.add(it['url']);items.append(it)
1142
+ if len(items)>=12:break
1143
+ return items[:20]
1144
+
1145
+
1146
+ # ===== FINAL6G: SOURCE-LIMITED FAST TOPICS AND FAST HOME =====
1147
+ # Limit hot hashtags/topic context to requested sources and make homepage APIs return quickly.
1148
+ _SOURCE_FEEDS = [
1149
+ ('VnExpress','https://vnexpress.net/rss/tin-moi-nhat.rss','vnexpress.net'),
1150
+ ('VnExpress Thời sự','https://vnexpress.net/rss/thoi-su.rss','vnexpress.net'),
1151
+ ('VnExpress Thế giới','https://vnexpress.net/rss/the-gioi.rss','vnexpress.net'),
1152
+ ('VnExpress Kinh doanh','https://vnexpress.net/rss/kinh-doanh.rss','vnexpress.net'),
1153
+ ('VnExpress Công nghệ','https://vnexpress.net/rss/so-hoa.rss','vnexpress.net'),
1154
+ ('VnExpress Thể thao','https://vnexpress.net/rss/the-thao.rss','vnexpress.net'),
1155
+ ('Dân trí','https://dantri.com.vn/rss/home.rss','dantri.com.vn'),
1156
+ ('Dân trí Xã hội','https://dantri.com.vn/rss/xa-hoi.rss','dantri.com.vn'),
1157
+ ('Dân trí Kinh doanh','https://dantri.com.vn/rss/kinh-doanh.rss','dantri.com.vn'),
1158
+ ('Dân trí Thể thao','https://dantri.com.vn/rss/the-thao.rss','dantri.com.vn'),
1159
+ ('Dân trí Công nghệ','https://dantri.com.vn/rss/suc-manh-so.rss','dantri.com.vn'),
1160
+ ('VTV','https://vtv.vn/rss/trang-chu.rss','vtv.vn'),
1161
+ ('VTV Thời sự','https://vtv.vn/rss/thoi-su.rss','vtv.vn'),
1162
+ ('GenK','https://genk.vn/rss/home.rss','genk.vn'),
1163
+ ('GenK AI','https://genk.vn/ai.rss','genk.vn'),
1164
+ ('VnReview','https://vnreview.vn/rss/tin-moi-nhat.rss','vnreview.vn'),
1165
+ ('VnReview Công nghệ','https://vnreview.vn/rss/cong-nghe.rss','vnreview.vn'),
1166
+ ('Vật Vờ Studio','https://vatvostudio.vn/feed/','vatvostudio.vn'),
1167
+ ('Thể thao văn hóa','https://thethaovanhoa.vn/rss/home.rss','thethaovanhoa.vn'),
1168
+ ('Thể thao văn hóa World Cup','https://thethaovanhoa.vn/rss/world-cup-2026.rss','thethaovanhoa.vn'),
1169
+ ]
1170
+ _SOURCE_CACHE={'t':0,'items':[]}
1171
+ _FAST_ROUTE_CACHE={}
1172
+
1173
+ def _feed_items_source_limited(max_per_feed=10):
1174
+ now=time.time()
1175
+ if _SOURCE_CACHE['items'] and now-_SOURCE_CACHE['t']<600:return _SOURCE_CACHE['items']
1176
+ items=[];seen=set()
1177
+ def one(feed):
1178
+ name,url,dom=feed;out=[]
1179
+ try:
1180
+ r=requests.get(url,headers=UA,timeout=3.5);r.encoding='utf-8'
1181
+ soup=BeautifulSoup(r.text,'xml')
1182
+ for it in soup.find_all('item')[:max_per_feed*2]:
1183
+ title=clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
1184
+ link=clean(it.find('link').get_text(strip=True) if it.find('link') else '')
1185
+ desc_raw=it.find('description').get_text(' ',strip=True) if it.find('description') else ''
1186
+ ds=BeautifulSoup(desc_raw,'lxml')
1187
+ img='';im=ds.find('img')
1188
+ if im:img=im.get('src','') or im.get('data-src','')
1189
+ desc=clean(ds.get_text(' ',strip=True))[:700]
1190
+ if title and link:
1191
+ out.append({'title':title,'url':link,'link':link,'source':name,'via':name,'domain':dom,'snippet':desc,'img':img})
1192
+ if len(out)>=max_per_feed:break
1193
+ except Exception:pass
1194
+ return out
1195
+ try:
1196
+ from concurrent.futures import ThreadPoolExecutor, as_completed
1197
+ with ThreadPoolExecutor(max_workers=8) as ex:
1198
+ futs=[ex.submit(one,f) for f in _SOURCE_FEEDS]
1199
+ for f in as_completed(futs,timeout=5.5):
1200
+ try:
1201
+ for it in f.result() or []:
1202
+ if it['url'] not in seen:
1203
+ seen.add(it['url']);items.append(it)
1204
+ except Exception:pass
1205
+ except Exception:
1206
+ for f in _SOURCE_FEEDS[:8]:
1207
+ for it in one(f):
1208
+ if it['url'] not in seen:
1209
+ seen.add(it['url']);items.append(it)
1210
+ _SOURCE_CACHE.update({'t':now,'items':items})
1211
+ return items
1212
+
1213
+ def _score_topic_source(topic,it):
1214
+ toks=[w.lower() for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',topic or '') if len(w)>1 and w.lower() not in STOP_WORDS]
1215
+ hay=(it.get('title','')+' '+it.get('snippet','')+' '+it.get('source','')).lower()
1216
+ if not toks:return 0
1217
+ score=sum((3 if len(t)>3 else 1) for t in toks if t in hay)
1218
+ if topic.lower().strip() and topic.lower().strip() in hay:score+=12
1219
+ return score
1220
+
1221
+ def _hot_topics():
1222
+ now=time.time()
1223
+ if _HOT_CACHE['d'] and now-_HOT_CACHE['t']<600:return _HOT_CACHE['d']
1224
+ freq={};display={}
1225
+ for it in _feed_items_source_limited(8)[:180]:
1226
+ title=re.sub(r'\s+-\s+.*$','',it.get('title',''))
1227
+ kws=[]
1228
+ for m in re.findall(r'([A-ZĐÀ-Ỹ][A-Za-zÀ-ỹ0-9]+(?:\s+[A-ZĐÀ-ỸA-Za-zÀ-ỹ0-9][A-Za-zÀ-ỹ0-9]+){1,4})',title):
1229
+ if len(m)>=6:kws.append(m)
1230
+ kws += _keywords_from_title(title)
1231
+ for kw in kws[:4]:
1232
+ words=[w for w in clean(kw).split() if w.lower() not in STOP_WORDS]
1233
+ if len(words)<2:continue
1234
+ kw=' '.join(words[:5])
1235
+ if 6<=len(kw)<=55:
1236
+ key=kw.lower();freq[key]=freq.get(key,0)+1;display[key]=kw
1237
+ topics=[]
1238
+ for key,_ in sorted(freq.items(),key=lambda x:x[1],reverse=True):
1239
+ kw=display[key];topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw})
1240
+ if len(topics)>=24:break
1241
+ for kw in ['Giá vàng trong nước','AI tại Việt Nam','Bóng đá Việt Nam','Kinh tế Việt Nam','Công nghệ AI','Vật Vờ Studio','World Cup 2026','Sức khỏe cộng đồng']:
1242
+ if not any(t['topic'].lower()==kw.lower() for t in topics):topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw})
1243
+ _HOT_CACHE.update({'t':now,'d':topics[:24]})
1244
+ return _HOT_CACHE['d']
1245
+
1246
+ def _fast_context(topic):
1247
+ now=time.time();key='source_limited_ctx:'+topic.lower().strip()
1248
+ if key in _FAST_TOPIC_CACHE and now-_FAST_TOPIC_CACHE[key]['t']<600:return _FAST_TOPIC_CACHE[key]['d']
1249
+ pool=_feed_items_source_limited(12)
1250
+ scored=[]
1251
+ for it in pool:
1252
+ sc=_score_topic_source(topic,it)
1253
+ if sc>0:scored.append((sc,it))
1254
+ if not scored:
1255
+ # Try broader matching by first token only before giving up.
1256
+ first=(_fast_topic_tokens(topic) or [''])[0]
1257
+ if first:
1258
+ for it in pool:
1259
+ if first in (it.get('title','')+' '+it.get('snippet','')).lower():scored.append((1,it))
1260
+ picked=[it for sc,it in sorted(scored,key=lambda x:(x[0],len(x[1].get('snippet',''))),reverse=True)[:8]]
1261
+ if not picked:picked=pool[:6]
1262
+ blocks=[];src=[]
1263
+ for it in picked:
1264
+ content=it.get('snippet','') or it.get('title','')
1265
+ blocks.append(f"NGUỒN: {it.get('source','')}\nTIÊU ĐỀ: {it.get('title','')}\nTÓM TẮT RSS:\n{content}")
1266
+ src.append({'title':it.get('title',''),'url':it.get('url',''),'via':it.get('source',''),'snippet':content})
1267
+ data={'context':'\n\n---\n\n'.join(blocks),'sources':src,'count':len(blocks)}
1268
+ _FAST_TOPIC_CACHE[key]={'t':now,'d':data}
1269
+ return data
1270
+
1271
+ # Override slow search functions to never crawl open web during topic generation.
1272
+ def _web_research_context(topic):
1273
+ return _fast_context(topic)
1274
+
1275
+ def _candidate_urls(topic):
1276
+ return _fast_context(topic).get('sources',[])
1277
+
1278
+ # Fast homepage endpoints from requested source RSS; no slow HTML scrapers.
1279
+ def _fast_homepage_sources():
1280
+ now=time.time();key='home_sources'
1281
+ if key in _FAST_ROUTE_CACHE and now-_FAST_ROUTE_CACHE[key]['t']<600:return _FAST_ROUTE_CACHE[key]['d']
1282
+ groups=[];seen=set()
1283
+ group_map=[('Tin mới','https://vnexpress.net/rss/tin-moi-nhat.rss','vne'),('Thời Sự','https://vnexpress.net/rss/thoi-su.rss','vne'),('Kinh Doanh','https://vnexpress.net/rss/kinh-doanh.rss','vne'),('Công Nghệ','https://vnexpress.net/rss/so-hoa.rss','vne'),('Dân Trí','https://dantri.com.vn/rss/home.rss','dantri'),('GenK','https://genk.vn/rss/home.rss','genk'),('VnReview','https://vnreview.vn/rss/tin-moi-nhat.rss','vnreview')]
1284
+ for g,u,s in group_map:
1285
+ for it in _rss_articles_fast(u,g,s,6) if '_rss_articles_fast' in globals() else []:
1286
+ if it['link'] not in seen:
1287
+ seen.add(it['link']);groups.append(it)
1288
+ _FAST_ROUTE_CACHE[key]={'t':now,'d':groups}
1289
+ return groups
1290
+
1291
+ for _p in ['/api/homepage','/api/dantri_hot','/api/vne_video','/api/highlights','/api/hot_topics','/api/topic_sources']:
1292
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)==_p and 'GET' in getattr(r,'methods',set()))]
1293
+ @app.get('/api/homepage')
1294
+ def api_homepage_source_fast():return JSONResponse(_fast_homepage_sources())
1295
+ @app.get('/api/dantri_hot')
1296
+ def api_dantri_hot_source_fast():
1297
+ data=[{**it,'source':'dantri','link':it.get('url') or it.get('link')} for it in _feed_items_source_limited(8) if it.get('domain')=='dantri.com.vn'][:12]
1298
+ return JSONResponse(data)
1299
+ @app.get('/api/vne_video')
1300
+ def api_vne_video_source_fast():
1301
+ return JSONResponse([]) # do not block homepage if VnEgo is slow
1302
+ @app.get('/api/highlights')
1303
+ def api_highlights_source_fast():return JSONResponse([])
1304
+ @app.get('/api/hot_topics')
1305
+ def api_hot_topics_source_fast():return JSONResponse({'topics':_hot_topics(),'sources':'vn_only'})
1306
+ @app.get('/api/topic_sources')
1307
+ def api_topic_sources_source_fast(topic:str=Query(...)):
1308
+ data=_fast_context(clean(topic));return JSONResponse({'count':data.get('count',0),'sources':data.get('sources',[]),'has_context':bool(data.get('context')),'mode':'source_limited_rss'})
1309
+
1310
+ # Override root: include all existing UI but add a script that prevents forced shorts refresh on initial load.
1311
+ ROOT_FAST_INJECT="""
1312
+ <script>
1313
+ (function(){
1314
+ const oldFetch=window.fetch;window.__allowShortRefresh=false;
1315
+ window.fetch=function(url,opts){try{let u=String(url||'');if(u.includes('/api/shorts?refresh=1')&&!window.__allowShortRefresh)url='/api/shorts';}catch(e){}return oldFetch.call(this,url,opts)};
1316
+ setTimeout(()=>{window.__allowShortRefresh=true;},8000);
1317
+ })();
1318
+ </script>
1319
+ """
1320
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))]
1321
+ @app.get('/')
1322
+ async def index_final_fast_sources():
1323
+ html=f5.f4.f3.f2.f1._load_index_html()
1324
+ body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT+FINAL6_INJECT+globals().get('FINAL6_FAST_HOME_INJECT','')+globals().get('FINAL6E_INJECT','')+globals().get('FINAL6F_INJECT','')+ROOT_FAST_INJECT
1325
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
ai_runtime_patch_fast.py ADDED
@@ -0,0 +1,188 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final patch v2: fix topic rewrite, remove duplicate short slide, full short interaction buttons."""
2
+ import re, threading, time, json, os, asyncio
3
+ import ai_runtime_final6 as f6
4
+ from ai_runtime_final6 import app, rt, f5, HTMLResponse, JSONResponse, Request, Query
5
+ import html as html_lib
6
+ from urllib.parse import urlparse
7
+
8
+ def clean(s):return re.sub(r"\s+"," ",html_lib.unescape(str(s or ""))).strip()
9
+ def _domain(u):
10
+ try:return urlparse(u or '').netloc.replace('www.','')
11
+ except:return ''
12
+ DATA_DIR="/data" if os.path.isdir('/data') else "/app/data"
13
+ os.makedirs(DATA_DIR,exist_ok=True)
14
+ SHORT_COMMENTS_FILE=os.path.join(DATA_DIR,'short_comments.json')
15
+ TTL_24H=86400;HAS_PERSISTENT=os.path.isdir('/data')
16
+ def _lj(p,d):
17
+ try:
18
+ if os.path.exists(p):return json.load(open(p,'r',encoding='utf-8'))
19
+ except:pass
20
+ return d
21
+ def _sj(p,d):
22
+ try:os.makedirs(os.path.dirname(p),exist_ok=True);open(p+'.tmp','w',encoding='utf-8').write(json.dumps(d,ensure_ascii=False));os.replace(p+'.tmp',p)
23
+ except:pass
24
+ def _cleanup():
25
+ n=int(time.time());ps=f5.base._load_ai_wall();f=[p for p in ps if n-int(p.get('ts') or 0)<TTL_24H]
26
+ if len(f)<len(ps):f5.base._save_ai_wall(f)
27
+ def _scrape(url,mc=8000):
28
+ try:d=f5.base.scrape_any_url(url);return(d.get('title',''),((d.get('summary','')+'\n'+d.get('text','')).strip())[:mc],d.get('image') or d.get('og_image') or '')
29
+ except:return('','','')
30
+ _bg_home={"t":0,"d":[]};_bg_shorts={"t":0,"d":[]};_bg_lock=False
31
+ def _bg():
32
+ global _bg_lock
33
+ if _bg_lock:return
34
+ _bg_lock=True
35
+ try:
36
+ if hasattr(f6,'_fast_homepage'):d=f6._fast_homepage();(_bg_home.update({"t":time.time(),"d":d}) if d else None)
37
+ raw=[];[raw.extend(f6._yt_ytdlp(h,20) or f6._yt_html(h,20)) for h in f6.YOUTUBE_HANDLES];raw.extend(f6._fallback_shorts())
38
+ seen=set();out=[v for v in raw if v.get('id') and v['id'] not in seen and not seen.add(v['id'])]
39
+ if out:_bg_shorts.update({"t":time.time(),"d":out[:40]})
40
+ _cleanup()
41
+ except:pass
42
+ finally:_bg_lock=False
43
+ @app.on_event("startup")
44
+ async def _s():threading.Thread(target=_bg,daemon=True).start()
45
+ threading.Thread(target=lambda:[time.sleep(600) or _bg() for _ in iter(int,1)],daemon=True).start()
46
+ app.router.routes=[r for r in app.router.routes if not (getattr(r,'path',None) in ('/api/homepage','/api/shorts','/api/ai_wall','/api/topic_post','/api/article/ask','/api/topic/rewrite','/api/rewrite_share','/api/url_wall','/api/short/comments','/api/short/comment','/api/storage_status','/') and any(m in getattr(r,'methods',set()) for m in ('GET','POST')))]
47
+ @app.get('/api/homepage')
48
+ def _h():
49
+ n=time.time()
50
+ if _bg_home['d']:(threading.Thread(target=_bg,daemon=True).start() if n-_bg_home['t']>300 else None);return JSONResponse(_bg_home['d'])
51
+ if hasattr(f6,'_fast_homepage'):d=f6._fast_homepage();_bg_home.update({"t":n,"d":d or []});return JSONResponse(d or [])
52
+ return JSONResponse([])
53
+ @app.get('/api/shorts')
54
+ def _sh(refresh:int=Query(default=0)):
55
+ n=time.time()
56
+ if _bg_shorts['d'] and (not refresh or n-_bg_shorts['t']<120):(threading.Thread(target=_bg,daemon=True).start() if n-_bg_shorts['t']>600 else None);return JSONResponse(_bg_shorts['d'])
57
+ return f6.api_shorts_final6(refresh=refresh) if hasattr(f6,'api_shorts_final6') else JSONResponse([])
58
+ @app.get('/api/ai_wall')
59
+ def _w():n=int(time.time());return JSONResponse({'posts':[p for p in f5.base._load_ai_wall() if n-int(p.get('ts') or 0)<TTL_24H],'persistent':HAS_PERSISTENT})
60
+ @app.get('/api/storage_status')
61
+ def _st():return JSONResponse({'persistent':HAS_PERSISTENT})
62
+ @app.get('/api/short/comments')
63
+ def _gc(id:str=Query(...)):return JSONResponse({'comments':_lj(SHORT_COMMENTS_FILE,{}).get(id,[])})
64
+ @app.post('/api/short/comment')
65
+ async def _pc(request:Request):
66
+ b=await request.json();v=str(b.get('id','')).strip();t=clean(b.get('text',''))
67
+ if not v or not t:return JSONResponse({'error':'missing'},status_code=400)
68
+ db=_lj(SHORT_COMMENTS_FILE,{});c=db.get(v,[]);c.insert(0,{'text':t[:300],'ts':int(time.time())});db[v]=c[:100];_sj(SHORT_COMMENTS_FILE,db);return JSONResponse({'comments':db[v]})
69
+ @app.post('/api/article/ask')
70
+ async def _ask(request:Request):
71
+ b=await request.json();q=clean(b.get('question',''));ctx=clean(b.get('context',''));url=clean(b.get('url',''))
72
+ if not q:return JSONResponse({'error':'missing question'},status_code=400)
73
+ title='';raw=''
74
+ if url:title,raw,_=_scrape(url,10000)
75
+ if not raw:raw=ctx[:12000]
76
+ ans=await f5.base.qwen_generate(f'Bạn là VNEWS AI. Nội dung: "{title}"\n{raw[:9000]}\n\nHỏi: "{q}"\n\nTrả lời tự nhiên bằng tiếng Việt.',max_tokens=1200)
77
+ return JSONResponse({'answer':ans or 'Chưa trả lời được.','title':title})
78
+ @app.post('/api/rewrite_share')
79
+ @app.post('/api/url_wall')
80
+ async def _rw(request:Request):
81
+ b=await request.json();url=clean(b.get('url',''));ctx=clean(b.get('context',''))
82
+ if not url.startswith('http'):return JSONResponse({'error':'URL không hợp lệ'},status_code=400)
83
+ title,raw,img=_scrape(url,14000)
84
+ if len(raw)<50:raw=ctx[:14000]
85
+ if len(raw)<50:return JSONResponse({'error':'Không đọc được bài'},status_code=422)
86
+ text=None
87
+ try:text=await asyncio.wait_for(f5.base.qwen_generate(f'Tóm tắt đăng Tường AI:\nTiêu đề: {title}\n{raw[:14000]}\n\n4-6 ý chính. Cuối ghi nguồn.',image_url=img or None,max_tokens=1000),timeout=30)
88
+ except:pass
89
+ if not text or len(text)<80:text=f"Tóm tắt: {title}\n\n{raw[:1200]}\n\nNguồn: {_domain(url)}"
90
+ post=f5.base.make_post(title or 'Bài viết',text,img,url,'rewrite',sources=[{'title':title,'url':url,'via':_domain(url)}])
91
+ ps=f5.base._load_ai_wall();ps.insert(0,post);f5.base._save_ai_wall(ps);return JSONResponse({'post':post})
92
+ @app.post('/api/topic/rewrite')
93
+ async def _tr(request:Request):
94
+ b=await request.json();pid=str(b.get('post_id','')).strip()
95
+ if not pid:return JSONResponse({'error':'missing post_id'},status_code=400)
96
+ ps=f5.base._load_ai_wall();p=next((x for x in ps if str(x.get('id'))==pid),None)
97
+ if not p:return JSONResponse({'error':'Bài không tồn tại'},status_code=404)
98
+ urls=list(dict.fromkeys([s['url'] for s in (p.get('source_details') or []) if s.get('url')]+[s['url'] for s in (p.get('sources') or []) if s.get('url')]))[:5]
99
+ parts=[]
100
+ for u in urls:t,r,_=_scrape(u,6000);(parts.append(f"[{_domain(u)}] {t}\n{r}") if r and len(r)>150 else None)
101
+ ac='\n---\n'.join(parts) if parts else (p.get('text') or '')
102
+ title=p.get('title','')
103
+ text=None
104
+ try:text=await asyncio.wait_for(f5.base.qwen_generate(f'Viết lại:\nChủ đề: {title}\n{ac[:16000]}\n\nTiêu đề mới + 4-6 ý + nguồn.',image_url=p.get('img'),max_tokens=1200),timeout=35)
105
+ except:pass
106
+ if not text or len(text)<100:text=f"Tóm tắt: {title}\n\n{ac[:1500]}\n\nNguồn: VNEWS AI"
107
+ np=f5.base.make_post('Rewrite: '+title,text,p.get('img',''),'','rewrite_topic',sources=p.get('sources',[]));np['images']=p.get('images',[])
108
+ all_p=f5.base._load_ai_wall();all_p.insert(0,np);f5.base._save_ai_wall(all_p);return JSONResponse({'post':np})
109
+ @app.post('/api/topic_post')
110
+ async def _tp(request:Request):
111
+ b=await request.json();topic=clean(b.get('topic',''))
112
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
113
+ img=f6._topic_image(topic);research=f6._fast_context(topic) if hasattr(f6,'_fast_context') else f6._web_research_context(topic)
114
+ ctx=research.get('context','');src=research.get('sources',[]);det=f6._extract_source_details_from_context(ctx,src) if hasattr(f6,'_extract_source_details_from_context') else []
115
+ if not ctx or not src:return JSONResponse({'error':'Không tìm được nội dung.'},status_code=422)
116
+ sb='\n\n'.join([f"[{i+1}] {d.get('title','')} ({d.get('via','')})\n{d.get('content','')[:1400]}" for i,d in enumerate(det)]) if det else ctx[:18000]
117
+ text=None
118
+ try:text=await asyncio.wait_for(f5.base.qwen_generate(f'Viết bài tiếng Việt VỀ: "{topic}"\nNGUỒN:\n{sb[:18000]}\nCHỈ viết về "{topic}". 5-8 đoạn. Cuối có nguồn.',image_url=img,max_tokens=1700),timeout=35)
119
+ except:pass
120
+ if not text or len(text)<300:text=f"{topic}: tổng hợp\n\n"+'\n'.join([f"• {d['title']}: {d.get('content','')[:300]}" for d in (det or [])[:6]])+"\n\nNguồn: "+', '.join(sorted({d.get('via','') for d in (det or []) if d.get('via')}))
121
+ post=f5.base.make_post(topic,text,img,'','topic_focused',sources=[s for s in src if s.get('url')]);post['images']=[img];post['source_details']=det
122
+ ps=f5.base._load_ai_wall();ps.insert(0,post);f5.base._save_ai_wall(ps);return JSONResponse({'post':post})
123
+
124
+ PATCH_INJECT=r'''
125
+ <style>
126
+ .short-cmt-panel{position:fixed;bottom:0;left:0;right:0;max-height:55vh;background:#181818;border-radius:16px 16px 0 0;z-index:99999;padding:14px;display:none;overflow-y:auto}.short-cmt-panel.active{display:block}.short-cmt-panel textarea{width:100%;background:#222;border:1px solid #444;color:#eee;border-radius:10px;padding:9px;margin:6px 0;min-height:60px}.short-cmt-panel button{background:#2d8659;border:0;color:#fff;border-radius:10px;padding:8px 12px;margin:4px}.cmt-item{background:#222;border-radius:8px;padding:7px;margin:5px 0;color:#ccc;font-size:12px}
127
+ .source-detail-box{margin-top:14px;background:#151515;border:1px solid #2b2b2b;border-radius:10px;padding:10px}.source-detail-item{background:#202020;border-radius:8px;padding:9px;margin:7px 0;cursor:pointer}.source-detail-title{font-size:12px;font-weight:700;color:#eee}.source-detail-content{font-size:12px;color:#bbb;line-height:1.5;white-space:pre-wrap;max-height:120px;overflow:hidden}.source-detail-item img{width:100%;aspect-ratio:16/9;object-fit:cover;border-radius:6px;margin-bottom:6px;background:#222}.source-vnews-btn{display:inline-block;margin-top:6px;background:#2d8659;color:#fff;padding:5px 10px;border-radius:12px;font-size:11px;font-weight:700}
128
+ .article-ai-ask{margin-top:12px;background:#141414;border:1px solid #2a2a2a;border-radius:10px;padding:10px}.article-ai-ask textarea{width:100%;min-height:60px;background:#222;border:1px solid #444;color:#eee;border-radius:10px;padding:9px}.article-ai-ask button{background:#2d8659;border:0;color:#fff;border-radius:10px;padding:8px 12px;margin-top:6px}.article-ai-answer{white-space:pre-wrap;color:#ccc;font-size:13px;line-height:1.55;margin-top:8px}
129
+ .storage-warn{background:#332200;border:1px solid #664400;color:#ffcc00;padding:8px 12px;border-radius:8px;font-size:11px;margin:6px 4px}
130
+ button[onclick*="rewriteCurrentArticle"]{display:none!important}
131
+ /* Hide ALL old Short AI slides from previous layers */
132
+ #ai-short-home,.ai-short-home,.ai-short-card-final{display:none!important}
133
+ .source-detail-box a[target="_blank"]{display:none!important}
134
+ </style>
135
+ <div id="short-cmt-panel" class="short-cmt-panel"></div>
136
+ <script>
137
+ (function(){
138
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
139
+ fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){let h=document.getElementById('view-home');if(h){let w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ <b>Persistent Storage chưa bật.</b> Bật: Space Settings → Persistent Storage → Small.';h.prepend(w);}}});
140
+
141
+ // === Short AI Slide on homepage (same as Dantri shorts) ===
142
+ async function renderShortAISlide(){let home=document.getElementById('view-home');if(!home)return;document.getElementById('short-ai-final-slide')?.remove();let wall=(await fetch('/api/ai_wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];let vids=wall.filter(p=>p.video);if(!vids.length)return;let wrap=document.createElement('div');wrap.id='short-ai-final-slide';wrap.className='slider-wrap';wrap.innerHTML='<div class="slider-header"><span class="slider-label">🎬 Short AI</span></div><div class="slider-track">'+vids.slice(0,30).map((p,i)=>`<div class="slider-item shorts-item" onclick="openAIShortFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${p.video}" muted preload="metadata" style="width:100%;height:100%;object-fit:cover"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`).join('')+'</div>';let comp=home.querySelector('.ai-compose');if(comp&&comp.nextSibling)comp.parentNode.insertBefore(wrap,comp.nextSibling);else home.prepend(wrap);}
143
+ setTimeout(renderShortAISlide,2500);
144
+
145
+ // === Source Details ===
146
+ function renderSourceDetails(post,container){let det=post.source_details||[];if(!det.length)return;container.querySelectorAll('.source-detail-box').forEach(e=>e.remove());let box=document.createElement('div');box.className='source-detail-box';box.innerHTML='<h3 style="font-size:14px;color:#5cb87a;margin-bottom:8px">📚 Bài nguồn</h3>'+det.map((s,i)=>`<div class="source-detail-item" data-url="${esc(s.url||'')}"><div class="source-detail-title">${i+1}. ${esc(s.title)}</div><div class="source-detail-content">${esc((s.content||'').slice(0,300))}</div><span class="source-vnews-btn">📖 Xem trên VNEWS</span></div>`).join('');container.appendChild(box);box.querySelectorAll('.source-detail-item').forEach(el=>{el.onclick=function(){let u=el.dataset.url;if(u&&typeof readArticle==='function')readArticle(u);}});det.forEach((s,i)=>{if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){let items=box.querySelectorAll('.source-detail-item');if(items[i]){let img=document.createElement('img');img.src=d.og_image||d.img;img.loading='lazy';img.onerror=function(){this.style.display='none'};items[i].prepend(img);}}}).catch(()=>{});});}
147
+
148
+ // === AI Wall Post View ===
149
+ async function readAIWallPost(i){let wall=(await fetch('/api/ai_wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];let p=wall[i];if(!p)return;showView('view-article');let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${p.img?`<img class="article-img" src="${p.img}">`:''}`;h+=`<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>`;h+=`<div class="article-actions"><button class="primary" onclick="doRewriteTopic(this,'${esc(p.id)}')">🤖 Rewrite AI đăng tường</button>${p.video?`<button onclick="openAIShortFeed(${i})">🎬 Xem Short</button>`:''}<button onclick="doShare('${esc(p.title)}','${location.origin}','${esc(p.img||'')}')">📤</button></div>`;h+=`<div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="article-ai-q" placeholder="Hỏi về nội dung..."></textarea><button onclick="askAIWall(${i})">Hỏi</button><div id="article-ai-ans" class="article-ai-answer"></div></div></div>`;document.getElementById('view-article').innerHTML=h;let art=document.querySelector('.article-view');if(art)renderSourceDetails(p,art);window.scrollTo(0,0);}
150
+ window.readAIWallPost=readAIWallPost;window.aiReadWallPatched=window.aiReadWall=window.readWallPost=function(i){readAIWallPost(i)};
151
+
152
+ // === Short AI Feed: FULL interaction buttons like Dantri Shorts ===
153
+ window.openAIShortFeed=async function(startIdx){let wall=(await fetch('/api/ai_wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];let vids=wall.filter(p=>p.video);if(!vids.length)return alert('Chưa có Short AI');let ordered=startIdx>0?vids.slice(startIdx).concat(vids.slice(0,startIdx)):vids;showView('view-tiktok');let h='<button class="back-btn" onclick="switchCat(\'home\')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';ordered.forEach((p,i)=>{h+=`<div class="tiktok-slide" data-id="${p.id}"><video src="${p.video}" playsinline loop></video><div class="tiktok-bottom"><span class="badge badge-ai">AI Short</span><p class="tiktok-title">${esc(p.title)}</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation()"><div class="icon">👁</div><div class="count">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();likeShort('${p.id}',this)"><div class="icon">❤️</div><div class="count">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();openShortComments('${p.id}')"><div class="icon">💬</div><div class="count" id="cc-${p.id}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();shareShort('${esc(p.title)}')"><div class="icon">📤</div><div class="count">Share</div></button></div><span class="tiktok-counter">${i+1}/${ordered.length}</span></div>`});h+='</div></div>';document.getElementById('view-tiktok').innerHTML=h;initShortFeed();ordered.forEach(p=>{fetch('/api/short/comments?id='+encodeURIComponent(p.id)).then(r=>r.json()).then(j=>{let el=document.getElementById('cc-'+p.id);if(el)el.textContent=(j.comments||[]).length}).catch(()=>{});});}
154
+ window.likeShort=function(id,btn){let c=btn.querySelector('.count');c.textContent=parseInt(c.textContent||0)+1;}
155
+ window.shareShort=function(title){if(navigator.share)navigator.share({title,url:location.href}).catch(()=>{});else{navigator.clipboard.writeText(location.href);alert('Đã sao chép link!');}}
156
+ function initShortFeed(){let feed=document.getElementById('tiktok-feed');if(!feed)return;let slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{let v=sl.querySelector('video');let fr=sl.querySelector('iframe');if(idx===i){if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;}else{if(v)v.pause();if(fr&&fr.src)fr.src='';}});cur=i}let t;feed.addEventListener('scroll',()=>{clearTimeout(t);t=setTimeout(()=>{let rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2,b=-1,d=1e9;slides.forEach((sl,i)=>{let dd=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(dd<d){d=dd;b=i}});if(b>=0)act(b)},130)});setTimeout(()=>act(0),300);slides.forEach(sl=>{let v=sl.querySelector('video');if(v)v.addEventListener('click',e=>{e.preventDefault();v.paused?v.play().catch(()=>{}):v.pause()})});}
157
+
158
+ // === Handlers ===
159
+ window.doRewriteTopic=async function(btn,pid){btn.disabled=true;btn.textContent='Đang rewrite...';try{let r=await fetch('/api/topic/rewrite',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({post_id:pid})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');alert('Rewrite thành công!');showRewriteResult(j.post);}catch(e){alert(e.message)}finally{btn.disabled=false;btn.textContent='🤖 Rewrite AI đăng tường';}};
160
+ window.doRewriteArticle=async function(btn){let url=(window._currentArticle&&window._currentArticle.url)||'';if(!url){let a=document.querySelector('#view-article a[href*="://"]');if(a)url=a.href;}if(!url){let text=document.querySelector('.article-view')?.innerText?.slice(0,14000)||'';if(text.length<100){alert('Không tìm được nội dung để rewrite');return;}btn.disabled=true;btn.textContent='Đang rewrite...';try{let r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:'https://vnews.local/inline',context:text})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error);alert('Rewrite thành công!');showRewriteResult(j.post);}catch(e){alert(e.message)}finally{btn.disabled=false;btn.textContent='🤖 Rewrite AI đăng tường';}return;}btn.disabled=true;btn.textContent='Đang rewrite...';try{let ctx=document.querySelector('.article-view')?.innerText?.slice(0,14000)||'';let r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,context:ctx})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');alert('Rewrite thành công!');showRewriteResult(j.post);}catch(e){alert(e.message)}finally{btn.disabled=false;btn.textContent='🤖 Rewrite AI đăng tường';}};
161
+ function showRewriteResult(post){if(!post)return;showView('view-article');document.getElementById('view-article').innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">Rewrite</span><h1 class="article-title">${esc(post.title)}</h1>${post.img?`<img class="article-img" src="${post.img}">`:''}` +`<p class="article-p" style="white-space:pre-wrap">${esc(post.text)}</p><div class="article-actions"><button class="primary" onclick="makeShortFromPost('${esc(post.id)}',this)">🎬 Tạo Short AI</button><button onclick="doShare('${esc(post.title)}','${location.origin}','${esc(post.img||'')}')">📤</button></div></div>`;window.scrollTo(0,0);}
162
+ window.makeShortFromPost=async function(pid,btn){if(btn){btn.disabled=true;btn.textContent='Đang tạo...';}try{let r=await fetch('/api/ai/short/'+pid,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice:'nu',emotion:'neutral',speed:1.2})});let j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');alert('Đã tạo Short AI!');renderShortAISlide();}catch(e){alert(e.message)}finally{if(btn){btn.disabled=false;btn.textContent='🎬 Tạo Short AI';}}};
163
+ window.rewriteCurrentArticle=function(){let btn=document.querySelector('[data-rw-article]');if(btn)doRewriteArticle(btn);};
164
+ window.askAIWall=async function(i){let q=document.getElementById('article-ai-q')?.value.trim();if(!q)return alert('Nhập câu hỏi');document.getElementById('article-ai-ans').textContent='Đang hỏi...';let wall=(await fetch('/api/ai_wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];let p=wall[i]||{};let ctx=(p.text||'');for(let s of (p.source_details||[]))ctx+='\n'+(s.content||'');try{let r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({question:q,context:ctx.slice(0,12000)})});let j=await r.json();document.getElementById('article-ai-ans').textContent=j.answer||'Không trả lời được';}catch(e){document.getElementById('article-ai-ans').textContent='Lỗi: '+e.message}};
165
+ window.askArticleAI=async function(){let q=document.getElementById('article-ai-question')?.value.trim();if(!q)return alert('Nhập câu hỏi');let a=document.getElementById('article-ai-answer');a.textContent='Đang hỏi...';let url=(window._currentArticle&&window._currentArticle.url)||'';let ctx=document.querySelector('.article-view')?.innerText?.slice(0,12000)||'';try{let r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,question:q,context:ctx})});let j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}};
166
+ window.openShortComments=async function(id){let panel=document.getElementById('short-cmt-panel');let j=await fetch('/api/short/comments?id='+encodeURIComponent(id)).then(r=>r.json()).catch(()=>({comments:[]}));panel.innerHTML=`<h3 style="color:#5cb87a">💬 Bình luận</h3><div id="cmt-list">${(j.comments||[]).map(c=>`<div class="cmt-item">${esc(c.text)}</div>`).join('')||'<div class="cmt-item" style="color:#777">Chưa có</div>'}</div><textarea id="cmt-text" placeholder="Bình luận..."></textarea><button onclick="submitShortCmt('${esc(id)}')">Gửi</button><button onclick="document.getElementById('short-cmt-panel').classList.remove('active')">Đóng</button>`;panel.classList.add('active');}
167
+ window.submitShortCmt=async function(id){let t=document.getElementById('cmt-text')?.value.trim();if(!t)return;let j=await fetch('/api/short/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id,text:t})}).then(r=>r.json()).catch(()=>({comments:[]}));document.getElementById('cmt-list').innerHTML=(j.comments||[]).map(c=>`<div class="cmt-item">${esc(c.text)}</div>`).join('');document.getElementById('cmt-text').value='';let el=document.getElementById('cc-'+id);if(el)el.textContent=(j.comments||[]).length;}
168
+
169
+ // === Patch regular articles ===
170
+ function patchArticle(){let art=document.querySelector('#view-article .article-view');if(!art)return;art.querySelectorAll('button[onclick*="rewriteCurrentArticle"],[data-rewrite],.rewrite-injected').forEach(e=>e.remove());art.querySelectorAll('.article-ai-ask').forEach((e,i)=>{if(i>0)e.remove();});if(!art.querySelector('[data-rw-article]')){let a=art.querySelector('.article-actions');if(a){let b=document.createElement('button');b.className='primary';b.setAttribute('data-rw-article','1');b.textContent='🤖 Rewrite AI đăng tường';b.onclick=function(){doRewriteArticle(b)};a.insertBefore(b,a.firstChild);}}if(!art.querySelector('.article-ai-ask')){let box=document.createElement('div');box.className='article-ai-ask';box.innerHTML='<h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="article-ai-question" placeholder="Hỏi..."></textarea><button onclick="askArticleAI()">Hỏi</button><div id="article-ai-answer" class="article-ai-answer"></div>';art.appendChild(box);}}
171
+ function patchShortBtns(){document.querySelectorAll('.tiktok-slide').forEach(sl=>{if(sl.dataset.cmtDone)return;sl.dataset.cmtDone='1';let id=sl.dataset.id||'';if(!id)return;let r=sl.querySelector('.tiktok-right');if(!r||r.querySelector('[data-cmt]'))return;let b=document.createElement('button');b.className='tiktok-right-btn';b.setAttribute('data-cmt','1');b.innerHTML='<div class="icon">💬</div><div class="count">0</div>';b.onclick=function(e){e.stopPropagation();openShortComments(id);};r.appendChild(b);fetch('/api/short/comments?id='+encodeURIComponent(id)).then(r=>r.json()).then(j=>{b.querySelector('.count').textContent=(j.comments||[]).length}).catch(()=>{});});}
172
+ function patchOldSourceLinks(){document.querySelectorAll('.source-detail-item a[target="_blank"],.source-detail-item a[href]').forEach(a=>{if(a.dataset.p7)return;a.dataset.p7='1';let url=a.href||'';a.removeAttribute('target');a.removeAttribute('href');a.textContent='📖 Xem trên VNEWS';a.className='source-vnews-btn';a.style.cursor='pointer';a.onclick=function(e){e.preventDefault();e.stopPropagation();if(url&&typeof readArticle==='function')readArticle(url);}});}
173
+
174
+ let oldRA=window.readArticle;if(oldRA){window.readArticle=async function(){let ret=await oldRA.apply(this,arguments);setTimeout(patchArticle,500);return ret;}}
175
+ let _hl=false;function dH(){if(_hl)return;_hl=true;setTimeout(()=>{if(typeof ensureHotTopics==='function')ensureHotTopics();if(typeof ensureNewsShortsHome==='function')ensureNewsShortsHome();},4000);}
176
+ if(document.readyState==='complete')dH();else window.addEventListener('load',dH);
177
+ setInterval(()=>{patchArticle();patchShortBtns();patchOldSourceLinks();},1500);
178
+ })();
179
+ </script>
180
+ '''
181
+
182
+ @app.get('/')
183
+ async def _index():
184
+ html=f5.f4.f3.f2.f1._load_index_html()
185
+ body=getattr(rt.old,'PATCH_INJECT','')+f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT
186
+ body+=getattr(f6,'FINAL6_INJECT','');body+=getattr(f6,'FINAL6_FAST_HOME_INJECT','');body+=getattr(f6,'FINAL6E_INJECT','')
187
+ body+=PATCH_INJECT
188
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
app/static/app_v2.js ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // === VNEWS Frontend v2 - Full Functions ===
2
+ // Updated: Voice selector + speed control + image gallery + auto voice detect
3
+
4
+ // === LOAD HOME ===
5
+ async function loadHome(){
6
+ const[featured,sh,wall,hlLeagues,ai,wcData]=await Promise.all([
7
+ fetch('/api/livescore/featured').then(r=>r.json()).catch(()=>null),
8
+ fetch('/api/shorts').then(r=>r.json()).catch(()=>[]),
9
+ fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]})),
10
+ fetch('/api/highlights/leagues').then(r=>r.json()).catch(()=>({})),
11
+ fetch('/api/genk_ai').then(r=>r.json()).catch(()=>[]),
12
+ fetch('/api/wc2026').then(r=>r.json()).catch(()=>null)
13
+ ]);
14
+ _hlLeagueData=hlLeagues;
15
+ _wc2026Data=wcData;
16
+ _shortsData=interleaveShorts(sh||[]);
17
+ _wallPosts=(wall&&wall.posts)||[];
18
+ let h='';
19
+ if(featured&&featured.home){const sc=featured.status==='live'?'':'upcoming';const st=featured.status==='live'?`🔴 ${featured.minute||'LIVE'}`:`⏰ ${featured.time}`;h+=`<div class="featured-match" onclick="openMatch('${featured.event_id}')"><div class="fm-league">${featured.league}</div><div class="fm-teams"><div class="fm-team"><img src="${featured.home_logo}" onerror="this.style.display='none'"><span>${featured.home}</span></div><div class="fm-score">${featured.score||'VS'}</div><div class="fm-team"><img src="${featured.away_logo}" onerror="this.style.display='none'"><span>${featured.away}</span></div></div><div class="fm-status ${sc}">${st}</div></div>`;}
20
+ h+=`<div class="ai-compose"><div class="ai-compose-title">🤖 AI viết bài</div><div class="ai-compose-row"><input id="topic-input" placeholder="Nhập chủ đề..."><button onclick="searchTopic()">Tìm nguồn</button></div><div class="ai-compose-row"><input id="url-input" placeholder="Dán URL bài viết..."><button class="secondary" onclick="rewriteUrl()">Rewrite</button></div><div id="hot-topics" class="hot-topic-row"></div></div>`;
21
+ h+='<div id="hashtag-box"></div>';
22
+ h+=`<div class="ls-section"><div class="ls-header"><h3>⚽ Livescore</h3></div><div class="ls-tabs"><span class="ls-tab active" data-tab="today" onclick="loadLivescore('today')">📅 Hôm nay</span><span class="ls-tab" data-tab="live" onclick="loadLivescore('live')">🔴 Live</span><span class="ls-tab" data-tab="incoming" onclick="loadLivescore('incoming')">⏰ Sắp tới</span><span class="ls-tab" data-tab="results" onclick="loadLivescore('results')">✅ Kết quả</span><span class="ls-tab" data-tab="bxh_nha" onclick="loadLivescore('bxh_nha')">🏆 NHA</span><span class="ls-tab" data-tab="bxh_laliga" onclick="loadLivescore('bxh_laliga')">🏆 La Liga</span></div><div class="ls-content" id="ls-content"><div class="loading">Đang tải...</div></div></div>`;
23
+ h+=`<div id="wc2026-live-section" class="wc2026-section"><div class="wc-header"><h2>🏆 World Cup 2026</h2><span class="wc-live-badge">● LIVE</span></div><div class="wc-tabs"><span class="wc-tab active" onclick="switchWCTab('news')">📰 Tin tức</span><span class="wc-tab" onclick="switchWCTab('fixtures')">📅 Lịch thi đấu</span><span class="wc-tab" onclick="switchWCTab('standings')">🏆 BXH</span><span class="wc-tab" onclick="switchWCTab('highlights')">🎬 Highlight</span><span class="wc-tab" onclick="switchWCTab('stats')">📊 Thống kê</span></div><div class="wc-content" id="wc-content"><div class="loading">Đang tải World Cup 2026...</div></div></div>`;
24
+ const wallPosts=_wallPosts;
25
+ const aiShorts=wallPosts.filter(p=>p.video);
26
+ if(aiShorts.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🎬 Short AI</span></div><div class="slider-track">';aiShorts.slice(0,20).forEach((p,i)=>{h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${p.video}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div></div>';}
27
+ if(_shortsData.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS</span><span class="slider-note">Mới nhất · xen kẽ</span></div><div class="slider-track">';_shortsData.slice(0,30).forEach((a,i)=>{const badge=a.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';h+=`<div class="slider-item shorts-item" onclick="openYTShortsFeed(${i})"><div class="slider-thumb shorts-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title"><span style="color:#f0c040;font-size:8px">${badge}</span> ${esc(a.title)}</div></div>`;});h+='</div></div>';}
28
+ if(wallPosts.length){h+=`<div class="slider-wrap" id="ai-wall-wrap"><div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">`;wallPosts.slice(0,20).forEach((p,i)=>{h+=makeWallItem(p,i)});h+='</div></div>';}
29
+ const HL_CONFIG={"world-cup":{name:"World Cup 2026",emoji:"🌍"},"premier-league":{name:"Premier League",emoji:"🏴"},"champions-league":{name:"Champions League",emoji:"⭐"},"la-liga":{name:"La Liga",emoji:"🇪🇸"},"serie-a":{name:"Serie A",emoji:"🇮🇹"},"bundesliga":{name:"Bundesliga",emoji:"🇩🇪"},"friendly":{name:"Giao hữu",emoji:"🤝"}};
30
+ for(const[key,cfg] of Object.entries(HL_CONFIG)){const vids=hlLeagues[key];if(!vids||!vids.length)continue;h+=`<div class="slider-wrap"><div class="slider-header"><span class="slider-label">${cfg.emoji} ${cfg.name}</span></div><div class="slider-track">`;vids.slice(0,8).forEach((a,i)=>{h+=`<div class="slider-item" onclick="openHighlightFeed('${key}',${i})"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
31
+ if(ai&&ai.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🤖 Ứng dụng AI</span></div><div class="slider-track">';ai.slice(0,12).forEach(a=>{h+=`<div class="slider-item" onclick="readArticle('${esc(a.link)}')"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}</div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
32
+ document.getElementById('view-home').innerHTML=h;
33
+ loadLivescore('today');loadHotTopics();
34
+ if(_wc2026Data)switchWCTab('news');
35
+ }
36
+
37
+ // === WALL POST HELPERS ===
38
+ function makeWallItem(p,i){
39
+ const hasVideo = p.video && p.video.length > 0;
40
+ const thumbContent = p.img
41
+ ? `<img src="${esc(p.img)}" onerror="this.style.display='none'">`
42
+ : (hasVideo ? `<video src="${esc(p.video)}" muted></video>` : '');
43
+ const videoBadge = hasVideo
44
+ ? `<div class="wall-video-badge">🎬</div>`
45
+ : '';
46
+ const videoBtn = hasVideo
47
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(${i})">▶ Xem Short</button>`
48
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(p.id||i)}',this)">🎬 Tạo Video</button>`;
49
+
50
+ return `<div class="wall-item" id="wall-item-${esc(p.id||i)}">
51
+ <div class="wall-thumb">
52
+ ${thumbContent}
53
+ ${videoBadge}
54
+ </div>
55
+ <div class="wall-title">${esc(p.title)}</div>
56
+ <div class="wall-text">${esc((p.text||'').slice(0,180))}</div>
57
+ <div class="wall-actions">
58
+ <button class="primary" onclick="readWallPost(${i})">Xem</button>
59
+ ${videoBtn}
60
+ </div>
61
+ </div>`;
62
+ }
63
+
64
+ // === GENERATE SHORT VIDEO FOR A WALL POST ===
65
+ async function makeShortVideo(postId, btn, voice, speed){
66
+ if(!postId)return;
67
+ const origText = btn ? btn.textContent : '🎬 Tạo Video';
68
+ if(btn){btn.disabled=true;btn.textContent='⏳ Đang tạo...';}
69
+ toast('⏳ Đang tạo video shorts...');
70
+ try{
71
+ let url = '/api/ai/short/'+encodeURIComponent(postId);
72
+ const params = [];
73
+ if(voice) params.push('voice='+encodeURIComponent(voice));
74
+ if(speed) params.push('speed='+encodeURIComponent(speed));
75
+ if(params.length) url += '?' + params.join('&');
76
+ const r = await fetch(url, {method:'POST'});
77
+ const j = await r.json();
78
+ if(!r.ok || j.error) throw new Error(j.error||'Lỗi tạo video');
79
+ toast('✅ Đã tạo video shorts!');
80
+ const p = _wallPosts.find(x => String(x.id) === String(postId));
81
+ if(p){
82
+ p.video = j.video;
83
+ const itemId = 'wall-item-'+postId;
84
+ const el = document.getElementById(itemId);
85
+ if(el){
86
+ const idx = _wallPosts.indexOf(p);
87
+ el.outerHTML = makeWallItem(p, idx);
88
+ const newEl = document.getElementById(itemId);
89
+ if(newEl) newEl.className = 'wall-item wall-item-new';
90
+ }
91
+ }
92
+ refreshShortAISlider();
93
+ }catch(e){
94
+ toast('❌ '+e.message);
95
+ if(btn){btn.disabled=false;btn.textContent=origText;}
96
+ }
97
+ }
98
+
99
+ // Refresh Short AI slider after video generation
100
+ function refreshShortAISlider(){
101
+ const aiShorts = _wallPosts.filter(p=>p.video);
102
+ let shortAISection = document.getElementById('short-ai-section');
103
+ if(aiShorts.length === 0){
104
+ if(shortAISection) shortAISection.remove();
105
+ return;
106
+ }
107
+ if(shortAISection){
108
+ const track = shortAISection.querySelector('.slider-track');
109
+ if(track){
110
+ let h = '';
111
+ aiShorts.slice(0,20).forEach((p,i)=>{
112
+ h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${esc(p.video)}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`;
113
+ });
114
+ track.innerHTML = h;
115
+ }
116
+ }
117
+ }
118
+
119
+ function prependWallPost(post){
120
+ _wallPosts.unshift(post);
121
+ const track=document.getElementById('ai-wall-track');
122
+ const wrap=document.getElementById('ai-wall-wrap');
123
+ const homeEl=document.getElementById('view-home');
124
+ if(!track||!wrap){
125
+ if(homeEl){
126
+ let insertBefore=homeEl.querySelector('.slider-wrap');
127
+ const newWrap=document.createElement('div');
128
+ newWrap.className='slider-wrap';
129
+ newWrap.id='ai-wall-wrap';
130
+ newWrap.innerHTML=`<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">${makeWallItem(post,0)}</div>`;
131
+ if(insertBefore){
132
+ homeEl.insertBefore(newWrap,insertBefore);
133
+ }else{
134
+ homeEl.appendChild(newWrap);
135
+ }
136
+ const firstItem=newWrap.querySelector('.wall-item');
137
+ if(firstItem)firstItem.className='wall-item wall-item-new';
138
+ }
139
+ return;
140
+ }
141
+ const div=document.createElement('div');
142
+ div.className='wall-item wall-item-new';
143
+ div.id='wall-item-'+(post.id||'new-'+Date.now());
144
+ const hasVideo = post.video && post.video.length > 0;
145
+ const thumbContent = post.img
146
+ ? `<img src="${esc(post.img)}" onerror="this.style.display='none'">`
147
+ : (hasVideo ? `<video src="${esc(post.video)}" muted></video>` : '');
148
+ const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
149
+ const videoBtn = hasVideo
150
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(0)">▶ Xem Short</button>`
151
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(post.id)}',this)">🎬 Tạo Video</button>`;
152
+ div.innerHTML=`<div class="wall-thumb">${thumbContent}${videoBadge}</div><div class="wall-title">${esc(post.title)}</div><div class="wall-text">${esc((post.text||'').slice(0,180))}</div><div class="wall-actions"><button class="primary" onclick="readWallPost(0)">Xem</button>${videoBtn}</div>`;
153
+ track.prepend(div);
154
+ track.scrollTo({left:0,behavior:'smooth'});
155
+ if(hasVideo) refreshShortAISlider();
156
+ }
157
+
158
+ // === REST OF FUNCTIONS ===
159
+ let _shortsData=[];
160
+ let _wallPosts=[];
161
+ let _currentView='home';
162
+ function interleaveShorts(shorts){const dt=shorts.filter(s=>s.channel==='baodantri7941');const sk=shorts.filter(s=>s.channel==='baosuckhoedoisongboyte');const result=[];let i=0,j=0;while(i<dt.length||j<sk.length){if(i<dt.length)result.push(dt[i++]);if(j<sk.length)result.push(sk[j++]);}return result;}
163
+ let _htPage=0,_htTopic='';
164
+ async function loadHotTopics(){const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));const el=document.getElementById('hot-topics');if(!el)return;el.innerHTML=(j.topics||[]).slice(0,18).map(t=>{const topicText=t.topic||t.label.replace(/^#/,'');return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;}).join('');if(j.topics&&j.topics[0]){const firstTopic=j.topics[0].topic||j.topics[0].label.replace(/^#/,'');setTimeout(()=>searchTopic(firstTopic),800);}}
165
+ function searchTopic(topic){if(!topic){topic=document.getElementById('topic-input')?.value.trim();if(!topic){alert('Nhập chủ đề');return;}}document.getElementById('topic-input').value='';_htTopic=topic;_htPage=0;showHashtagSources(topic,0);}
166
+ async function showHashtagSources(topic,page){const box=document.getElementById('hashtag-box');if(!box)return;if(page===0)box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Đang tìm...</div></div>`;try{const r=await fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=${page}`);const j=await r.json();const sources=j.sources||[];if(!sources.length&&page===0){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#888;padding:8px">Không tìm được bài viết liên quan</div></div>`;return;}let h='';if(page===0)h=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)} <span style="font-size:10px;color:#888">(${j.total} bài từ 8 nguồn)</span></h3><div id="ht-list">`;sources.forEach((s,i)=>{const idx=page*8+i;h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${idx}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')}</div></div></div>`;});if(page===0){h+=`</div><button class="hashtag-rewrite-btn" onclick="rewriteHashtag('${esc(topic).replace(/'/g,"\\'")}')">🤖 Rewrite AI tổng hợp & đăng tường</button>`;if(j.has_more)h+=`<button class="hashtag-load-more" id="ht-more" onclick="loadMoreHashtag()">Tải thêm ▼</button>`;h+=`</div>`;box.innerHTML=h;}else{document.getElementById('ht-list')?.insertAdjacentHTML('beforeend',h);const btn=document.getElementById('ht-more');if(btn){if(!j.has_more)btn.remove();else{btn.disabled=false;btn.textContent='Tải thêm ▼';}}}sources.forEach((s,i)=>{const idx=page*8+i;if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+idx);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#e74c3c;padding:8px">Lỗi: ${esc(e.message)}</div></div>`;}}
167
+ function loadMoreHashtag(){_htPage++;const btn=document.getElementById('ht-more');if(btn){btn.disabled=true;btn.textContent='Đang tải...';}showHashtagSources(_htTopic,_htPage);}
168
+ async function rewriteHashtag(topic){const btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}try{const r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');toast('✅ Đã đăng Tường AI!');if(btn)btn.textContent='✅ Đăng thành công!';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message);if(btn){btn.disabled=false;btn.textContent='🤖 Rewrite AI';}}}
169
+ async function loadLivescore(tab){document.querySelectorAll('.ls-tab').forEach(t=>t.classList.remove('active'));document.querySelector(`.ls-tab[data-tab="${tab}"]`)?.classList.add('active');const el=document.getElementById('ls-content');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';let ep='/api/livescore/'+tab;if(tab.startsWith('bxh_'))ep='/api/livescore/standings/'+tab.replace('bxh_','');try{const r=await fetch(ep);const d=await r.json();el.innerHTML=d.html&&d.html.length>50?d.html:'<div class="loading">Không có dữ liệu</div>';bindMatchClicks(el);}catch(e){el.innerHTML='<div class="loading">Lỗi</div>';}}
170
+ function bindMatchClicks(el){el.querySelectorAll('.match-detail').forEach(md=>{md.addEventListener('click',function(e){e.preventDefault();const a=this.querySelector('.status a');if(a){const m=(a.getAttribute('href')||'').match(/\/tran-dau\/(\d+)\//);if(m)openMatch(m[1])}})});el.querySelectorAll('a').forEach(a=>a.addEventListener('click',e=>e.preventDefault()))}
171
+ function openMatch(id){if(!id)return;_currentEventId=id;document.getElementById('match-overlay').classList.add('active');document.body.style.overflow='hidden';loadMatchTab('comm')}
172
+ function closeMatch(){document.getElementById('match-overlay').classList.remove('active');document.body.style.overflow=''}
173
+ async function loadMatchTab(tab){document.querySelectorAll('.mo-tab').forEach(t=>t.classList.remove('active'));document.querySelectorAll('.mo-tab').forEach(t=>{if((tab==='comm'&&t.textContent==='Diễn biến')||(tab==='stats'&&t.textContent==='Thống kê'))t.classList.add('active')});const el=document.getElementById('mo-body');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch(tab==='stats'?`/api/match/${_currentEventId}/stats`:`/api/match/${_currentEventId}/commentaries`);const d=await r.json();el.innerHTML=d.html||'<div class="loading">Không có dữ liệu</div>'}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
174
+ async function doInteract(videoId,type){try{const r=await fetch('/api/v2/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,type})});return await r.json();}catch(e){return null;}}
175
+ async function getInteractions(videoId){try{return await fetch('/api/v2/interactions?id='+encodeURIComponent(videoId)).then(r=>r.json());}catch(e){return{views:0,likes:0,comments:0};}}
176
+ async function getComments(videoId){try{const j=await fetch('/api/v2/comments?id='+encodeURIComponent(videoId)).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
177
+ async function postComment(videoId,text){try{const j=await fetch('/api/v2/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,text})}).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
178
+ function buildTikTokSlide(opts){return`<div class="tiktok-slide" data-vid="${esc(opts.videoId)}">${opts.vtag}<div class="tiktok-bottom"><span class="badge ${opts.badgeClass||'badge-fpt'}">${opts.badge||''}</span><p class="tiktok-title">${esc(opts.title)}</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation();doView('${esc(opts.videoId)}',this)"><div class="icon">👁</div><div class="count" id="vc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doLike('${esc(opts.videoId)}',this)"><div class="icon">❤️</div><div class="count" id="lc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();toggleComments('${esc(opts.videoId)}',${opts.idx})"><div class="icon">💬</div><div class="count" id="cc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doShare('${esc(opts.title)}','${esc(opts.shareUrl||'')}','')"><div class="icon">📤</div></button>${opts.extraBtn||''}</div><span class="tiktok-counter">${opts.idx+1}/${opts.total}</span><div class="inline-comments" id="cmt-inline-${opts.idx}" style="display:none"></div></div>`;}
179
+ async function doView(videoId,btn){const j=await doInteract(videoId,'view');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.views);}}
180
+ async function doLike(videoId,btn){const j=await doInteract(videoId,'like');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.likes);}}
181
+ function fmtNum(n){if(!n)return'0';if(n>=1000000)return(n/1000000).toFixed(1)+'M';if(n>=1000)return(n/1000).toFixed(1)+'K';return String(n);}
182
+ async function loadCounters(videoIds){for(let i=0;i<videoIds.length;i++){const id=videoIds[i];if(!id)continue;const j=await getInteractions(id);const vc=document.getElementById('vc-'+i);if(vc)vc.textContent=fmtNum(j.views);const lc=document.getElementById('lc-'+i);if(lc)lc.textContent=fmtNum(j.likes);const cc=document.getElementById('cc-'+i);if(cc)cc.textContent=fmtNum(j.comments);}}
183
+ async function toggleComments(videoId,idx){const panel=document.getElementById('cmt-inline-'+idx);if(!panel)return;if(panel.style.display!=='none'){panel.style.display='none';return;}panel.style.display='block';panel.innerHTML='<div style="padding:8px;color:#888;font-size:11px">Đang tải...</div>';const cmts=await getComments(videoId);renderInlineComments(panel,videoId,idx,cmts);}
184
+ function renderInlineComments(panel,videoId,idx,cmts){let h='<div class="inline-cmt-header"><span>💬 Bình luận</span><button onclick="document.getElementById(\'cmt-inline-'+idx+'\').style.display=\'none\'">✕</button></div><div class="inline-cmt-list">';if(cmts.length){cmts.slice(-30).forEach(c=>{h+=`<div class="inline-cmt-item"><span class="inline-cmt-time">${c.time||''}</span>${esc(c.text)}</div>`;});}else{h+='<div style="color:#777;font-size:11px;padding:4px">Chưa có bình luận</div>';}h+=`</div><div class="inline-cmt-input"><input id="cmt-input-${idx}" placeholder="Viết bình luận..." onkeydown="if(event.key==='Enter')submitInlineCmt('${esc(videoId)}',${idx})"><button onclick="submitInlineCmt('${esc(videoId)}',${idx})">Gửi</button></div>`;panel.innerHTML=h;const list=panel.querySelector('.inline-cmt-list');if(list)list.scrollTop=list.scrollHeight;}
185
+ async function submitInlineCmt(videoId,idx){const inp=document.getElementById('cmt-input-'+idx);if(!inp)return;const text=inp.value.trim();if(!text)return;inp.value='';inp.disabled=true;const cmts=await postComment(videoId,text);inp.disabled=false;const panel=document.getElementById('cmt-inline-'+idx);if(panel)renderInlineComments(panel,videoId,idx,cmts);const cc=document.getElementById('cc-'+idx);if(cc)cc.textContent=fmtNum(cmts.length);}
186
+ function initTikTokFeed(){const feed=document.getElementById('tiktok-feed');if(!feed)return;const slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{const v=sl.querySelector('video');const fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls&&!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){const hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,()=>v.play().catch(()=>{}));v._hls=hls}else if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;const vid=sl.dataset.vid;if(vid&&!sl._viewed){sl._viewed=true;doInteract(vid,'view').then(j=>{if(j){const vc=document.getElementById('vc-'+idx);if(vc)vc.textContent=fmtNum(j.views);}});}}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null}}if(fr&&fr.src)fr.src=''}});cur=i}let sT;feed.addEventListener('scroll',()=>{clearTimeout(sT);sT=setTimeout(()=>{const rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2;let best=-1,bestD=1e9;slides.forEach((sl,i)=>{const d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i}});if(best>=0)act(best)},150)});setTimeout(()=>act(0),400);slides.forEach(sl=>{const v=sl.querySelector('video');if(v)v.addEventListener('click',e=>{e.preventDefault();v.paused?v.play().catch(()=>{}):v.pause()})});const ids=[...slides].map(sl=>sl.dataset.vid||'');loadCounters(ids);}
187
+ async function openHighlightFeed(league,idx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';let articles=(_hlLeagueData||{})[league]||[];if(!articles.length){try{articles=await fetch('/api/highlights/'+league).then(r=>r.json())}catch(e){articles=[]}}if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return}const vids=[];const results=await Promise.all(articles.map(async(a,i)=>{try{const r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));const v=await r.json();if(v&&v.src)return{...a,...v,_idx:i}}catch(e){}return null}));results.forEach(r=>{if(r)vids.push(r)});vids.sort((a,b)=>a._idx-b._idx);if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return}let ti=vids.findIndex(v=>v._idx===idx);if(ti<0)ti=0;const ordered=ti>0?[...vids.slice(ti),...vids.slice(0,ti)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Highlight</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const isYT=v.type==='youtube',isHLS=!isYT&&v.src?.includes('.m3u8'),poster=v.poster?` poster="${v.poster}"`:'';const vtag=isYT?`<iframe data-yt-src="${v.src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`:isHLS?`<video playsinline preload="none"${poster} data-hls="${v.src}" loop controls></video>`:`<video playsinline preload="none"${poster} loop controls><source src="${v.src}" type="video/mp4"></video>`;const videoId='hl-'+league+'-'+(v.id||v._idx);h+=buildTikTokSlide({vtag,title:v.title,badge:'HL',badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:v.link||'',extraBtn:`<button class="tiktok-right-btn" onclick="event.stopPropagation();this.closest('.tiktok-slide').classList.toggle('ratio-wide')"><div class="icon">⬜</div><div class="count">16:9</div></button>`});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
188
+ async function openYTShortsFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=_shortsData.length?_shortsData:await fetch('/api/shorts').then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có shorts</div>';return}const ordered=startIdx>0?[...arts.slice(startIdx),...arts.slice(0,startIdx)]:arts;let h=`<button class="back-btn" onclick="switchCat('home')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const id=v.id||'';const src=`https://www.youtube.com/embed/${id}?autoplay=1&rel=0&playsinline=1`;const vtag=`<iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`;const badge=v.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';const videoId='yt-'+id;h+=buildTikTokSlide({vtag,title:v.title,badge,badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:'https://youtube.com/watch?v='+id});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
189
+ async function openShortAIFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const wall=(await fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];const vids=wall.filter(p=>p.video);if(!vids.length){el.innerHTML='<div class="loading">Chưa có Short AI</div>';return}const ordered=startIdx>0?[...vids.slice(startIdx),...vids.slice(0,startIdx)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((p,i)=>{const vtag=`<video src="${p.video}" playsinline loop controls></video>`;const videoId='ai-'+(p.id||i);h+=buildTikTokSlide({vtag,title:p.title,badge:'AI',badgeClass:'badge-ai',videoId,idx:i,total:ordered.length,shareUrl:SPACE});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
190
+ async function readArticle(url){showView('view-article');const el=document.getElementById('view-article');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/article?url='+encodeURIComponent(url));const data=await r.json();if(data&&!data.error&&data.body&&data.body.length){_currentArticle={url,data};let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><h1 class="article-title">${esc(data.title)}</h1>`;if(data.summary)h+=`<div class="article-summary">${esc(data.summary)}</div>`;const seen={};data.body.forEach(b=>{if(b.type==='p')h+=`<p class="article-p">${b.text}</p>`;else if(b.type==='img'&&b.src&&!seen[b.src]){seen[b.src]=1;h+=`<img class="article-img" src="${esc(b.src)}" onerror="this.style.display='none'">`}else if(b.type==='heading')h+=`<h2 class="article-h2">${esc(b.text)}</h2>`});h+=`<div class="article-actions"><button class="primary" onclick="rewriteArticle()">🤖 Rewrite AI đăng tường</button><button onclick="doShare('${esc(data.title)}','${esc(url)}','${esc(data.og_image||'')}')">📤</button><button onclick="window.open('${esc(url)}','_blank')">🔗 Gốc</button></div><div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="ask-q" placeholder="Hỏi về bài viết..."></textarea><button onclick="askAI()">Hỏi</button><div id="ask-a" class="article-ai-answer"></div></div></div>`;el.innerHTML=h;window.scrollTo(0,0);return;}}catch(e){}el.innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="loading"><p>Không đọc được.</p><a href="${esc(url)}" target="_blank" style="color:#5cb87a">Mở gốc →</a></div>`;}
191
+ async function rewriteArticle(){const url=_currentArticle?.url;if(!url)return;toast('⏳ Đang rewrite...');try{const r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,context:document.querySelector('.article-view')?.innerText?.slice(0,14000)||''})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng Tường AI!');if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
192
+ async function rewriteUrl(){const url=document.getElementById('url-input')?.value.trim();if(!url)return alert('Dán URL');toast('⏳ Đang rewrite...');try{const r=await fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng!');document.getElementById('url-input').value='';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
193
+ async function askAI(){const q=document.getElementById('ask-q')?.value.trim();if(!q)return alert('Nhập câu hỏi');const a=document.getElementById('ask-a');a.textContent='Đang hỏi...';try{const r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:_currentArticle?.url||'',question:q,context:document.querySelector('.article-view')?.innerText?.slice(0,12000)||''})});const j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}}
194
+ async function readWallPost(i){const p=_wallPosts[i];if(!p)return;showView('view-article');
195
+ // Build image gallery HTML
196
+ const images = p.images || [];
197
+ let imgGallery = '';
198
+ if(images.length > 0){
199
+ imgGallery = '<div class="article-image-gallery">';
200
+ images.forEach((imgUrl, idx) => {
201
+ if(idx === 0){
202
+ imgGallery += `<img class="article-img article-hero-img" src="${esc(imgUrl)}" onerror="this.style.display='none" loading="eager">`;
203
+ } else {
204
+ if(idx === 1) imgGallery += '<div class="gallery-thumbs">';
205
+ imgGallery += `<div class="gallery-thumb"><img src="${esc(imgUrl)}" onerror="this.parentElement.style.display='none'" loading="lazy"></div>`;
206
+ }
207
+ });
208
+ if(images.length > 1) imgGallery += '</div>';
209
+ imgGallery += '</div>';
210
+ }
211
+ const hasVideo = p.video && p.video.length > 0;
212
+ const voiceOptions = [
213
+ {id:'hoaimy', label:'🎙️ Nữ — Hoài My'},
214
+ {id:'namminh', label:'🎙️ Nam — Nam Minh'},
215
+ ];
216
+ let voiceSelector = '';
217
+ if(!hasVideo){
218
+ voiceSelector = `<div class="tts-selector"><div class="tts-selector-label">🎙️ Chọn giọng đọc:</div><div class="tts-voice-btns">`;
219
+ voiceOptions.forEach(v=>{
220
+ voiceSelector += `<button class="tts-voice-btn" onclick="document.querySelectorAll('.tts-voice-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('selected-voice').value='${v.id}'">${v.label}</button>`;
221
+ });
222
+ voiceSelector += `</div><div class="tts-speed-row"><span>Tốc độ:</span><select id="selected-speed"><option value="1.0">1.0x — Bình thường</option><option value="1.2" selected>1.2x — Nhanh</option><option value="1.5">1.5x — Rất nhanh</option><option value="0.8">0.8x — Chậm</option></select></div>`;
223
+ voiceSelector += `<input type="hidden" id="selected-voice" value="hoaimy"></div>`;
224
+ }
225
+ document.getElementById('view-article').innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${imgGallery}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>${hasVideo?`<video class="article-img" src="${esc(p.video)}" controls playsinline style="max-height:400px"></video>`:''}<div class="article-actions">${hasVideo?`<button onclick="openShortAIFeed(${i})">🎬 Xem Short</button>${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🔄 Tạo lại Short</button>`:`${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🎬 Tạo Video Shorts</button>`}<button onclick="doShare('${esc(p.title)}','${SPACE}','${esc(p.img||'')}')">📤</button></div></div>`;
226
+ const firstVoiceBtn = document.querySelector('.tts-voice-btn');
227
+ if(firstVoiceBtn) firstVoiceBtn.classList.add('active');
228
+ window.scrollTo(0,0)}
229
+ async function loadNewsTab(){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/homepage');const news=await r.json();if(!news.length){el.innerHTML='<div class="loading">Không có tin</div>';return}const groups={};news.forEach(a=>{if(!groups[a.group])groups[a.group]=[];groups[a.group].push(a)});let h='';for(const[g,arts] of Object.entries(groups)){h+=`<div class="section-title">${g}</div><div class="grid">`;arts.slice(0,6).forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'VnE')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>'}el.innerHTML=h}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
230
+ async function loadCat(id){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=await fetch('/api/category/'+id).then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có tin</div>';return}let h='<div class="grid">';arts.forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>';el.innerHTML=h}
231
+ fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){const home=document.getElementById('view-home');if(home){const w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ Persistent Storage chưa bật.';home.prepend(w)}}}).catch(()=>{});
232
+
233
+ // === AUTO-OPEN SHARE LINKS (/s?url=... sets pending_article) ===
234
+ (function(){
235
+ try{
236
+ const pa=localStorage.getItem('pending_article');
237
+ const pv=localStorage.getItem('pending_video');
238
+ if(pa){
239
+ localStorage.removeItem('pending_article');
240
+ setTimeout(()=>{
241
+ if(typeof readArticle==='function') readArticle(pa);
242
+ },1500);
243
+ }
244
+ if(pv){
245
+ localStorage.removeItem('pending_video');
246
+ try{
247
+ const v=JSON.parse(pv);
248
+ if(v&&v.url) setTimeout(()=>{window.open(v.url,'_blank')},1500);
249
+ }catch(e){}
250
+ }
251
+ }catch(e){}
252
+ })();
app_clean.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VNEWS Clean Backend - serves static/index_v2.html directly.
3
+ No injection layers. All APIs from existing modules preserved.
4
+ Comments feature REMOVED per user request.
5
+ """
6
+ import sys, os
7
+
8
+ # Import the full chain which registers all API endpoints on the FastAPI app
9
+ from app_main import app, _search_all, _clean
10
+
11
+ # Now override the root '/' to serve our clean frontend
12
+ from fastapi import Query, Request
13
+ from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
14
+ from fastapi.staticfiles import StaticFiles
15
+ import os
16
+
17
+ # Remove old '/' route
18
+ app.router.routes = [r for r in app.router.routes if not (
19
+ getattr(r, 'path', None) == '/' and 'GET' in getattr(r, 'methods', set())
20
+ )]
21
+
22
+ # Remove comment endpoints (user requested removal)
23
+ app.router.routes = [r for r in app.router.routes if not (
24
+ getattr(r, 'path', None) in ('/api/short/comments', '/api/short/comment')
25
+ )]
26
+
27
+ # Mount static files
28
+ STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static')
29
+ app.mount('/static', StaticFiles(directory=STATIC_DIR), name='static')
30
+
31
+ @app.get('/')
32
+ async def serve_index():
33
+ """Serve the clean v2 frontend - single HTML file, no injection."""
34
+ index_path = os.path.join(STATIC_DIR, 'index_v2.html')
35
+ if os.path.exists(index_path):
36
+ return FileResponse(index_path, media_type='text/html')
37
+ return HTMLResponse('<h1>VNEWS</h1><p>index_v2.html not found</p>', status_code=500)
38
+
39
+ # Keep /api/hashtag/sources using direct search (not Google News)
40
+ # This was already overridden in app_main.py with _search_all
41
+ # Just make sure it's accessible
42
+
43
+ # Storage status endpoint
44
+ @app.get('/api/storage_status')
45
+ def storage_status():
46
+ """Check if persistent storage is enabled."""
47
+ data_dir = '/data'
48
+ persistent = os.path.isdir(data_dir) and os.access(data_dir, os.W_OK)
49
+ return JSONResponse({'persistent': persistent, 'path': data_dir})
50
+
51
+ # Categories for the tab bar
52
+ @app.get('/api/categories')
53
+ def get_categories():
54
+ """Return category list for frontend tab bar."""
55
+ return JSONResponse([]) # Categories moved into News tab, homepage shows media content
56
+
57
+ # Share page
58
+ @app.get('/s')
59
+ async def share_page(url: str = '', title: str = '', img: str = ''):
60
+ """OG share page for social media."""
61
+ html = f'''<!DOCTYPE html><html><head>
62
+ <meta property="og:title" content="{_clean(title)}">
63
+ <meta property="og:url" content="{_clean(url)}">
64
+ <meta property="og:image" content="{_clean(img)}">
65
+ <meta property="og:type" content="article">
66
+ <meta property="og:site_name" content="VNEWS">
67
+ <meta http-equiv="refresh" content="0;url={_clean(url) or '/'}">
68
+ </head><body>Redirecting...</body></html>'''
69
+ return HTMLResponse(html)
app_final.py ADDED
@@ -0,0 +1,213 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Final wrapper with complete highlight override including interaction buttons.
2
+ PLUS: Hashtag inline sources on homepage with rewrite button."""
3
+ import json, os, time
4
+ from app_patch_unified import *
5
+ from app_patch_unified import app, UNIFIED_INJECT, f5, f6, rt, PATCH_INJECT
6
+ from fastapi.responses import HTMLResponse, JSONResponse
7
+ from fastapi import Request, Query
8
+
9
+ DATA_DIR="/data" if os.path.isdir('/data') else "/app/data"
10
+ os.makedirs(DATA_DIR,exist_ok=True)
11
+ HL_STATS_FILE=os.path.join(DATA_DIR,'highlight_stats.json')
12
+
13
+ def _load_hl():
14
+ try:
15
+ if os.path.exists(HL_STATS_FILE):return json.load(open(HL_STATS_FILE,'r',encoding='utf-8'))
16
+ except:pass
17
+ return {}
18
+ def _save_hl(db):
19
+ try:open(HL_STATS_FILE+'.tmp','w',encoding='utf-8').write(json.dumps(db,ensure_ascii=False));os.replace(HL_STATS_FILE+'.tmp',HL_STATS_FILE)
20
+ except:pass
21
+
22
+ app.router.routes=[r for r in app.router.routes if not (
23
+ (getattr(r,'path',None)=='/api/highlight/interact' and 'POST' in getattr(r,'methods',set())) or
24
+ (getattr(r,'path',None)=='/api/highlight/stats' and 'GET' in getattr(r,'methods',set())) or
25
+ (getattr(r,'path',None)=='/api/hashtag/sources' and 'GET' in getattr(r,'methods',set())) or
26
+ (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))
27
+ )]
28
+
29
+ @app.post('/api/highlight/interact')
30
+ async def _hl_act(request:Request):
31
+ b=await request.json();vid=str(b.get('id','')).strip();action=str(b.get('action','')).strip()
32
+ if not vid or action not in ('view','like','share'):return JSONResponse({'error':'invalid'},status_code=400)
33
+ db=_load_hl();st=db.get(vid,{'views':0,'likes':0,'shares':0})
34
+ st[action+'s']=st.get(action+'s',0)+1
35
+ db[vid]=st;_save_hl(db);return JSONResponse({'stats':st})
36
+
37
+ @app.get('/api/highlight/stats')
38
+ def _hl_stats(ids:str=Query(default='')):
39
+ db=_load_hl();out={}
40
+ for vid in ids.split(','):
41
+ vid=vid.strip()
42
+ if vid:out[vid]=db.get(vid,{'views':0,'likes':0,'shares':0})
43
+ return JSONResponse({'stats':out})
44
+
45
+ @app.get('/api/hashtag/sources')
46
+ def _hashtag_sources(topic:str=Query(...)):
47
+ """Return sources for a hashtag topic to display inline on homepage."""
48
+ research=f6._fast_context(topic) if hasattr(f6,'_fast_context') else f6._web_research_context(topic)
49
+ sources=research.get('sources',[])
50
+ # Add og:image for each source
51
+ from ai_runtime_patch_fast import _scrape
52
+ for s in sources[:6]:
53
+ if s.get('url') and not s.get('img'):
54
+ try:_,_,img=_scrape(s['url'],500)
55
+ except:img=''
56
+ s['img']=img if img and len(img)>20 else ''
57
+ return JSONResponse({'sources':sources[:6],'topic':topic})
58
+
59
+ # PRE_KILL fix
60
+ UNIFIED_INJECT_FIXED = UNIFIED_INJECT.replace(
61
+ """Object.defineProperty(window,'renderAIShorts7',{get:function(){return function(){}},set:function(){},configurable:true});""",
62
+ """Object.defineProperty(window,'renderAIShorts7',{get:function(){return function(){}},set:function(){},configurable:true});
63
+ Object.defineProperty(window,'renderPatchedWall',{get:function(){return function(){}},set:function(){},configurable:true});
64
+ Object.defineProperty(window,'renderAiShorts',{get:function(){return function(){}},set:function(){},configurable:true});
65
+ Object.defineProperty(window,'renderWall',{get:function(){return function(){}},set:function(){},configurable:true});
66
+ Object.defineProperty(window,'renderAIShorts',{get:function(){return function(){}},set:function(){},configurable:true});
67
+ Object.defineProperty(window,'loadPatchedWall',{get:function(){return function(){}},set:function(){},configurable:true});
68
+ Object.defineProperty(window,'refreshFinalWall3',{get:function(){return function(){}},set:function(){},configurable:true});"""
69
+ )
70
+
71
+ # Fix highlight fetch
72
+ UNIFIED_INJECT_FIXED = UNIFIED_INJECT_FIXED.replace(
73
+ "var articles=(window._hlLeagueData||{})[league]||[];\n if(!articles.length){el.innerHTML=",
74
+ "var articles=(window._hlLeagueData||{})[league]||[];\n if(!articles.length){try{var _r=await fetch('/api/highlights/'+league);articles=await _r.json();if(!Array.isArray(articles))articles=[];}catch(e){articles=[];}}\n if(!articles.length){el.innerHTML="
75
+ )
76
+
77
+ # Highlight full override (same as 5a5b626)
78
+ HIGHLIGHT_FULL_OVERRIDE = r'''
79
+ <style>
80
+ .tiktok-slide.ratio-wide video,.tiktok-slide.ratio-wide iframe{object-fit:contain!important}
81
+ .hl-ask-panel{position:fixed;bottom:0;left:0;right:0;max-height:50vh;background:#181818;border-radius:16px 16px 0 0;z-index:99999;padding:14px;display:none;overflow-y:auto}.hl-ask-panel.active{display:block}.hl-ask-panel textarea,.hl-ask-panel input{width:100%;background:#222;border:1px solid #444;color:#eee;border-radius:10px;padding:9px;margin:6px 0}.hl-ask-panel button{background:#2d8659;border:0;color:#fff;border-radius:10px;padding:8px 12px;margin:4px}.hl-ask-answer{white-space:pre-wrap;color:#ccc;font-size:12px;margin-top:8px}
82
+ .hashtag-sources{margin:8px 4px;background:#1a1a1a;border:1px solid #2a2a2a;border-radius:10px;padding:10px}.hashtag-sources h3{font-size:13px;color:#5cb87a;margin-bottom:8px}.hashtag-src-item{display:flex;gap:8px;padding:8px;background:#202020;border-radius:8px;margin:6px 0;cursor:pointer}.hashtag-src-item:active{opacity:.8}.hashtag-src-img{flex:0 0 80px;aspect-ratio:16/9;background:#333;border-radius:6px;overflow:hidden}.hashtag-src-img img{width:100%;height:100%;object-fit:cover}.hashtag-src-text{flex:1;min-width:0}.hashtag-src-title{font-size:12px;font-weight:700;color:#eee;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}.hashtag-src-via{font-size:10px;color:#888;margin-top:2px}.hashtag-rewrite-btn{width:100%;margin-top:8px;background:#2d8659;border:0;color:#fff;padding:9px;border-radius:10px;font-size:12px;font-weight:700;cursor:pointer}
83
+ </style>
84
+ <div id="hl-ask-panel" class="hl-ask-panel"></div>
85
+ <script>
86
+ (function(){
87
+ function esc(s){return String(s||'').replace(/[&<>"']/g,function(m){return{'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]});}
88
+
89
+ // === HASHTAG INLINE: click hashtag → show sources on homepage + rewrite button ===
90
+ window.showHashtagSources=async function(topic){
91
+ var home=document.getElementById('view-home');if(!home)return;
92
+ document.getElementById('hashtag-sources-box')?.remove();
93
+ var box=document.createElement('div');box.id='hashtag-sources-box';box.className='hashtag-sources';
94
+ box.innerHTML='<h3>🔍 '+esc(topic)+'</h3><div style="color:#888;font-size:11px">Đang tìm nguồn...</div>';
95
+ var compose=home.querySelector('.ai-compose');
96
+ if(compose)compose.after(box);else home.prepend(box);
97
+ try{
98
+ var r=await fetch('/api/hashtag/sources?topic='+encodeURIComponent(topic));
99
+ var j=await r.json();var sources=j.sources||[];
100
+ if(!sources.length){box.innerHTML='<h3>🔍 '+esc(topic)+'</h3><div style="color:#888;font-size:12px">Không tìm được nguồn</div>';return;}
101
+ var h='<h3>🔍 '+esc(topic)+' <span style="font-size:10px;color:#888">('+sources.length+' nguồn)</span></h3>';
102
+ sources.forEach(function(s){
103
+ h+='<div class="hashtag-src-item" onclick="if(typeof readArticle===\'function\')readArticle(\''+esc(s.url||'')+'\')">';
104
+ h+='<div class="hashtag-src-img">'+(s.img?'<img src="'+esc(s.img)+'" onerror="this.style.display=\'none\'">':'')+'</div>';
105
+ h+='<div class="hashtag-src-text"><div class="hashtag-src-title">'+esc(s.title)+'</div><div class="hashtag-src-via">'+esc(s.via||s.source||'')+'</div></div>';
106
+ h+='</div>';
107
+ });
108
+ h+='<button class="hashtag-rewrite-btn" onclick="rewriteHashtagTopic(\''+esc(topic)+'\')">🤖 Rewrite AI tổng hợp nguồn & đăng tường</button>';
109
+ box.innerHTML=h;
110
+ }catch(e){box.innerHTML='<h3>🔍 '+esc(topic)+'</h3><div style="color:#e74c3c;font-size:12px">Lỗi: '+esc(e.message)+'</div>';}
111
+ };
112
+
113
+ window.rewriteHashtagTopic=async function(topic){
114
+ var btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}
115
+ try{
116
+ var r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic:topic})});
117
+ var j=await r.json();
118
+ if(!r.ok||j.error)throw new Error(j.error||'Lỗi');
119
+ if(btn)btn.textContent='✅ Đã đăng lên Tường AI!';
120
+ setTimeout(function(){document.getElementById('hashtag-sources-box')?.remove();},2000);
121
+ }catch(e){
122
+ if(btn){btn.disabled=false;btn.textContent='❌ '+e.message;}
123
+ }
124
+ };
125
+
126
+ // Override hashtag chip click to use showHashtagSources instead of topic input
127
+ setTimeout(function(){
128
+ document.querySelectorAll('.hot-chip').forEach(function(chip){
129
+ chip.onclick=function(e){
130
+ e.preventDefault();e.stopPropagation();
131
+ var topic=chip.textContent.replace(/^#/,'').trim();
132
+ if(topic)showHashtagSources(topic);
133
+ };
134
+ });
135
+ },3000);
136
+ // Re-patch after hot topics load
137
+ setInterval(function(){
138
+ document.querySelectorAll('.hot-chip:not([data-patched])').forEach(function(chip){
139
+ chip.dataset.patched='1';
140
+ chip.onclick=function(e){
141
+ e.preventDefault();e.stopPropagation();
142
+ var topic=chip.textContent.replace(/^#/,'').trim();
143
+ if(topic)showHashtagSources(topic);
144
+ };
145
+ });
146
+ },2000);
147
+
148
+ // === FULL openLeaguePlayer override (same as before) ===
149
+ window.openLeaguePlayer=async function(league,idx){
150
+ showView('view-tiktok');document.querySelectorAll('.cat').forEach(function(x){x.classList.remove('active')});
151
+ var el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải highlight...</div>';
152
+ var cfg=(window.HL_CONFIG||{})[league]||{name:league,emoji:'🎬'};
153
+ var articles=(window._hlLeagueData||{})[league]||[];
154
+ if(!articles.length){try{var resp=await fetch('/api/highlights/'+league);articles=await resp.json();if(!Array.isArray(articles))articles=[];}catch(e){articles=[];}}
155
+ if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return;}
156
+ var vids=[];var results=await Promise.all(articles.map(async function(a,i){try{var r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));var v=await r.json();if(v&&v.src)return Object.assign({},a,v,{_idx:i});}catch(e){}return null;}));results.forEach(function(r){if(r)vids.push(r);});vids.sort(function(a,b){return a._idx-b._idx;});
157
+ if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return;}
158
+ var ti=vids.findIndex(function(v){return v._idx===idx;});if(ti<0)ti=0;var ordered=ti>0?vids.slice(ti).concat(vids.slice(0,ti)):vids;
159
+ var h='<button class="back-btn" onclick="switchCat(\'home\')">← '+esc(cfg.emoji)+' '+esc(cfg.name)+'</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';
160
+ ordered.forEach(function(v,i){var hlid=encodeURIComponent(v.link||v.title);var isYT=v.type==='youtube';var isHLS=!isYT&&v.src&&v.src.indexOf('.m3u8')>-1;var poster=v.poster?' poster="'+v.poster+'"':'';var vtag=isYT?'<iframe data-yt-src="'+v.src+'" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture" style="width:100%;height:100%;border:none"></iframe>':isHLS?'<video playsinline preload="none"'+poster+' data-hls="'+v.src+'" loop controls style="width:100%;height:100%;object-fit:cover"></video>':'<video playsinline preload="none"'+poster+' loop controls style="width:100%;height:100%;object-fit:cover"><source src="'+v.src+'" type="video/mp4"></video>';h+='<div class="tiktok-slide" id="tslide-'+i+'" data-hlid="'+hlid+'">'+vtag+'<div class="tiktok-bottom"><span class="badge badge-fpt">'+esc(cfg.name)+'</span><p class="tiktok-title">'+esc(v.title)+'</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation();hlAct(this,\'view\')"><div class="icon">👁</div><div class="count" data-a="views">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();hlAct(this,\'like\')"><div class="icon">❤️</div><div class="count" data-a="likes">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();openHlComments(\''+hlid+'\')"><div class="icon">💬</div><div class="count">BL</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();openHlAsk(\''+hlid+'\',\''+esc(v.title)+'\')"><div class="icon">🤖</div><div class="count">Hỏi</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();hlAct(this,\'share\');if(typeof doShareVideo===\'function\')doShareVideo(\''+esc(v.title)+'\',\''+esc(v.link||'')+'\',\''+esc(v.poster||v.img||'')+'\',\'highlights\')"><div class="icon">📤</div><div class="count" data-a="shares">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();toggleHlRatio(this)"><div class="icon">⬜</div><div class="count">16:9</div></button></div><span class="tiktok-counter">'+(i+1)+'/'+ordered.length+'</span></div>';});
161
+ h+='</div></div>';el.innerHTML=h;
162
+ var feed=document.getElementById('tiktok-feed');if(!feed)return;var slides=feed.querySelectorAll('.tiktok-slide');var cur=-1;
163
+ function act(i){if(i===cur)return;slides.forEach(function(sl,idx){var v=sl.querySelector('video');var fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls){if(!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){var hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,function(){v.play().catch(function(){});});v._hls=hls;}else if(v._hls)v.play().catch(function(){});}else if(v)v.play().catch(function(){});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;hlAct(sl.querySelector('.tiktok-right .tiktok-right-btn'),'view');}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null;}}if(fr&&fr.src)fr.src='';}});cur=i;}
164
+ var sT;feed.addEventListener('scroll',function(){clearTimeout(sT);sT=setTimeout(function(){var rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2,best=-1,bestD=1e9;slides.forEach(function(sl,i){var d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i;}});if(best>=0)act(best);},150);});
165
+ setTimeout(function(){act(0);},400);slides.forEach(function(sl){var v=sl.querySelector('video');if(v)v.addEventListener('click',function(e){e.preventDefault();v.paused?v.play().catch(function(){}):v.pause();});});
166
+ var ids=[];slides.forEach(function(sl){if(sl.dataset.hlid)ids.push(sl.dataset.hlid);});
167
+ if(ids.length)fetch('/api/highlight/stats?ids='+ids.join(',')).then(function(r){return r.json()}).then(function(j){var stats=j.stats||{};slides.forEach(function(sl){var st=stats[sl.dataset.hlid];if(!st)return;var r=sl.querySelector('.tiktok-right');if(!r)return;var vc=r.querySelector('[data-a="views"]');if(vc)vc.textContent=st.views||0;var lc=r.querySelector('[data-a="likes"]');if(lc)lc.textContent=st.likes||0;var sc=r.querySelector('[data-a="shares"]');if(sc)sc.textContent=st.shares||0;});}).catch(function(){});
168
+ };
169
+ window.hlAct=async function(btn,action){var slide=btn?btn.closest('.tiktok-slide'):null;var id=slide?slide.dataset.hlid:'';if(!id)return;try{var r=await fetch('/api/highlight/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:id,action:action})});var j=await r.json();if(j.stats&&slide){var right=slide.querySelector('.tiktok-right');if(right){var vc=right.querySelector('[data-a="views"]');if(vc)vc.textContent=j.stats.views||0;var lc=right.querySelector('[data-a="likes"]');if(lc)lc.textContent=j.stats.likes||0;var sc=right.querySelector('[data-a="shares"]');if(sc)sc.textContent=j.stats.shares||0;}}}catch(e){}};
170
+ window.toggleHlRatio=function(btn){var slide=btn.closest('.tiktok-slide');if(!slide)return;slide.classList.toggle('ratio-wide');var label=btn.querySelector('.count');if(label)label.textContent=slide.classList.contains('ratio-wide')?'1:1':'16:9';};
171
+ window.openHlComments=async function(id){var panel=document.getElementById('hl-ask-panel');var j=await fetch('/api/short/comments?id='+id).then(function(r){return r.json()}).catch(function(){return{comments:[]}});var cmts=j.comments||[];panel.innerHTML='<h3 style="color:#5cb87a;font-size:14px">💬 Bình luận</h3><div id="hl-cmt-list">'+(cmts.map(function(c){return'<div style="background:#222;border-radius:8px;padding:7px;margin:5px 0;color:#ccc;font-size:12px">'+esc(c.text)+'</div>'}).join('')||'<div style="color:#777;font-size:12px">Chưa có</div>')+'</div><textarea id="hl-cmt-text" placeholder="Bình luận..."></textarea><button onclick="submitHlCmt(\''+id+'\')">Gửi</button><button onclick="document.getElementById(\'hl-ask-panel\').classList.remove(\'active\')">Đóng</button>';panel.classList.add('active');};
172
+ window.submitHlCmt=async function(id){var t=document.getElementById('hl-cmt-text');if(!t||!t.value.trim())return;var j=await fetch('/api/short/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:id,text:t.value.trim()})}).then(function(r){return r.json()}).catch(function(){return{comments:[]}});document.getElementById('hl-cmt-list').innerHTML=(j.comments||[]).map(function(c){return'<div style="background:#222;border-radius:8px;padding:7px;margin:5px 0;color:#ccc;font-size:12px">'+esc(c.text)+'</div>'}).join('');t.value='';};
173
+ window.openHlAsk=function(id,title){var panel=document.getElementById('hl-ask-panel');panel.innerHTML='<h3 style="color:#5cb87a;font-size:14px">🤖 Hỏi AI</h3><input id="hl-ask-q" placeholder="Hỏi về: '+esc(title)+'..."><div id="hl-ask-ans" class="hl-ask-answer"></div><button onclick="submitHlAsk(\''+id+'\',\''+esc(title)+'\')">Hỏi</button><button onclick="document.getElementById(\'hl-ask-panel\').classList.remove(\'active\')">Đóng</button>';panel.classList.add('active');};
174
+ window.submitHlAsk=async function(id,title){var q=document.getElementById('hl-ask-q');if(!q||!q.value.trim())return;var ans=document.getElementById('hl-ask-ans');ans.textContent='Đang hỏi...';try{var r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({question:q.value.trim(),context:'Video highlight: '+decodeURIComponent(title||id)})});var j=await r.json();ans.textContent=j.answer||'Không trả lời được';}catch(e){ans.textContent='Lỗi: '+e.message}};
175
+ })();
176
+ </script>
177
+ '''
178
+
179
+ EXTRA_WALL_FIX = r'''
180
+ <style>[data-wall-live="1"]{display:none!important}</style>
181
+ <script>
182
+ (function(){
183
+ var _wc=setInterval(function(){
184
+ var home=document.getElementById('view-home');if(!home||!home.classList.contains('active'))return;
185
+ var has=document.getElementById('short-ai-final-slide');
186
+ if(!has&&typeof renderShortAISlide==='function')renderShortAISlide();
187
+ if(!document.querySelector('.slider-wrap[data-wall-live]')){
188
+ fetch('/api/ai_wall').then(function(r){return r.json()}).then(function(j){
189
+ var posts=(j&&j.posts)||[];if(!posts.length)return;
190
+ if(typeof window._serverWall!=='undefined')window._serverWall=posts;
191
+ if(typeof prependWallPost==='function')prependWallPost(posts[0]);
192
+ }).catch(function(){});
193
+ }
194
+ },4000);
195
+ setTimeout(function(){clearInterval(_wc);},30000);
196
+ })();
197
+ </script>
198
+ '''
199
+
200
+ @app.get('/')
201
+ async def _index_fixed():
202
+ html=f5.f4.f3.f2.f1._load_index_html()
203
+ body=''
204
+ body+=getattr(rt.old,'PATCH_INJECT','')
205
+ body+=f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT
206
+ body+=getattr(f6,'FINAL6_INJECT','')
207
+ body+=getattr(f6,'FINAL6_FAST_HOME_INJECT','')
208
+ body+=getattr(f6,'FINAL6E_INJECT','')
209
+ body+=PATCH_INJECT
210
+ body+=UNIFIED_INJECT_FIXED
211
+ body+=HIGHLIGHT_FULL_OVERRIDE
212
+ body+=EXTRA_WALL_FIX
213
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
app_main.py ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """VNEWS v2 - Clean frontend. CRITICAL: removes ALL old routes before registering new ones."""
2
+ from app_run import *
3
+ from app_run import app, f5, f6, rt, PATCH_INJECT, UNIFIED_INJECT_FIXED, HIGHLIGHT_FULL_OVERRIDE, EXTRA_WALL_FIX, FAST_HASHTAG_JS
4
+ from fastapi.responses import HTMLResponse, JSONResponse, FileResponse, Response
5
+ from fastapi.staticfiles import StaticFiles
6
+ from fastapi import Query, Request
7
+ import requests as req
8
+ from urllib.parse import quote
9
+ from bs4 import BeautifulSoup
10
+ import re, html as html_lib, os, json, threading, time
11
+ from concurrent.futures import ThreadPoolExecutor, as_completed
12
+
13
+ def _clean(s):return re.sub(r"\s+"," ",html_lib.unescape(str(s or ""))).strip()
14
+ _STOP_WORDS=set('và của các những một được trong với cho tại sau trước khi không người việt nam hôm nay mới nhất nóng tin tức cập nhật theo từ đến là có thì'.split())
15
+
16
+ def _relevance_score(topic, title):
17
+ topic_lower = topic.lower().strip();title_lower = (title or '').lower()
18
+ if topic_lower in title_lower: return 10
19
+ topic_words = [w for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+', topic_lower) if len(w) > 1 and w not in _STOP_WORDS]
20
+ if not topic_words: return 0
21
+ matched = sum(1 for w in topic_words if w in title_lower)
22
+ ratio = matched / len(topic_words) if topic_words else 0
23
+ return int(ratio * 8) if ratio >= 0.6 else 0
24
+
25
+ def _search_vnexpress(topic,limit=8):
26
+ items=[]
27
+ try:
28
+ r=req.get(f"https://timkiem.vnexpress.net/?q={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=10);soup=BeautifulSoup(r.text,'lxml')
29
+ for art in soup.select('article.item-news')[:limit]:
30
+ a=art.select_one('h2 a, h3 a')
31
+ if a and a.get('href'):items.append({'title':_clean(a.get('title','') or a.get_text(strip=True)),'url':a['href'],'via':'VnExpress'})
32
+ except:pass
33
+ return items
34
+ def _search_dantri(topic,limit=8):
35
+ items=[]
36
+ try:
37
+ r=req.get(f"https://dantri.com.vn/tim-kiem/{quote(topic)}.htm",headers={'User-Agent':'Mozilla/5.0'},timeout=10);soup=BeautifulSoup(r.text,'lxml')
38
+ for a in soup.select('h3 a[href], .article-title a[href]')[:limit*2]:
39
+ t=_clean(a.get_text(strip=True));href=a.get('href','')
40
+ if t and len(t)>15:
41
+ if not href.startswith('http'):href='https://dantri.com.vn'+href
42
+ if 'dantri.com.vn' in href:items.append({'title':t,'url':href,'via':'Dân Trí'})
43
+ if len(items)>=limit:break
44
+ except:pass
45
+ return items
46
+ def _search_vietnamnet(topic,limit=6):
47
+ items=[]
48
+ try:
49
+ r=req.get(f"https://vietnamnet.vn/tim-kiem?q={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=10);soup=BeautifulSoup(r.text,'lxml')
50
+ for a in soup.select('h3 a[href], .horizontalPost__main-title a')[:limit*2]:
51
+ t=_clean(a.get_text(strip=True));href=a.get('href','')
52
+ if t and len(t)>15:
53
+ if not href.startswith('http'):href='https://vietnamnet.vn'+href
54
+ if 'vietnamnet.vn' in href:items.append({'title':t,'url':href,'via':'VietNamNet'})
55
+ if len(items)>=limit:break
56
+ except:pass
57
+ return items
58
+ def _search_all(topic, limit=40):
59
+ all_items=[]
60
+ with ThreadPoolExecutor(5) as ex:
61
+ futs=[ex.submit(_search_vnexpress,topic,10),ex.submit(_search_dantri,topic,10),ex.submit(_search_vietnamnet,topic,8)]
62
+ for f in as_completed(futs,timeout=12):
63
+ try:all_items.extend(f.result())
64
+ except:pass
65
+ seen=set();unique=[]
66
+ for i in all_items:
67
+ if i.get('url') and i['url'] not in seen:seen.add(i['url']);unique.append(i)
68
+ return unique[:limit]
69
+
70
+ # Remove old routes
71
+ app.router.routes = [r for r in app.router.routes if not (
72
+ (getattr(r, 'path', None) == '/' and 'GET' in getattr(r, 'methods', set())) or
73
+ (getattr(r, 'path', None) == '/api/hashtag/sources' and 'GET' in getattr(r, 'methods', set())) or
74
+ (getattr(r, 'path', None) in ('/api/short/comments', '/api/short/comment'))
75
+ )]
76
+ app.routes[:] = [r for r in app.routes if not (
77
+ hasattr(r, 'path') and getattr(r, 'path', None) == '/' and
78
+ hasattr(r, 'methods') and 'GET' in getattr(r, 'methods', set())
79
+ )]
80
+
81
+ STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
82
+
83
+ @app.get('/api/hashtag/sources')
84
+ def _ht(topic:str=Query(...), page:int=Query(default=0)):
85
+ all_items=_search_all(topic, 40)
86
+ scored = [(s,item) for item in all_items if (s:=_relevance_score(topic, item.get('title','')))>0]
87
+ scored.sort(key=lambda x: x[0], reverse=True)
88
+ filtered = [item for _, item in scored]
89
+ if len(filtered) < 3: filtered = all_items
90
+ per_page=6;start=page*per_page;end=start+per_page
91
+ return JSONResponse({'sources':filtered[start:end],'topic':topic,'page':page,'has_more':end<len(filtered),'total':len(filtered)})
92
+
93
+ @app.get('/api/categories')
94
+ def _categories():return JSONResponse([])
95
+ @app.get('/api/storage_status')
96
+ def _storage():return JSONResponse({'persistent':os.path.isdir('/data') and os.access('/data', os.W_OK)})
97
+ @app.get('/s')
98
+ async def _share(url:str='',title:str='',img:str=''):
99
+ return HTMLResponse(f'<!DOCTYPE html><html><head><meta property="og:title" content="{_clean(title)}"><meta property="og:image" content="{_clean(img)}"><meta http-equiv="refresh" content="0;url={_clean(url) or "/"}"></head><body>Redirecting...</body></html>')
100
+
101
+ @app.get('/api/proxy/page')
102
+ def proxy_page(url: str = Query(...)):
103
+ try:
104
+ r = req.get(url, headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36','Accept-Language':'vi-VN,vi;q=0.9','Referer':'https://hd.xemtv.net/'}, timeout=15)
105
+ return HTMLResponse(content=r.text)
106
+ except:
107
+ return HTMLResponse(content='', status_code=502)
108
+
109
+ @app.get('/api/proxy/hls')
110
+ def proxy_hls(url: str = Query(...)):
111
+ try:
112
+ headers = {
113
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
114
+ 'Accept': '*/*',
115
+ 'Accept-Language': 'vi-VN,vi;q=0.9',
116
+ 'Referer': 'https://fptplay.vn/',
117
+ 'Origin': 'https://fptplay.vn',
118
+ }
119
+ r = req.get(url, headers=headers, timeout=15)
120
+ content_type = r.headers.get('Content-Type', 'application/vnd.apple.mpegurl')
121
+ text = r.text
122
+ base_url = url.rsplit('/', 1)[0] + '/'
123
+ def _rewrite_url(m):
124
+ seg_url = m.group(0)
125
+ if seg_url.startswith('http'):
126
+ return '/api/proxy/seg?url=' + quote(seg_url, safe='')
127
+ elif seg_url.startswith('/'):
128
+ return '/api/proxy/seg?url=' + quote(base_url.rsplit('/', 2)[0] + seg_url, safe='')
129
+ else:
130
+ return '/api/proxy/seg?url=' + quote(base_url + seg_url, safe='')
131
+ text = re.sub(r'https?://[^\s"\'<>]+\.(ts|m3u8)[^\s"\'<>]*', _rewrite_url, text)
132
+ return HTMLResponse(content=text, media_type=content_type)
133
+ except:
134
+ return HTMLResponse(content='', status_code=502)
135
+
136
+ @app.get('/api/proxy/seg')
137
+ def proxy_seg(url: str = Query(...)):
138
+ try:
139
+ headers = {
140
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
141
+ 'Referer': 'https://fptplay.vn/',
142
+ 'Origin': 'https://fptplay.vn',
143
+ }
144
+ r = req.get(url, headers=headers, timeout=15)
145
+ content_type = r.headers.get('Content-Type', 'video/MP2T')
146
+ return Response(content=r.content, media_type=content_type)
147
+ except:
148
+ return Response(content=b'', status_code=502)
149
+
150
+ # Interactions
151
+ DATA_DIR = '/data' if os.path.isdir('/data') else os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data')
152
+ os.makedirs(DATA_DIR, exist_ok=True)
153
+ INTERACTIONS_FILE = os.path.join(DATA_DIR, 'interactions_v2.json')
154
+ COMMENTS_FILE = os.path.join(DATA_DIR, 'comments_v2.json')
155
+ _interact_lock = threading.Lock()
156
+ _comment_lock = threading.Lock()
157
+ def _load_json(path):
158
+ try:
159
+ if os.path.exists(path):
160
+ with open(path,'r',encoding='utf-8') as f:return json.load(f)
161
+ except:pass
162
+ return {}
163
+ def _save_json(path, data):
164
+ try:
165
+ tmp=path+'.tmp'
166
+ with open(tmp,'w',encoding='utf-8') as f:json.dump(data,f,ensure_ascii=False)
167
+ os.replace(tmp,path)
168
+ except:pass
169
+
170
+ @app.post('/api/v2/interact')
171
+ async def api_interact(request:Request):
172
+ body=await request.json();vid=str(body.get('id','')).strip();itype=str(body.get('type','')).strip()
173
+ if not vid or itype not in('view','like'):return JSONResponse({'error':'invalid'},status_code=400)
174
+ with _interact_lock:
175
+ db=_load_json(INTERACTIONS_FILE)
176
+ if vid not in db:db[vid]={'views':0,'likes':0,'comments':0}
177
+ db[vid][itype+'s']=db[vid].get(itype+'s',0)+1
178
+ _save_json(INTERACTIONS_FILE,db);return JSONResponse(db[vid])
179
+ @app.get('/api/v2/interactions')
180
+ def api_get_interactions(id:str=Query(...)):
181
+ with _interact_lock:return JSONResponse(_load_json(INTERACTIONS_FILE).get(id.strip(),{'views':0,'likes':0,'comments':0}))
182
+ @app.get('/api/v2/comments')
183
+ def api_get_comments(id:str=Query(...)):
184
+ with _comment_lock:return JSONResponse({'comments':_load_json(COMMENTS_FILE).get(id.strip(),[])})
185
+ @app.post('/api/v2/comment')
186
+ async def api_post_comment(request:Request):
187
+ body=await request.json();vid=str(body.get('id','')).strip();text=str(body.get('text','')).strip()[:500]
188
+ if not vid or not text:return JSONResponse({'error':'invalid'},status_code=400)
189
+ comment={'text':text,'time':time.strftime('%H:%M %d/%m',time.localtime()),'ts':int(time.time())}
190
+ with _comment_lock:
191
+ db=_load_json(COMMENTS_FILE)
192
+ if vid not in db:db[vid]=[]
193
+ db[vid].append(comment)
194
+ if len(db[vid])>200:db[vid]=db[vid][-200:]
195
+ _save_json(COMMENTS_FILE,db);comments=db[vid]
196
+ with _interact_lock:
197
+ idb=_load_json(INTERACTIONS_FILE)
198
+ if vid not in idb:idb[vid]={'views':0,'likes':0,'comments':0}
199
+ idb[vid]['comments']=len(comments);_save_json(INTERACTIONS_FILE,idb)
200
+ return JSONResponse({'comments':comments})
201
+
202
+ # World Cup 2026 API
203
+ from wc2026_scraper import (
204
+ scrape_summary, scrape_fixtures, scrape_standings, scrape_stats,
205
+ scrape_wc_news, scrape_road_to_wc, get_wc2026_all,
206
+ scrape_history, scrape_h2h, scrape_lineups, scrape_match_detail
207
+ )
208
+
209
+ @app.get('/api/wc2026')
210
+ def api_wc2026_all():return JSONResponse(get_wc2026_all())
211
+ @app.get('/api/wc2026/summary')
212
+ def api_wc2026_summary():return JSONResponse(scrape_summary())
213
+ @app.get('/api/wc2026/fixtures')
214
+ def api_wc2026_fixtures():return JSONResponse(scrape_fixtures())
215
+ @app.get('/api/wc2026/standings')
216
+ def api_wc2026_standings():return JSONResponse(scrape_standings())
217
+ @app.get('/api/wc2026/stats')
218
+ def api_wc2026_stats():return JSONResponse(scrape_stats())
219
+ @app.get('/api/wc2026/history')
220
+ def api_wc2026_history():return JSONResponse(scrape_history())
221
+ @app.get('/api/wc2026/news')
222
+ def api_wc2026_news():return JSONResponse(scrape_wc_news())
223
+ @app.get('/api/wc2026/road')
224
+ def api_wc2026_road():return JSONResponse(scrape_road_to_wc())
225
+ @app.get('/api/wc2026/h2h/{event_id}')
226
+ def api_wc2026_h2h(event_id:int):return JSONResponse(scrape_h2h(event_id))
227
+ @app.get('/api/wc2026/lineups/{event_id}')
228
+ def api_wc2026_lineups(event_id:int):return JSONResponse(scrape_lineups(event_id))
229
+ @app.get('/api/wc2026/match/{event_id}')
230
+ def api_wc2026_match(event_id:int):return JSONResponse(scrape_match_detail(event_id))
231
+
232
+ # Match Detail API (for any match from bongda.com.vn)
233
+ from match_detail import fetch_match_detail, fetch_match_detail_by_url, _bongda_api
234
+
235
+ @app.get('/api/match/{event_id}/detail')
236
+ def api_match_detail(event_id: int, url: str = Query(default=None)):
237
+ """Get complete match detail. Optional 'url' param with full bongda URL (with slug) for HTML scraping."""
238
+ if url:
239
+ return JSONResponse(fetch_match_detail_by_url(url))
240
+ return JSONResponse(fetch_match_detail(event_id))
241
+
242
+ @app.get('/api/match/{event_id}/commentaries')
243
+ def api_match_commentaries(event_id: int):
244
+ """Get match commentaries from bongda API."""
245
+ comm = _bongda_api("/api/fixtures/commentaries", {"event_id": event_id})
246
+ if comm and comm.get("status") == "success":
247
+ html = comm.get("html", "")
248
+ if html and len(html.strip()) > 10:
249
+ return JSONResponse({"html": html})
250
+ return JSONResponse({"html": ""})
251
+
252
+ @app.get('/api/match/{event_id}/stats')
253
+ def api_match_stats(event_id: int):
254
+ """Get match player performance stats from bongda API."""
255
+ perf = _bongda_api("/api/event-standing/player-performance", {"event_id": event_id})
256
+ if perf and perf.get("status") == "success":
257
+ html = perf.get("html", "")
258
+ if html and len(html.strip()) > 10:
259
+ return JSONResponse({"html": html})
260
+ return JSONResponse({"html": ""})
261
+
262
+ @app.get('/api/match/detail')
263
+ def api_match_detail_by_url(url: str = Query(...)):
264
+ """Get match detail by full bongda.com.vn URL."""
265
+ return JSONResponse(fetch_match_detail_by_url(url))
266
+
267
+ def _wc2026_bg_refresh():
268
+ time.sleep(10)
269
+ while True:
270
+ try:get_wc2026_all()
271
+ except:pass
272
+ time.sleep(90)
273
+ threading.Thread(target=_wc2026_bg_refresh,daemon=True).start()
274
+
275
+ # Serve frontend
276
+ @app.get('/')
277
+ async def _index_v2():
278
+ index_path = os.path.join(STATIC_DIR, 'index_v2.html')
279
+ if os.path.exists(index_path):
280
+ return FileResponse(index_path, media_type='text/html')
281
+ return HTMLResponse('<html><body><h1>VNEWS v2</h1><p>index_v2.html not found</p></body></html>')
282
+
283
+ app.mount('/static', StaticFiles(directory=STATIC_DIR), name='vnews_static')
app_patch_unified.py ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VNEWS Unified Patch v2
3
+ ======================
4
+ Single file replacing app_entry.py + patch_extra.py functionality.
5
+ No conflicts, no duplicate slides, no DOM destruction.
6
+
7
+ Features:
8
+ 1. Tường AI persistent (fix FINAL6E destroying DOM)
9
+ 2. Source details with image + description + "Xem trên VNEWS"
10
+ 3. Highlight = TikTok fullheight 1:1 crop center with interaction buttons
11
+ 4. Rewrite auto-title, no "xem trên VNEWS" junk
12
+ 5. Topic post uses source og:image instead of AI image
13
+ 6. Fast homepage load (non-blocking)
14
+ """
15
+ from ai_runtime_patch_fast import *
16
+ from ai_runtime_patch_fast import app, f5, f6, rt, PATCH_INJECT, _scrape, _domain, clean, _bg, _bg_home, _bg_shorts
17
+ from fastapi.responses import HTMLResponse, JSONResponse
18
+ from fastapi import Request, Query
19
+ import asyncio, re, threading, time
20
+
21
+ DEFAULT_IMG = "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
22
+
23
+ # ============================================================
24
+ # REMOVE ALL CONFLICTING ROUTES — we redefine them cleanly
25
+ # ============================================================
26
+ _OVERRIDE_PATHS = {'/api/homepage','/api/shorts','/api/topic_post','/api/topic/rewrite','/api/rewrite_share','/api/url_wall','/'}
27
+ app.router.routes = [r for r in app.router.routes if not (getattr(r,'path',None) in _OVERRIDE_PATHS and any(m in getattr(r,'methods',set()) for m in ('GET','POST')))]
28
+
29
+ # ============================================================
30
+ # FAST HOMEPAGE + SHORTS (non-blocking)
31
+ # ============================================================
32
+ @app.get('/api/homepage')
33
+ def _homepage():
34
+ if _bg_home['d']:
35
+ if time.time()-_bg_home['t']>300:threading.Thread(target=_bg,daemon=True).start()
36
+ return JSONResponse(_bg_home['d'])
37
+ threading.Thread(target=_bg,daemon=True).start()
38
+ return JSONResponse([])
39
+
40
+ @app.get('/api/shorts')
41
+ def _shorts(refresh:int=Query(default=0)):
42
+ if _bg_shorts['d']:
43
+ if time.time()-_bg_shorts['t']>600:threading.Thread(target=_bg,daemon=True).start()
44
+ return JSONResponse(_bg_shorts['d'])
45
+ threading.Thread(target=_bg,daemon=True).start()
46
+ return JSONResponse([])
47
+
48
+ # ============================================================
49
+ # HELPERS
50
+ # ============================================================
51
+ def _extract_title(text):
52
+ if not text:return 'Bài viết AI'
53
+ lines=[l.strip() for l in text.strip().split('\n') if l.strip()]
54
+ if lines:
55
+ first=re.sub(r'^[#*\-•\d\.\)\s]+','',lines[0]).strip()
56
+ if 10<=len(first)<=120:return first
57
+ return lines[0][:100] if lines else 'Bài viết AI'
58
+
59
+ def _clean_text(text):
60
+ if not text:return text
61
+ for junk in ['xem trên VNEWS','Xem trên VNEWS','📖 Xem trên VNEWS','đọc trên VNEWS','Đọc trên VNEWS','Mở nguồn gốc','mở nguồn gốc','📖 Đọc trên']:
62
+ text=text.replace(junk,'')
63
+ return re.sub(r'\n{3,}','\n\n',text).strip()
64
+
65
+ def _source_image(sources, details):
66
+ for s in (details or [])+(sources or []):
67
+ url=s.get('url','')
68
+ if not url:continue
69
+ try:_,_,img=_scrape(url,500)
70
+ except:img=''
71
+ if img and 'pollinations' not in img and len(img)>20:return img
72
+ return ''
73
+
74
+ def _ensure_img(img):
75
+ return img if (img and len(img)>20 and img.startswith('http')) else DEFAULT_IMG
76
+
77
+ # ============================================================
78
+ # TOPIC POST (source image instead of AI image)
79
+ # ============================================================
80
+ @app.post('/api/topic_post')
81
+ async def _topic(request:Request):
82
+ b=await request.json();topic=clean(b.get('topic',''))
83
+ if not topic:return JSONResponse({'error':'missing topic'},status_code=400)
84
+ research=f6._fast_context(topic) if hasattr(f6,'_fast_context') else f6._web_research_context(topic)
85
+ ctx=research.get('context','');src=research.get('sources',[])
86
+ det=f6._extract_source_details_from_context(ctx,src) if hasattr(f6,'_extract_source_details_from_context') else []
87
+ if not ctx or not src:return JSONResponse({'error':'Không tìm được nội dung.'},status_code=422)
88
+ img=_ensure_img(_source_image(src,det) or f6._topic_image(topic))
89
+ sb='\n\n'.join([f"[{i+1}] {d.get('title','')} ({d.get('via','')})\n{d.get('content','')[:1400]}" for i,d in enumerate(det)]) if det else ctx[:18000]
90
+ text=None
91
+ try:text=await asyncio.wait_for(f5.base.qwen_generate(f'Viết bài tiếng Việt VỀ: "{topic}"\nNGUỒN:\n{sb[:18000]}\nCHỈ viết về "{topic}". 5-8 đoạn. Cuối có nguồn.',image_url=img,max_tokens=1700),timeout=35)
92
+ except:pass
93
+ if not text or len(text)<300:
94
+ text=f"{topic}: tổng hợp\n\n"+'\n'.join([f"• {d['title']}: {d.get('content','')[:300]}" for d in (det or [])[:6]])+"\n\nNguồn: "+', '.join(sorted({d.get('via','') for d in (det or []) if d.get('via')}))
95
+ text=_clean_text(text)
96
+ post=f5.base.make_post(topic,text,img,'','topic_focused',sources=[s for s in src if s.get('url')])
97
+ post['images']=[img];post['source_details']=det
98
+ ps=f5.base._load_ai_wall();ps.insert(0,post);f5.base._save_ai_wall(ps)
99
+ return JSONResponse({'post':post})
100
+
101
+ # ============================================================
102
+ # REWRITE (auto-title, clean text)
103
+ # ============================================================
104
+ @app.post('/api/rewrite_share')
105
+ @app.post('/api/url_wall')
106
+ async def _rewrite(request:Request):
107
+ b=await request.json();url=clean(b.get('url',''));ctx=clean(b.get('context',''))
108
+ if not url.startswith('http'):return JSONResponse({'error':'URL không hợp lệ'},status_code=400)
109
+ title,raw,img=_scrape(url,14000)
110
+ if len(raw)<50:raw=ctx[:14000]
111
+ if len(raw)<50:return JSONResponse({'error':'Không đọc được bài'},status_code=422)
112
+ img=_ensure_img(img)
113
+ prompt=f"""Tóm tắt bài viết thành bản tin ngắn. Dòng đầu tiên là tiêu đề mới hấp dẫn (tự đặt, không copy gốc).
114
+
115
+ Tiêu đề gốc: {title}
116
+ Nội dung:
117
+ {raw[:14000]}
118
+
119
+ Yêu cầu:
120
+ - Dòng 1: Tiêu đề MỚI ngắn gọn hấp dẫn.
121
+ - Tiếp: 4-6 ý chính.
122
+ - Cuối: nguồn.
123
+ - KHÔNG viết bất kỳ cụm điều hướng nào."""
124
+ text=None
125
+ try:text=await asyncio.wait_for(f5.base.qwen_generate(prompt,image_url=img,max_tokens=1000),timeout=30)
126
+ except:pass
127
+ if not text or len(text)<80:text=f"{title}\n\n{raw[:1200]}\n\nNguồn: {_domain(url)}"
128
+ text=_clean_text(text)
129
+ ai_title=_extract_title(text)
130
+ lines=text.strip().split('\n')
131
+ body='\n'.join(lines[1:]).strip() if lines and lines[0].strip()==ai_title else text
132
+ post=f5.base.make_post(ai_title,_clean_text(body),img,url,'rewrite',sources=[{'title':title,'url':url,'via':_domain(url)}])
133
+ ps=f5.base._load_ai_wall();ps.insert(0,post);f5.base._save_ai_wall(ps)
134
+ return JSONResponse({'post':post})
135
+
136
+ @app.post('/api/topic/rewrite')
137
+ async def _topic_rewrite(request:Request):
138
+ b=await request.json();pid=str(b.get('post_id','')).strip()
139
+ if not pid:return JSONResponse({'error':'missing post_id'},status_code=400)
140
+ ps=f5.base._load_ai_wall();p=next((x for x in ps if str(x.get('id'))==pid),None)
141
+ if not p:return JSONResponse({'error':'Bài không tồn tại'},status_code=404)
142
+ urls=list(dict.fromkeys([s['url'] for s in (p.get('source_details') or []) if s.get('url')]+[s['url'] for s in (p.get('sources') or []) if s.get('url')]))[:5]
143
+ parts=[];best_img=''
144
+ for u in urls:
145
+ t,r,uimg=_scrape(u,6000)
146
+ if r and len(r)>150:parts.append(f"[{_domain(u)}] {t}\n{r}")
147
+ if not best_img and uimg and len(uimg)>20:best_img=uimg
148
+ ac='\n---\n'.join(parts) if parts else (p.get('text') or '')
149
+ img=_ensure_img(best_img or p.get('img',''))
150
+ prompt=f"""Viết lại thành bản tóm tắt mới. Dòng đầu là tiêu đề mới hấp dẫn.
151
+
152
+ Chủ đề: {p.get('title','')}
153
+ Nguồn:
154
+ {ac[:16000]}
155
+
156
+ Yêu cầu: Dòng 1 = tiêu đề mới. Tiếp: 4-6 ý. Cuối: nguồn. KHÔNG viết cụm điều hướng."""
157
+ text=None
158
+ try:text=await asyncio.wait_for(f5.base.qwen_generate(prompt,image_url=img,max_tokens=1200),timeout=35)
159
+ except:pass
160
+ if not text or len(text)<100:text=f"Tóm tắt: {p.get('title','')}\n\n{ac[:1500]}\n\nNguồn: VNEWS AI"
161
+ text=_clean_text(text)
162
+ ai_title=_extract_title(text)
163
+ lines=text.strip().split('\n')
164
+ body='\n'.join(lines[1:]).strip() if lines and lines[0].strip()==ai_title else text
165
+ np=f5.base.make_post(ai_title,_clean_text(body),img,'','rewrite_topic',sources=p.get('sources',[]));np['images']=[img]
166
+ all_p=f5.base._load_ai_wall();all_p.insert(0,np);f5.base._save_ai_wall(all_p)
167
+ return JSONResponse({'post':np})
168
+
169
+ # ============================================================
170
+ # UNIFIED INJECT: everything in one clean block
171
+ # ============================================================
172
+ UNIFIED_INJECT = r'''
173
+ <script>
174
+ // === PRE-KILL: prevent old code from destroying Tường AI and Short AI slides ===
175
+ Object.defineProperty(window,'renderTopicWallE',{get:function(){return function(){}},set:function(){},configurable:true});
176
+ Object.defineProperty(window,'renderAIShortHome',{get:function(){return function(){}},set:function(){},configurable:true});
177
+ Object.defineProperty(window,'renderAIShorts7',{get:function(){return function(){}},set:function(){},configurable:true});
178
+ </script>
179
+ <style>
180
+ /* Tiktok right panel for shorts/highlights */
181
+ .tiktok-slide{position:relative!important}
182
+ .tiktok-right{position:absolute!important;right:8px!important;bottom:100px!important;display:flex!important;flex-direction:column!important;align-items:center!important;gap:14px!important;z-index:5!important}
183
+ .tiktok-right-btn{display:flex!important;flex-direction:column!important;align-items:center!important;gap:2px!important;background:none!important;border:0!important;color:#fff!important;cursor:pointer!important}
184
+ .tiktok-right-btn .icon{width:42px!important;height:42px!important;border-radius:50%!important;background:rgba(255,255,255,.12)!important;display:flex!important;align-items:center!important;justify-content:center!important;font-size:20px!important}
185
+ .tiktok-right-btn .count{font-size:10px!important;color:#ddd!important}
186
+ /* Highlight: TikTok feed with 1:1 crop center */
187
+ .tiktok-slide video{object-fit:cover!important}
188
+ /* Hide duplicate slides/walls from old layers */
189
+ #ai-short-home,.ai-short-home,.ai-short-card-final,[id*="ai-shorts-patched"]{display:none!important}
190
+ /* Progress toast */
191
+ #short-progress-toast{position:fixed;bottom:70px;left:50%;transform:translateX(-50%);background:#2d8659;color:#fff;padding:10px 20px;border-radius:20px;font-size:12px;z-index:99998;box-shadow:0 4px 12px rgba(0,0,0,.4);display:none}
192
+ /* Source details */
193
+ .source-detail-box{margin-top:14px;background:#151515;border:1px solid #2b2b2b;border-radius:10px;padding:10px}
194
+ .source-detail-item{background:#202020;border-radius:8px;padding:9px;margin:7px 0;cursor:pointer}
195
+ .source-detail-item:active{opacity:.8}
196
+ .source-detail-title{font-size:12px;font-weight:700;color:#eee}
197
+ .source-detail-content{font-size:11px;color:#bbb;line-height:1.4;max-height:80px;overflow:hidden;margin-top:4px}
198
+ .source-detail-item img{width:100%;aspect-ratio:16/9;object-fit:cover;border-radius:6px;margin-bottom:6px;background:#222}
199
+ .source-vnews-btn{display:inline-block;margin-top:6px;background:#2d8659;color:#fff;padding:4px 10px;border-radius:10px;font-size:10px;font-weight:700}
200
+ /* Livescore */
201
+ .ls-content{max-height:480px;overflow-y:auto;padding:0 6px 8px;font-size:12px;color:#ddd}.ls-content ul{list-style:none;padding:0;margin:0}.ls-content .title-content{display:flex;gap:6px;align-items:center;background:#222;border-radius:4px;margin:4px 0;padding:5px 8px}.ls-content .title-content img{width:18px;height:18px}.ls-content .title-content strong{font-size:11px;color:#ccc}.ls-content .match-detail{padding:6px;border-bottom:1px solid #262626;cursor:pointer}.ls-content .match-detail:hover{background:#1a2a1f}.ls-content .match{display:flex;flex-wrap:wrap;align-items:center;gap:4px}.ls-content .datetime{width:100%;font-size:9px;color:#888}.ls-content .teams{display:flex;width:100%;align-items:center;gap:4px}.ls-content .team{flex:1;display:flex;align-items:center;gap:4px;min-width:0}.ls-content .team .name{font-size:11px;color:#ddd;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.ls-content .team .logo img{width:18px;height:18px}.ls-content .home-team{justify-content:flex-end;text-align:right}.ls-content .status{flex:0 0 54px;text-align:center}.ls-content .status a{color:#fff;text-decoration:none;font-weight:800;font-size:12px}.ls-content .status .label{font-size:8px;color:#888;display:block}.ls-content .status .label.live{color:#e74c3c}.ls-content .info,.ls-content .btns{display:none}.ls-content table,.mo-body table{width:100%;border-collapse:collapse;font-size:11px;color:#ccc}.ls-content table th,.mo-body table th{background:#222;color:#999;padding:5px 4px;font-size:10px;border-bottom:1px solid #333}.ls-content table td,.mo-body table td{padding:4px 3px;border-bottom:1px solid #1a1a1a}.ls-content table .team-name,.mo-body table .team-name{display:flex;align-items:center;gap:4px}.ls-content table .team-name img,.mo-body table .team-name img{width:16px;height:16px}.ls-content table .pts{font-weight:800;color:#f0c040}.mo-body{padding:8px;font-size:12px;color:#ddd}.mo-body ul{list-style:none;padding:0}.mo-body li{padding:5px 0;border-bottom:1px solid #222}
202
+ </style>
203
+ <div id="short-progress-toast"></div>
204
+ <script>
205
+ (function(){
206
+ function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]));}
207
+
208
+ // === Progress toast ===
209
+ window.showShortProgress=function(msg){var t=document.getElementById('short-progress-toast');if(t){t.textContent=msg;t.style.display='block';}};
210
+ window.hideShortProgress=function(){var t=document.getElementById('short-progress-toast');if(t)t.style.display='none';};
211
+ window.makeShortFromPost=async function(pid,btn){
212
+ showShortProgress('⏳ Đang tạo Short AI...');if(btn){btn.disabled=true;btn.textContent='Đang tạo...';}
213
+ try{var r=await fetch('/api/ai/short/'+pid,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice:'nu',emotion:'neutral',speed:1.2})});var j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');showShortProgress('✅ Đã tạo!');setTimeout(hideShortProgress,3000);if(typeof renderShortAISlide==='function')renderShortAISlide();}catch(e){showShortProgress('❌ '+e.message);setTimeout(hideShortProgress,4000);}finally{if(btn){btn.disabled=false;btn.textContent='🎬 Tạo Short AI';}}
214
+ };
215
+
216
+ // === Remove duplicate slides ===
217
+ setInterval(function(){document.querySelectorAll('#ai-short-home,.ai-short-home,[id*="ai-shorts-patched"]').forEach(function(el){if(el.id!=='short-ai-final-slide')el.remove();});},3000);
218
+
219
+ // === Override openLeaguePlayer: TikTok vertical feed, 1:1 crop center ===
220
+ window.openLeaguePlayer=async function(league,idx){
221
+ showView('view-tiktok');document.querySelectorAll('.cat').forEach(x=>x.classList.remove('active'));
222
+ var el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';
223
+ var cfg=(window.HL_CONFIG||{})[league]||{name:league,emoji:'🎬'};
224
+ var articles=(window._hlLeagueData||{})[league]||[];
225
+ if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return;}
226
+ var vids=[];
227
+ var results=await Promise.all(articles.map(async function(a,i){try{var r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));var v=await r.json();if(v&&v.src)return Object.assign({},a,v,{_idx:i});}catch(e){}return null;}));
228
+ results.forEach(function(r){if(r)vids.push(r);});
229
+ vids.sort(function(a,b){return a._idx-b._idx;});
230
+ if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return;}
231
+ var ti=vids.findIndex(function(v){return v._idx===idx;});if(ti<0)ti=0;
232
+ var ordered=ti>0?vids.slice(ti).concat(vids.slice(0,ti)):vids;
233
+ var h='<button class="back-btn" onclick="switchCat(\'home\')">← '+cfg.emoji+' '+cfg.name+'</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">';
234
+ ordered.forEach(function(v,i){
235
+ var isYT=v.type==='youtube';var isHLS=!isYT&&v.src&&v.src.indexOf('.m3u8')>-1;
236
+ var poster=v.poster?' poster="'+v.poster+'"':'';
237
+ var vtag=isYT?'<iframe data-yt-src="'+v.src+'" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture" style="width:100%;height:100%;border:none"></iframe>':isHLS?'<video playsinline preload="none"'+poster+' data-hls="'+v.src+'" loop controls style="width:100%;height:100%;object-fit:cover"></video>':'<video playsinline preload="none"'+poster+' loop controls style="width:100%;height:100%;object-fit:cover"><source src="'+v.src+'" type="video/mp4"></video>';
238
+ h+='<div class="tiktok-slide" id="tslide-'+i+'">'+vtag+'<div class="tiktok-bottom"><span class="badge badge-fpt">'+esc(cfg.name)+'</span><p class="tiktok-title">'+esc(v.title)+'</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation()"><div class="icon">👁</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation()"><div class="icon">❤️</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();if(typeof doShareVideo===\'function\')doShareVideo(\''+esc(v.title)+'\',\''+esc(v.link||'')+'\',\''+esc(v.poster||v.img||'')+'\',\'highlights\')"><div class="icon">📤</div></button></div><span class="tiktok-counter">'+(i+1)+'/'+ordered.length+'</span></div>';
239
+ });
240
+ h+='</div></div>';el.innerHTML=h;
241
+ // Init feed
242
+ var feed=document.getElementById('tiktok-feed');if(!feed)return;
243
+ var slides=feed.querySelectorAll('.tiktok-slide');var cur=-1;
244
+ function act(i){if(i===cur)return;slides.forEach(function(sl,idx){var v=sl.querySelector('video');var fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls){if(!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){var hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,function(){v.play().catch(function(){});});v._hls=hls;}else if(v._hls)v.play().catch(function(){});}else if(v)v.play().catch(function(){});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null;}}if(fr&&fr.src)fr.src='';}});cur=i;}
245
+ var sT;feed.addEventListener('scroll',function(){clearTimeout(sT);sT=setTimeout(function(){var rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2,best=-1,bestD=1e9;slides.forEach(function(sl,i){var d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i;}});if(best>=0)act(best);},150);});
246
+ setTimeout(function(){act(0);},400);
247
+ slides.forEach(function(sl){var v=sl.querySelector('video');if(v)v.addEventListener('click',function(e){e.preventDefault();v.paused?v.play().catch(function(){}):v.pause();});});
248
+ };
249
+
250
+ // === Block slow YouTube refresh on first load ===
251
+ var _origFetch=window.fetch,_allowRefresh=false;
252
+ window.fetch=function(url,opts){try{if(String(url).indexOf('/api/shorts?refresh=1')>-1&&!_allowRefresh)url='/api/shorts';}catch(e){}return _origFetch.call(this,url,opts);};
253
+ setTimeout(function(){_allowRefresh=true;},8000);
254
+ })();
255
+ </script>
256
+ '''
257
+
258
+ # ============================================================
259
+ # ROOT ROUTE: inject order matters
260
+ # ============================================================
261
+ @app.get('/')
262
+ async def _index():
263
+ html = f5.f4.f3.f2.f1._load_index_html()
264
+ # Inject order: PRE_KILL (in UNIFIED) → old injects → PATCH_INJECT → UNIFIED
265
+ body = ''
266
+ body += getattr(rt.old,'PATCH_INJECT','')
267
+ body += f5.f4.f3.f2.f1.FINAL_INJECT + f5.f4.f3.FINAL3_INJECT + f5.f4.FINAL4_INJECT + f5.FINAL5_INJECT
268
+ body += getattr(f6,'FINAL6_INJECT','')
269
+ body += getattr(f6,'FINAL6_FAST_HOME_INJECT','')
270
+ body += getattr(f6,'FINAL6E_INJECT','') # Keep it — our PRE_KILL in UNIFIED neutralizes its destructive parts
271
+ body += PATCH_INJECT
272
+ body += UNIFIED_INJECT # This goes LAST and contains PRE_KILL at the TOP (runs first in browser)
273
+ return HTMLResponse(html.replace('</body>', body + '\n</body>') if '</body>' in html else html + body)
app_run.py ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Wrapper: hashtag via Google News with pagination, strict relevance, load more."""
2
+ from app_final import *
3
+ from app_final import app, f6, f5, rt, PATCH_INJECT, UNIFIED_INJECT_FIXED, HIGHLIGHT_FULL_OVERRIDE, EXTRA_WALL_FIX
4
+ from fastapi.responses import HTMLResponse, JSONResponse
5
+ from fastapi import Query, Request
6
+ import requests as req
7
+ from urllib.parse import quote
8
+ from bs4 import BeautifulSoup
9
+ import re, html as html_lib
10
+
11
+ def _clean(s):return re.sub(r"\s+"," ",html_lib.unescape(str(s or ""))).strip()
12
+
13
+ def _follow_redirect(url):
14
+ try:
15
+ r=req.head(url,allow_redirects=True,timeout=10,headers={'User-Agent':'Mozilla/5.0'})
16
+ return r.url
17
+ except:
18
+ try:r=req.get(url,allow_redirects=True,timeout=10,headers={'User-Agent':'Mozilla/5.0'},stream=True);u=r.url;r.close();return u
19
+ except:return url
20
+
21
+ def _scrape_any_article(url):
22
+ if 'news.google.com' in url or 'google.com/rss' in url:url=_follow_redirect(url)
23
+ try:
24
+ r=req.get(url,headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36','Accept-Language':'vi-VN,vi;q=0.9,en;q=0.8'},timeout=15,allow_redirects=True)
25
+ r.encoding='utf-8';soup=BeautifulSoup(r.text,'lxml')
26
+ for tag in soup.find_all(['script','style','nav','footer','aside','form','noscript','iframe']):tag.decompose()
27
+ h1=soup.find('h1');ogt=soup.find('meta',property='og:title')
28
+ title=(h1.get_text(' ',strip=True) if h1 else '') or (ogt.get('content','') if ogt else '') or (soup.title.get_text(strip=True) if soup.title else '')
29
+ ogd=soup.find('meta',property='og:description') or soup.find('meta',attrs={'name':'description'})
30
+ summary=ogd.get('content','') if ogd else ''
31
+ ogi=soup.find('meta',property='og:image') or soup.find('meta',attrs={'name':'twitter:image'})
32
+ og_image=ogi.get('content','') if ogi else ''
33
+ if og_image and og_image.startswith('//'):og_image='https:'+og_image
34
+ selectors=['article','main','.article-content','.detail-content','.singular-content','.fck_detail','.content-detail','.entry-content','.story-body','.knc-content','.cms-body']
35
+ block=None
36
+ for sel in selectors:
37
+ el=soup.select_one(sel)
38
+ if el and len(el.find_all('p'))>=2:block=el;break
39
+ if not block:
40
+ best=None;best_score=0
41
+ for el in soup.find_all(['article','main','section','div']):
42
+ ps=el.find_all('p');score=len(ps)*100+sum(len(p.get_text())for p in ps[:10])
43
+ if score>best_score:best=el;best_score=score
44
+ block=best or soup.body or soup
45
+ body=[]
46
+ for el in block.find_all(['p','h2','h3','figure','img'],recursive=True):
47
+ if el.name=='p':
48
+ t=_clean(el.get_text(' ',strip=True))
49
+ if len(t)>30:body.append({'type':'p','text':t})
50
+ elif el.name in ('h2','h3'):
51
+ t=_clean(el.get_text(' ',strip=True))
52
+ if t:body.append({'type':'heading','text':t})
53
+ elif el.name in ('figure','img'):
54
+ im=el if el.name=='img' else el.find('img')
55
+ if im:
56
+ src=im.get('data-src') or im.get('data-original') or im.get('src') or ''
57
+ if src and 'base64' not in src:
58
+ if src.startswith('//'):src='https:'+src
59
+ body.append({'type':'img','src':src})
60
+ if not body and summary:body=[{'type':'p','text':summary}]
61
+ return {'title':_clean(title),'summary':_clean(summary),'og_image':og_image,'body':body[:50],'source':'generic','url':url}
62
+ except:return None
63
+
64
+ def _google_news_search_all(topic, limit=30):
65
+ """Get ALL results from Google News RSS for a topic — no filtering here, filter in endpoint."""
66
+ items=[]
67
+ try:
68
+ url='https://news.google.com/rss/search?q='+quote(topic)+'&hl=vi&gl=VN&ceid=VN:vi'
69
+ r=req.get(url,headers={'User-Agent':'Mozilla/5.0'},timeout=10);r.encoding='utf-8'
70
+ soup=BeautifulSoup(r.text,'xml')
71
+ for it in soup.find_all('item')[:limit]:
72
+ title=_clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
73
+ link=_clean(it.find('link').get_text(strip=True) if it.find('link') else '')
74
+ src=_clean(it.find('source').get_text(' ',strip=True) if it.find('source') else '')
75
+ pub=_clean(it.find('pubDate').get_text(strip=True) if it.find('pubDate') else '')
76
+ if not title or not link:continue
77
+ items.append({'title':title,'url':link,'via':src,'snippet':'','pubDate':pub})
78
+ except:pass
79
+ return items
80
+
81
+ def _filter_relevant(items, topic):
82
+ """Strict filter: topic keywords MUST appear in title."""
83
+ topic_lower=topic.lower()
84
+ topic_words=[w for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',topic_lower) if len(w)>2]
85
+ filtered=[]
86
+ for s in items:
87
+ title_lower=s.get('title','').lower()
88
+ # Whole phrase match OR majority of words match
89
+ if topic_lower in title_lower:
90
+ filtered.append(s);continue
91
+ if topic_words:
92
+ match=sum(1 for w in topic_words if w in title_lower)
93
+ if match>=len(topic_words)*0.6:
94
+ filtered.append(s)
95
+ return filtered
96
+
97
+ # Override endpoints
98
+ app.router.routes=[r for r in app.router.routes if not (
99
+ (getattr(r,'path',None)=='/api/hashtag/sources' and 'GET' in getattr(r,'methods',set())) or
100
+ (getattr(r,'path',None)=='/api/article' and 'GET' in getattr(r,'methods',set())) or
101
+ (getattr(r,'path',None)=='/' and 'GET' in getattr(r,'methods',set()))
102
+ )]
103
+
104
+ @app.get('/api/article')
105
+ def _article_universal(url:str=Query(...)):
106
+ data=_scrape_any_article(url)
107
+ if data and data.get('body'):return JSONResponse(data)
108
+ from main import scrape_vne_article,scrape_bbc_article,scrape_dantri_article,scrape_genk_article,scrape_ttvh_article
109
+ if 'vnexpress.net' in url:d=scrape_vne_article(url)
110
+ elif 'bbc.com' in url:d=scrape_bbc_article(url)
111
+ elif 'dantri.com.vn' in url:d=scrape_dantri_article(url)
112
+ elif 'genk.vn' in url:d=scrape_genk_article(url)
113
+ elif 'thethaovanhoa.vn' in url:d=scrape_ttvh_article(url)
114
+ else:d=None
115
+ if d and d.get('body'):return JSONResponse(d)
116
+ return JSONResponse({'error':'Không đọc được bài viết','url':url})
117
+
118
+ @app.get('/api/hashtag/sources')
119
+ def _hashtag_paged(topic:str=Query(...),page:int=Query(default=0)):
120
+ """Google News search with pagination. page=0 returns first 6, page=1 returns next 6, etc."""
121
+ all_items=_google_news_search_all(topic,30)
122
+ filtered=_filter_relevant(all_items,topic)
123
+ # If strict filter too harsh, fallback to all
124
+ if len(filtered)<3:filtered=all_items
125
+ per_page=6;start=page*per_page;end=start+per_page
126
+ page_items=filtered[start:end]
127
+ has_more=end<len(filtered)
128
+ return JSONResponse({'sources':page_items,'topic':topic,'page':page,'has_more':has_more,'total':len(filtered)})
129
+
130
+ FAST_HASHTAG_JS = r'''
131
+ <style>
132
+ .hashtag-loading{display:flex;align-items:center;gap:8px;padding:12px;color:#888;font-size:12px}
133
+ .hashtag-spinner{width:16px;height:16px;border:2px solid #333;border-top-color:#5cb87a;border-radius:50%;animation:ht-spin .8s linear infinite}
134
+ @keyframes ht-spin{to{transform:rotate(360deg)}}
135
+ .hashtag-load-more{width:100%;margin-top:8px;background:#222;border:1px solid #333;color:#ccc;padding:9px;border-radius:10px;font-size:12px;cursor:pointer}.hashtag-load-more:active{opacity:.7}
136
+ </style>
137
+ <script>
138
+ (function(){
139
+ function esc(s){return String(s||'').replace(/[&<>"']/g,function(m){return{'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]});}
140
+ var _htPage=0,_htTopic='',_htImgIdx=0;
141
+
142
+ window.readArticle=async function(url){
143
+ showView('view-article');var el=document.getElementById('view-article');el.innerHTML='<div class="loading">Đang tải...</div>';
144
+ try{var r=await fetch('/api/article?url='+encodeURIComponent(url));var data=await r.json();
145
+ if(data&&!data.error&&data.body&&data.body.length){window._currentArticle={url:url,data:data};var h='<button class="back-btn" onclick="switchCat(\'home\')">← Quay lại</button><div class="article-view"><h1 class="article-title">'+esc(data.title)+'</h1>';if(data.summary)h+='<div class="article-summary">'+esc(data.summary)+'</div>';var seen={};data.body.forEach(function(b){if(b.type==='p')h+='<p class="article-p">'+b.text+'</p>';else if(b.type==='img'&&b.src&&!seen[b.src]){seen[b.src]=1;h+='<img class="article-img" src="'+esc(b.src)+'" onerror="this.style.display=\'none\'">';}else if(b.type==='heading')h+='<h2 class="article-h2">'+esc(b.text)+'</h2>';});h+='<div class="article-actions"><button class="primary" onclick="doRewriteArticle(this)">🤖 Rewrite AI đăng tường</button><button onclick="doShare(\''+esc(data.title)+'\',\''+esc(url)+'\',\''+esc(data.og_image||'')+'\')">📤</button><button onclick="window.open(\''+esc(url)+'\',\'_blank\')">🔗 Gốc</button></div><div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="article-ai-question" placeholder="Hỏi..."></textarea><button onclick="askArticleAI()">Hỏi</button><div id="article-ai-answer" class="article-ai-answer"></div></div></div>';el.innerHTML=h;window.scrollTo(0,0);return;}}catch(e){}
146
+ el.innerHTML='<button class="back-btn" onclick="switchCat(\'home\')">← Quay lại</button><div class="loading"><p>Không đọc được.</p><a href="'+esc(url)+'" target="_blank" style="color:#5cb87a">Mở gốc →</a></div>';
147
+ };
148
+ window.doRewriteArticle=async function(btn){var url=(window._currentArticle&&window._currentArticle.url)||'';if(!url){alert('Không có URL');return;}var ctx=document.querySelector('.article-view')?.innerText?.slice(0,14000)||'';btn.disabled=true;btn.textContent='Đang rewrite...';try{var r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:url,context:ctx})});var j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');alert('Đã đăng Tường AI!');}catch(e){alert(e.message);}finally{btn.disabled=false;btn.textContent='🤖 Rewrite AI đăng tường';}};
149
+ window.askArticleAI=async function(){var q=document.getElementById('article-ai-question')?.value.trim();if(!q)return alert('Nhập câu hỏi');var a=document.getElementById('article-ai-answer');a.textContent='Đang hỏi...';var url=(window._currentArticle&&window._currentArticle.url)||'';var ctx=document.querySelector('.article-view')?.innerText?.slice(0,12000)||'';try{var r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:url,question:q,context:ctx})});var j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}};
150
+
151
+ function renderSources(sources,append){
152
+ var list=document.getElementById('hashtag-src-list');if(!list)return;
153
+ var h='';
154
+ sources.forEach(function(s){
155
+ var idx=_htImgIdx++;
156
+ h+='<div class="hashtag-src-item" onclick="readArticle(\''+esc(s.url||'')+'\')">';
157
+ h+='<div class="hashtag-src-img" id="ht-img-'+idx+'"></div>';
158
+ h+='<div class="hashtag-src-text"><div class="hashtag-src-title">'+esc(s.title)+'</div><div class="hashtag-src-via">'+esc(s.via||'')+(s.pubDate?' · '+esc(s.pubDate.split(',')[0]||''):'')+'</div></div>';
159
+ h+='</div>';
160
+ // Lazy load image
161
+ setTimeout(function(){fetch('/api/article?url='+encodeURIComponent(s.url)).then(function(r){return r.json()}).then(function(d){if(d&&(d.og_image||d.img)){var el=document.getElementById('ht-img-'+idx);if(el)el.innerHTML='<img src="'+esc(d.og_image||d.img)+'" onerror="this.style.display=\'none\'" loading="lazy">';}}).catch(function(){});},idx*500);
162
+ });
163
+ if(append)list.insertAdjacentHTML('beforeend',h);else list.innerHTML=h;
164
+ }
165
+
166
+ window.showHashtagSources=async function(topic){
167
+ _htTopic=topic;_htPage=0;_htImgIdx=0;
168
+ var home=document.getElementById('view-home');if(!home)return;
169
+ document.getElementById('hashtag-sources-box')?.remove();
170
+ var box=document.createElement('div');box.id='hashtag-sources-box';box.className='hashtag-sources';
171
+ box.innerHTML='<h3>🔍 '+esc(topic)+'</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Đang tìm bài viết mới nhất...</div>';
172
+ var compose=home.querySelector('.ai-compose');
173
+ if(compose)compose.after(box);else home.prepend(box);
174
+ box.scrollIntoView({behavior:'smooth',block:'start'});
175
+ try{
176
+ var r=await fetch('/api/hashtag/sources?topic='+encodeURIComponent(topic)+'&page=0');
177
+ var j=await r.json();var sources=j.sources||[];
178
+ if(!sources.length){box.innerHTML='<h3>🔍 '+esc(topic)+'</h3><div style="color:#888;font-size:12px;padding:8px">Không tìm được bài viết liên quan</div>';return;}
179
+ var h='<h3>🔍 '+esc(topic)+' <span style="font-size:10px;color:#888">('+j.total+' bài mới nhất từ Google News)</span></h3>';
180
+ h+='<div id="hashtag-src-list"></div>';
181
+ h+='<button class="hashtag-rewrite-btn" onclick="rewriteHashtagTopic(\''+esc(topic)+'\')">🤖 Rewrite AI tổng hợp & đăng tường</button>';
182
+ if(j.has_more)h+='<button class="hashtag-load-more" id="ht-load-more" onclick="loadMoreSources()">Tải thêm bài viết ▼</button>';
183
+ box.innerHTML=h;
184
+ renderSources(sources,false);
185
+ }catch(e){box.innerHTML='<h3>🔍 '+esc(topic)+'</h3><div style="color:#e74c3c;font-size:12px;padding:8px">Lỗi: '+esc(e.message)+'</div>';}
186
+ };
187
+
188
+ window.loadMoreSources=async function(){
189
+ _htPage++;var btn=document.getElementById('ht-load-more');
190
+ if(btn){btn.textContent='Đang tải...';btn.disabled=true;}
191
+ try{
192
+ var r=await fetch('/api/hashtag/sources?topic='+encodeURIComponent(_htTopic)+'&page='+_htPage);
193
+ var j=await r.json();var sources=j.sources||[];
194
+ renderSources(sources,true);
195
+ if(!j.has_more&&btn)btn.remove();
196
+ else if(btn){btn.textContent='Tải thêm bài viết ▼';btn.disabled=false;}
197
+ }catch(e){if(btn){btn.textContent='Lỗi, thử lại';btn.disabled=false;}}
198
+ };
199
+
200
+ window.rewriteHashtagTopic=async function(topic){var btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}try{var r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic:topic})});var j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');if(btn)btn.textContent='✅ Đã đăng!';setTimeout(function(){document.getElementById('hashtag-sources-box')?.remove();},2000);}catch(e){if(btn){btn.disabled=false;btn.textContent='❌ '+e.message;}}};
201
+ window.createTopicPost=function(){var inp=document.getElementById('ai-topic-input');var topic=(inp&&inp.value||'').trim();if(!topic){alert('Nhập chủ đề');return;}showHashtagSources(topic);if(inp)inp.value='';};
202
+ window.createTopicPostFinal5=function(){var inp=document.getElementById('ai-topic-input-final5')||document.getElementById('ai-topic-input');var topic=(inp&&inp.value||'').trim();if(!topic){alert('Nhập chủ đề');return;}showHashtagSources(topic);if(inp)inp.value='';};
203
+ })();
204
+ </script>
205
+ '''
206
+
207
+ @app.get('/')
208
+ async def _index_run():
209
+ html=f5.f4.f3.f2.f1._load_index_html()
210
+ body=''
211
+ body+=getattr(rt.old,'PATCH_INJECT','')
212
+ body+=f5.f4.f3.f2.f1.FINAL_INJECT+f5.f4.f3.FINAL3_INJECT+f5.f4.FINAL4_INJECT+f5.FINAL5_INJECT
213
+ body+=getattr(f6,'FINAL6_INJECT','')
214
+ body+=getattr(f6,'FINAL6_FAST_HOME_INJECT','')
215
+ body+=getattr(f6,'FINAL6E_INJECT','')
216
+ body+=PATCH_INJECT
217
+ body+=UNIFIED_INJECT_FIXED
218
+ body+=HIGHLIGHT_FULL_OVERRIDE
219
+ body+=EXTRA_WALL_FIX
220
+ body+=FAST_HASHTAG_JS
221
+ return HTMLResponse(html.replace('</body>',body+'\n</body>') if '</body>' in html else html+body)
app_v2_entry.py CHANGED
@@ -423,8 +423,9 @@ def _get_hot_topics():
423
  @app.get('/api/hot_topics')
424
  def api_hot_topics():return JSONResponse({'topics':_get_hot_topics()})
425
  @app.get('/')
 
426
  async def serve_index():
427
- p=os.path.join(STATIC_DIR,'index_v2.html')
428
  if os.path.exists(p):return FileResponse(p,media_type='text/html')
429
  return HTMLResponse('<h1>VNEWS</h1>')
430
  @app.get('/api/hashtag/sources')
 
423
  @app.get('/api/hot_topics')
424
  def api_hot_topics():return JSONResponse({'topics':_get_hot_topics()})
425
  @app.get('/')
426
+ # Serve v3
427
  async def serve_index():
428
+ p=os.path.join(STATIC_DIR,'index_v3.html')
429
  if os.path.exists(p):return FileResponse(p,media_type='text/html')
430
  return HTMLResponse('<h1>VNEWS</h1>')
431
  @app.get('/api/hashtag/sources')
app_v2_entry_test.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VNEWS App v2 - Main application with match detail API
3
+ """
4
+ import os, json, re, time, asyncio, hashlib, logging, threading, importlib, sys
5
+ from datetime import datetime, timezone, timedelta
6
+ from pathlib import Path
7
+ from typing import Optional
8
+
9
+ import httpx
10
+ import requests
11
+ from fastapi import FastAPI, HTTPException, Query
12
+ from fastapi.responses import JSONResponse, FileResponse, HTMLResponse
13
+ from fastapi.staticfiles import StaticFiles
14
+ from fastapi.templating import Jinja2Templates
15
+
16
+ # ... (rest of app_v2_entry.py content)
app_v2_entry_v2.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VNEWS App v2 - Main application with match detail API
3
+ """
4
+ import os, json, re, time, asyncio, hashlib, logging, threading, importlib
5
+ from datetime import datetime, timezone, timedelta
6
+ from pathlib import Path
7
+ from typing import Optional
8
+
9
+ import httpx
10
+ import requests
11
+ from fastapi import FastAPI, HTTPException, Query
12
+ from fastapi.responses import JSONResponse, FileResponse, HTMLResponse
13
+ from fastapi.staticfiles import StaticFiles
14
+ from fastapi.templating import Jinja2Templates
15
+
16
+ # ... (rest of app_v2_entry.py content)
main.py CHANGED
@@ -34,7 +34,27 @@ SHORTS_FALLBACK = [
34
  {"id":"iMdFmWvYdlo","title":"Cô gái người Nga yêu thời trang và đất nước Việt Nam | #shorts","channel":"baodantri7941"},
35
  {"id":"IVaRc6moEv8","title":"Người nông dân Trung Quốc đột quỵ, bệnh viện giúp bán sạch 4 tấn táo | #shorts","channel":"baodantri7941"},
36
  {"id":"uVxqPxToItU","title":"Công an vào cuộc vụ người phụ nữ chửi bới, hành hung tài xế ô tô ở Hà Nội | #shorts","channel":"baodantri7941"},
37
- {"id":"VAfgNNgZDRs","title":"Khởi tố 4 đối tượng ném bom xăng vào nhà dân ở Đồng Nai | #shorts","channel":"baodantri7941"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  ]
39
  for _v in SHORTS_FALLBACK:
40
  _v["link"]="https://www.youtube.com/watch?v="+_v["id"]
@@ -107,6 +127,7 @@ def _parse_match_from_li(li, status_type="live"):
107
  if len(spans)>=4:minute=spans[3].get_text(strip=True)
108
  if not score and status_el and status_el.select_one(".vs"):score="VS"
109
  league=league_el.get_text(strip=True) if league_el else ""
 
110
  match_url = ""
111
  if status_el:
112
  href = status_el.get("href", "")
@@ -114,6 +135,7 @@ def _parse_match_from_li(li, status_type="live"):
114
  match_url = "https://bongda.com.vn" + href if href.startswith("/") else href
115
  return{"home":home_el.get_text(strip=True),"away":away_el.get_text(strip=True),"score":score or"VS","minute":minute,"league":league,"time":time_el.get_text(strip=True) if time_el else "","event_id":event_id,"url":match_url,"home_logo":home_logo.get("src","") if home_logo else "","away_logo":away_logo.get("src","") if away_logo else "","status":status_type}
116
 
 
117
  @app.get("/api/proxy/m3u8")
118
  def proxy_m3u8(url: str = Query(...)):
119
  try:
@@ -150,6 +172,7 @@ def proxy_video(url: str = Query(...), request: Request = None):
150
 
151
  @app.get("/api/proxy/img")
152
  def proxy_img(url: str = Query(...)):
 
153
  try:
154
  r = requests.get(url, headers={**HEADERS, "Referer": "https://dantri.com.vn/"}, timeout=10)
155
  if r.status_code != 200:return Response(status_code=502)
@@ -157,6 +180,107 @@ def proxy_img(url: str = Query(...)):
157
  return Response(content=r.content, media_type=ct, headers={"Cache-Control": "public, max-age=86400", "Access-Control-Allow-Origin": "*"})
158
  except:return Response(status_code=502)
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  @app.get("/api/livescore/live")
161
  def api_livescore_live():return JSONResponse({"html":_cached("ls_live",lambda:fetch_bongda_api("/api/fixtures/live"),ttl=_cache_ttl_live)})
162
  @app.get("/api/livescore/incoming")
@@ -177,10 +301,12 @@ def api_match_commentaries(event_id:int):return JSONResponse({"html":fetch_bongd
177
  @app.get("/api/match/{event_id}/stats")
178
  def api_match_stats(event_id:int):return JSONResponse({"html":fetch_bongda_api(f"/api/event-standing/player-performance?event_id={event_id}")})
179
 
 
180
  from match_detail_v2 import fetch_match_detail, fetch_match_detail_by_url
181
 
182
  @app.get("/api/match/{event_id}/detail")
183
  def api_match_detail(event_id: int, url: str = Query(default="")):
 
184
  try:
185
  if url:
186
  data = fetch_match_detail_by_url(url)
@@ -211,6 +337,7 @@ def api_livescore_featured():
211
  return None
212
  return JSONResponse(_cached("ls_featured",_f,ttl=30))
213
 
 
214
  @app.get("/api/shorts")
215
  def api_shorts():return JSONResponse(_cached("yt_shorts_v3",scrape_shorts,ttl=_cache_ttl_yt))
216
  @app.get("/api/short-stats")
@@ -291,137 +418,7 @@ def api_bdp_videos():
291
  return arts[:20]
292
  except:return[]
293
  return JSONResponse(_cached("bdp_videos",_f))
294
-
295
- def scrape_xemlaibongda():return _scrape_xemlaibongda_page("",20)
296
- def scrape_highlights_by_league(league_key):
297
- if league_key not in HL_LEAGUES:return[]
298
- return _scrape_xemlaibongda_page(HL_LEAGUES[league_key]["path"],20)
299
-
300
- def scrape_all_league_highlights():
301
- results = {}
302
- def _fetch(key):return key, scrape_highlights_by_league(key)
303
- with ThreadPoolExecutor(8) as ex:
304
- futs = [ex.submit(_fetch, k) for k in HL_LEAGUES]
305
- for f in as_completed(futs):
306
- try:
307
- key, vids = f.result()
308
- if vids:results[key] = vids
309
- except:pass
310
- return results
311
-
312
- def _scrape_xemlaibongda_page(page_path, limit=20):
313
- try:
314
- url = f"https://xemlaibongda.top/{page_path}" if page_path else "https://xemlaibongda.top/"
315
- r=requests.get(url,headers=HEADERS,timeout=15)
316
- if r.status_code!=200:return[]
317
- r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml");videos=[];seen=set()
318
- for a in soup.find_all("a",href=True):
319
- href=a.get("href","")
320
- if"/video/" not in href:continue
321
- if not href.startswith("http"):href="https://xemlaibongda.top"+href
322
- if href in seen:continue
323
- seen.add(href);slug=href.split("/video/")[-1].rstrip("/")
324
- title=slug.replace("-"," ").title()
325
- title=re.sub(r'\d{4}\s*\d{2}\s*\d{2}$','',title).strip()
326
- title=re.sub(r'\s+V\s+',' vs ',title);title=re.sub(r'\s+Vs\s+',' vs ',title)
327
- img=a.find("img") or (a.parent.find("img") if a.parent else None)
328
- img_src=""
329
- if img:img_src=img.get("data-src","") or img.get("src","") or img.get("data-lazy","")
330
- if not img_src:img_src=f"https://img.refooty.com/thumbnail/{slug}.webp"
331
- videos.append({"title":title,"link":href,"img":img_src,"source":"xemlaibongda"})
332
- if len(videos)>=limit:break
333
- return videos
334
- except:return[]
335
-
336
- def _yt_channel_shorts(channel, count=15):
337
- try:
338
- url=f"https://www.youtube.com/@{channel}/shorts"
339
- r=requests.get(url,headers={**HEADERS,"Accept-Language":"vi,en;q=0.8"},timeout=15)
340
- if r.status_code!=200:return[]
341
- html=r.text
342
- ids=[];items=[]
343
- for m in re.finditer(r'"videoId":"([A-Za-z0-9_-]{11})"',html):
344
- vid=m.group(1)
345
- if vid in ids:continue
346
- ids.append(vid)
347
- snip=html[max(0,m.start()-900):m.start()+1600]
348
- title=""
349
- mt=re.search(r'"title":\{"runs":\[\{"text":"([^"]+)"',snip)
350
- if not mt:mt=re.search(r'"accessibilityText":"([^"]+)"',snip)
351
- if mt:title=html_lib.unescape(mt.group(1)).replace('\n',' ').strip()
352
- if not title:title="YouTube Short"
353
- items.append({"title":title,"link":f"https://www.youtube.com/watch?v={vid}","img":f"https://i.ytimg.com/vi/{vid}/hqdefault.jpg","source":"yt","id":vid,"channel":channel})
354
- if len(items)>=count:break
355
- return items
356
- except:return[]
357
- def scrape_shorts():
358
- vids=[]
359
- with ThreadPoolExecutor(2) as ex:
360
- futs=[ex.submit(_yt_channel_shorts,ch,24) for ch in ["baodantri7941","baosuckhoedoisongboyte"]]
361
- for f in as_completed(futs):
362
- try:
363
- r=f.result()
364
- if r:vids.extend(r)
365
- except:pass
366
- merged=[];seen=set()
367
- for v in vids+SHORTS_FALLBACK:
368
- vid=v.get("id")
369
- if not vid or vid in seen:continue
370
- seen.add(vid);merged.append(v)
371
- return merged[:40]
372
-
373
- def extract_xemlaibongda_video(url):
374
- try:
375
- r=requests.get(url,headers=HEADERS,timeout=15)
376
- if r.status_code!=200:return None
377
- r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml");video=soup.find("video")
378
- if video:
379
- src=video.get("src","");poster=video.get("poster","")
380
- if not src:
381
- source=video.find("source")
382
- if source:src=source.get("src","")
383
- if src:return{"src":src,"poster":poster,"type":"hls" if".m3u8" in src else"video"}
384
- m3u8s=re.findall(r'(https?://[^\s"\'<>]+\.m3u8)',r.text)
385
- if m3u8s:
386
- og=soup.find("meta",property="og:image");poster=og.get("content","") if og else ""
387
- return{"src":m3u8s[0],"poster":poster,"type":"hls"}
388
- return None
389
- except:return None
390
-
391
- VNE_CATS={"thoi-su":("https://vnexpress.net/thoi-su","Thời Sự"),"the-gioi":("https://vnexpress.net/the-gioi","Thế Giới"),"kinh-doanh":("https://vnexpress.net/kinh-doanh","Kinh Doanh"),"the-thao":("https://vnexpress.net/the-thao","Thể Thao"),"giai-tri":("https://vnexpress.net/giai-tri","Giải Trí"),"suc-khoe":("https://vnexpress.net/suc-khoe","Sức Khỏe"),"phap-luat":("https://vnexpress.net/phap-luat","Pháp Luật"),"giao-duc":("https://vnexpress.net/giao-duc","Giáo Dục"),"du-lich":("https://vnexpress.net/du-lich","Du Lịch"),"doi-song":("https://vnexpress.net/doi-song","Đời Sống")}
392
- @app.get("/api/homepage")
393
- def api_homepage():
394
- def _f():
395
- articles=[]
396
- with ThreadPoolExecutor(12) as ex:
397
- futs={ex.submit(scrape_vne,VNE_CATS[k][0]):VNE_CATS[k][1] for k in["thoi-su","the-gioi","kinh-doanh","the-thao","giai-tri","phap-luat","giao-duc","du-lich","doi-song"]}
398
- futs[ex.submit(scrape_bbc_vietnamese)]="BBC"
399
- for f in as_completed(futs):
400
- try:
401
- for a in f.result():a["group"]=futs[f];articles.append(a)
402
- except:pass
403
- return articles
404
- return JSONResponse(_cached("homepage",_f))
405
- @app.get("/api/category/{cat_id}")
406
- def api_category(cat_id:str):
407
- def _f():
408
- if cat_id=="bbc":return scrape_bbc_vietnamese()
409
- if cat_id=="cong-nghe":return scrape_genk_ai()
410
- if cat_id in VNE_CATS:arts=scrape_vne(VNE_CATS[cat_id][0]);[a.update({"group":VNE_CATS[cat_id][1]}) for a in arts];return arts
411
- return[]
412
- return JSONResponse(_cached(f"cat_{cat_id}",_f))
413
- @app.get("/api/categories")
414
- def api_categories():
415
- cats=[{"id":"bbc","name":"BBC Tiếng Việt","source":"bbc"},{"id":"cong-nghe","name":"Công Nghệ","source":"genk"}]
416
- for k,(u,n) in VNE_CATS.items():cats.append({"id":k,"name":n,"source":"vne"})
417
- return JSONResponse(cats)
418
- @app.get("/api/dantri_hot")
419
- def api_dantri_hot():return JSONResponse(_cached("dantri_hot",scrape_dantri_hot))
420
- @app.get("/api/genk_ai")
421
- def api_genk_ai():return JSONResponse(_cached("genk_ai",scrape_genk_ai,ttl=_cache_ttl))
422
- @app.get("/api/worldcup2026")
423
- def api_worldcup2026():return JSONResponse(_cached("ttvh_worldcup",scrape_ttvh_worldcup,ttl=_cache_ttl))
424
-
425
  def scrape_vne(cat_url):
426
  try:
427
  soup=_get(cat_url);arts=[]
@@ -437,7 +434,6 @@ def scrape_vne(cat_url):
437
  arts.append({"title":t,"link":lk,"img":img,"source":"vne"})
438
  return arts
439
  except:return[]
440
-
441
  def scrape_vne_article(url):
442
  try:
443
  soup=_get(url);h1=soup.select_one("h1.title-detail");desc=soup.select_one("p.description")
@@ -453,7 +449,6 @@ def scrape_vne_article(url):
453
  elif ch.name in("h2","h3"):body.append({"type":"heading","text":ch.get_text(strip=True)})
454
  return{"title":h1.get_text(strip=True) if h1 else "","summary":desc.get_text(strip=True) if desc else "","og_image":og_img,"body":body,"source":"vne","url":url}
455
  except:return None
456
-
457
  def _scrape_dantri_homepage(cat_filter=None):
458
  try:
459
  soup=_get("https://dantri.com.vn/");arts=[];seen=set()
@@ -474,7 +469,6 @@ def _scrape_dantri_homepage(cat_filter=None):
474
  return arts
475
  except:return[]
476
  def scrape_dantri_hot():return _scrape_dantri_homepage()
477
-
478
  def scrape_dantri_congnghe():
479
  try:
480
  soup=_get("https://dantri.com.vn/");arts=[];seen=set()
@@ -494,8 +488,8 @@ def scrape_dantri_congnghe():
494
  if len(arts)>=15:break
495
  return arts
496
  except:return[]
497
-
498
  def scrape_genk_ai():
 
499
  try:
500
  r=requests.get("https://genk.vn/ai.chn",headers=HEADERS,timeout=15)
501
  if r.status_code!=200:return[]
@@ -554,7 +548,6 @@ def scrape_dantri_article(url):
554
  else:desc=sapo.get_text(strip=True)
555
  return{"title":h1.get_text(strip=True) if h1 else "","summary":desc,"og_image":og_img,"body":body,"source":"dantri","url":url}
556
  except:return None
557
-
558
  def scrape_bbc_vietnamese():
559
  try:
560
  r=requests.get("https://www.bbc.com/vietnamese",headers={"User-Agent":"Mozilla/5.0","Accept-Language":"en-GB"},timeout=15);r.encoding="utf-8"
@@ -589,6 +582,7 @@ def scrape_bbc_article(url):
589
  except:return None
590
 
591
  def scrape_ttvh_worldcup():
 
592
  try:
593
  r=requests.get("https://thethaovanhoa.vn/rss/world-cup-2026.rss",headers=HEADERS,timeout=15);r.encoding="utf-8"
594
  soup=BeautifulSoup(r.text,"xml");arts=[];seen=set()
@@ -650,6 +644,39 @@ def scrape_ttvh_article(url):
650
  return {"title":h1.get_text(strip=True) if h1 else fallback_title,"summary":desc,"og_image":og_img,"body":body,"source":"ttvh","url":url}
651
  except:return None
652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
653
  def scrape_genk_article(url):
654
  try:
655
  r=requests.get(url,headers=HEADERS,timeout=15);r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml")
@@ -676,3 +703,251 @@ def api_article(url:str=Query(...)):
676
  elif"thethaovanhoa.vn" in url:data=scrape_ttvh_article(url)
677
  else:data=None
678
  return JSONResponse(data if data else{"error":"not supported"})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  {"id":"iMdFmWvYdlo","title":"Cô gái người Nga yêu thời trang và đất nước Việt Nam | #shorts","channel":"baodantri7941"},
35
  {"id":"IVaRc6moEv8","title":"Người nông dân Trung Quốc đột quỵ, bệnh viện giúp bán sạch 4 tấn táo | #shorts","channel":"baodantri7941"},
36
  {"id":"uVxqPxToItU","title":"Công an vào cuộc vụ người phụ nữ chửi bới, hành hung tài xế ô tô ở Hà Nội | #shorts","channel":"baodantri7941"},
37
+ {"id":"VAfgNNgZDRs","title":"Khởi tố 4 đối tượng ném bom xăng vào nhà dân ở Đồng Nai | #shorts","channel":"baodantri7941"},
38
+ {"id":"sBH_-zGh0Xw","title":"Vì sao Times New Roman vẫn nổi tiếng sau hàng chục năm? | #shorts","channel":"baodantri7941"},
39
+ {"id":"woKn5f2bLHM","title":"Quảng Ninh ngập sâu diện rộng sau đợt mưa lớn | #shorts","channel":"baodantri7941"},
40
+ {"id":"bcpgRoxbLPw","title":"Giông lốc quật bay mái tôn ở TP.HCM | #shorts","channel":"baodantri7941"},
41
+ {"id":"ZIIC5osy544","title":"Bé trai Trung Quốc rơi từ tầng 11 vẫn sống sót kỳ diệu | #shorts","channel":"baodantri7941"},
42
+ {"id":"uTMJ49NQpyc","title":"Sau lớp mascot 40kg: Câu chuyện mưu sinh của người trẻ ở TPHCM | #shorts","channel":"baodantri7941"},
43
+ {"id":"7Pd6vZ2Lz1M","title":"Hành động ấm lòng của người đàn ông tham gia tìm kiếm 5 học sinh tử vong ở sông Lô | SKĐS","channel":"baosuckhoedoisongboyte"},
44
+ {"id":"SlHLt_ZyPiE","title":"Xử phạt người đàn ông xóa số điện thoại cứu hộ trên cao tốc Bắc - Nam | SKĐS","channel":"baosuckhoedoisongboyte"},
45
+ {"id":"IUOprcJyYr4","title":"Phụ nữ táo bón có phải do lười ăn rau? | SKĐS #shorts","channel":"baosuckhoedoisongboyte"},
46
+ {"id":"YY8ojFNE-AU","title":"Quái xế tự quay clip nẹt pô, đánh võng đăng TikTok bị xử lý | SKĐS","channel":"baosuckhoedoisongboyte"},
47
+ {"id":"OV7_oGdQGII","title":"Bố cô dâu khóc sụt sùi rồi quẩy cực sung gây bão mạng | SKĐS","channel":"baosuckhoedoisongboyte"},
48
+ {"id":"FoxhFyz2skY","title":"Người đàn ông nước ngoài đập phá ô tô, bẻ cần gạt nước ở Đà Nẵng | SKĐS","channel":"baosuckhoedoisongboyte"},
49
+ {"id":"R1oC_I8dFPU","title":"Thanh niên buông tay lái, đứng trên xe máy khi đổ đèo ở Đắk Lắk | SKĐS","channel":"baosuckhoedoisongboyte"},
50
+ {"id":"U0Ft6ChWAIo","title":"Cô giáo kể phút tháo chạy khỏi xe khách trước khi bị lũ vò nát ở Cao Bằng | SKĐS","channel":"baosuckhoedoisongboyte"},
51
+ {"id":"hH0ANeze_4E","title":"Liên tiếp hàng chục con bò bị sét đánh chết trong ngày mưa dông | SKĐS","channel":"baosuckhoedoisongboyte"},
52
+ {"id":"pXWt0QbAzRQ","title":"Va chạm giao thông, người phụ nữ lăng mạ tài xế ô tô | SKĐS","channel":"baosuckhoedoisongboyte"},
53
+ {"id":"UWWLPY1OYt4","title":"CSGT chặn xe khách khống chế đối tượng cướp dây chuyền tại Gia Lai | SKĐS","channel":"baosuckhoedoisongboyte"},
54
+ {"id":"AxhVTQutsuo","title":"Xuất tinh sớm và những hiểu lầm thường gặp | SKĐS #shorts","channel":"baosuckhoedoisongboyte"},
55
+ {"id":"cNy6FgaNxYM","title":"Cô dâu khóc sưng mắt vì 6 chỉ vàng không cánh mà bay trong ngày cưới | SKĐS","channel":"baosuckhoedoisongboyte"},
56
+ {"id":"IDt_S6q59Ro","title":"Chở bạn gái không đội mũ bảo hiểm, thanh niên đấm CSGT | SKĐS","channel":"baosuckhoedoisongboyte"},
57
+ {"id":"LFxJ9Ik6W0A","title":"Mệnh lệnh từ trái tim: CSGT Hà Nội mở đường đưa bé 5 tháng tuổi đi cấp cứu | SKĐS","channel":"baosuckhoedoisongboyte"}
58
  ]
59
  for _v in SHORTS_FALLBACK:
60
  _v["link"]="https://www.youtube.com/watch?v="+_v["id"]
 
127
  if len(spans)>=4:minute=spans[3].get_text(strip=True)
128
  if not score and status_el and status_el.select_one(".vs"):score="VS"
129
  league=league_el.get_text(strip=True) if league_el else ""
130
+ # Extract full URL with slug for direct scraping
131
  match_url = ""
132
  if status_el:
133
  href = status_el.get("href", "")
 
135
  match_url = "https://bongda.com.vn" + href if href.startswith("/") else href
136
  return{"home":home_el.get_text(strip=True),"away":away_el.get_text(strip=True),"score":score or"VS","minute":minute,"league":league,"time":time_el.get_text(strip=True) if time_el else "","event_id":event_id,"url":match_url,"home_logo":home_logo.get("src","") if home_logo else "","away_logo":away_logo.get("src","") if away_logo else "","status":status_type}
137
 
138
+ # ===== VIDEO PROXY =====
139
  @app.get("/api/proxy/m3u8")
140
  def proxy_m3u8(url: str = Query(...)):
141
  try:
 
172
 
173
  @app.get("/api/proxy/img")
174
  def proxy_img(url: str = Query(...)):
175
+ """Proxy images from sources that block hotlinking (DanTri CDN)."""
176
  try:
177
  r = requests.get(url, headers={**HEADERS, "Referer": "https://dantri.com.vn/"}, timeout=10)
178
  if r.status_code != 200:return Response(status_code=502)
 
180
  return Response(content=r.content, media_type=ct, headers={"Cache-Control": "public, max-age=86400", "Access-Control-Allow-Origin": "*"})
181
  except:return Response(status_code=502)
182
 
183
+ # ===== XEMLAIBONGDA HIGHLIGHTS =====
184
+ def _scrape_xemlaibongda_page(page_path, limit=20):
185
+ try:
186
+ url = f"https://xemlaibongda.top/{page_path}" if page_path else "https://xemlaibongda.top/"
187
+ r=requests.get(url,headers=HEADERS,timeout=15)
188
+ if r.status_code!=200:return[]
189
+ r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml");videos=[];seen=set()
190
+ for a in soup.find_all("a",href=True):
191
+ href=a.get("href","")
192
+ if"/video/" not in href:continue
193
+ if not href.startswith("http"):href="https://xemlaibongda.top"+href
194
+ if href in seen:continue
195
+ seen.add(href);slug=href.split("/video/")[-1].rstrip("/")
196
+ title=slug.replace("-"," ").title()
197
+ title=re.sub(r'\d{4}\s*\d{2}\s*\d{2}$','',title).strip()
198
+ title=re.sub(r'\s+V\s+',' vs ',title);title=re.sub(r'\s+Vs\s+',' vs ',title)
199
+ img=a.find("img") or (a.parent.find("img") if a.parent else None)
200
+ img_src=""
201
+ if img:img_src=img.get("data-src","") or img.get("src","") or img.get("data-lazy","")
202
+ if not img_src:img_src=f"https://img.refooty.com/thumbnail/{slug}.webp"
203
+ videos.append({"title":title,"link":href,"img":img_src,"source":"xemlaibongda"})
204
+ if len(videos)>=limit:break
205
+ return videos
206
+ except:return[]
207
+
208
+ def scrape_xemlaibongda():return _scrape_xemlaibongda_page("",20)
209
+ def scrape_highlights_by_league(league_key):
210
+ if league_key not in HL_LEAGUES:return[]
211
+ return _scrape_xemlaibongda_page(HL_LEAGUES[league_key]["path"],20)
212
+
213
+ def scrape_all_league_highlights():
214
+ results = {}
215
+ def _fetch(key):return key, scrape_highlights_by_league(key)
216
+ with ThreadPoolExecutor(8) as ex:
217
+ futs = [ex.submit(_fetch, k) for k in HL_LEAGUES]
218
+ for f in as_completed(futs):
219
+ try:
220
+ key, vids = f.result()
221
+ if vids:results[key] = vids
222
+ except:pass
223
+ return results
224
+
225
+ def extract_xemlaibongda_video(url):
226
+ try:
227
+ r=requests.get(url,headers=HEADERS,timeout=15)
228
+ if r.status_code!=200:return None
229
+ r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml");video=soup.find("video")
230
+ if video:
231
+ src=video.get("src","");poster=video.get("poster","")
232
+ if not src:
233
+ source=video.find("source")
234
+ if source:src=source.get("src","")
235
+ if src:return{"src":src,"poster":poster,"type":"hls" if".m3u8" in src else"video"}
236
+ m3u8s=re.findall(r'(https?://[^\s"\'<>]+\.m3u8)',r.text)
237
+ if m3u8s:
238
+ og=soup.find("meta",property="og:image");poster=og.get("content","") if og else ""
239
+ return{"src":m3u8s[0],"poster":poster,"type":"hls"}
240
+ return None
241
+ except:return None
242
+
243
+ # ===== YOUTUBE SHORTS =====
244
+ def _yt_channel_shorts(channel, count=15):
245
+ """Fast scrape YouTube shorts tab without yt-dlp. Returns newest-first IDs/titles."""
246
+ try:
247
+ url=f"https://www.youtube.com/@{channel}/shorts"
248
+ r=requests.get(url,headers={**HEADERS,"Accept-Language":"vi,en;q=0.8"},timeout=15)
249
+ if r.status_code!=200:return[]
250
+ html=r.text
251
+ ids=[];items=[]
252
+ for m in re.finditer(r'"videoId":"([A-Za-z0-9_-]{11})"',html):
253
+ vid=m.group(1)
254
+ if vid in ids:continue
255
+ ids.append(vid)
256
+ snip=html[max(0,m.start()-900):m.start()+1600]
257
+ title=""
258
+ mt=re.search(r'"title":\{"runs":\[\{"text":"([^"]+)"',snip)
259
+ if not mt:mt=re.search(r'"accessibilityText":"([^"]+)"',snip)
260
+ if mt:title=html_lib.unescape(mt.group(1)).replace('\n',' ').strip()
261
+ if not title:title="YouTube Short"
262
+ items.append({"title":title,"link":f"https://www.youtube.com/watch?v={vid}","img":f"https://i.ytimg.com/vi/{vid}/hqdefault.jpg","source":"yt","id":vid,"channel":channel})
263
+ if len(items)>=count:break
264
+ return items
265
+ except:return[]
266
+ def scrape_shorts():
267
+ """Stable shorts feed: fast HTML scrape + static fallback so slide never disappears."""
268
+ vids=[]
269
+ with ThreadPoolExecutor(2) as ex:
270
+ futs=[ex.submit(_yt_channel_shorts,ch,24) for ch in ["baodantri7941","baosuckhoedoisongboyte"]]
271
+ for f in as_completed(futs):
272
+ try:
273
+ r=f.result()
274
+ if r:vids.extend(r)
275
+ except:pass
276
+ merged=[];seen=set()
277
+ for v in vids+SHORTS_FALLBACK:
278
+ vid=v.get("id")
279
+ if not vid or vid in seen:continue
280
+ seen.add(vid);merged.append(v)
281
+ return merged[:40]
282
+
283
+ # ===== LIVESCORE =====
284
  @app.get("/api/livescore/live")
285
  def api_livescore_live():return JSONResponse({"html":_cached("ls_live",lambda:fetch_bongda_api("/api/fixtures/live"),ttl=_cache_ttl_live)})
286
  @app.get("/api/livescore/incoming")
 
301
  @app.get("/api/match/{event_id}/stats")
302
  def api_match_stats(event_id:int):return JSONResponse({"html":fetch_bongda_api(f"/api/event-standing/player-performance?event_id={event_id}")})
303
 
304
+ # ===== MATCH DETAIL (server-side scrape from bongda.com.vn) =====
305
  from match_detail_v2 import fetch_match_detail, fetch_match_detail_by_url
306
 
307
  @app.get("/api/match/{event_id}/detail")
308
  def api_match_detail(event_id: int, url: str = Query(default="")):
309
+ """Get full match detail by scraping bongda.com.vn server-side."""
310
  try:
311
  if url:
312
  data = fetch_match_detail_by_url(url)
 
337
  return None
338
  return JSONResponse(_cached("ls_featured",_f,ttl=30))
339
 
340
+ # ===== VIDEO APIs =====
341
  @app.get("/api/shorts")
342
  def api_shorts():return JSONResponse(_cached("yt_shorts_v3",scrape_shorts,ttl=_cache_ttl_yt))
343
  @app.get("/api/short-stats")
 
418
  return arts[:20]
419
  except:return[]
420
  return JSONResponse(_cached("bdp_videos",_f))
421
+ # ===== NEWS =====
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  def scrape_vne(cat_url):
423
  try:
424
  soup=_get(cat_url);arts=[]
 
434
  arts.append({"title":t,"link":lk,"img":img,"source":"vne"})
435
  return arts
436
  except:return[]
 
437
  def scrape_vne_article(url):
438
  try:
439
  soup=_get(url);h1=soup.select_one("h1.title-detail");desc=soup.select_one("p.description")
 
449
  elif ch.name in("h2","h3"):body.append({"type":"heading","text":ch.get_text(strip=True)})
450
  return{"title":h1.get_text(strip=True) if h1 else "","summary":desc.get_text(strip=True) if desc else "","og_image":og_img,"body":body,"source":"vne","url":url}
451
  except:return None
 
452
  def _scrape_dantri_homepage(cat_filter=None):
453
  try:
454
  soup=_get("https://dantri.com.vn/");arts=[];seen=set()
 
469
  return arts
470
  except:return[]
471
  def scrape_dantri_hot():return _scrape_dantri_homepage()
 
472
  def scrape_dantri_congnghe():
473
  try:
474
  soup=_get("https://dantri.com.vn/");arts=[];seen=set()
 
488
  if len(arts)>=15:break
489
  return arts
490
  except:return[]
 
491
  def scrape_genk_ai():
492
+ """Scrape AI articles from genk.vn - readable in-app"""
493
  try:
494
  r=requests.get("https://genk.vn/ai.chn",headers=HEADERS,timeout=15)
495
  if r.status_code!=200:return[]
 
548
  else:desc=sapo.get_text(strip=True)
549
  return{"title":h1.get_text(strip=True) if h1 else "","summary":desc,"og_image":og_img,"body":body,"source":"dantri","url":url}
550
  except:return None
 
551
  def scrape_bbc_vietnamese():
552
  try:
553
  r=requests.get("https://www.bbc.com/vietnamese",headers={"User-Agent":"Mozilla/5.0","Accept-Language":"en-GB"},timeout=15);r.encoding="utf-8"
 
582
  except:return None
583
 
584
  def scrape_ttvh_worldcup():
585
+ """Scrape all World Cup 2026 articles from The Thao Van Hoa RSS."""
586
  try:
587
  r=requests.get("https://thethaovanhoa.vn/rss/world-cup-2026.rss",headers=HEADERS,timeout=15);r.encoding="utf-8"
588
  soup=BeautifulSoup(r.text,"xml");arts=[];seen=set()
 
644
  return {"title":h1.get_text(strip=True) if h1 else fallback_title,"summary":desc,"og_image":og_img,"body":body,"source":"ttvh","url":url}
645
  except:return None
646
 
647
+ VNE_CATS={"thoi-su":("https://vnexpress.net/thoi-su","Thời Sự"),"the-gioi":("https://vnexpress.net/the-gioi","Thế Giới"),"kinh-doanh":("https://vnexpress.net/kinh-doanh","Kinh Doanh"),"the-thao":("https://vnexpress.net/the-thao","Thể Thao"),"giai-tri":("https://vnexpress.net/giai-tri","Giải Trí"),"suc-khoe":("https://vnexpress.net/suc-khoe","Sức Khỏe"),"phap-luat":("https://vnexpress.net/phap-luat","Pháp Luật"),"giao-duc":("https://vnexpress.net/giao-duc","Giáo Dục"),"du-lich":("https://vnexpress.net/du-lich","Du Lịch"),"doi-song":("https://vnexpress.net/doi-song","Đời Sống")}
648
+ @app.get("/api/homepage")
649
+ def api_homepage():
650
+ def _f():
651
+ articles=[]
652
+ with ThreadPoolExecutor(12) as ex:
653
+ futs={ex.submit(scrape_vne,VNE_CATS[k][0]):VNE_CATS[k][1] for k in["thoi-su","the-gioi","kinh-doanh","the-thao","giai-tri","phap-luat","giao-duc","du-lich","doi-song"]}
654
+ futs[ex.submit(scrape_bbc_vietnamese)]="BBC"
655
+ for f in as_completed(futs):
656
+ try:
657
+ for a in f.result():a["group"]=futs[f];articles.append(a)
658
+ except:pass
659
+ return articles
660
+ return JSONResponse(_cached("homepage",_f))
661
+ @app.get("/api/category/{cat_id}")
662
+ def api_category(cat_id:str):
663
+ def _f():
664
+ if cat_id=="bbc":return scrape_bbc_vietnamese()
665
+ if cat_id=="cong-nghe":return scrape_genk_ai()
666
+ if cat_id in VNE_CATS:arts=scrape_vne(VNE_CATS[cat_id][0]);[a.update({"group":VNE_CATS[cat_id][1]}) for a in arts];return arts
667
+ return[]
668
+ return JSONResponse(_cached(f"cat_{cat_id}",_f))
669
+ @app.get("/api/categories")
670
+ def api_categories():
671
+ cats=[{"id":"bbc","name":"BBC Tiếng Việt","source":"bbc"},{"id":"cong-nghe","name":"Công Nghệ","source":"genk"}]
672
+ for k,(u,n) in VNE_CATS.items():cats.append({"id":k,"name":n,"source":"vne"})
673
+ return JSONResponse(cats)
674
+ @app.get("/api/dantri_hot")
675
+ def api_dantri_hot():return JSONResponse(_cached("dantri_hot",scrape_dantri_hot))
676
+ @app.get("/api/genk_ai")
677
+ def api_genk_ai():return JSONResponse(_cached("genk_ai",scrape_genk_ai,ttl=_cache_ttl))
678
+ @app.get("/api/worldcup2026")
679
+ def api_worldcup2026():return JSONResponse(_cached("ttvh_worldcup",scrape_ttvh_worldcup,ttl=_cache_ttl))
680
  def scrape_genk_article(url):
681
  try:
682
  r=requests.get(url,headers=HEADERS,timeout=15);r.encoding="utf-8";soup=BeautifulSoup(r.text,"lxml")
 
703
  elif"thethaovanhoa.vn" in url:data=scrape_ttvh_article(url)
704
  else:data=None
705
  return JSONResponse(data if data else{"error":"not supported"})
706
+ def _web_context(topic):
707
+ """Collect real web/news context for a topic."""
708
+ bits=[]
709
+ try:
710
+ rss="https://news.google.com/rss/search?q="+quote(topic)+"&hl=vi&gl=VN&ceid=VN:vi"
711
+ r=requests.get(rss,headers=HEADERS,timeout=12);r.encoding="utf-8"
712
+ soup=BeautifulSoup(r.text,"xml")
713
+ for it in soup.find_all("item")[:8]:
714
+ title=it.find("title").get_text(" ",strip=True) if it.find("title") else ""
715
+ src=it.find("source").get_text(" ",strip=True) if it.find("source") else ""
716
+ if title:bits.append((title+(" — "+src if src else ""))[:280])
717
+ except:pass
718
+ if bits:return "\n".join(bits)
719
+ try:
720
+ r=requests.get("https://html.duckduckgo.com/html/?q="+quote(topic),headers=HEADERS,timeout=12);r.encoding="utf-8"
721
+ soup=BeautifulSoup(r.text,"lxml")
722
+ for res in soup.select(".result")[:6]:
723
+ t=res.select_one(".result__title");sn=res.select_one(".result__snippet")
724
+ line=((t.get_text(" ",strip=True) if t else "")+" — "+(sn.get_text(" ",strip=True) if sn else "")).strip(" —")
725
+ if line:bits.append(line[:280])
726
+ except:pass
727
+ return "\n".join(bits)
728
+
729
+ def _jina_read(url):
730
+ try:
731
+ ju="https://r.jina.ai/http://"+url
732
+ r=requests.get(ju,headers=HEADERS,timeout=25);r.encoding="utf-8"
733
+ if r.status_code!=200 or not r.text:return None
734
+ lines=[x.rstrip() for x in r.text.splitlines()]
735
+ title="";img="";body=[];summary=""
736
+ for ln in lines[:40]:
737
+ if ln.startswith("Title:"):title=ln.replace("Title:","",1).strip()
738
+ elif ln.startswith("Image:"):img=ln.replace("Image:","",1).strip()
739
+ elif ln.startswith("Description:"):summary=ln.replace("Description:","",1).strip()
740
+ for ln in lines:
741
+ t=ln.strip()
742
+ if not t or t.startswith(("Title:","URL Source:","Published Time:","Markdown Content:","Image:","Description:")):continue
743
+ if len(t)>40:body.append({"type":"p","text":t})
744
+ if not body and summary:body=[{"type":"p","text":summary}]
745
+ return {"title":title or url,"summary":summary,"og_image":img,"body":body[:80],"source":"jina","url":url}
746
+ except:return None
747
+
748
+ def _scrape_generic_article(url):
749
+ try:
750
+ hdr={**HEADERS,"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}
751
+ r=requests.get(url,headers=hdr,timeout=15);r.encoding="utf-8"
752
+ ct=r.headers.get("content-type","").lower()
753
+ if r.status_code>=400 or "text/html" not in ct:
754
+ jr=_jina_read(url)
755
+ if jr:return jr
756
+ soup=BeautifulSoup(r.text,"lxml")
757
+ for tag in soup.find_all(["script","style","nav","footer","aside","form"]):tag.decompose()
758
+ h1=soup.find("h1")
759
+ ogt=soup.find("meta",property="og:title");title=h1.get_text(strip=True) if h1 else (ogt.get("content","") if ogt else "")
760
+ ogd=soup.find("meta",property="og:description");desc=ogd.get("content","") if ogd else ""
761
+ ogi=soup.find("meta",property="og:image");img=ogi.get("content","") if ogi else ""
762
+ main=soup.find("article") or soup.find("main") or soup.body
763
+ body=[]
764
+ if main:
765
+ for el in main.find_all(["p","h2","h3","figure","img"],recursive=True):
766
+ if el.name=="p":
767
+ t=el.get_text(" ",strip=True)
768
+ if t and len(t)>35:body.append({"type":"p","text":t})
769
+ elif el.name in ("h2","h3"):
770
+ t=el.get_text(" ",strip=True)
771
+ if t:body.append({"type":"heading","text":t})
772
+ elif el.name in ("figure","img"):
773
+ im=el if el.name=="img" else el.find("img")
774
+ if im:
775
+ src=im.get("data-src") or im.get("src","") or im.get("data-original","")
776
+ if src and "base64" not in src:body.append({"type":"img","src":src})
777
+ if not body:
778
+ jr=_jina_read(url)
779
+ if jr and jr.get("body"):return jr
780
+ if not body and desc:body=[{"type":"p","text":desc}]
781
+ return {"title":title or url,"summary":desc,"og_image":img,"body":body,"source":"generic","url":url}
782
+ except:
783
+ return _jina_read(url)
784
+
785
+ def _article_by_url(url):
786
+ if "vnexpress.net" in url:return scrape_vne_article(url)
787
+ if "bbc.com" in url:return scrape_bbc_article(url)
788
+ if "dantri.com.vn" in url:return scrape_dantri_article(url)
789
+ if "genk.vn" in url:return scrape_genk_article(url)
790
+ if "thethaovanhoa.vn" in url:return scrape_ttvh_article(url)
791
+ return _scrape_generic_article(url)
792
+
793
+ def _call_qwen(prompt, max_tokens=1800):
794
+ """Try Qwen2.5-VL via HF router; return None if unavailable."""
795
+ try:
796
+ token=os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACEHUB_API_TOKEN") or os.environ.get("VAISTUDIO")
797
+ if not token:return None
798
+ headers={"Authorization":"Bearer "+token,"Content-Type":"application/json"}
799
+ payload={"model":"Qwen/Qwen2.5-VL-7B-Instruct","messages":[{"role":"user","content":prompt}],"max_tokens":max_tokens,"temperature":0.7}
800
+ r=requests.post("https://router.huggingface.co/v1/chat/completions",headers=headers,json=payload,timeout=75)
801
+ if r.status_code>=300:return None
802
+ j=r.json();return j.get("choices",[{}])[0].get("message",{}).get("content")
803
+ except:return None
804
+
805
+ def _collect_article_text(data, limit=28000):
806
+ title=(data or {}).get("title","");summary=(data or {}).get("summary","")
807
+ parts=[]
808
+ if summary:parts.append(summary)
809
+ for b in (data or {}).get("body",[]):
810
+ if b.get("type")=="heading":parts.append("## "+b.get("text","") )
811
+ elif b.get("type")=="p":parts.append(b.get("text","") )
812
+ text="\n".join([p.strip() for p in parts if p and p.strip()])
813
+ return title,text[:limit]
814
+
815
+ def _ai_rewrite_article(data,tone="tu-nhien"):
816
+ title,text=_collect_article_text(data)
817
+ prompt=("Bạn là biên tập viên báo điện tử tiếng Việt. Hãy viết lại bài dưới đây bằng ngôn ngữ tự nhiên, mạch lạc, không cắt khúc, không bỏ ý quan trọng. "
818
+ "Giữ đúng sự thật, không bịa, không thêm thông tin ngoài bài. Văn phong: "+tone+". "
819
+ "Đầu ra gồm: tiêu đề hấp dẫn, đoạn sapo 2-3 câu, các đoạn nội dung ngắn dễ đọc, và 3 gạch đầu dòng điểm chính.\n\n"
820
+ "TIÊU ĐỀ GỐC: "+title+"\n\nNỘI DUNG GỐC:\n"+text)
821
+ out=_call_qwen(prompt,2200)
822
+ if out and len(out)>300:return out.strip()
823
+ # Fallback: complete non-truncated rewrite using full collected text chunks
824
+ paras=[p.strip() for p in text.split("\n") if len(p.strip())>30]
825
+ body="\n\n".join(paras[:18])
826
+ bullets="\n".join(["• "+p[:220]+("..." if len(p)>220 else "") for p in paras[:5]])
827
+ return ("Bản tin AI viết lại: "+title+"\n\n"+
828
+ (paras[0] if paras else "")+"\n\n"+body+"\n\nĐiểm chính:\n"+bullets).strip()
829
+
830
+ def _image_for_topic(topic):
831
+ return "https://image.pollinations.ai/prompt/"+quote("editorial illustration, Vietnamese news, "+topic,safe="")+"?width=1024&height=576&nologo=true"
832
+
833
+ def _topic_articles(topic,limit=5):
834
+ items=[];seen=set()
835
+ try:
836
+ rss="https://news.google.com/rss/search?q="+quote(topic)+"&hl=vi&gl=VN&ceid=VN:vi"
837
+ r=requests.get(rss,headers=HEADERS,timeout=12);r.encoding="utf-8"
838
+ soup=BeautifulSoup(r.text,"xml")
839
+ for it in soup.find_all("item")[:limit*3]:
840
+ title=it.find("title").get_text(" ",strip=True) if it.find("title") else ""
841
+ link=it.find("link").get_text(strip=True) if it.find("link") else ""
842
+ src=it.find("source").get_text(" ",strip=True) if it.find("source") else ""
843
+ if not title or not link or link in seen:continue
844
+ seen.add(link);items.append({"title":title,"link":link,"source":src})
845
+ if len(items)>=limit:break
846
+ except:pass
847
+ return items
848
+
849
+ def _topic_article_context(topic):
850
+ """Filter readable article sources by topic, then summarize actual article bodies."""
851
+ raw_keys=[k.lower() for k in re.findall(r"[\wÀ-ỹ]+",topic) if len(k)>2]
852
+ # Drop ultra-generic tokens; keep domain words such as giáo/dục, bóng/đá, world/cup.
853
+ stop={"trong","năm","the","and","của","cho","với","một","các","những","hiện","nay"}
854
+ keys=[k for k in raw_keys if k not in stop]
855
+ candidates=[];seen=set()
856
+ def add_items(items):
857
+ for a in items or []:
858
+ link=a.get("link","");title=a.get("title","")
859
+ if not link or link in seen:continue
860
+ seen.add(link);candidates.append(a)
861
+ try:add_items(scrape_genk_ai())
862
+ except:pass
863
+ try:add_items(scrape_dantri_congnghe())
864
+ except:pass
865
+ try:add_items(scrape_ttvh_worldcup())
866
+ except:pass
867
+ scored=[];img=""
868
+ for a in candidates[:40]:
869
+ data=_article_by_url(a.get("link",""))
870
+ if not data or not data.get("body"):continue
871
+ title=data.get("title") or a.get("title","")
872
+ ps=[b.get("text","") for b in data.get("body",[]) if b.get("type")=="p" and len(b.get("text",""))>40]
873
+ excerpt=" ".join(ps)[:1800] or data.get("summary","")
874
+ hay=(title+" "+excerpt).lower()
875
+ score=sum(1 for k in keys if k in hay)
876
+ # Require topic relevance when we have meaningful keys.
877
+ if keys and score==0:continue
878
+ if len(keys)>=2 and score<2 and not any(" ".join(keys[i:i+2]) in hay for i in range(len(keys)-1)):continue
879
+ scored.append((score,title,a.get("link",""),excerpt,data.get("og_image") or a.get("img","") or ""))
880
+ scored=sorted(scored,key=lambda x:x[0],reverse=True)[:5]
881
+ chunks=[]
882
+ for score,title,link,excerpt,im in scored:
883
+ if not img and im:img=im
884
+ chunks.append("BÀI: "+title+"\nURL: "+link+"\nNỘI DUNG LỌC: "+excerpt)
885
+ if chunks:return "\n\n".join(chunks),img
886
+ return _web_context(topic),""
887
+
888
+ def _topic_post_text(topic):
889
+ ctx,img=_topic_article_context(topic)
890
+ prompt=("Bạn là cây bút báo điện tử tiếng Việt. Hãy lọc các thông tin thực tế trong những nguồn dưới đây để viết một bài tóm tắt theo chủ đề: "+topic+
891
+ ". Không viết chung chung. Chỉ dùng dữ kiện có trong nguồn; nếu nguồn khác nhau thì tổng hợp khách quan. "
892
+ "Đầu ra gồm: tiêu đề, sapo, các ý chính theo bullet, phần phân tích ngắn và kết luận.\n\nNGUỒN THỰC TẾ:\n"+ctx)
893
+ out=_call_qwen(prompt,1800)
894
+ if out and len(out)>300:return out.strip()
895
+ if ctx:
896
+ return "Bài tóm tắt theo chủ đề: "+topic+"\n\nDữ liệu thực tế đã lọc:\n"+ctx[:3500]+"\n\nTóm tắt: Các nguồn trên cho thấy chủ đề này đang có nhiều diễn biến đáng chú ý. Khi viết bài, nên nêu rõ bối cảnh, các điểm mới, tác động thực tế và những điều còn cần kiểm chứng."
897
+ return "Chưa thu thập được dữ liệu đủ rõ cho chủ đề: "+topic
898
+
899
+ @app.get("/api/wall")
900
+ def api_wall():return JSONResponse({"posts":_load_wall()[:50]})
901
+
902
+ @app.post("/api/rewrite_share")
903
+ async def api_rewrite_share(request:Request):
904
+ try:body=await request.json()
905
+ except:body={}
906
+ url=str(body.get("url","")).strip();tone=str(body.get("tone","tu-nhien")).strip()
907
+ if not url:return JSONResponse({"error":"missing url"},status_code=400)
908
+ data=_article_by_url(url)
909
+ if not data or not data.get("title") or (not data.get("body") and not data.get("summary")):
910
+ return JSONResponse({"error":"Không đọc được bài viết"},status_code=422)
911
+ post={"id":hashlib.md5((url+str(time.time())).encode()).hexdigest()[:12],"url":url,"title":data.get("title",""),"img":data.get("og_image","") or "","text":_ai_rewrite_article(data,tone),"ts":int(time.time()),"source":data.get("source","")}
912
+ posts=_load_wall();posts.insert(0,post);_save_wall(posts)
913
+ return JSONResponse({"post":post})
914
+
915
+ @app.post("/api/topic_post")
916
+ async def api_topic_post(request:Request):
917
+ try:body=await request.json()
918
+ except:body={}
919
+ topic=str(body.get("topic","")).strip()
920
+ if not topic:return JSONResponse({"error":"missing topic"},status_code=400)
921
+ ctx_img=_topic_article_context(topic)[1]
922
+ post={"id":hashlib.md5((topic+str(time.time())).encode()).hexdigest()[:12],"url":"","title":topic,"img":ctx_img or _image_for_topic(topic),"text":_topic_post_text(topic),"ts":int(time.time()),"source":"ai-topic"}
923
+ posts=_load_wall();posts.insert(0,post);_save_wall(posts)
924
+ return JSONResponse({"post":post})
925
+
926
+ @app.post("/api/url_wall")
927
+ async def api_url_wall(request:Request):
928
+ try:body=await request.json()
929
+ except:body={}
930
+ url=str(body.get("url","")).strip()
931
+ if not url:return JSONResponse({"error":"missing url"},status_code=400)
932
+ data=_article_by_url(url)
933
+ if not data or not data.get("title"):
934
+ return JSONResponse({"error":"Không đọc được URL"},status_code=422)
935
+ post={"id":hashlib.md5((url+str(time.time())).encode()).hexdigest()[:12],"url":url,"title":data.get("title",""),"img":data.get("og_image","") or "","text":_ai_rewrite_article(data,"ngan-gon-tu-nhien"),"ts":int(time.time()),"source":data.get("source","")}
936
+ posts=_load_wall();posts.insert(0,post);_save_wall(posts)
937
+ return JSONResponse({"post":post})
938
+
939
+ @app.get("/v")
940
+ async def video_share(url:str=Query(default=""),title:str=Query(default="VNEWS Video"),img:str=Query(default=""),type:str=Query(default="highlights")):
941
+ decoded_url=unquote(url);decoded_title=unquote(title)
942
+ redirect_script=f'<script>localStorage.setItem("pending_video",JSON.stringify({{"url":"{decoded_url}","type":"{type}"}}));location.href="{SPACE_URL}";</script>' if decoded_url else f'<script>location.href="{SPACE_URL}";</script>'
943
+ return HTMLResponse(f'<!DOCTYPE html><html><head><meta charset="utf-8"><title>{decoded_title}</title></head><body style="background:#111;color:#fff;text-align:center;padding:40px"><p>⏳</p>{redirect_script}</body></html>')
944
+ @app.get("/s")
945
+ async def share_redirect(url:str=Query(default=""),title:str=Query(default="VNEWS"),img:str=Query(default="")):
946
+ decoded_url=unquote(url)
947
+ if decoded_url and decoded_url.startswith("http"):
948
+ return RedirectResponse(url=decoded_url, status_code=302)
949
+ return RedirectResponse(url=SPACE_URL, status_code=302)
950
+ @app.get("/")
951
+ async def index():
952
+ with open("/app/static/index.html","r",encoding="utf-8") as f:return HTMLResponse(content=f.read())
953
+ app.mount("/static",StaticFiles(directory="/app/static"),name="static")
main_patch.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # PATCH: Add these 2 lines to main.py right after "app = FastAPI()"
2
+ # Line 1: from vtv_api import router as vtv_router
3
+ # Line 2: app.include_router(vtv_router)
4
+ #
5
+ # This enables the VTV1-VTV10 + VTVPrime stream endpoints:
6
+ # GET /api/vtv/streams - Get all channel streams
7
+ # GET /api/vtv/stream/{id} - Get specific channel stream
8
+ # GET /api/proxy/page?url=... - Proxy web pages (for xemtv PHP scraping)
match_detail.py ADDED
@@ -0,0 +1,309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Match Detail Scraper for bongda.com.vn
3
+ """
4
+ import requests, re, json, time, threading
5
+ from bs4 import BeautifulSoup
6
+
7
+ def _sp(html):
8
+ try:
9
+ return BeautifulSoup(html, 'lxml')
10
+ except:
11
+ return BeautifulSoup(html, 'html.parser')
12
+
13
+ BH = {
14
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
15
+ "Accept": "application/json, text/javascript, */*; q=0.01",
16
+ "Referer": "https://bongda.com.vn/",
17
+ "X-Requested-With": "XMLHttpRequest",
18
+ }
19
+ HH = {
20
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
21
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
22
+ "Referer": "https://bongda.com.vn/",
23
+ }
24
+
25
+ def _cl(s):
26
+ return re.sub(r'\s+', ' ', str(s or '')).strip()
27
+
28
+ def _api(ep, params=None):
29
+ try:
30
+ url = f"https://bongda.com.vn{ep}"
31
+ if params:
32
+ url += "?" + "&".join(f"{k}={v}" for k, v in params.items())
33
+ r = requests.get(url, headers=BH, timeout=15)
34
+ if r.status_code == 200:
35
+ try: return r.json()
36
+ except: pass
37
+ except: pass
38
+ return None
39
+
40
+ def _get_teams(soup):
41
+ info = {}
42
+ tel = soup.select_one('.teams')
43
+ if not tel:
44
+ return info
45
+ he = tel.select_one('.team.home, .home-team')
46
+ if he:
47
+ ne = he.select_one('p:not(.logo)') or he.find('p')
48
+ if ne: info['home_team'] = _cl(ne.get_text())
49
+ lo = he.select_one('img')
50
+ if lo: info['home_logo'] = lo.get('src', '')
51
+ le = he if he.name == 'a' else he.find('a')
52
+ if le and le.get('href'):
53
+ m = re.search(r'/doi-bong/(\d+)/', le['href'])
54
+ if m: info['home_team_id'] = m.group(1)
55
+ ae = tel.select_one('.team.away, .away-team')
56
+ if ae:
57
+ ne = ae.select_one('p:not(.logo)') or ae.find('p')
58
+ if ne: info['away_team'] = _cl(ne.get_text())
59
+ lo = ae.select_one('img')
60
+ if lo: info['away_logo'] = lo.get('src', '')
61
+ le = ae if ae.name == 'a' else ae.find('a')
62
+ if le and le.get('href'):
63
+ m = re.search(r'/doi-bong/(\d+)/', le['href'])
64
+ if m: info['away_team_id'] = m.group(1)
65
+ sc = tel.select_one('.score')
66
+ if sc:
67
+ parts = [_cl(p.get_text()) for p in sc.select('p')]
68
+ if len(parts) >= 2: info['score'] = f"{parts[0]} - {parts[1]}"
69
+ lb = sc.select_one('.label')
70
+ if lb: info['status_label'] = _cl(lb.get_text())
71
+ return info
72
+
73
+ def _get_timeline(soup):
74
+ tl = []
75
+ el = soup.select_one('.timeline')
76
+ if not el: return tl
77
+ half = ''
78
+ for c in el.children:
79
+ if not hasattr(c, 'name') or not c.name: continue
80
+ t = _cl(c.get_text())
81
+ if not t: continue
82
+ if t in ['H1','H2','Hiệp 1','Hiệp 2']:
83
+ half = t; continue
84
+ m = re.match(r"(\d+'\+?\d*)", t)
85
+ if m:
86
+ tl.append({'time': m.group(1), 'text': t[m.end():].strip(), 'half': half})
87
+ elif len(t) > 5:
88
+ tl.append({'time': '', 'text': t, 'half': half})
89
+ return tl
90
+
91
+ def _get_events(soup):
92
+ evts = []
93
+ for el in soup.select('.event'):
94
+ e = {}
95
+ cl = ' '.join(el.get('class', []))
96
+ e['team'] = 'home' if 'home' in cl else ('away' if 'away' in cl else '')
97
+ ps = [_cl(p.get_text()) for p in el.select('p')]
98
+ ps = [p for p in ps if p]
99
+ if ps: e['players'] = ps
100
+ tl = el.select_one('.time, .minute, span')
101
+ if tl: e['time'] = _cl(tl.get_text())
102
+ evts.append(e)
103
+ return evts
104
+
105
+ def _get_stats(soup):
106
+ st = {}
107
+ for sel in ['.match-stats','[class*="stats"]']:
108
+ el = soup.select_one(sel)
109
+ if el and len(str(el)) > 50:
110
+ for row in el.select('li,tr,.stat-row'):
111
+ cells = row.select('td,span,p')
112
+ if len(cells) >= 3:
113
+ lb = _cl(cells[0].get_text())
114
+ if lb: st[lb] = {'home': _cl(cells[1].get_text()), 'away': _cl(cells[2].get_text())}
115
+ if st: break
116
+ return st
117
+
118
+ def _get_h2h(soup):
119
+ h2h = {'matches': [], 'stats': {}}
120
+ for sel in ['.head-to-head','[class*="h2h"]']:
121
+ el = soup.select_one(sel)
122
+ if el and len(str(el)) > 50:
123
+ for it in el.select('li,tr,.match-item'):
124
+ m = {}
125
+ cells = it.select('td,span,p')
126
+ if len(cells) >= 3:
127
+ m['date'] = _cl(cells[0].get_text())
128
+ m['home'] = _cl(cells[1].get_text())
129
+ m['score'] = _cl(cells[2].get_text())
130
+ if m.get('home'):
131
+ if len(cells) > 3: m['away'] = _cl(cells[3].get_text())
132
+ h2h['matches'].append(m)
133
+ if h2h['matches']: break
134
+ return h2h
135
+
136
+ def _get_form(soup):
137
+ f = {'home': [], 'away': []}
138
+ for sel in ['.form-guide','[class*="form"]']:
139
+ el = soup.select_one(sel)
140
+ if el and len(str(el)) > 50:
141
+ items = el.select('li,.form-item,tr')
142
+ for it in items[:10]:
143
+ t = _cl(it.get_text())
144
+ if t: f['home'].append({'text': t})
145
+ for it in items[10:20]:
146
+ t = _cl(it.get_text())
147
+ if t: f['away'].append({'text': t})
148
+ break
149
+ return f
150
+
151
+ def _get_info(soup):
152
+ info = {}
153
+ mi = soup.select_one('.match-info')
154
+ if mi:
155
+ te = mi.select_one('.times,li')
156
+ if te: info['datetime'] = _cl(te.get_text())
157
+ le = soup.select_one('.league,.tournament,[class*="league"]')
158
+ if le: info['league'] = _cl(le.get_text())
159
+ return info
160
+
161
+ def _scrape(url):
162
+ print(f"[DEBUG] _scrape: {url[:80]}", flush=True)
163
+ try:
164
+ r = requests.get(url, headers=HH, timeout=15, allow_redirects=True)
165
+ print(f"[DEBUG] HTTP={r.status_code}", flush=True)
166
+ if r.status_code != 200:
167
+ return False, {}
168
+ sp = _sp(r.text)
169
+ d = {}
170
+
171
+ teams = _get_teams(sp)
172
+ print(f"[DEBUG] teams={teams}", flush=True)
173
+ if teams: d['info'] = teams
174
+
175
+ mi = _get_info(sp)
176
+ if mi:
177
+ d.setdefault('info', {}).update(mi)
178
+
179
+ tl = _get_timeline(sp)
180
+ if tl:
181
+ d['timeline'] = tl
182
+ d['commentaries_html'] = '\n'.join([f"{t.get('time','')} {t.get('text','')}" for t in tl])
183
+
184
+ ev = _get_events(sp)
185
+ if ev: d['events'] = ev
186
+
187
+ st = _get_stats(sp)
188
+ if st:
189
+ d['stats_parsed'] = st
190
+ d['stats_html'] = str(st)
191
+
192
+ h2h = _get_h2h(sp)
193
+ if h2h.get('matches'): d['h2h_matches'] = h2h['matches']
194
+ if h2h.get('stats'): d['h2h_stats'] = h2h['stats']
195
+
196
+ if '/preview/' in url:
197
+ fm = _get_form(sp)
198
+ if fm.get('home'): d['home_form'] = fm['home']
199
+ if fm.get('away'): d['away_form'] = fm['away']
200
+
201
+ print(f"[DEBUG] success keys={list(d.keys())}", flush=True)
202
+ return True, d
203
+ except Exception as e:
204
+ import traceback
205
+ print(f"[DEBUG] error: {e}", flush=True)
206
+ traceback.print_exc()
207
+ return False, {}
208
+
209
+ def fetch_match_detail_by_url(url):
210
+ m = re.search(r'/tran-dau/(\d+)/', url)
211
+ if not m: return {"error": "Could not extract event_id", "found": False}
212
+ event_id = int(m.group(1))
213
+ res = {"event_id": event_id, "found": False, "sections": []}
214
+ _fetch_api(event_id, res)
215
+ ok, d = _scrape(url)
216
+ print(f"[DEBUG] by_url: ok={ok} d_keys={list(d.keys())}", flush=True)
217
+ if ok: _merge(res, d)
218
+ return res
219
+
220
+ def fetch_match_detail(event_id):
221
+ print(f"[DEBUG] fetch_match_detail({event_id})", flush=True)
222
+ res = {"event_id": event_id, "found": False, "sections": []}
223
+ _fetch_api(event_id, res)
224
+
225
+ for pt in ["centre", "preview"]:
226
+ url = f"https://bongda.com.vn/tran-dau/{event_id}/{pt}/"
227
+ ok, d = _scrape(url)
228
+ print(f"[DEBUG] {pt}: ok={ok}", flush=True)
229
+ if ok:
230
+ _merge(res, d)
231
+ if res.get("found"): break
232
+
233
+ print(f"[DEBUG] final: found={res['found']} sections={res['sections']}", flush=True)
234
+ return res
235
+
236
+ def _fetch_api(eid, res):
237
+ pm = _api("/api/event-standing/pre-match", {"event_id": eid})
238
+ res["pre_match"] = pm
239
+ res["pre_match_html"] = pm.get("html","") if pm and pm.get("status")=="success" and len(pm.get("html","").strip())>10 else ""
240
+
241
+ hm = _api("/api/fixtures/h2h-match", {"event_id": eid})
242
+ res["h2h_match"] = hm
243
+ if hm and hm.get("status")=="success":
244
+ h = hm.get("html","")
245
+ if len(h.strip())>10:
246
+ res["h2h_html"] = h
247
+ res["sections"].append("h2h")
248
+ else: res["h2h_html"] = ""
249
+
250
+ hs = _api("/api/fixtures/h2h-stats", {"event_id": eid})
251
+ res["h2h_stats"] = hs
252
+ if hs and hs.get("status")=="success":
253
+ h = hs.get("html","")
254
+ if len(h.strip())>10:
255
+ res["h2h_stats_html"] = h
256
+ res["sections"].append("h2h_stats")
257
+ try:
258
+ sp = _sp(h)
259
+ stats = {}
260
+ for row in sp.select('li,tr,.stat-row'):
261
+ cells = row.select('td,span,p')
262
+ if len(cells)>=3:
263
+ lb = _cl(cells[0].get_text())
264
+ if lb: stats[lb] = {'home': _cl(cells[1].get_text()), 'away': _cl(cells[2].get_text())}
265
+ if stats: res["h2h_stats_parsed"] = stats
266
+ except: pass
267
+ else: res["h2h_stats_html"] = ""
268
+
269
+ pf = _api("/api/event-standing/player-performance", {"event_id": eid})
270
+ res["performance"] = pf
271
+ if pf and pf.get("status")=="success" and len(pf.get("html","").strip())>10:
272
+ res["stats_html"] = pf["html"]
273
+ res["sections"].append("stats")
274
+ else: res["stats_html"] = ""
275
+
276
+ cm = _api("/api/fixtures/commentaries", {"event_id": eid})
277
+ if cm and cm.get("status")=="success" and len(cm.get("html","").strip())>10:
278
+ res["commentaries_html"] = cm["html"]
279
+ res["sections"].append("commentaries")
280
+ elif not res.get("commentaries_html"): res["commentaries_html"] = ""
281
+
282
+ def _merge(res, d):
283
+ if d.get("info"):
284
+ res.setdefault("info", {}).update(d["info"])
285
+ res["found"] = True
286
+ if "info" not in res["sections"]: res["sections"].append("info")
287
+ if d.get("timeline"):
288
+ res["timeline"] = d["timeline"]
289
+ if not res.get("commentaries_html"): res["commentaries_html"] = d.get("commentaries_html","")
290
+ res["sections"].append("commentaries")
291
+ if d.get("events"):
292
+ res["events"] = d["events"]
293
+ res["sections"].append("events")
294
+ if d.get("stats_parsed"):
295
+ res["stats_parsed"] = d["stats_parsed"]
296
+ if not res.get("stats_html"): res["stats_html"] = d.get("stats_html","")
297
+ res["sections"].append("stats")
298
+ if d.get("h2h_matches"):
299
+ res["h2h"] = d["h2h_matches"]
300
+ res["sections"].append("h2h")
301
+ if d.get("h2h_stats"):
302
+ res["h2h_stats_parsed"] = d["h2h_stats"]
303
+ res["sections"].append("h2h_stats")
304
+ if d.get("home_form"):
305
+ res["home_form"] = d["home_form"]
306
+ res["sections"].append("home_form")
307
+ if d.get("away_form"):
308
+ res["away_form"] = d["away_form"]
309
+ res["sections"].append("away_form")
patch_extra.py CHANGED
@@ -35,7 +35,7 @@ window.makeShortFromPost=async function(pid,btn){
35
  var r=await fetch('/api/ai/short/'+pid,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice:'nu',emotion:'neutral',speed:1.2})});
36
  var j=await r.json();
37
  if(!r.ok||j.error)throw new Error(j.error||'Lỗi');
38
- showShortProgress('✅ Đã tạo Short AI!');
39
  setTimeout(hideShortProgress,3000);
40
  if(typeof renderShortAISlide==='function')renderShortAISlide();
41
  }catch(e){
 
35
  var r=await fetch('/api/ai/short/'+pid,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({voice:'nu',emotion:'neutral',speed:1.2})});
36
  var j=await r.json();
37
  if(!r.ok||j.error)throw new Error(j.error||'Lỗi');
38
+ showShortProgress('✅ Đ�ã tạo Short AI!');
39
  setTimeout(hideShortProgress,3000);
40
  if(typeof renderShortAISlide==='function')renderShortAISlide();
41
  }catch(e){
patch_runtime.py CHANGED
@@ -1,22 +1,32 @@
1
- """Runtime patch layer for VNEWS."""
2
- import hashlib, time, os
 
 
 
 
 
3
  from urllib.parse import quote
 
4
  import requests
5
  from bs4 import BeautifulSoup
6
  from fastapi import Request
7
  from fastapi.responses import JSONResponse, HTMLResponse
 
8
  import main as _main
9
 
10
  app = _main.app
11
  DEFAULT_IMG = "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
12
 
 
13
  def _remove_routes(paths):
14
  app.router.routes = [r for r in app.router.routes if getattr(r, "path", None) not in set(paths)]
15
 
 
16
  def _safe_text(v):
17
  return (v or "").strip()
18
 
19
- def _ensure_article(url):
 
20
  data = None
21
  try:
22
  if hasattr(_main, "_article_by_url"):
@@ -71,6 +81,7 @@ def _ensure_article(url):
71
  data.update({"title": title, "summary": summary, "og_image": img, "body": body, "url": url})
72
  return data
73
 
 
74
  def _rewrite(data, tone="tu-nhien"):
75
  try:
76
  if hasattr(_main, "_ai_rewrite_article"):
@@ -87,6 +98,7 @@ def _rewrite(data, tone="tu-nhien"):
87
  body = "\n\n".join(ps[:10])
88
  return (f"Bản tin AI viết lại: {title}\n\n{lead}\n\n{body}\n\nĐiểm chính:\n{points}").strip()
89
 
 
90
  def _topic_image(topic):
91
  try:
92
  if hasattr(_main, "_image_for_topic"):
@@ -95,6 +107,7 @@ def _topic_image(topic):
95
  pass
96
  return "https://image.pollinations.ai/prompt/" + quote("editorial illustration Vietnamese news " + topic, safe="") + "?width=1024&height=576&nologo=true"
97
 
 
98
  def _save_post(post):
99
  try:
100
  posts = _main._load_wall() if hasattr(_main, "_load_wall") else []
@@ -108,8 +121,10 @@ def _save_post(post):
108
  pass
109
  return post
110
 
 
111
  _remove_routes(["/api/url_wall", "/api/topic_post", "/api/rewrite_share", "/"])
112
 
 
113
  @app.post("/api/url_wall")
114
  async def patched_url_wall(request: Request):
115
  try:
@@ -138,10 +153,12 @@ async def patched_url_wall(request: Request):
138
  except Exception as e:
139
  return JSONResponse({"error": "Không tạo được tóm tắt URL", "detail": str(e)[:300]}, status_code=500)
140
 
 
141
  @app.post("/api/rewrite_share")
142
  async def patched_rewrite_share(request: Request):
143
  return await patched_url_wall(request)
144
 
 
145
  @app.post("/api/topic_post")
146
  async def patched_topic_post(request: Request):
147
  try:
@@ -180,6 +197,7 @@ async def patched_topic_post(request: Request):
180
  except Exception as e:
181
  return JSONResponse({"error": "Không tạo được bài theo chủ đề", "detail": str(e)[:300]}, status_code=500)
182
 
 
183
  _FRONTEND_PATCH = r'''
184
  <script>
185
  (function(){
@@ -243,6 +261,7 @@ _FRONTEND_PATCH = r'''
243
  </script>
244
  '''
245
 
 
246
  @app.get("/")
247
  async def patched_index():
248
  try:
 
1
+ """Runtime patch layer for VNEWS.
2
+ Keeps the current large app intact, but replaces fragile AI wall endpoints with
3
+ stable JSON endpoints and injects frontend safeJson wrappers.
4
+ """
5
+ import hashlib
6
+ import time
7
+ import os
8
  from urllib.parse import quote
9
+
10
  import requests
11
  from bs4 import BeautifulSoup
12
  from fastapi import Request
13
  from fastapi.responses import JSONResponse, HTMLResponse
14
+
15
  import main as _main
16
 
17
  app = _main.app
18
  DEFAULT_IMG = "https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg"
19
 
20
+
21
  def _remove_routes(paths):
22
  app.router.routes = [r for r in app.router.routes if getattr(r, "path", None) not in set(paths)]
23
 
24
+
25
  def _safe_text(v):
26
  return (v or "").strip()
27
 
28
+
29
+ def _ensure_article(url: str):
30
  data = None
31
  try:
32
  if hasattr(_main, "_article_by_url"):
 
81
  data.update({"title": title, "summary": summary, "og_image": img, "body": body, "url": url})
82
  return data
83
 
84
+
85
  def _rewrite(data, tone="tu-nhien"):
86
  try:
87
  if hasattr(_main, "_ai_rewrite_article"):
 
98
  body = "\n\n".join(ps[:10])
99
  return (f"Bản tin AI viết lại: {title}\n\n{lead}\n\n{body}\n\nĐiểm chính:\n{points}").strip()
100
 
101
+
102
  def _topic_image(topic):
103
  try:
104
  if hasattr(_main, "_image_for_topic"):
 
107
  pass
108
  return "https://image.pollinations.ai/prompt/" + quote("editorial illustration Vietnamese news " + topic, safe="") + "?width=1024&height=576&nologo=true"
109
 
110
+
111
  def _save_post(post):
112
  try:
113
  posts = _main._load_wall() if hasattr(_main, "_load_wall") else []
 
121
  pass
122
  return post
123
 
124
+
125
  _remove_routes(["/api/url_wall", "/api/topic_post", "/api/rewrite_share", "/"])
126
 
127
+
128
  @app.post("/api/url_wall")
129
  async def patched_url_wall(request: Request):
130
  try:
 
153
  except Exception as e:
154
  return JSONResponse({"error": "Không tạo được tóm tắt URL", "detail": str(e)[:300]}, status_code=500)
155
 
156
+
157
  @app.post("/api/rewrite_share")
158
  async def patched_rewrite_share(request: Request):
159
  return await patched_url_wall(request)
160
 
161
+
162
  @app.post("/api/topic_post")
163
  async def patched_topic_post(request: Request):
164
  try:
 
197
  except Exception as e:
198
  return JSONResponse({"error": "Không tạo được bài theo chủ đề", "detail": str(e)[:300]}, status_code=500)
199
 
200
+
201
  _FRONTEND_PATCH = r'''
202
  <script>
203
  (function(){
 
261
  </script>
262
  '''
263
 
264
+
265
  @app.get("/")
266
  async def patched_index():
267
  try:
restore.py DELETED
@@ -1,40 +0,0 @@
1
- """Restore all files from commit 60e310a on HuggingFace Hub"""
2
- import os, sys
3
-
4
- def run():
5
- try:
6
- from huggingface_hub import HfApi, snapshot_download
7
- except ImportError:
8
- os.system(f"{sys.executable} -m pip install -q huggingface_hub")
9
- from huggingface_hub import HfApi, snapshot_download
10
-
11
- repo_id = "bep40/VNEWS"
12
- revision = "60e310a"
13
- target_dir = "/app"
14
-
15
- print(f"[RESTORE] Downloading snapshot from {repo_id}@{revision}...", flush=True)
16
-
17
- # Download to temp dir
18
- import tempfile
19
- tmpdir = tempfile.mkdtemp()
20
- local_dir = snapshot_download(repo_id=repo_id, revision=revision, repo_type="space", local_dir=tmpdir)
21
-
22
- print(f"[RESTORE] Downloaded to {local_dir}", flush=True)
23
-
24
- # Copy files to /app (overwrite)
25
- import shutil
26
- for root, dirs, files in os.walk(local_dir):
27
- for f in files:
28
- src = os.path.join(root, f)
29
- rel = os.path.relpath(src, local_dir)
30
- dst = os.path.join(target_dir, rel)
31
- os.makedirs(os.path.dirname(dst), exist_ok=True)
32
- shutil.copy2(src, dst)
33
- print(f"[RESTORE] {rel}", flush=True)
34
-
35
- # Cleanup
36
- shutil.rmtree(tmpdir)
37
- print(f"[RESTORE] Done! All files restored from {revision}", flush=True)
38
-
39
- if __name__ == "__main__":
40
- run()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
static/app_v3.js ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // === VNEWS Frontend v2 - Full Functions ===
2
+ // Updated: Voice selector + speed control + image gallery + auto voice detect
3
+
4
+ // === LOAD HOME ===
5
+ async function loadHome(){
6
+ const[featured,sh,wall,hlLeagues,ai,wcData]=await Promise.all([
7
+ fetch('/api/livescore/featured').then(r=>r.json()).catch(()=>null),
8
+ fetch('/api/shorts').then(r=>r.json()).catch(()=>[]),
9
+ fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]})),
10
+ fetch('/api/highlights/leagues').then(r=>r.json()).catch(()=>({})),
11
+ fetch('/api/genk_ai').then(r=>r.json()).catch(()=>[]),
12
+ fetch('/api/wc2026').then(r=>r.json()).catch(()=>null)
13
+ ]);
14
+ _hlLeagueData=hlLeagues;
15
+ _wc2026Data=wcData;
16
+ _shortsData=interleaveShorts(sh||[]);
17
+ _wallPosts=(wall&&wall.posts)||[];
18
+ let h='';
19
+ if(featured&&featured.home){
20
+ const sc=featured.status==='live'?'':'upcoming';
21
+ const st=featured.status==='live'?`🔴 ${featured.minute||'LIVE'}`:`⏰ ${featured.time}`;
22
+ // Safely encode for HTML attribute: escape quotes, angle brackets, ampersands
23
+ const eid = String(featured.event_id||'').replace(/[<>&"']/g,'');
24
+ const mUrl = String(featured.url||'').replace(/[<>&"']/g,'');
25
+ const fHome = String(featured.home||'').replace(/[<>&"']/g,'');
26
+ const fAway = String(featured.away||'').replace(/[<>&"']/g,'');
27
+ const fLeague = String(featured.league||'').replace(/[<>&"']/g,'');
28
+ const fScore = String(featured.score||'VS').replace(/[<>&"']/g,'');
29
+ const fHomeLogo = String(featured.home_logo||'').replace(/[<>&"']/g,'');
30
+ const fAwayLogo = String(featured.away_logo||'').replace(/[<>&"']/g,'');
31
+ const safeTitle = `${fHome} vs ${fAway} — ${fLeague}`;
32
+ h+=`<div class="featured-match" data-event-id="${eid}" data-url="${mUrl}" onclick="openMatch('${eid}','${mUrl}')" title="${safeTitle}">`+
33
+ `<div class="fm-league">${fLeague}</div>`+
34
+ `<div class="fm-teams">`+
35
+ `<div class="fm-team"><img src="${fHomeLogo}" onerror="this.style.display='none'"><span>${fHome}</span></div>`+
36
+ `<div class="fm-score">${fScore}</div>`+
37
+ `<div class="fm-team"><img src="${fAwayLogo}" onerror="this.style.display='none'"><span>${fAway}</span></div>`+
38
+ `</div>`+
39
+ `<div class="fm-status ${sc}">${st}</div>`+
40
+ `</div>`;
41
+ }
42
+ h+=`<div class="ai-compose"><div class="ai-compose-title">🤖 AI viết bài</div><div class="ai-compose-row"><input id="topic-input" placeholder="Nhập chủ đề..."><button onclick="searchTopic()">Tìm nguồn</button></div><div class="ai-compose-row"><input id="url-input" placeholder="Dán URL bài viết..."><button class="secondary" onclick="rewriteUrl()">Rewrite</button></div><div id="hot-topics" class="hot-topic-row"></div></div>`;
43
+ h+='<div id="hashtag-box"></div>';
44
+ h+=`<div class="ls-section"><div class="ls-header"><h3>⚽ Livescore</h3></div><div class="ls-tabs"><span class="ls-tab active" data-tab="today" onclick="loadLivescore('today')">📅 Hôm nay</span><span class="ls-tab" data-tab="live" onclick="loadLivescore('live')">🔴 Live</span><span class="ls-tab" data-tab="incoming" onclick="loadLivescore('incoming')">⏰ Sắp tới</span><span class="ls-tab" data-tab="results" onclick="loadLivescore('results')">✅ Kết quả</span><span class="ls-tab" data-tab="bxh_nha" onclick="loadLivescore('bxh_nha')">🏆 NHA</span><span class="ls-tab" data-tab="bxh_laliga" onclick="loadLivescore('bxh_laliga')">🏆 La Liga</span></div><div class="ls-content" id="ls-content"><div class="loading">Đang tải...</div></div></div>`;
45
+ h+=`<div id="wc2026-live-section" class="wc2026-section"><div class="wc-header"><h2>🏆 World Cup 2026</h2><span class="wc-live-badge">● LIVE</span></div><div class="wc-tabs"><span class="wc-tab active" onclick="switchWCTab('news')">📰 Tin tức</span><span class="wc-tab" onclick="switchWCTab('fixtures')">📅 Lịch thi đấu</span><span class="wc-tab" onclick="switchWCTab('standings')">🏆 BXH</span><span class="wc-tab" onclick="switchWCTab('highlights')">🎬 Highlight</span><span class="wc-tab" onclick="switchWCTab('stats')">📊 Thống kê</span></div><div class="wc-content" id="wc-content"><div class="loading">Đang tải World Cup 2026...</div></div></div>`;
46
+ const wallPosts=_wallPosts;
47
+ const aiShorts=wallPosts.filter(p=>p.video);
48
+ if(aiShorts.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🎬 Short AI</span></div><div class="slider-track">';aiShorts.slice(0,20).forEach((p,i)=>{h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${p.video}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div></div>';}
49
+ if(_shortsData.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS</span><span class="slider-note">Mới nhất · xen kẽ</span></div><div class="slider-track">';_shortsData.slice(0,30).forEach((a,i)=>{const badge=a.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';h+=`<div class="slider-item shorts-item" onclick="openYTShortsFeed(${i})"><div class="slider-thumb shorts-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title"><span style="color:#f0c040;font-size:8px">${badge}</span> ${esc(a.title)}</div></div>`});h+='</div></div>';}
50
+ if(wallPosts.length){h+=`<div class="slider-wrap" id="ai-wall-wrap"><div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">`;wallPosts.slice(0,20).forEach((p,i)=>{h+=makeWallItem(p,i)});h+='</div></div>';}
51
+ const HL_CONFIG={"world-cup":{name:"World Cup 2026",emoji:"🌍"},"premier-league":{name:"Premier League",emoji:"🏴"},"champions-league":{name:"Champions League",emoji:"⭐"},"la-liga":{name:"La Liga",emoji:"🇪🇸"},"serie-a":{name:"Serie A",emoji:"🇮🇹"},"bundesliga":{name:"Bundesliga",emoji:"🇩🇪"},"friendly":{name:"Giao hữu",emoji:"🤝"}};
52
+ for(const[key,cfg] of Object.entries(HL_CONFIG)){const vids=hlLeagues[key];if(!vids||!vids.length)continue;h+=`<div class="slider-wrap"><div class="slider-header"><span class="slider-label">${cfg.emoji} ${cfg.name}</span></div><div class="slider-track">`;vids.slice(0,8).forEach((a,i)=>{h+=`<div class="slider-item" onclick="openHighlightFeed('${key}',${i})"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
53
+ if(ai&&ai.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🤖 Ứng dụng AI</span></div><div class="slider-track">';ai.slice(0,12).forEach(a=>{h+=`<div class="slider-item" onclick="readArticle('${esc(a.link)}')"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}</div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
54
+ document.getElementById('view-home').innerHTML=h;
55
+ loadLivescore('today');loadHotTopics();
56
+ if(_wc2026Data)switchWCTab('news');
57
+ }
58
+
59
+ // === WALL POST HELPERS ===
60
+ function makeWallItem(p,i){
61
+ const hasVideo = p.video && p.video.length > 0;
62
+ const thumbContent = p.img
63
+ ? `<img src="${esc(p.img)}" onerror="this.style.display='none'">`
64
+ : (hasVideo ? `<video src="${esc(p.video)}" muted></video>` : '');
65
+ const videoBadge = hasVideo
66
+ ? `<div class="wall-video-badge">🎬</div>`
67
+ : '';
68
+ const videoBtn = hasVideo
69
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(${i})">▶ Xem Short</button>`
70
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(p.id||i)}',this)">🎬 Tạo Video</button>`;
71
+
72
+ return `<div class="wall-item" id="wall-item-${esc(p.id||i)}">
73
+ <div class="wall-thumb">
74
+ ${thumbContent}
75
+ ${videoBadge}
76
+ </div>
77
+ <div class="wall-title">${esc(p.title)}</div>
78
+ <div class="wall-text">${esc((p.text||'').slice(0,180))}</div>
79
+ <div class="wall-actions">
80
+ <button class="primary" onclick="readWallPost(${i})">Xem</button>
81
+ ${videoBtn}
82
+ </div>
83
+ </div>`;
84
+ }
85
+
86
+ // === GENERATE SHORT VIDEO FOR A WALL POST ===
87
+ async function makeShortVideo(postId, btn, voice, speed){
88
+ if(!postId)return;
89
+ const origText = btn ? btn.textContent : '🎬 Tạo Video';
90
+ if(btn){btn.disabled=true;btn.textContent='⏳ Đang tạo...';}
91
+ toast('⏳ Đang tạo video shorts...');
92
+ try{
93
+ let url = '/api/ai/short/'+encodeURIComponent(postId);
94
+ const params = [];
95
+ if(voice) params.push('voice='+encodeURIComponent(voice));
96
+ if(speed) params.push('speed='+encodeURIComponent(speed));
97
+ if(params.length) url += '?' + params.join('&');
98
+ const r = await fetch(url, {method:'POST'});
99
+ const j = await r.json();
100
+ if(!r.ok || j.error) throw new Error(j.error||'Lỗi tạo video');
101
+ toast('✅ Đã tạo video shorts!');
102
+ const p = _wallPosts.find(x => String(x.id) === String(postId));
103
+ if(p){
104
+ p.video = j.video;
105
+ const itemId = 'wall-item-'+postId;
106
+ const el = document.getElementById(itemId);
107
+ if(el){
108
+ const idx = _wallPosts.indexOf(p);
109
+ el.outerHTML = makeWallItem(p, idx);
110
+ const newEl = document.getElementById(itemId);
111
+ if(newEl) newEl.className = 'wall-item wall-item-new';
112
+ }
113
+ }
114
+ refreshShortAISlider();
115
+ }catch(e){
116
+ toast('❌ '+e.message);
117
+ if(btn){btn.disabled=false;btn.textContent=origText;}
118
+ }
119
+ }
120
+
121
+ // Refresh Short AI slider after video generation
122
+ function refreshShortAISlider(){
123
+ const aiShorts = _wallPosts.filter(p=>p.video);
124
+ let shortAISection = document.getElementById('short-ai-section');
125
+ if(aiShorts.length === 0){
126
+ if(shortAISection) shortAISection.remove();
127
+ return;
128
+ }
129
+ if(shortAISection){
130
+ const track = shortAISection.querySelector('.slider-track');
131
+ if(track){
132
+ let h = '';
133
+ aiShorts.slice(0,20).forEach((p,i)=>{
134
+ h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${esc(p.video)}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`;
135
+ });
136
+ track.innerHTML = h;
137
+ }
138
+ }
139
+ }
140
+
141
+ function prependWallPost(post){
142
+ _wallPosts.unshift(post);
143
+ const track=document.getElementById('ai-wall-track');
144
+ const wrap=document.getElementById('ai-wall-wrap');
145
+ const homeEl=document.getElementById('view-home');
146
+ if(!track||!wrap){
147
+ if(homeEl){
148
+ let insertBefore=homeEl.querySelector('.slider-wrap');
149
+ const newWrap=document.createElement('div');
150
+ newWrap.className='slider-wrap';
151
+ newWrap.id='ai-wall-wrap';
152
+ newWrap.innerHTML=`<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">${makeWallItem(post,0)}</div>`;
153
+ if(insertBefore){
154
+ homeEl.insertBefore(newWrap,insertBefore);
155
+ }else{
156
+ homeEl.appendChild(newWrap);
157
+ }
158
+ const firstItem=newWrap.querySelector('.wall-item');
159
+ if(firstItem)firstItem.className='wall-item wall-item-new';
160
+ }
161
+ return;
162
+ }
163
+ const div=document.createElement('div');
164
+ div.className='wall-item wall-item-new';
165
+ div.id='wall-item-'+(post.id||'new-'+Date.now());
166
+ const hasVideo = post.video && post.video.length > 0;
167
+ const thumbContent = post.img
168
+ ? `<img src="${esc(post.img)}" onerror="this.style.display='none'">`
169
+ : (hasVideo ? `<video src="${esc(post.video)}" muted></video>` : '');
170
+ const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
171
+ const videoBtn = hasVideo
172
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(0)">▶ Xem Short</button>`
173
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(post.id)}',this)">🎬 Tạo Video</button>`;
174
+ div.innerHTML=`<div class="wall-thumb">${thumbContent}${videoBadge}</div><div class="wall-title">${esc(post.title)}</div><div class="wall-text">${esc((post.text||'').slice(0,180))}</div><div class="wall-actions"><button class="primary" onclick="readWallPost(0)">Xem</button>${videoBtn}</div>`;
175
+ track.prepend(div);
176
+ track.scrollTo({left:0,behavior:'smooth'});
177
+ if(hasVideo) refreshShortAISlider();
178
+ }
179
+
180
+ // === REST OF FUNCTIONS ===
181
+ let _shortsData=[];
182
+ let _wallPosts=[];
183
+ let _currentView='home';
184
+ let _currentEventId=null;
185
+ let _currentMatchUrl=null;
186
+ function interleaveShorts(shorts){const dt=shorts.filter(s=>s.channel==='baodantri7941');const sk=shorts.filter(s=>s.channel==='baosuckhoedoisongboyte');const result=[];let i=0,j=0;while(i<dt.length||j<sk.length){if(i<dt.length)result.push(dt[i++]);if(j<sk.length)result.push(sk[j++]);}return result;}
187
+ let _htPage=0,_htTopic='';
188
+ async function loadHotTopics(){const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));const el=document.getElementById('hot-topics');if(!el)return;el.innerHTML=(j.topics||[]).slice(0,18).map(t=>{const topicText=t.topic||t.label.replace(/^#/,'');return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;}).join('');if(j.topics&&j.topics[0]){const firstTopic=j.topics[0].topic||j.topics[0].label.replace(/^#/,'');setTimeout(()=>searchTopic(firstTopic),800);}}
189
+ function searchTopic(topic){if(!topic){topic=document.getElementById('topic-input')?.value.trim();if(!topic){alert('Nhập chủ đề');return;}}document.getElementById('topic-input').value='';_htTopic=topic;_htPage=0;showHashtagSources(topic,0);}
190
+ async function showHashtagSources(topic,page){const box=document.getElementById('hashtag-box');if(!box)return;if(page===0)box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Đang tìm...</div></div>`;try{const r=await fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=${page}`);const j=await r.json();const sources=j.sources||[];if(!sources.length&&page===0){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#888;padding:8px">Không tìm được bài viết liên quan</div></div>`;return;}let h='';if(page===0)h=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)} <span style="font-size:10px;color:#888">(${j.total} bài từ 8 nguồn)</span></h3><div id="ht-list">`;sources.forEach((s,i)=>{const idx=page*8+i;h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${idx}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')}</div></div></div>`;});if(page===0){h+=`</div><button class="hashtag-rewrite-btn" onclick="rewriteHashtag('${esc(topic).replace(/'/g,"\\'")}')">🤖 Rewrite AI tổng hợp & đăng tường</button>`;if(j.has_more)h+=`<button class="hashtag-load-more" id="ht-more" onclick="loadMoreHashtag()">Tải thêm ▼</button>`;h+=`</div>`;box.innerHTML=h;}else{document.getElementById('ht-list')?.insertAdjacentHTML('beforeend',h);const btn=document.getElementById('ht-more');if(btn){if(!j.has_more)btn.remove();else{btn.disabled=false;btn.textContent='Tải thêm ▼';}}}sources.forEach((s,i)=>{const idx=page*8+i;if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+idx);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#e74c3c;padding:8px">Lỗi: ${esc(e.message)}</div></div>`;}}
191
+ function loadMoreHashtag(){_htPage++;const btn=document.getElementById('ht-more');if(btn){btn.disabled=true;btn.textContent='Đang tải...';}showHashtagSources(_htTopic,_htPage);}
192
+ async function rewriteHashtag(topic){const btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}try{const r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');toast('✅ Đã đăng Tường AI!');if(btn)btn.textContent='✅ Đăng thành công!';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message);if(btn){btn.disabled=false;btn.textContent='🤖 Rewrite AI';}}}
193
+ async function loadLivescore(tab){document.querySelectorAll('.ls-tab').forEach(t=>t.classList.remove('active'));document.querySelector(`.ls-tab[data-tab="${tab}"]`)?.classList.add('active');const el=document.getElementById('ls-content');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';let ep='/api/livescore/'+tab;if(tab.startsWith('bxh_'))ep='/api/livescore/standings/'+tab.replace('bxh_','');try{const r=await fetch(ep);const d=await r.json();el.innerHTML=d.html&&d.html.length>50?d.html:'<div class="loading">Không có dữ liệu</div>';bindMatchClicks(el);}catch(e){el.innerHTML='<div class="loading">Lỗi</div>';}}
194
+ function bindMatchClicks(el){
195
+ if(!el) return;
196
+ el.querySelectorAll('.match-detail').forEach(md=>{
197
+ md.style.cursor='pointer';
198
+ // Remove old listeners to avoid duplicates (mark as bound)
199
+ if(md._bound) return;
200
+ md._bound = true;
201
+ md.addEventListener('click',function(e){
202
+ // Don't intercept clicks on interactive elements inside the row
203
+ const tag = e.target.tagName?.toLowerCase();
204
+ if(tag === 'a' || tag === 'button' || tag === 'input') {
205
+ e.preventDefault();
206
+ e.stopPropagation();
207
+ }
208
+ // Find ANY link with /tran-dau/ inside this match-detail row
209
+ const links = this.querySelectorAll('a[href*="/tran-dau/"]');
210
+ let bestA = null;
211
+ links.forEach(a => {
212
+ const href = a.getAttribute('href') || '';
213
+ // Prefer links with both event_id AND slug (fuller URL)
214
+ if(href.match(/\/tran-dau\/\d+\/(centre|preview|quan-cau|video)\//)) {
215
+ bestA = a;
216
+ } else if(!bestA && href.match(/\/tran-dau\/\d+\//)) {
217
+ bestA = a;
218
+ }
219
+ });
220
+ if(!bestA) return;
221
+ e.preventDefault();
222
+ e.stopPropagation();
223
+ const href = bestA.getAttribute('href') || '';
224
+ const m = href.match(/\/tran-dau\/(\d+)\//);
225
+ if(m){
226
+ const fullUrl = href.startsWith('http') ? href : 'https://bongda.com.vn' + href;
227
+ openMatch(m[1], fullUrl);
228
+ }
229
+ });
230
+ });
231
+ // Prevent default navigation on all links inside livescore (but let match-detail click handler work)
232
+ el.querySelectorAll('a').forEach(a=>{
233
+ a.addEventListener('click',e=>{
234
+ e.preventDefault();
235
+ e.stopPropagation();
236
+ });
237
+ });
238
+ }
239
+ function openMatch(id,url){if(!id)return;_currentEventId=id;if(url)_currentMatchUrl=url;document.getElementById('match-overlay').classList.add('active');document.body.style.overflow='hidden';loadMatchTab('detail')}
240
+ function closeMatch(){document.getElementById('match-overlay').classList.remove('active');document.body.style.overflow=''}
241
+ async function loadMatchTab(tab){document.querySelectorAll('.mo-tab').forEach(t=>t.classList.remove('active'));document.querySelectorAll('.mo-tab').forEach(t=>{if((tab==='comm'&&t.textContent==='Diễn biến')||(tab==='stats'&&t.textContent==='Thống kê')||(tab==='detail'&&t.textContent.includes('Chi tiết')))t.classList.add('active')});const el=document.getElementById('mo-body');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';try{let apiUrl;if(tab==='stats')apiUrl=`/api/match/${_currentEventId}/stats`;else if(tab==='comm')apiUrl=`/api/match/${_currentEventId}/commentaries`;else{apiUrl=`/api/match/${_currentEventId}/detail`;if(_currentMatchUrl)apiUrl+='?url='+encodeURIComponent(_currentMatchUrl)}const r=await fetch(apiUrl);if(!r.ok){el.innerHTML='<div class="loading">Lỗi máy chủ ('+r.status+')</div>';return}const d=await r.json();if(d.error){el.innerHTML='<div class="loading">'+esc(d.error)+'</div>';return}if(tab==='detail'&&typeof renderMatchDetail==='function'){renderMatchDetail(el,d);return}el.innerHTML=d.html||'<div class="loading">Không có dữ liệu</div>'}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
242
+ async function doInteract(videoId,type){try{const r=await fetch('/api/v2/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,type})});return await r.json();}catch(e){return null;}}
243
+ async function getInteractions(videoId){try{return await fetch('/api/v2/interactions?id='+encodeURIComponent(videoId)).then(r=>r.json());}catch(e){return{views:0,likes:0,comments:0};}}
244
+ async function getComments(videoId){try{const j=await fetch('/api/v2/comments?id='+encodeURIComponent(videoId)).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
245
+ async function postComment(videoId,text){try{const j=await fetch('/api/v2/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,text})}).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
246
+ function buildTikTokSlide(opts){return`<div class="tiktok-slide" data-vid="${esc(opts.videoId)}">${opts.vtag}<div class="tiktok-bottom"><span class="badge ${opts.badgeClass||'badge-fpt'}">${opts.badge||''}</span><p class="tiktok-title">${esc(opts.title)}</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation();doView('${esc(opts.videoId)}',this)"><div class="icon">👁</div><div class="count" id="vc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doLike('${esc(opts.videoId)}',this)"><div class="icon">❤️</div><div class="count" id="lc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();toggleComments('${esc(opts.videoId)}',${opts.idx})"><div class="icon">💬</div><div class="count" id="cc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doShare('${esc(opts.title)}','${esc(opts.shareUrl||'')}','')"><div class="icon">📤</div></button>${opts.extraBtn||''}</div><span class="tiktok-counter">${opts.idx+1}/${opts.total}</span><div class="inline-comments" id="cmt-inline-${opts.idx}" style="display:none"></div></div>`;}
247
+ async function doView(videoId,btn){const j=await doInteract(videoId,'view');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.views);}}
248
+ async function doLike(videoId,btn){const j=await doInteract(videoId,'like');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.likes);}}
249
+ function fmtNum(n){if(!n)return'0';if(n>=1000000)return(n/1000000).toFixed(1)+'M';if(n>=1000)return(n/1000).toFixed(1)+'K';return String(n);}
250
+ async function loadCounters(videoIds){for(let i=0;i<videoIds.length;i++){const id=videoIds[i];if(!id)continue;const j=await getInteractions(id);const vc=document.getElementById('vc-'+i);if(vc)vc.textContent=fmtNum(j.views);const lc=document.getElementById('lc-'+i);if(lc)lc.textContent=fmtNum(j.likes);const cc=document.getElementById('cc-'+i);if(cc)cc.textContent=fmtNum(j.comments);}}
251
+ async function toggleComments(videoId,idx){const panel=document.getElementById('cmt-inline-'+idx);if(!panel)return;if(panel.style.display!=='none'){panel.style.display='none';return;}panel.style.display='block';panel.innerHTML='<div style="padding:8px;color:#888;font-size:11px">Đang tải...</div>';const cmts=await getComments(videoId);renderInlineComments(panel,videoId,idx,cmts);}
252
+ function renderInlineComments(panel,videoId,idx,cmts){let h='<div class="inline-cmt-header"><span>💬 Bình luận</span><button onclick="document.getElementById(\'cmt-inline-'+idx+'\').style.display=\'none\'">✕</button></div><div class="inline-cmt-list">';if(cmts.length){cmts.slice(-30).forEach(c=>{h+=`<div class="inline-cmt-item"><span class="inline-cmt-time">${c.time||''}</span>${esc(c.text)}</div>`;});}else{h+='<div style="color:#777;font-size:11px;padding:4px">Chưa có bình luận</div>';}h+=`</div><div class="inline-cmt-input"><input id="cmt-input-${idx}" placeholder="Viết bình luận..." onkeydown="if(event.key==='Enter')submitInlineCmt('${esc(videoId)}',${idx})"><button onclick="submitInlineCmt('${esc(videoId)}',${idx})">Gửi</button></div>`;panel.innerHTML=h;const list=panel.querySelector('.inline-cmt-list');if(list)list.scrollTop=list.scrollHeight;}
253
+ async function submitInlineCmt(videoId,idx){const inp=document.getElementById('cmt-input-'+idx);if(!inp)return;const text=inp.value.trim();if(!text)return;inp.value='';inp.disabled=true;const cmts=await postComment(videoId,text);inp.disabled=false;const panel=document.getElementById('cmt-inline-'+idx);if(panel)renderInlineComments(panel,videoId,idx,cmts);const cc=document.getElementById('cc-'+idx);if(cc)cc.textContent=fmtNum(cmts.length);}
254
+ function initTikTokFeed(){const feed=document.getElementById('tiktok-feed');if(!feed)return;const slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{const v=sl.querySelector('video');const fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls&&!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){const hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,()=>v.play().catch(()=>{}));v._hls=hls}else if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;const vid=sl.dataset.vid;if(vid&&!sl._viewed){sl._viewed=true;doInteract(vid,'view').then(j=>{if(j){const vc=document.getElementById('vc-'+idx);if(vc)vc.textContent=fmtNum(j.views);}});}}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null}}if(fr&&fr.src)fr.src=''}});cur=i}let sT;feed.addEventListener('scroll',()=>{clearTimeout(sT);sT=setTimeout(()=>{const rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2;let best=-1,bestD=1e9;slides.forEach((sl,i)=>{const d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i}});if(best>=0)act(best)},150)});setTimeout(()=>act(0),400);slides.forEach(sl=>{const v=sl.querySelector('video');if(v)v.addEventListener('click',e=>{e.preventDefault();v.paused?v.play().catch(()=>{}):v.pause()})});const ids=[...slides].map(sl=>sl.dataset.vid||'');loadCounters(ids)}
255
+ async function openHighlightFeed(league,idx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';let articles=(_hlLeagueData||{})[league]||[];if(!articles.length){try{articles=await fetch('/api/highlights/'+league).then(r=>r.json())}catch(e){articles=[]}}if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return}const vids=[];const results=await Promise.all(articles.map(async(a,i)=>{try{const r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));const v=await r.json();if(v&&v.src)return{...a,...v,_idx:i}}catch(e){}return null}));results.forEach(r=>{if(r)vids.push(r)});vids.sort((a,b)=>a._idx-b._idx);if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return}let ti=vids.findIndex(v=>v._idx===idx);if(ti<0)ti=0;const ordered=ti>0?[...vids.slice(ti),...vids.slice(0,ti)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Highlight</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const isYT=v.type==='youtube',isHLS=!isYT&&v.src?.includes('.m3u8'),poster=v.poster?` poster="${v.poster}"`:'';const vtag=isYT?`<iframe data-yt-src="${v.src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`:isHLS?`<video playsinline preload="none"${poster} data-hls="${v.src}" loop controls></video>`:`<video playsinline preload="none"${poster} loop controls><source src="${v.src}" type="video/mp4"></video>`;const videoId='hl-'+league+'-'+(v.id||v._idx);h+=buildTikTokSlide({vtag,title:v.title,badge:'HL',badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:v.link||'',extraBtn:`<button class="tiktok-right-btn" onclick="event.stopPropagation();this.closest('.tiktok-slide').classList.toggle('ratio-wide')"><div class="icon">⬜</div><div class="count">16:9</div></button>`});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
256
+ async function openYTShortsFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=_shortsData.length?_shortsData:await fetch('/api/shorts').then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có shorts</div>';return}const ordered=startIdx>0?[...arts.slice(startIdx),...arts.slice(0,startIdx)]:arts;let h=`<button class="back-btn" onclick="switchCat('home')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const id=v.id||'';const src=`https://www.youtube.com/embed/${id}?autoplay=1&rel=0&playsinline=1`;const vtag=`<iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`;const badge=v.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';const videoId='yt-'+id;h+=buildTikTokSlide({vtag,title:v.title,badge,badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:'https://youtube.com/watch?v='+id});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
257
+ async function openShortAIFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const wall=(await fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];const vids=wall.filter(p=>p.video);if(!vids.length){el.innerHTML='<div class="loading">Chưa có Short AI</div>';return}const ordered=startIdx>0?[...vids.slice(startIdx),...vids.slice(0,startIdx)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((p,i)=>{const vtag=`<video src="${p.video}" playsinline loop controls></video>`;const videoId='ai-'+(p.id||i);h+=buildTikTokSlide({vtag,title:p.title,badge:'AI',badgeClass:'badge-ai',videoId,idx:i,total:ordered.length,shareUrl:SPACE});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
258
+ async function readArticle(url){showView('view-article');const el=document.getElementById('view-article');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/article?url='+encodeURIComponent(url));const data=await r.json();if(data&&!data.error&&data.body&&data.body.length){_currentArticle={url,data};let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><h1 class="article-title">${esc(data.title)}</h1>`;if(data.summary)h+=`<div class="article-summary">${esc(data.summary)}</div>`;const seen={};data.body.forEach(b=>{if(b.type==='p')h+=`<p class="article-p">${b.text}</p>`;else if(b.type==='img'&&b.src&&!seen[b.src]){seen[b.src]=1;h+=`<img class="article-img" src="${esc(b.src)}" onerror="this.style.display='none'">`}else if(b.type==='heading')h+=`<h2 class="article-h2">${esc(b.text)}</h2>`});h+=`<div class="article-actions"><button class="primary" onclick="rewriteArticle()">🤖 Rewrite AI đăng tường</button><button onclick="doShare('${esc(data.title)}','${esc(url)}','${esc(data.og_image||'')}')">📤</button><button onclick="window.open('${esc(url)}','_blank')">🔗 Gốc</button></div><div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="ask-q" placeholder="Hỏi về bài viết..."></textarea><button onclick="askAI()">Hỏi</button><div id="ask-a" class="article-ai-answer"></div></div></div>`;el.innerHTML=h;window.scrollTo(0,0);return;}}catch(e){}el.innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="loading"><p>Không đọc được.</p><a href="${esc(url)}" target="_blank" style="color:#5cb87a">Mở gốc →</a></div>`;}
259
+ async function rewriteArticle(){const url=_currentArticle?.url;if(!url)return;toast('⏳ Đang rewrite...');try{const r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,context:document.querySelector('.article-view')?.innerText?.slice(0,14000)||''})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng Tường AI!');if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
260
+ async function rewriteUrl(){const url=document.getElementById('url-input')?.value.trim();if(!url)return alert('Dán URL');toast('⏳ Đang rewrite...');try{const r=await fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng!');document.getElementById('url-input').value='';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
261
+ async function askAI(){const q=document.getElementById('ask-q')?.value.trim();if(!q)return alert('Nhập câu hỏi');const a=document.getElementById('ask-a');a.textContent='Đang hỏi...';try{const r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:_currentArticle?.url||'',question:q,context:document.querySelector('.article-view')?.innerText?.slice(0,12000)||''})});const j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}}
262
+ async function readWallPost(i){const p=_wallPosts[i];if(!p)return;showView('view-article');
263
+ const images = p.images || [];
264
+ let imgGallery = '';
265
+ if(images.length > 0){
266
+ imgGallery = '<div class="article-image-gallery">';
267
+ images.forEach((imgUrl, idx) => {
268
+ if(idx === 0){
269
+ imgGallery += `<img class="article-img article-hero-img" src="${esc(imgUrl)}" onerror="this.style.display='none" loading="eager">`;
270
+ } else {
271
+ if(idx === 1) imgGallery += '<div class="gallery-thumbs">';
272
+ imgGallery += `<div class="gallery-thumb"><img src="${esc(imgUrl)}" onerror="this.parentElement.style.display='none'" loading="lazy"></div>`;
273
+ }
274
+ });
275
+ if(images.length > 1) imgGallery += '</div>';
276
+ imgGallery += '</div>';
277
+ }
278
+ const hasVideo = p.video && p.video.length > 0;
279
+ const voiceOptions = [
280
+ {id:'hoaimy', label:'🎙️ Nữ — Hoài My'},
281
+ {id:'namminh', label:'🎙️ Nam — Nam Minh'},
282
+ ];
283
+ let voiceSelector = '';
284
+ if(!hasVideo){
285
+ voiceSelector = `<div class="tts-selector"><div class="tts-selector-label">🎙️ Chọn giọng đọc:</div><div class="tts-voice-btns">`;
286
+ voiceOptions.forEach(v=>{
287
+ voiceSelector += `<button class="tts-voice-btn" onclick="document.querySelectorAll('.tts-voice-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('selected-voice').value='${v.id}'">${v.label}</button>`;
288
+ });
289
+ voiceSelector += `</div><div class="tts-speed-row"><span>Tốc độ:</span><select id="selected-speed"><option value="1.0">1.0x — Bình thường</option><option value="1.2" selected>1.2x — Nhanh</option><option value="1.5">1.5x — Rất nhanh</option><option value="0.8">0.8x — Chậm</option></select></div>`;
290
+ voiceSelector += `<input type="hidden" id="selected-voice" value="hoaimy"></div>`;
291
+ }
292
+ document.getElementById('view-article').innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${imgGallery}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>${hasVideo?`<video class="article-img" src="${esc(p.video)}" controls playsinline style="max-height:400px"></video>`:''}<div class="article-actions">${hasVideo?`<button onclick="openShortAIFeed(${i})">🎬 Xem Short</button>${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🔄 Tạo lại Short</button>`:`${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🎬 Tạo Video Shorts</button>`}<button onclick="doShare('${esc(p.title)}','${SPACE}','${esc(p.img||'')}')">📤</button></div></div>`;
293
+ const firstVoiceBtn = document.querySelector('.tts-voice-btn');
294
+ if(firstVoiceBtn) firstVoiceBtn.classList.add('active');
295
+ window.scrollTo(0,0)}
296
+ async function loadNewsTab(){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/homepage');const news=await r.json();if(!news.length){el.innerHTML='<div class="loading">Không có tin</div>';return}const groups={};news.forEach(a=>{if(!groups[a.group])groups[a.group]=[];groups[a.group].push(a)});let h='';for(const[g,arts] of Object.entries(groups)){h+=`<div class="section-title">${g}</div><div class="grid">`;arts.slice(0,6).forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'VnE')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>'}el.innerHTML=h}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
297
+ async function loadCat(id){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=await fetch('/api/category/'+id).then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có tin</div>';return}let h='<div class="grid">';arts.forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>';el.innerHTML=h}
298
+ fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){const home=document.getElementById('view-home');if(home){const w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ Persistent Storage chưa bật.';home.prepend(w)}}}).catch(()=>{});
299
+
300
+ // === AUTO-OPEN SHARE LINKS (/s?url=... sets pending_article) ===
301
+ (function(){
302
+ try{
303
+ const pa=localStorage.getItem('pending_article');
304
+ const pv=localStorage.getItem('pending_video');
305
+ if(pa){
306
+ localStorage.removeItem('pending_article');
307
+ setTimeout(()=>{
308
+ if(typeof readArticle==='function') readArticle(pa);
309
+ },1500);
310
+ }
311
+ if(pv){
312
+ localStorage.removeItem('pending_video');
313
+ try{
314
+ const v=JSON.parse(pv);
315
+ if(v&&v.url) setTimeout(()=>{window.open(v.url,'_blank')},1500);
316
+ }catch(e){}
317
+ }
318
+ }catch(e){}
319
+ })();
static/{app_v2.js → app_v4.js} RENAMED
@@ -1,7 +1,6 @@
1
- // === VNEWS Frontend v2.1 - Consolidated + Fixed ===
2
- // Fixes: VTV6 auto-play, fast single-source load, match detail fallback, no-blocking homepage
3
 
4
- // ===== VTV CHANNELS =====
5
  const VTV_CHANNELS = [
6
  {id:'vtv1', name:'VTV1', badge:'Tin tức'},
7
  {id:'vtv2', name:'VTV2', badge:'Khoa học'},
@@ -17,39 +16,22 @@ const VTV_CHANNELS = [
17
  ];
18
 
19
  const VTV_EPG = {
20
- vtv1:[{t:'06:00',n:'Nhật ký ngày mai'},{t:'07:00',n:'Thời sự sáng'},{t:'09:00',n:'Thời sự'},{t:'12:00',n:'Thời sự trưa'},{t:'19:00',n:'Thời sự tối'},{t:'21:00',n:'Thời sự đêm'}],
21
- vtv2:[{t:'06:00',n:'Khoa học & CN'},{t:'08:00',n:'Thế giới tự nhiên'},{t:'10:00',n:'Khoa học 360'},{t:'14:00',n:'Sức khỏe'},{t:'20:00',n:'Khoa học & Tương lai'}],
22
- vtv3:[{t:'06:00',n:'Sáng vui'},{t:'08:00',n:'Phim truyện'},{t:'12:00',n:'Âm nhạc'},{t:'16:00',n:'Giải trí chiều'},{t:'20:00',n:'Phim đặc biệt'}],
23
  vtv4:[{t:'06:00',n:'News'},{t:'08:00',n:'World News'},{t:'12:00',n:'Midday News'},{t:'18:00',n:'Evening News'},{t:'20:00',n:'World Today'}],
24
- vtv5:[{t:'06:00',n:'Thời sự miền Nam'},{t:'10:00',n:'Phim truyện'},{t:'14:00',n:'Giải trí'},{t:'18:00',n:'Thời sự chiều'},{t:'22:00',n:'Thời sự tối'}],
25
- vtv6:[{t:'06:00',n:'Khởi động ngày mới'},{t:'08:00',n:'Thanh niên & Sáng tạo'},{t:'12:00',n:'Nhịp sống trẻ'},{t:'18:00',n:'Thời sự trẻ'},{t:'20:00',n:'Đêm nhạc'}],
26
- vtv7:[{t:'06:00',n:'Giáo dục sáng'},{t:'08:00',n:'Học mọi lúc'},{t:'12:00',n:'Giáo dục trưa'},{t:'16:00',n:'Thiếu nhi'},{t:'20:00',n:'Tài liệu GD'}],
27
- vtv8:[{t:'06:00',n:'Thời sự miền Trung'},{t:'10:00',n:'Phim truyện'},{t:'14:00',n:'Giải trí'},{t:'18:00',n:'Thời sự chiều'},{t:'22:00',n:'Thời sự tối'}],
28
- vtv9:[{t:'06:00',n:'Thời sự miền Bắc'},{t:'10:00',n:'Phim truyện'},{t:'14:00',n:'Giải trí'},{t:'18:00',n:'Thời sự chiều'},{t:'22:00',n:'Thời sự tối'}],
29
- vtv10:[{t:'06:00',n:'Thời sự Tây Nam Bộ'},{t:'10:00',n:'Phim truyện'},{t:'14:00',n:'Giải trí'},{t:'18:00',n:'Thời sự chiều'},{t:'22:00',n:'Thời sự tối'}],
30
- vtvprime:[{t:'06:00',n:'Prime Morning'},{t:'10:00',n:'Prime Sports'},{t:'14:00',n:'Prime Drama'},{t:'18:00',n:'Prime Evening'},{t:'20:00',n:'Prime Night'}],
31
- };
32
-
33
- // Static sources — ONLY the first (fastest) source per channel for initial load
34
- // Additional sources used only if primary fails
35
- const VTV_STATIC_SOURCES = {
36
- vtv1:["https://live-a.fptplay53.net/live/media/vtv1/live247-hls-avc/index.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv1-manifest.m3u8","https://live.fptplay53.net/fnxch2/vtv1hd_abr.smil/chunklist.m3u8"],
37
- vtv2:["https://live-a.fptplay53.net/live/media/vtv2/live247-hls-avc/index.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv2-manifest.m3u8","https://live.fptplay53.net/fnxch2/vtv2hd_abr.smil/chunklist.m3u8"],
38
- vtv3:["https://live-a.fptplay53.net/live/media/vtv3/live247-hls-avc/index.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv3-manifest.m3u8","https://live.fptplay53.net/fnxch2/vtv3hd_abr.smil/chunklist.m3u8"],
39
- vtv4:["https://live-a.fptplay53.net/live/media/vtv4/live247-hls-avc/index.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv4-manifest.m3u8"],
40
- vtv5:["https://live-a.fptplay53.net/live/media/vtv5/live247-hls-avc/index.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv5-manifest.m3u8","https://live-a.fptplay53.net/live/media/VTV5HD/live_hls_avc/index.m3u8"],
41
- vtv6:["https://live-a.fptplay53.net/live/media/vtv6/live247-hls-avc/index.m3u8","https://live-a.fptplay53.net/live/media/vtv6/live247-hls-avc/vtv6-avc1_5600000=10000-mp4a_131600=20000.m3u8"],
42
- vtv7:["https://live-a.fptplay53.net/live/media/vtv7/live247-hls-avc/index.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv7-manifest.m3u8","https://live.fptplay53.net/fnxhd1/vtv7hd_vhls.smil/chunklist_b5000000.m3u8"],
43
- vtv8:["https://live.fptplay53.net/epzhd1/vtv8hd_vhls.smil/chunklist.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv8-manifest.m3u8"],
44
- vtv9:["https://live.fptplay53.net/fnxhd1/vtv9hd_vhls.smil/chunklist.m3u8","https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv9-manifest.m3u8"],
45
- vtv10:["https://live-a.fptplay53.net/live/media/vtv10/live247-hls-avc/index.m3u8","https://live-a.fptplay53.net/live/media/vtv10/live247-hls-avc/vtv10-avc1_5600000=10000-mp4a_131600=20000.m3u8"],
46
- vtvprime:["https://live.fptplay53.net/fnxch2/vtvprime_abr.smil/chunklist.m3u8"],
47
  };
48
 
49
  let _vtvStreams = {};
50
  let _vtvCurrentCh = null;
51
  let _vtvHls = null;
52
- let _vtvLoaded = false;
53
 
54
  function buildVTVEPG(chId){
55
  const epg = VTV_EPG[chId] || [];
@@ -59,73 +41,61 @@ function buildVTVEPG(chId){
59
  epg.forEach(item => {
60
  const itemH = parseInt(item.t.split(':')[0], 10);
61
  const isNow = itemH <= curH && (itemH + 2) > curH;
62
- items += `<div class="vtv-epg-item${isNow?' now':''}"><span class="epg-t">${item.t}</span><span class="epg-n">${item.n}</span></div>`;
63
  });
64
- return `<div class="vtv-epg"><div class="vtv-epg-title">📋 Lịch phát sóng</div><div class="vtv-epg-list">${items}</div></div>`;
 
 
 
 
 
65
  }
66
 
67
  function buildVTVBlockHTML(){
68
  let tabs = '';
69
  VTV_CHANNELS.forEach(ch => {
70
- tabs += `<button class="vtv-tab" id="vtvt-${ch.id}" onclick="_vtvPlay('${ch.id}')">${ch.name}</button>`;
71
  });
72
  return `<div class="vtv-wrap" id="vtv-block">
73
  <div class="vtv-head"><span class="vtv-title">📺 VTV Trực Tuyến</span><span class="vtv-badge">● LIVE</span></div>
74
  <div class="vtv-tabs">${tabs}</div>
75
- <div class="vtv-player-area">
76
  <div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div>
77
  <video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video>
78
  <div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="_vtvRetry()">Thử lại</button></div>
79
  </div>
80
- <div id="vtv-epg-wrap"></div>
81
  </div>`;
82
  }
83
 
84
  async function loadVTVStreams(){
85
- if(_vtvLoaded) return;
86
- _vtvLoaded = true;
87
-
88
- const hideLoader = () => { const el = document.getElementById('vtv-load'); if(el) el.style.display = 'none'; };
89
-
90
- // Step 1: IMMEDIATELY populate from static sources (no waiting)
91
- // This makes all tabs clickable right away
92
- VTV_CHANNELS.forEach(ch => {
93
- const sources = VTV_STATIC_SOURCES[ch.id] || [];
94
- _vtvStreams[ch.id] = sources.map(u => '/api/proxy/m3u8/vtv?url=' + encodeURIComponent(u));
95
- const tab = document.getElementById('vtvt-'+ch.id);
96
- if(tab){
97
- tab.style.opacity = '1';
98
- }
99
- });
100
-
101
- // Hide loader immediately since static sources are ready
102
- hideLoader();
103
-
104
- // Step 2: Auto-play VTV6 right away using static source
105
- if(!_vtvCurrentCh){
106
- _vtvPlay('vtv6');
107
- }
108
-
109
- // Step 3: In background, try API for potentially better/fresher URLs
110
- // This does NOT block anything — just updates _vtvStreams for next channel switch
111
  try {
112
- const ctrl = new AbortController();
113
- const tid = setTimeout(() => ctrl.abort(), 3000);
114
- const r = await fetch('/api/vtv/streams', {signal: ctrl.signal});
115
- clearTimeout(tid);
116
  if(r.ok){
117
  const data = await r.json();
118
  VTV_CHANNELS.forEach(ch => {
119
  const info = data[ch.id];
120
- const sources = (info && info.all_sources && info.all_sources.length > 0) ? info.all_sources : (info && info.stream_url ? [info.stream_url] : []);
121
- if(sources.length > 0){
122
- _vtvStreams[ch.id] = sources.map(u => '/api/proxy/m3u8/vtv?url=' + encodeURIComponent(u));
 
123
  }
124
  });
125
  }
126
  } catch(e) {
127
- console.warn('VTV API background refresh skipped:', e);
128
  }
 
 
 
 
 
 
 
 
 
 
 
 
129
  }
130
 
131
  function _vtvRetry(){
@@ -147,7 +117,7 @@ function _vtvPlay(chId){
147
  errEl.style.display = 'none';
148
  loadEl.style.display = 'flex';
149
  loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + ch.name + '...';
150
- if(_vtvHls){ try{_vtvHls.destroy();}catch(e){} _vtvHls = null; }
151
  const urls = _vtvStreams[chId] || [];
152
  if(urls.length === 0){
153
  loadEl.style.display = 'none';
@@ -155,97 +125,62 @@ function _vtvPlay(chId){
155
  errMsg.textContent = chId === 'vtvprime' ? 'VTVPrime: Kênh trả phí.' : ch.name + ': Không tìm thấy luồng.';
156
  return;
157
  }
158
- const epgWrap = document.getElementById('vtv-epg-wrap');
159
- if(epgWrap) epgWrap.innerHTML = buildVTVEPG(chId);
160
- // Try sources one by one, 3s timeout each
161
- _vtvLoadOne(video, urls, 0, ch.name, loadEl, errEl, errMsg);
 
 
 
 
 
162
  }
163
 
164
- function _vtvLoadOne(video, urls, idx, name, loadEl, errEl, errMsg){
165
  if(idx >= urls.length){
166
  loadEl.style.display = 'none';
167
  errEl.style.display = 'flex';
168
- errMsg.textContent = name + ': Tất cả nguồn đều lỗi.';
169
  return;
170
  }
171
  const src = urls[idx];
172
- loadEl.innerHTML = '<div class="vtv-spinner"></div>' + name + (urls.length > 1 ? ' (' + (idx+1) + '/' + urls.length + ')' : '') + '...';
173
-
174
- let failTimer = null;
175
- let hlsInst = null;
176
- let done = false;
177
-
178
- const next = () => {
179
- if(done) return;
180
- done = true;
181
- if(failTimer) clearTimeout(failTimer);
182
- if(hlsInst){ try{hlsInst.destroy();}catch(e){} }
183
- if(_vtvHls === hlsInst) _vtvHls = null;
184
- if(idx + 1 < urls.length){
185
- _vtvLoadOne(video, urls, idx+1, name, loadEl, errEl, errMsg);
186
- } else {
187
- loadEl.style.display = 'none';
188
- errEl.style.display = 'flex';
189
- errMsg.textContent = name + ': Tất cả nguồn đều lỗi.';
190
- }
191
- };
192
-
193
- const ok = () => {
194
- if(done) return;
195
- done = true;
196
- if(failTimer) clearTimeout(failTimer);
197
- _vtvHls = hlsInst;
198
- video.play().catch(()=>{});
199
- loadEl.style.display='none';
200
- video.style.display='block';
201
- };
202
-
203
- // 3s timeout per source — fast fail
204
- failTimer = setTimeout(next, 3000);
205
-
206
  if(typeof Hls !== 'undefined' && Hls.isSupported()){
207
- const hls = new Hls({
208
- enableWorker:true, lowLatencyMode:true, startLevel:-1,
209
- capLevelToPlayerSize:true, maxBufferLength:3, maxMaxBufferLength:6,
210
- manifestLoadingTimeOut: 3000,
211
- levelLoadingTimeOut: 3000,
212
- fragLoadingTimeOut: 3000,
213
- });
214
- hlsInst = hls;
215
  hls.loadSource(src);
216
  hls.attachMedia(video);
217
- hls.on(Hls.Events.MANIFEST_PARSED, ok);
218
- hls.on(Hls.Events.ERROR, (ev, data) => { if(data.fatal) next(); });
 
 
 
 
 
 
 
 
 
 
 
219
  } else if(video.canPlayType('application/vnd.apple.mpegurl')){
220
  video.src = src;
221
- video.addEventListener('loadedmetadata', ok, {once:true});
222
- video.addEventListener('error', next, {once:true});
223
  } else {
224
- clearTimeout(failTimer);
225
- loadEl.style.display = 'none'; errEl.style.display = 'flex';
226
- errMsg.textContent = 'Trình duyệt không hỗ trợ HLS';
227
  }
228
  }
229
 
230
  // === LOAD HOME ===
231
  async function loadHome(){
232
- // Load VTV streams IMMEDIATELY (non-blocking, uses static sources first)
233
- loadVTVStreams();
234
-
235
- // Helper: fetch with timeout
236
- const fetchWithTimeout = (url, ms) => {
237
- const ctrl = new AbortController();
238
- const tid = setTimeout(() => ctrl.abort(), ms);
239
- return fetch(url, {signal: ctrl.signal}).finally(() => clearTimeout(tid));
240
- };
241
-
242
  const[featured,sh,wall,hlLeagues,ai,wcData]=await Promise.all([
243
- fetchWithTimeout('/api/livescore/featured', 8000).then(r=>r.json()).catch(()=>null),
244
- fetchWithTimeout('/api/shorts', 15000).then(r=>r.json()).catch(()=>[]),
245
- fetchWithTimeout('/api/wall', 8000).then(r=>r.json()).catch(()=>({posts:[]})),
246
- fetchWithTimeout('/api/highlights/leagues', 15000).then(r=>r.json()).catch(()=>({})),
247
- fetchWithTimeout('/api/genk_ai', 12000).then(r=>r.json()).catch(()=>[]),
248
- fetchWithTimeout('/api/wc2026', 15000).then(r=>r.json()).catch(()=>null)
249
  ]);
250
  _hlLeagueData=hlLeagues;
251
  _wc2026Data=wcData;
@@ -253,26 +188,26 @@ async function loadHome(){
253
  _wallPosts=(wall&&wall.posts)||[];
254
  let h='';
255
 
256
- // VTV BLOCK — first thing on homepage
257
  h += buildVTVBlockHTML();
258
 
259
- if(featured&&featured.home&&featured.event_id){
260
  const sc=featured.status==='live'?'':'upcoming';
261
  const st=featured.status==='live'?`🔴 ${featured.minute||'LIVE'}`:`⏰ ${featured.time}`;
262
- const eid=String(featured.event_id||'').replace(/[<>&"']/g,'');
263
- const mUrl=String(featured.url||'').replace(/[<>&"']/g,'');
264
- const fH=String(featured.home||'').replace(/[<>&"']/g,'');
265
- const fA=String(featured.away||'').replace(/[<>&"']/g,'');
266
- const fL=String(featured.league||'').replace(/[<>&"']/g,'');
267
- const fS=String(featured.score||'VS').replace(/[<>&"']/g,'');
268
- const fHL=String(featured.home_logo||'').replace(/[<>&"']/g,'');
269
- const fAL=String(featured.away_logo||'').replace(/[<>&"']/g,'');
270
- h+=`<div class="featured-match" data-event-id="${eid}" data-url="${mUrl}" onclick="openMatch('${eid}','${mUrl}')" title="${fH} vs ${fA} — ${fL}">`+
271
- `<div class="fm-league">${fL}</div>`+
272
  `<div class="fm-teams">`+
273
- `<div class="fm-team"><img src="${fHL}" onerror="this.style.display='none'"><span>${fH}</span></div>`+
274
- `<div class="fm-score">${fS}</div>`+
275
- `<div class="fm-team"><img src="${fAL}" onerror="this.style.display='none'"><span>${fA}</span></div>`+
276
  `</div>`+
277
  `<div class="fm-status ${sc}">${st}</div>`+
278
  `</div>`;
@@ -290,6 +225,18 @@ async function loadHome(){
290
  for(const[key,cfg] of Object.entries(HL_CONFIG)){const vids=hlLeagues[key];if(!vids||!vids.length)continue;h+=`<div class="slider-wrap"><div class="slider-header"><span class="slider-label">${cfg.emoji} ${cfg.name}</span></div><div class="slider-track">`;vids.slice(0,8).forEach((a,i)=>{h+=`<div class="slider-item" onclick="openHighlightFeed('${key}',${i})"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
291
  if(ai&&ai.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🤖 Ứng dụng AI</span></div><div class="slider-track">';ai.slice(0,12).forEach(a=>{h+=`<div class="slider-item" onclick="readArticle('${esc(a.link)}')"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}</div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
292
  document.getElementById('view-home').innerHTML=h;
 
 
 
 
 
 
 
 
 
 
 
 
293
  loadLivescore('today');loadHotTopics();
294
  if(_wc2026Data)switchWCTab('news');
295
  }
@@ -297,22 +244,12 @@ async function loadHome(){
297
  // === WALL POST HELPERS ===
298
  function makeWallItem(p,i){
299
  const hasVideo = p.video && p.video.length > 0;
300
- const thumbContent = p.img
301
- ? `<img src="${esc(p.img)}" onerror="this.style.display='none'">`
302
- : (hasVideo ? `<video src="${esc(p.video)}" muted></video>` : '');
303
  const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
304
  const videoBtn = hasVideo
305
  ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(${i})">▶ Xem Short</button>`
306
  : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(p.id||i)}',this)">🎬 Tạo Video</button>`;
307
- return `<div class="wall-item" id="wall-item-${esc(p.id||i)}">
308
- <div class="wall-thumb">${thumbContent}${videoBadge}</div>
309
- <div class="wall-title">${esc(p.title)}</div>
310
- <div class="wall-text">${esc((p.text||'').slice(0,180))}</div>
311
- <div class="wall-actions">
312
- <button class="primary" onclick="readWallPost(${i})">Xem</button>
313
- ${videoBtn}
314
- </div>
315
- </div>`;
316
  }
317
 
318
  async function makeShortVideo(postId, btn, voice, speed){
@@ -357,9 +294,7 @@ function refreshShortAISlider(){
357
  const track = shortAISection.querySelector('.slider-track');
358
  if(track){
359
  let h = '';
360
- aiShorts.slice(0,20).forEach((p,i)=>{
361
- h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${esc(p.video)}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`;
362
- });
363
  track.innerHTML = h;
364
  }
365
  }
@@ -374,8 +309,7 @@ function prependWallPost(post){
374
  if(homeEl){
375
  let insertBefore=homeEl.querySelector('.slider-wrap');
376
  const newWrap=document.createElement('div');
377
- newWrap.className='slider-wrap';
378
- newWrap.id='ai-wall-wrap';
379
  newWrap.innerHTML=`<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">${makeWallItem(post,0)}</div>`;
380
  if(insertBefore){ homeEl.insertBefore(newWrap,insertBefore); }else{ homeEl.appendChild(newWrap); }
381
  const firstItem=newWrap.querySelector('.wall-item');
@@ -387,9 +321,7 @@ function prependWallPost(post){
387
  div.className='wall-item wall-item-new';
388
  div.id='wall-item-'+(post.id||'new-'+Date.now());
389
  const hasVideo = post.video && post.video.length > 0;
390
- const thumbContent = post.img
391
- ? `<img src="${esc(post.img)}" onerror="this.style.display='none'">`
392
- : (hasVideo ? `<video src="${esc(post.video)}" muted></video>` : '');
393
  const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
394
  const videoBtn = hasVideo
395
  ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(0)">▶ Xem Short</button>`
@@ -400,7 +332,6 @@ function prependWallPost(post){
400
  if(hasVideo) refreshShortAISlider();
401
  }
402
 
403
- // === GLOBALS ===
404
  let _shortsData=[];
405
  let _wallPosts=[];
406
  let _currentView='home';
@@ -408,60 +339,28 @@ let _currentEventId=null;
408
  let _currentMatchUrl=null;
409
  function interleaveShorts(shorts){const dt=shorts.filter(s=>s.channel==='baodantri7941');const sk=shorts.filter(s=>s.channel==='baosuckhoedoisongboyte');const result=[];let i=0,j=0;while(i<dt.length||j<sk.length){if(i<dt.length)result.push(dt[i++]);if(j<sk.length)result.push(sk[j++]);}return result;}
410
  let _htPage=0,_htTopic='';
411
-
412
- // === HOT TOPICS (multi-hashtag) ===
413
- async function loadHotTopics(){
414
- const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));
415
- const el=document.getElementById('hot-topics');if(!el)return;
416
- const topics=j.topics||[];
417
- el.innerHTML=topics.slice(0,18).map(t=>{
418
- const topicText=t.topic||t.label.replace(/^#/,'');
419
- return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;
420
- }).join('');
421
- // Load multi-hashtag hot news
422
- if(topics.length>=2){
423
- loadMultiHashtag(topics.slice(0,3).map(t=>t.topic||t.label.replace(/^#/,'')));
424
- }else if(topics.length){
425
- searchTopic(topics[0].topic||topics[0].label.replace(/^#/,''));
426
- }
427
- }
428
-
429
- async function loadMultiHashtag(topicList){
430
- const box=document.getElementById('hashtag-box');if(!box)return;
431
- box.innerHTML=`<div class="hashtag-sources"><h3>🔥 Tin HOT tổng hợp</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Tổng hợp từ ${topicList.length} chủ đề nóng nhất...</div></div>`;
432
- try{
433
- const results=await Promise.all(topicList.map(topic=>
434
- fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=0`).then(r=>r.json()).catch(()=>({sources:[]}))
435
- ));
436
- const all=[];const seen=new Set();
437
- const mx=Math.max(...results.map(r=>(r.sources||[]).length));
438
- for(let i=0;i<mx;i++){
439
- for(let j=0;j<results.length;j++){
440
- const src=(results[j].sources||[])[i];
441
- if(src&&src.url&&!seen.has(src.url)){seen.add(src.url);src._topic=topicList[j];all.push(src);}
442
- }
443
- }
444
- if(!all.length){box.innerHTML=`<div class="hashtag-sources"><h3>🔥 Tin HOT</h3><div style="color:#888;padding:8px">Đang cập nhật...</div></div>`;return;}
445
- let h=`<div class="hashtag-sources"><h3>🔥 Tin HOT tổng hợp <span style="font-size:10px;color:#888">(${all.length} bài · ${topicList.length} chủ đề)</span></h3><div id="ht-list">`;
446
- all.slice(0,12).forEach((s,i)=>{
447
- h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${i}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')} · <span style="color:#f0c040;font-size:9px">#${esc(s._topic||'')}</span></div></div></div>`;
448
- });
449
- h+=`</div><div style="display:flex;gap:4px;flex-wrap:wrap;margin-top:8px">`;
450
- topicList.forEach(t=>{h+=`<button class="hot-chip" onclick="searchTopic('${t.replace(/'/g,"\\'")}')" style="font-size:10px">🔍 #${esc(t)}</button>`;});
451
- h+=`</div></div>`;
452
- box.innerHTML=h;
453
- all.slice(0,12).forEach((s,i)=>{if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+i);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});
454
- _htTopic=topicList[0];
455
- }catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔥 Tin HOT</h3><div style="color:#e74c3c;padding:8px">Lỗi tải tin</div></div>`;}
456
- }
457
-
458
  function searchTopic(topic){if(!topic){topic=document.getElementById('topic-input')?.value.trim();if(!topic){alert('Nhập chủ đề');return;}}document.getElementById('topic-input').value='';_htTopic=topic;_htPage=0;showHashtagSources(topic,0);}
459
  async function showHashtagSources(topic,page){const box=document.getElementById('hashtag-box');if(!box)return;if(page===0)box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Đang tìm...</div></div>`;try{const r=await fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=${page}`);const j=await r.json();const sources=j.sources||[];if(!sources.length&&page===0){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#888;padding:8px">Không tìm được bài viết liên quan</div></div>`;return;}let h='';if(page===0)h=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)} <span style="font-size:10px;color:#888">(${j.total} bài từ 8 nguồn)</span></h3><div id="ht-list">`;sources.forEach((s,i)=>{const idx=page*8+i;h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${idx}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')}</div></div></div>`;});if(page===0){h+=`</div><button class="hashtag-rewrite-btn" onclick="rewriteHashtag('${esc(topic).replace(/'/g,"\\'")}')">🤖 Rewrite AI tổng hợp & đăng tường</button>`;if(j.has_more)h+=`<button class="hashtag-load-more" id="ht-more" onclick="loadMoreHashtag()">Tải thêm ▼</button>`;h+=`</div>`;box.innerHTML=h;}else{document.getElementById('ht-list')?.insertAdjacentHTML('beforeend',h);const btn=document.getElementById('ht-more');if(btn){if(!j.has_more)btn.remove();else{btn.disabled=false;btn.textContent='Tải thêm ▼';}}}sources.forEach((s,i)=>{const idx=page*8+i;if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+idx);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#e74c3c;padding:8px">Lỗi: ${esc(e.message)}</div></div>`;}}
460
  function loadMoreHashtag(){_htPage++;const btn=document.getElementById('ht-more');if(btn){btn.disabled=true;btn.textContent='Đang tải...';}showHashtagSources(_htTopic,_htPage);}
461
  async function rewriteHashtag(topic){const btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}try{const r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');toast('✅ Đã đăng Tường AI!');if(btn)btn.textContent='✅ Đăng thành công!';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message);if(btn){btn.disabled=false;btn.textContent='🤖 Rewrite AI';}}}
462
 
463
- // === LIVESCORE ===
464
- async function loadLivescore(tab){document.querySelectorAll('.ls-tab').forEach(t=>t.classList.remove('active'));document.querySelector(`.ls-tab[data-tab="${tab}"]`)?.classList.add('active');const el=document.getElementById('ls-content');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';let ep='/api/livescore/'+tab;if(tab.startsWith('bxh_'))ep='/api/livescore/standings/'+tab.replace('bxh_','');try{const r=await fetch(ep);const d=await r.json();el.innerHTML=d.html&&d.html.length>50?d.html:'<div class="loading">Không có dữ liệu</div>';bindMatchClicks(el);}catch(e){el.innerHTML='<div class="loading">Lỗi</div>';}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
 
466
  function bindMatchClicks(el){
467
  if(!el) return;
@@ -470,181 +369,62 @@ function bindMatchClicks(el){
470
  md._bound = true;
471
  md.style.cursor='pointer';
472
  md.addEventListener('click',function(e){
473
- if(e.target.closest('a')) return;
474
- const statusA=this.querySelector('.status a');
475
- const teamA=this.querySelector('.teams a[href*="/tran-dau/"]');
476
- const a = statusA || teamA;
477
- if(a){
478
- e.preventDefault();
479
- e.stopPropagation();
480
- const href=a.getAttribute('href')||'';
481
- const m=href.match(/\/tran-dau\/(\d+)\//);
482
- if(m){
483
- const fullUrl=href.startsWith('http')?href:'https://bongda.com.vn'+href;
484
- openMatch(m[1],fullUrl);
485
- }
486
  }
487
  });
488
  });
489
  }
490
 
491
- // === MATCH DETAIL ===
492
  function openMatch(id,url){if(!id)return;_currentEventId=id;if(url)_currentMatchUrl=url;document.getElementById('match-overlay').classList.add('active');document.body.style.overflow='hidden';loadMatchTab('detail')}
493
  function closeMatch(){document.getElementById('match-overlay').classList.remove('active');document.body.style.overflow=''}
494
-
495
- async function loadMatchTab(tab){
496
- document.querySelectorAll('.mo-tab').forEach(t=>t.classList.remove('active'));
497
- document.querySelectorAll('.mo-tab').forEach(t=>{
498
- if((tab==='comm'&&t.textContent==='Diễn biến')||(tab==='stats'&&t.textContent==='Thống kê')||(tab==='detail'&&t.textContent.includes('Chi tiết')))t.classList.add('active')
499
- });
500
- const el=document.getElementById('mo-body');
501
- if(!el)return;
502
- el.innerHTML='<div class="loading">Đang tải...</div>';
503
- try{
504
- let apiUrl;
505
- if(tab==='stats')apiUrl=`/api/match/${_currentEventId}/stats`;
506
- else if(tab==='comm')apiUrl=`/api/match/${_currentEventId}/commentaries`;
507
- else{
508
- apiUrl=`/api/match/${_currentEventId}/detail`;
509
- if(_currentMatchUrl)apiUrl+='?url='+encodeURIComponent(_currentMatchUrl);
510
- }
511
- const r=await fetch(apiUrl);
512
- if(!r.ok){el.innerHTML='<div class="loading">Lỗi máy chủ ('+r.status+')</div>';return}
513
- const d=await r.json();
514
- if(d.error){el.innerHTML='<div class="loading">'+esc(d.error)+'</div>';return}
515
-
516
- // For detail tab: ALWAYS show match info, even if scraping "found:false"
517
- if(tab==='detail'){
518
- // Use renderMatchDetail from match_detail_v2.js if available (richer UI)
519
- if(typeof renderMatchDetail==='function'){
520
- renderMatchDetail(el,d);
521
- return;
522
- }
523
- // Fallback: always show at least basic info
524
- const info = d.info || {};
525
- const home = info.home_team || info.home || d.home || '';
526
- const away = info.away_team || info.away || d.away || '';
527
- const score = info.score || d.score || 'VS';
528
- const league = info.league || info.status_label || d.league || '';
529
- const statusLabel = info.status_label || '';
530
- const homeLogo = info.home_logo || '';
531
- const awayLogo = info.away_logo || '';
532
-
533
- if(!home && !away){
534
- el.innerHTML='<div class="loading">Không tìm thấy dữ liệu trận đấu</div>';
535
- return;
536
- }
537
-
538
- let h = '<div class="mdv6-header"><div class="mdv6-teams">';
539
- h+='<div class="mdv6-team">';
540
- if(homeLogo)h+='<img src="'+esc(homeLogo)+'" alt="'+esc(home)+'">';
541
- h+='<span>'+esc(home)+'</span></div>';
542
- h+='<div class="mdv6-vs">';
543
- h+='<span class="mdv6-score">'+esc(score)+'</span>';
544
- if(statusLabel){
545
- const sl=statusLabel;let cls='finished';
546
- if(sl==='LIVE'||sl==='H1'||sl==='H2')cls='live';
547
- h+='<span class="mdv6-status '+cls+'">'+esc(sl)+'</span>';
548
- }
549
- h+='</div><div class="mdv6-team">';
550
- if(awayLogo)h+='<img src="'+esc(awayLogo)+'" alt="'+esc(away)+'">';
551
- h+='<span>'+esc(away)+'</span></div></div>';
552
- if(league)h+='<div style="text-align:center;color:#5cb87a;font-size:9px;margin-top:4px">'+esc(league)+'</div>';
553
- h+='</div>';
554
-
555
- if(d.found===false){
556
- h+='<div class="mdv6-warn" style="margin-top:10px">⚠️ Chi tiết trận đấu chưa có sẵn. Hiển thị thông tin cơ bản.</div>';
557
- }
558
-
559
- // Show events if available
560
- if(d.events && d.events.length>0){
561
- h+='<div class="mdv6-sec"><div class="mdv6-sec-title">📢 Diễn biến</div><div class="mdv6-timeline">';
562
- let lastPeriod='';
563
- d.events.forEach(ev=>{
564
- if(ev.period&&ev.period!==lastPeriod){
565
- h+='<div class="mdv6-period">'+esc(ev.period)+'</div>';
566
- lastPeriod=ev.period;
567
- }
568
- const isHome=ev.team==='home';
569
- const badge=isHome?'<span class="mdv6-ev-team home">HOME</span>':'<span class="mdv6-ev-team away">AWAY</span>';
570
- let icon='',title='',detail='';
571
- if(ev.type==='goal'){icon='⚽';title='BÀN THẮNG';detail=esc(ev.players);}
572
- else if(ev.type==='redcard'){icon='🟥';title='THỺ ĐỎ';detail=esc(ev.players);}
573
- else if(ev.type==='yellowcard'){icon='🟨';title='THỺ VÀNG';detail=esc(ev.players);}
574
- else if(ev.type==='substitution'){icon='↔️';title='THAY ĐỔI';detail=esc(ev.players);}
575
- else{icon='•';detail=esc(ev.players);}
576
- h+='<div class="mdv6-ev"><span class="mdv6-ev-time">'+esc(ev.time||'')+'</span><span class="mdv6-ev-icon">'+icon+'</span><div class="mdv6-ev-body">'+badge+' <span class="mdv6-ev-title">'+title+'</span>';
577
- if(detail)h+='<div class="mdv6-ev-detail">'+detail+'</div>';
578
- h+='</div></div>';
579
- });
580
- h+='</div></div>';
581
- }
582
-
583
- h+='<div style="text-align:center;margin-top:12px"><a href="https://bongda.com.vn/tran-dau/'+esc(String(_currentEventId||''))+'" target="_blank" style="color:#5cb87a;font-size:11px">Xem trên bongda.com.vn →</a></div>';
584
- el.innerHTML=h;
585
- return;
586
- }
587
- el.innerHTML=d.html||'<div class="loading">Không có dữ liệu</div>';
588
- }catch(e){
589
- el.innerHTML='<div class="loading">Lỗi kết nối</div>';
590
- }
591
- }
592
-
593
- // === INTERACTIONS ===
594
  async function doInteract(videoId,type){try{const r=await fetch('/api/v2/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,type})});return await r.json();}catch(e){return null;}}
595
  async function getInteractions(videoId){try{return await fetch('/api/v2/interactions?id='+encodeURIComponent(videoId)).then(r=>r.json());}catch(e){return{views:0,likes:0,comments:0};}}
596
  async function getComments(videoId){try{const j=await fetch('/api/v2/comments?id='+encodeURIComponent(videoId)).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
597
  async function postComment(videoId,text){try{const j=await fetch('/api/v2/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,text})}).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
598
- function fmtNum(n){if(!n)return'0';if(n>=1000000)return(n/1000000).toFixed(1)+'M';if(n>=1000)return(n/1000).toFixed(1)+'K';return String(n);}
599
-
600
  function buildTikTokSlide(opts){return`<div class="tiktok-slide" data-vid="${esc(opts.videoId)}">${opts.vtag}<div class="tiktok-bottom"><span class="badge ${opts.badgeClass||'badge-fpt'}">${opts.badge||''}</span><p class="tiktok-title">${esc(opts.title)}</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation();doView('${esc(opts.videoId)}',this)"><div class="icon">👁</div><div class="count" id="vc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doLike('${esc(opts.videoId)}',this)"><div class="icon">❤️</div><div class="count" id="lc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();toggleComments('${esc(opts.videoId)}',${opts.idx})"><div class="icon">💬</div><div class="count" id="cc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doShare('${esc(opts.title)}','${esc(opts.shareUrl||'')}','')"><div class="icon">📤</div></button>${opts.extraBtn||''}</div><span class="tiktok-counter">${opts.idx+1}/${opts.total}</span><div class="inline-comments" id="cmt-inline-${opts.idx}" style="display:none"></div></div>`;}
601
  async function doView(videoId,btn){const j=await doInteract(videoId,'view');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.views);}}
602
  async function doLike(videoId,btn){const j=await doInteract(videoId,'like');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.likes);}}
 
603
  async function loadCounters(videoIds){for(let i=0;i<videoIds.length;i++){const id=videoIds[i];if(!id)continue;const j=await getInteractions(id);const vc=document.getElementById('vc-'+i);if(vc)vc.textContent=fmtNum(j.views);const lc=document.getElementById('lc-'+i);if(lc)lc.textContent=fmtNum(j.likes);const cc=document.getElementById('cc-'+i);if(cc)cc.textContent=fmtNum(j.comments);}}
604
  async function toggleComments(videoId,idx){const panel=document.getElementById('cmt-inline-'+idx);if(!panel)return;if(panel.style.display!=='none'){panel.style.display='none';return;}panel.style.display='block';panel.innerHTML='<div style="padding:8px;color:#888;font-size:11px">Đang tải...</div>';const cmts=await getComments(videoId);renderInlineComments(panel,videoId,idx,cmts);}
605
  function renderInlineComments(panel,videoId,idx,cmts){let h='<div class="inline-cmt-header"><span>💬 Bình luận</span><button onclick="document.getElementById(\'cmt-inline-'+idx+'\').style.display=\'none\'">✕</button></div><div class="inline-cmt-list">';if(cmts.length){cmts.slice(-30).forEach(c=>{h+=`<div class="inline-cmt-item"><span class="inline-cmt-time">${c.time||''}</span>${esc(c.text)}</div>`;});}else{h+='<div style="color:#777;font-size:11px;padding:4px">Chưa có bình luận</div>';}h+=`</div><div class="inline-cmt-input"><input id="cmt-input-${idx}" placeholder="Viết bình luận..." onkeydown="if(event.key==='Enter')submitInlineCmt('${esc(videoId)}',${idx})"><button onclick="submitInlineCmt('${esc(videoId)}',${idx})">Gửi</button></div>`;panel.innerHTML=h;const list=panel.querySelector('.inline-cmt-list');if(list)list.scrollTop=list.scrollHeight;}
606
  async function submitInlineCmt(videoId,idx){const inp=document.getElementById('cmt-input-'+idx);if(!inp)return;const text=inp.value.trim();if(!text)return;inp.value='';inp.disabled=true;const cmts=await postComment(videoId,text);inp.disabled=false;const panel=document.getElementById('cmt-inline-'+idx);if(panel)renderInlineComments(panel,videoId,idx,cmts);const cc=document.getElementById('cc-'+idx);if(cc)cc.textContent=fmtNum(cmts.length);}
607
-
608
  function initTikTokFeed(){const feed=document.getElementById('tiktok-feed');if(!feed)return;const slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{const v=sl.querySelector('video');const fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls&&!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){const hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,()=>v.play().catch(()=>{}));v._hls=hls}else if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;const vid=sl.dataset.vid;if(vid&&!sl._viewed){sl._viewed=true;doInteract(vid,'view').then(j=>{if(j){const vc=document.getElementById('vc-'+idx);if(vc)vc.textContent=fmtNum(j.views);}});}}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null}}if(fr&&fr.src)fr.src=''}});cur=i}let sT;feed.addEventListener('scroll',()=>{clearTimeout(sT);sT=setTimeout(()=>{const rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2;let best=-1,bestD=1e9;slides.forEach((sl,i)=>{const d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i}});if(best>=0)act(best)},150)});setTimeout(()=>act(0),400);slides.forEach(sl=>{const v=sl.querySelector('video');if(v)v.addEventListener('click',e=>{e.preventDefault();v.paused?v.play().catch(()=>{}):v.pause()})});const ids=[...slides].map(sl=>sl.dataset.vid||'');loadCounters(ids)}
609
-
610
  async function openHighlightFeed(league,idx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';let articles=(_hlLeagueData||{})[league]||[];if(!articles.length){try{articles=await fetch('/api/highlights/'+league).then(r=>r.json())}catch(e){articles=[]}}if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return}const vids=[];const results=await Promise.all(articles.map(async(a,i)=>{try{const r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));const v=await r.json();if(v&&v.src)return{...a,...v,_idx:i}}catch(e){}return null}));results.forEach(r=>{if(r)vids.push(r)});vids.sort((a,b)=>a._idx-b._idx);if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return}let ti=vids.findIndex(v=>v._idx===idx);if(ti<0)ti=0;const ordered=ti>0?[...vids.slice(ti),...vids.slice(0,ti)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Highlight</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const isYT=v.type==='youtube',isHLS=!isYT&&v.src?.includes('.m3u8'),poster=v.poster?` poster="${v.poster}"`:'';const vtag=isYT?`<iframe data-yt-src="${v.src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`:isHLS?`<video playsinline preload="none"${poster} data-hls="${v.src}" loop controls></video>`:`<video playsinline preload="none"${poster} loop controls><source src="${v.src}" type="video/mp4"></video>`;const videoId='hl-'+league+'-'+(v.id||v._idx);h+=buildTikTokSlide({vtag,title:v.title,badge:'HL',badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:v.link||'',extraBtn:`<button class="tiktok-right-btn" onclick="event.stopPropagation();this.closest('.tiktok-slide').classList.toggle('ratio-wide')"><div class="icon">⬜</div><div class="count">16:9</div></button>`});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
611
-
612
  async function openYTShortsFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=_shortsData.length?_shortsData:await fetch('/api/shorts').then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có shorts</div>';return}const ordered=startIdx>0?[...arts.slice(startIdx),...arts.slice(0,startIdx)]:arts;let h=`<button class="back-btn" onclick="switchCat('home')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const id=v.id||'';const src=`https://www.youtube.com/embed/${id}?autoplay=1&rel=0&playsinline=1`;const vtag=`<iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`;const badge=v.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';const videoId='yt-'+id;h+=buildTikTokSlide({vtag,title:v.title,badge,badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:'https://youtube.com/watch?v='+id});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
613
-
614
  async function openShortAIFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const wall=(await fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];const vids=wall.filter(p=>p.video);if(!vids.length){el.innerHTML='<div class="loading">Chưa có Short AI</div>';return}const ordered=startIdx>0?[...vids.slice(startIdx),...vids.slice(0,startIdx)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((p,i)=>{const vtag=`<video src="${p.video}" playsinline loop controls></video>`;const videoId='ai-'+(p.id||i);h+=buildTikTokSlide({vtag,title:p.title,badge:'AI',badgeClass:'badge-ai',videoId,idx:i,total:ordered.length,shareUrl:SPACE});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
615
-
616
  async function readArticle(url){showView('view-article');const el=document.getElementById('view-article');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/article?url='+encodeURIComponent(url));const data=await r.json();if(data&&!data.error&&data.body&&data.body.length){_currentArticle={url,data};let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><h1 class="article-title">${esc(data.title)}</h1>`;if(data.summary)h+=`<div class="article-summary">${esc(data.summary)}</div>`;const seen={};data.body.forEach(b=>{if(b.type==='p')h+=`<p class="article-p">${b.text}</p>`;else if(b.type==='img'&&b.src&&!seen[b.src]){seen[b.src]=1;h+=`<img class="article-img" src="${esc(b.src)}" onerror="this.style.display='none'">`}else if(b.type==='heading')h+=`<h2 class="article-h2">${esc(b.text)}</h2>`});h+=`<div class="article-actions"><button class="primary" onclick="rewriteArticle()">🤖 Rewrite AI đăng tường</button><button onclick="doShare('${esc(data.title)}','${esc(url)}','${esc(data.og_image||'')}')">📤</button><button onclick="window.open('${esc(url)}','_blank')">🔗 Gốc</button></div><div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="ask-q" placeholder="Hỏi về bài viết..."></textarea><button onclick="askAI()">Hỏi</button><div id="ask-a" class="article-ai-answer"></div></div></div>`;el.innerHTML=h;window.scrollTo(0,0);return;}}catch(e){}el.innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="loading"><p>Không đọc được.</p><a href="${esc(url)}" target="_blank" style="color:#5cb87a">Mở gốc →</a></div>`;}
617
  async function rewriteArticle(){const url=_currentArticle?.url;if(!url)return;toast('⏳ Đang rewrite...');try{const r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,context:document.querySelector('.article-view')?.innerText?.slice(0,14000)||''})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng Tường AI!');if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
618
  async function rewriteUrl(){const url=document.getElementById('url-input')?.value.trim();if(!url)return alert('Dán URL');toast('⏳ Đang rewrite...');try{const r=await fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng!');document.getElementById('url-input').value='';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
619
  async function askAI(){const q=document.getElementById('ask-q')?.value.trim();if(!q)return alert('Nhập câu hỏi');const a=document.getElementById('ask-a');a.textContent='Đang hỏi...';try{const r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:_currentArticle?.url||'',question:q,context:document.querySelector('.article-view')?.innerText?.slice(0,12000)||''})});const j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}}
620
-
621
  async function readWallPost(i){const p=_wallPosts[i];if(!p)return;showView('view-article');
622
  const images = p.images || [];
623
  let imgGallery = '';
624
  if(images.length > 0){
625
  imgGallery = '<div class="article-image-gallery">';
626
  images.forEach((imgUrl, idx) => {
627
- if(idx === 0){
628
- imgGallery += `<img class="article-img article-hero-img" src="${esc(imgUrl)}" onerror="this.style.display='none" loading="eager">`;
629
- } else {
630
- if(idx === 1) imgGallery += '<div class="gallery-thumbs">';
631
- imgGallery += `<div class="gallery-thumb"><img src="${esc(imgUrl)}" onerror="this.parentElement.style.display='none'" loading="lazy"></div>`;
632
- }
633
  });
634
  if(images.length > 1) imgGallery += '</div>';
635
  imgGallery += '</div>';
636
  }
637
  const hasVideo = p.video && p.video.length > 0;
638
- const voiceOptions = [
639
- {id:'hoaimy', label:'🎙️ Nữ — Hoài My'},
640
- {id:'namminh', label:'🎙️ Nam — Nam Minh'},
641
- ];
642
  let voiceSelector = '';
643
  if(!hasVideo){
644
  voiceSelector = `<div class="tts-selector"><div class="tts-selector-label">🎙️ Chọn giọng đọc:</div><div class="tts-voice-btns">`;
645
- voiceOptions.forEach(v=>{
646
- voiceSelector += `<button class="tts-voice-btn" onclick="document.querySelectorAll('.tts-voice-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('selected-voice').value='${v.id}'">${v.label}</button>`;
647
- });
648
  voiceSelector += `</div><div class="tts-speed-row"><span>Tốc độ:</span><select id="selected-speed"><option value="1.0">1.0x — Bình thường</option><option value="1.2" selected>1.2x — Nhanh</option><option value="1.5">1.5x — Rất nhanh</option><option value="0.8">0.8x — Chậm</option></select></div>`;
649
  voiceSelector += `<input type="hidden" id="selected-voice" value="hoaimy"></div>`;
650
  }
@@ -652,11 +432,15 @@ async function readWallPost(i){const p=_wallPosts[i];if(!p)return;showView('view
652
  const firstVoiceBtn = document.querySelector('.tts-voice-btn');
653
  if(firstVoiceBtn) firstVoiceBtn.classList.add('active');
654
  window.scrollTo(0,0)}
655
-
656
  async function loadNewsTab(){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/homepage');const news=await r.json();if(!news.length){el.innerHTML='<div class="loading">Không có tin</div>';return}const groups={};news.forEach(a=>{if(!groups[a.group])groups[a.group]=[];groups[a.group].push(a)});let h='';for(const[g,arts] of Object.entries(groups)){h+=`<div class="section-title">${g}</div><div class="grid">`;arts.slice(0,6).forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'VnE')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>'}el.innerHTML=h}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
657
  async function loadCat(id){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=await fetch('/api/category/'+id).then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có tin</div>';return}let h='<div class="grid">';arts.forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>';el.innerHTML=h}
 
658
 
659
- fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){const home=document.getElementById('view-home');if(home){const w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ Persistent Storage chưa bật.';home.prepend(w)}}}).catch(()=>{});
660
-
661
- // === AUTO-START ===
662
- if(document.readyState==='complete'){loadHome();}else{window.addEventListener('load',loadHome);}
 
 
 
 
 
1
+ // === VNEWS Frontend v4 VTV inline + fixed livescore clicks ===
 
2
 
3
+ // === VTV CHANNELS CONFIG ===
4
  const VTV_CHANNELS = [
5
  {id:'vtv1', name:'VTV1', badge:'Tin tức'},
6
  {id:'vtv2', name:'VTV2', badge:'Khoa học'},
 
16
  ];
17
 
18
  const VTV_EPG = {
19
+ vtv1:[{t:'06:00',n:'Nhật ký ngày mai'},{t:'07:00',n:'Thời sự sáng'},{t:'12:00',n:'Thời sự trưa'},{t:'19:00',n:'Thời sự tối'},{t:'21:00',n:'Thời sự đêm'}],
20
+ vtv2:[{t:'06:00',n:'Khoa học & CN'},{t:'08:00',n:'Thế giới tự nhiên'},{t:'12:00',n:'Đi tìm giải pháp'},{t:'18:00',n:'Thế giới động vật'},{t:'20:00',n:'Khoa học & Tương lai'}],
21
+ vtv3:[{t:'06:00',n:'Sáng vui'},{t:'08:00',n:'Phim truyện'},{t:'12:00',n:'Âm nhạc'},{t:'18:00',n:'Tạp kỹ thuật số'},{t:'20:00',n:'Phim truyện đặc biệt'}],
22
  vtv4:[{t:'06:00',n:'News'},{t:'08:00',n:'World News'},{t:'12:00',n:'Midday News'},{t:'18:00',n:'Evening News'},{t:'20:00',n:'World Today'}],
23
+ vtv5:[{t:'06:00',n:'Thời sự miền Nam'},{t:'08:00',n:'Thiếu nhi'},{t:'12:00',n:'Thời sự trưa'},{t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'}],
24
+ vtv6:[{t:'06:00',n:'Khởi động'},{t:'08:00',n:'Thanh niên & Sáng tạo'},{t:'12:00',n:'Nhịp sống trẻ'},{t:'18:00',n:'Thời sự trẻ'},{t:'20:00',n:'Đêm nhạc'}],
25
+ vtv7:[{t:'06:00',n:'Giáo dục sáng'},{t:'08:00',n:'Học mọi lúc'},{t:'12:00',n:'Giáo dục trưa'},{t:'18:00',n:'Giáo dục chiều'},{t:'20:00',n:'Tài liệu GD'}],
26
+ vtv8:[{t:'06:00',n:'Thời sự miền Trung'},{t:'08:00',n:'Văn hóa'},{t:'12:00',n:'Thời sự trưa'},{t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'}],
27
+ vtv9:[{t:'06:00',n:'Thời sự miền Bắc'},{t:'08:00',n:'Văn hóa'},{t:'12:00',n:'Thời sự trưa'},{t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'}],
28
+ vtv10:[{t:'06:00',n:'Thời sự Tây Nam Bộ'},{t:'08:00',n:'Văn hóa đồng bằng'},{t:'12:00',n:'Thời sự trưa'},{t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'}],
29
+ vtvprime:[{t:'06:00',n:'Prime Morning'},{t:'08:00',n:'Prime Cinema'},{t:'12:00',n:'Prime News'},{t:'18:00',n:'Prime Evening'},{t:'20:00',n:'Prime Night'}],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  };
31
 
32
  let _vtvStreams = {};
33
  let _vtvCurrentCh = null;
34
  let _vtvHls = null;
 
35
 
36
  function buildVTVEPG(chId){
37
  const epg = VTV_EPG[chId] || [];
 
41
  epg.forEach(item => {
42
  const itemH = parseInt(item.t.split(':')[0], 10);
43
  const isNow = itemH <= curH && (itemH + 2) > curH;
44
+ items += `<div class="vtv-epg-item${isNow?' now':''}"><div class="epg-t">${item.t}</div><div class="epg-n">${item.n}</div></div>`;
45
  });
46
+ return `<div class="vtv-epg" id="vtv-epg"><div class="vtv-epg-header"><span class="vtv-epg-title">📋 Lịch phát sóng</span><button class="vtv-epg-toggle" onclick="_vtvToggleEPG()">Ẩn/Hiện</button></div><div class="vtv-epg-list" id="vtv-epg-list">${items}</div></div>`;
47
+ }
48
+
49
+ function _vtvToggleEPG(){
50
+ const list = document.getElementById('vtv-epg-list');
51
+ if(list) list.style.display = list.style.display === 'none' ? 'flex' : 'none';
52
  }
53
 
54
  function buildVTVBlockHTML(){
55
  let tabs = '';
56
  VTV_CHANNELS.forEach(ch => {
57
+ tabs += `<button class="vtv-tab off" id="vtvt-${ch.id}" onclick="_vtvPlay('${ch.id}')">${ch.name}</button>`;
58
  });
59
  return `<div class="vtv-wrap" id="vtv-block">
60
  <div class="vtv-head"><span class="vtv-title">📺 VTV Trực Tuyến</span><span class="vtv-badge">● LIVE</span></div>
61
  <div class="vtv-tabs">${tabs}</div>
62
+ <div class="vtv-frame">
63
  <div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div>
64
  <video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video>
65
  <div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="_vtvRetry()">Thử lại</button></div>
66
  </div>
 
67
  </div>`;
68
  }
69
 
70
  async function loadVTVStreams(){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  try {
72
+ const r = await fetch('/api/vtv/streams', {signal: AbortSignal.timeout(10000)});
 
 
 
73
  if(r.ok){
74
  const data = await r.json();
75
  VTV_CHANNELS.forEach(ch => {
76
  const info = data[ch.id];
77
+ if(info && info.stream_url){
78
+ _vtvStreams[ch.id] = ['/api/proxy/m3u8/vtv?url=' + encodeURIComponent(info.stream_url)];
79
+ } else {
80
+ _vtvStreams[ch.id] = [];
81
  }
82
  });
83
  }
84
  } catch(e) {
85
+ console.warn('VTV API error:', e);
86
  }
87
+ VTV_CHANNELS.forEach(ch => {
88
+ const tab = document.getElementById('vtvt-'+ch.id);
89
+ if(tab){
90
+ if(_vtvStreams[ch.id] && _vtvStreams[ch.id].length > 0){
91
+ tab.classList.remove('off');
92
+ tab.textContent = ch.name;
93
+ } else {
94
+ tab.style.opacity = '0.35';
95
+ tab.textContent = ch.name + ' ✕';
96
+ }
97
+ }
98
+ });
99
  }
100
 
101
  function _vtvRetry(){
 
117
  errEl.style.display = 'none';
118
  loadEl.style.display = 'flex';
119
  loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + ch.name + '...';
120
+ if(_vtvHls){ _vtvHls.destroy(); _vtvHls = null; }
121
  const urls = _vtvStreams[chId] || [];
122
  if(urls.length === 0){
123
  loadEl.style.display = 'none';
 
125
  errMsg.textContent = chId === 'vtvprime' ? 'VTVPrime: Kênh trả phí.' : ch.name + ': Không tìm thấy luồng.';
126
  return;
127
  }
128
+ const epgEl = document.getElementById('vtv-epg');
129
+ if(epgEl) epgEl.remove();
130
+ const frame = document.querySelector('.vtv-frame');
131
+ if(frame){
132
+ const d = document.createElement('div');
133
+ d.innerHTML = buildVTVEPG(chId);
134
+ frame.appendChild(d.firstElementChild);
135
+ }
136
+ _vtvTryPlay(video, urls, 0, ch.name, loadEl, errEl, errMsg);
137
  }
138
 
139
+ function _vtvTryPlay(video, urls, idx, name, loadEl, errEl, errMsg){
140
  if(idx >= urls.length){
141
  loadEl.style.display = 'none';
142
  errEl.style.display = 'flex';
143
+ errMsg.textContent = name + ': Tất cả nguồn lỗi.';
144
  return;
145
  }
146
  const src = urls[idx];
147
+ loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + name + ' (' + (idx+1) + '/' + urls.length + ')...';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  if(typeof Hls !== 'undefined' && Hls.isSupported()){
149
+ const hls = new Hls({enableWorker:true, lowLatencyMode:true, startLevel:-1, capLevelToPlayerSize:true, maxBufferLength:20});
150
+ _vtvHls = hls;
 
 
 
 
 
 
151
  hls.loadSource(src);
152
  hls.attachMedia(video);
153
+ hls.on(Hls.Events.MANIFEST_PARSED, () => { video.play().catch(()=>{}); loadEl.style.display='none'; video.style.display='block'; });
154
+ let recAttempts = 0;
155
+ hls.on(Hls.Events.ERROR, (ev, data) => {
156
+ if(data.fatal){
157
+ if(data.type === Hls.ErrorTypes.NETWORK_ERROR){
158
+ recAttempts++;
159
+ if(recAttempts <= 3){ setTimeout(() => hls.startLoad(), 2000); }
160
+ else { hls.destroy(); _vtvHls = null; _vtvTryPlay(video, urls, idx+1, name, loadEl, errEl, errMsg); }
161
+ } else if(data.type === Hls.ErrorTypes.MEDIA_ERROR){
162
+ try { hls.recoverMediaError(); } catch(e) {}
163
+ } else { hls.destroy(); _vtvHls = null; _vtvTryPlay(video, urls, idx+1, name, loadEl, errEl, errMsg); }
164
+ }
165
+ });
166
  } else if(video.canPlayType('application/vnd.apple.mpegurl')){
167
  video.src = src;
168
+ video.addEventListener('loadedmetadata', () => { video.play().catch(()=>{}); loadEl.style.display='none'; video.style.display='block'; }, {once:true});
169
+ video.addEventListener('error', () => { _vtvTryPlay(video, urls, idx+1, name, loadEl, errEl, errMsg); }, {once:true});
170
  } else {
171
+ loadEl.style.display = 'none'; errEl.style.display = 'flex'; errMsg.textContent = 'Trình duyệt không hỗ trợ HLS';
 
 
172
  }
173
  }
174
 
175
  // === LOAD HOME ===
176
  async function loadHome(){
 
 
 
 
 
 
 
 
 
 
177
  const[featured,sh,wall,hlLeagues,ai,wcData]=await Promise.all([
178
+ fetch('/api/livescore/featured').then(r=>r.json()).catch(()=>null),
179
+ fetch('/api/shorts').then(r=>r.json()).catch(()=>[]),
180
+ fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]})),
181
+ fetch('/api/highlights/leagues').then(r=>r.json()).catch(()=>({})),
182
+ fetch('/api/genk_ai').then(r=>r.json()).catch(()=>[]),
183
+ fetch('/api/wc2026').then(r=>r.json()).catch(()=>null)
184
  ]);
185
  _hlLeagueData=hlLeagues;
186
  _wc2026Data=wcData;
 
188
  _wallPosts=(wall&&wall.posts)||[];
189
  let h='';
190
 
191
+ // ===== VTV BLOCK — rendered inline, no external JS needed =====
192
  h += buildVTVBlockHTML();
193
 
194
+ if(featured&&featured.home){
195
  const sc=featured.status==='live'?'':'upcoming';
196
  const st=featured.status==='live'?`🔴 ${featured.minute||'LIVE'}`:`⏰ ${featured.time}`;
197
+ const eid = String(featured.event_id||'').replace(/[<>&"']/g,'');
198
+ const mUrl = String(featured.url||'').replace(/[<>&"']/g,'');
199
+ const fHome = String(featured.home||'').replace(/[<>&"']/g,'');
200
+ const fAway = String(featured.away||'').replace(/[<>&"']/g,'');
201
+ const fLeague = String(featured.league||'').replace(/[<>&"']/g,'');
202
+ const fScore = String(featured.score||'VS').replace(/[<>&"']/g,'');
203
+ const fHomeLogo = String(featured.home_logo||'').replace(/[<>&"']/g,'');
204
+ const fAwayLogo = String(featured.away_logo||'').replace(/[<>&"']/g,'');
205
+ h+=`<div class="featured-match" data-event-id="${eid}" data-url="${mUrl}" onclick="openMatch('${eid}','${mUrl}')" title="${fHome} vs ${fAway} — ${fLeague}">`+
206
+ `<div class="fm-league">${fLeague}</div>`+
207
  `<div class="fm-teams">`+
208
+ `<div class="fm-team"><img src="${fHomeLogo}" onerror="this.style.display='none'"><span>${fHome}</span></div>`+
209
+ `<div class="fm-score">${fScore}</div>`+
210
+ `<div class="fm-team"><img src="${fAwayLogo}" onerror="this.style.display='none'"><span>${fAway}</span></div>`+
211
  `</div>`+
212
  `<div class="fm-status ${sc}">${st}</div>`+
213
  `</div>`;
 
225
  for(const[key,cfg] of Object.entries(HL_CONFIG)){const vids=hlLeagues[key];if(!vids||!vids.length)continue;h+=`<div class="slider-wrap"><div class="slider-header"><span class="slider-label">${cfg.emoji} ${cfg.name}</span></div><div class="slider-track">`;vids.slice(0,8).forEach((a,i)=>{h+=`<div class="slider-item" onclick="openHighlightFeed('${key}',${i})"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
226
  if(ai&&ai.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🤖 Ứng dụng AI</span></div><div class="slider-track">';ai.slice(0,12).forEach(a=>{h+=`<div class="slider-item" onclick="readArticle('${esc(a.link)}')"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}</div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
227
  document.getElementById('view-home').innerHTML=h;
228
+
229
+ // Load VTV streams after DOM is ready
230
+ loadVTVStreams().then(() => {
231
+ const tryOrder = ['vtv6','vtv1','vtv2','vtv3','vtv4','vtv5','vtv7','vtv8','vtv9','vtv10'];
232
+ for(const chId of tryOrder){
233
+ if(_vtvStreams[chId] && _vtvStreams[chId].length > 0){
234
+ setTimeout(() => _vtvPlay(chId), 300);
235
+ return;
236
+ }
237
+ }
238
+ });
239
+
240
  loadLivescore('today');loadHotTopics();
241
  if(_wc2026Data)switchWCTab('news');
242
  }
 
244
  // === WALL POST HELPERS ===
245
  function makeWallItem(p,i){
246
  const hasVideo = p.video && p.video.length > 0;
247
+ const thumbContent = p.img ? `<img src="${esc(p.img)}" onerror="this.style.display='none'">` : (hasVideo ? `<video src="${esc(p.video)}" muted></video>` : '');
 
 
248
  const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
249
  const videoBtn = hasVideo
250
  ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(${i})">▶ Xem Short</button>`
251
  : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(p.id||i)}',this)">🎬 Tạo Video</button>`;
252
+ return `<div class="wall-item" id="wall-item-${esc(p.id||i)}"><div class="wall-thumb">${thumbContent}${videoBadge}</div><div class="wall-title">${esc(p.title)}</div><div class="wall-text">${esc((p.text||'').slice(0,180))}</div><div class="wall-actions"><button class="primary" onclick="readWallPost(${i})">Xem</button>${videoBtn}</div></div>`;
 
 
 
 
 
 
 
 
253
  }
254
 
255
  async function makeShortVideo(postId, btn, voice, speed){
 
294
  const track = shortAISection.querySelector('.slider-track');
295
  if(track){
296
  let h = '';
297
+ aiShorts.slice(0,20).forEach((p,i)=>{ h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${esc(p.video)}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`; });
 
 
298
  track.innerHTML = h;
299
  }
300
  }
 
309
  if(homeEl){
310
  let insertBefore=homeEl.querySelector('.slider-wrap');
311
  const newWrap=document.createElement('div');
312
+ newWrap.className='slider-wrap'; newWrap.id='ai-wall-wrap';
 
313
  newWrap.innerHTML=`<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">${makeWallItem(post,0)}</div>`;
314
  if(insertBefore){ homeEl.insertBefore(newWrap,insertBefore); }else{ homeEl.appendChild(newWrap); }
315
  const firstItem=newWrap.querySelector('.wall-item');
 
321
  div.className='wall-item wall-item-new';
322
  div.id='wall-item-'+(post.id||'new-'+Date.now());
323
  const hasVideo = post.video && post.video.length > 0;
324
+ const thumbContent = post.img ? `<img src="${esc(post.img)}" onerror="this.style.display='none'">` : (hasVideo ? `<video src="${esc(post.video)}" muted></video>` : '');
 
 
325
  const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
326
  const videoBtn = hasVideo
327
  ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(0)">▶ Xem Short</button>`
 
332
  if(hasVideo) refreshShortAISlider();
333
  }
334
 
 
335
  let _shortsData=[];
336
  let _wallPosts=[];
337
  let _currentView='home';
 
339
  let _currentMatchUrl=null;
340
  function interleaveShorts(shorts){const dt=shorts.filter(s=>s.channel==='baodantri7941');const sk=shorts.filter(s=>s.channel==='baosuckhoedoisongboyte');const result=[];let i=0,j=0;while(i<dt.length||j<sk.length){if(i<dt.length)result.push(dt[i++]);if(j<sk.length)result.push(sk[j++]);}return result;}
341
  let _htPage=0,_htTopic='';
342
+ async function loadHotTopics(){const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));const el=document.getElementById('hot-topics');if(!el)return;el.innerHTML=(j.topics||[]).slice(0,18).map(t=>{const topicText=t.topic||t.label.replace(/^#/,'');return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;}).join('');if(j.topics&&j.topics[0]){const firstTopic=j.topics[0].topic||j.topics[0].label.replace(/^#/,'');setTimeout(()=>searchTopic(firstTopic),800);}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
343
  function searchTopic(topic){if(!topic){topic=document.getElementById('topic-input')?.value.trim();if(!topic){alert('Nhập chủ đề');return;}}document.getElementById('topic-input').value='';_htTopic=topic;_htPage=0;showHashtagSources(topic,0);}
344
  async function showHashtagSources(topic,page){const box=document.getElementById('hashtag-box');if(!box)return;if(page===0)box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Đang tìm...</div></div>`;try{const r=await fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=${page}`);const j=await r.json();const sources=j.sources||[];if(!sources.length&&page===0){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#888;padding:8px">Không tìm được bài viết liên quan</div></div>`;return;}let h='';if(page===0)h=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)} <span style="font-size:10px;color:#888">(${j.total} bài từ 8 nguồn)</span></h3><div id="ht-list">`;sources.forEach((s,i)=>{const idx=page*8+i;h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${idx}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')}</div></div></div>`;});if(page===0){h+=`</div><button class="hashtag-rewrite-btn" onclick="rewriteHashtag('${esc(topic).replace(/'/g,"\\'")}')">🤖 Rewrite AI tổng hợp & đăng tường</button>`;if(j.has_more)h+=`<button class="hashtag-load-more" id="ht-more" onclick="loadMoreHashtag()">Tải thêm ▼</button>`;h+=`</div>`;box.innerHTML=h;}else{document.getElementById('ht-list')?.insertAdjacentHTML('beforeend',h);const btn=document.getElementById('ht-more');if(btn){if(!j.has_more)btn.remove();else{btn.disabled=false;btn.textContent='Tải thêm ▼';}}}sources.forEach((s,i)=>{const idx=page*8+i;if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+idx);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#e74c3c;padding:8px">Lỗi: ${esc(e.message)}</div></div>`;}}
345
  function loadMoreHashtag(){_htPage++;const btn=document.getElementById('ht-more');if(btn){btn.disabled=true;btn.textContent='Đang tải...';}showHashtagSources(_htTopic,_htPage);}
346
  async function rewriteHashtag(topic){const btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}try{const r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');toast('✅ Đã đăng Tường AI!');if(btn)btn.textContent='✅ Đăng thành công!';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message);if(btn){btn.disabled=false;btn.textContent='🤖 Rewrite AI';}}}
347
 
348
+ // === LIVESCORE — FIXED: use closest() instead of e.target.tagName === 'a' ===
349
+ async function loadLivescore(tab){
350
+ document.querySelectorAll('.ls-tab').forEach(t=>t.classList.remove('active'));
351
+ document.querySelector(`.ls-tab[data-tab="${tab}"]`)?.classList.add('active');
352
+ const el=document.getElementById('ls-content');
353
+ if(!el)return;
354
+ el.innerHTML='<div class="loading">Đang tải...</div>';
355
+ let ep='/api/livescore/'+tab;
356
+ if(tab.startsWith('bxh_'))ep='/api/livescore/standings/'+tab.replace('bxh_','');
357
+ try{
358
+ const r=await fetch(ep);
359
+ const d=await r.json();
360
+ el.innerHTML=d.html&&d.html.length>50?d.html:'<div class="loading">Không có dữ liệu</div>';
361
+ bindMatchClicks(el);
362
+ }catch(e){el.innerHTML='<div class="loading">Lỗi</div>';}
363
+ }
364
 
365
  function bindMatchClicks(el){
366
  if(!el) return;
 
369
  md._bound = true;
370
  md.style.cursor='pointer';
371
  md.addEventListener('click',function(e){
372
+ // Find the closest anchor with /tran-dau/ — works even when clicking text inside <a>
373
+ const a = e.target.closest('a[href*="/tran-dau/"]');
374
+ if(!a) return; // No match link found, let it be
375
+ e.preventDefault();
376
+ e.stopPropagation();
377
+ const href = a.getAttribute('href') || '';
378
+ const m = href.match(/\/tran-dau\/(\d+)\//);
379
+ if(m){
380
+ const fullUrl = href.startsWith('http') ? href : 'https://bongda.com.vn' + href;
381
+ openMatch(m[1], fullUrl);
 
 
 
382
  }
383
  });
384
  });
385
  }
386
 
 
387
  function openMatch(id,url){if(!id)return;_currentEventId=id;if(url)_currentMatchUrl=url;document.getElementById('match-overlay').classList.add('active');document.body.style.overflow='hidden';loadMatchTab('detail')}
388
  function closeMatch(){document.getElementById('match-overlay').classList.remove('active');document.body.style.overflow=''}
389
+ async function loadMatchTab(tab){document.querySelectorAll('.mo-tab').forEach(t=>t.classList.remove('active'));document.querySelectorAll('.mo-tab').forEach(t=>{if((tab==='comm'&&t.textContent==='Diễn biến')||(tab==='stats'&&t.textContent==='Thống kê')||(tab==='detail'&&t.textContent.includes('Chi tiết')))t.classList.add('active')});const el=document.getElementById('mo-body');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';try{let apiUrl;if(tab==='stats')apiUrl=`/api/match/${_currentEventId}/stats`;else if(tab==='comm')apiUrl=`/api/match/${_currentEventId}/commentaries`;else{apiUrl=`/api/match/${_currentEventId}/detail`;if(_currentMatchUrl)apiUrl+='?url='+encodeURIComponent(_currentMatchUrl)}const r=await fetch(apiUrl);if(!r.ok){el.innerHTML='<div class="loading">Lỗi máy chủ ('+r.status+')</div>';return}const d=await r.json();if(d.error){el.innerHTML='<div class="loading">'+esc(d.error)+'</div>';return}if(tab==='detail'&&typeof renderMatchDetail==='function'){renderMatchDetail(el,d);return}el.innerHTML=d.html||'<div class="loading">Không có dữ liệu</div>'}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
  async function doInteract(videoId,type){try{const r=await fetch('/api/v2/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,type})});return await r.json();}catch(e){return null;}}
391
  async function getInteractions(videoId){try{return await fetch('/api/v2/interactions?id='+encodeURIComponent(videoId)).then(r=>r.json());}catch(e){return{views:0,likes:0,comments:0};}}
392
  async function getComments(videoId){try{const j=await fetch('/api/v2/comments?id='+encodeURIComponent(videoId)).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
393
  async function postComment(videoId,text){try{const j=await fetch('/api/v2/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,text})}).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
 
 
394
  function buildTikTokSlide(opts){return`<div class="tiktok-slide" data-vid="${esc(opts.videoId)}">${opts.vtag}<div class="tiktok-bottom"><span class="badge ${opts.badgeClass||'badge-fpt'}">${opts.badge||''}</span><p class="tiktok-title">${esc(opts.title)}</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation();doView('${esc(opts.videoId)}',this)"><div class="icon">👁</div><div class="count" id="vc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doLike('${esc(opts.videoId)}',this)"><div class="icon">❤️</div><div class="count" id="lc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();toggleComments('${esc(opts.videoId)}',${opts.idx})"><div class="icon">💬</div><div class="count" id="cc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doShare('${esc(opts.title)}','${esc(opts.shareUrl||'')}','')"><div class="icon">📤</div></button>${opts.extraBtn||''}</div><span class="tiktok-counter">${opts.idx+1}/${opts.total}</span><div class="inline-comments" id="cmt-inline-${opts.idx}" style="display:none"></div></div>`;}
395
  async function doView(videoId,btn){const j=await doInteract(videoId,'view');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.views);}}
396
  async function doLike(videoId,btn){const j=await doInteract(videoId,'like');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.likes);}}
397
+ function fmtNum(n){if(!n)return'0';if(n>=1000000)return(n/1000000).toFixed(1)+'M';if(n>=1000)return(n/1000).toFixed(1)+'K';return String(n);}
398
  async function loadCounters(videoIds){for(let i=0;i<videoIds.length;i++){const id=videoIds[i];if(!id)continue;const j=await getInteractions(id);const vc=document.getElementById('vc-'+i);if(vc)vc.textContent=fmtNum(j.views);const lc=document.getElementById('lc-'+i);if(lc)lc.textContent=fmtNum(j.likes);const cc=document.getElementById('cc-'+i);if(cc)cc.textContent=fmtNum(j.comments);}}
399
  async function toggleComments(videoId,idx){const panel=document.getElementById('cmt-inline-'+idx);if(!panel)return;if(panel.style.display!=='none'){panel.style.display='none';return;}panel.style.display='block';panel.innerHTML='<div style="padding:8px;color:#888;font-size:11px">Đang tải...</div>';const cmts=await getComments(videoId);renderInlineComments(panel,videoId,idx,cmts);}
400
  function renderInlineComments(panel,videoId,idx,cmts){let h='<div class="inline-cmt-header"><span>💬 Bình luận</span><button onclick="document.getElementById(\'cmt-inline-'+idx+'\').style.display=\'none\'">✕</button></div><div class="inline-cmt-list">';if(cmts.length){cmts.slice(-30).forEach(c=>{h+=`<div class="inline-cmt-item"><span class="inline-cmt-time">${c.time||''}</span>${esc(c.text)}</div>`;});}else{h+='<div style="color:#777;font-size:11px;padding:4px">Chưa có bình luận</div>';}h+=`</div><div class="inline-cmt-input"><input id="cmt-input-${idx}" placeholder="Viết bình luận..." onkeydown="if(event.key==='Enter')submitInlineCmt('${esc(videoId)}',${idx})"><button onclick="submitInlineCmt('${esc(videoId)}',${idx})">Gửi</button></div>`;panel.innerHTML=h;const list=panel.querySelector('.inline-cmt-list');if(list)list.scrollTop=list.scrollHeight;}
401
  async function submitInlineCmt(videoId,idx){const inp=document.getElementById('cmt-input-'+idx);if(!inp)return;const text=inp.value.trim();if(!text)return;inp.value='';inp.disabled=true;const cmts=await postComment(videoId,text);inp.disabled=false;const panel=document.getElementById('cmt-inline-'+idx);if(panel)renderInlineComments(panel,videoId,idx,cmts);const cc=document.getElementById('cc-'+idx);if(cc)cc.textContent=fmtNum(cmts.length);}
 
402
  function initTikTokFeed(){const feed=document.getElementById('tiktok-feed');if(!feed)return;const slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{const v=sl.querySelector('video');const fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls&&!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){const hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,()=>v.play().catch(()=>{}));v._hls=hls}else if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;const vid=sl.dataset.vid;if(vid&&!sl._viewed){sl._viewed=true;doInteract(vid,'view').then(j=>{if(j){const vc=document.getElementById('vc-'+idx);if(vc)vc.textContent=fmtNum(j.views);}});}}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null}}if(fr&&fr.src)fr.src=''}});cur=i}let sT;feed.addEventListener('scroll',()=>{clearTimeout(sT);sT=setTimeout(()=>{const rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2;let best=-1,bestD=1e9;slides.forEach((sl,i)=>{const d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i}});if(best>=0)act(best)},150)});setTimeout(()=>act(0),400);slides.forEach(sl=>{const v=sl.querySelector('video');if(v)v.addEventListener('click',e=>{e.preventDefault();v.paused?v.play().catch(()=>{}):v.pause()})});const ids=[...slides].map(sl=>sl.dataset.vid||'');loadCounters(ids)}
 
403
  async function openHighlightFeed(league,idx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';let articles=(_hlLeagueData||{})[league]||[];if(!articles.length){try{articles=await fetch('/api/highlights/'+league).then(r=>r.json())}catch(e){articles=[]}}if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return}const vids=[];const results=await Promise.all(articles.map(async(a,i)=>{try{const r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));const v=await r.json();if(v&&v.src)return{...a,...v,_idx:i}}catch(e){}return null}));results.forEach(r=>{if(r)vids.push(r)});vids.sort((a,b)=>a._idx-b._idx);if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return}let ti=vids.findIndex(v=>v._idx===idx);if(ti<0)ti=0;const ordered=ti>0?[...vids.slice(ti),...vids.slice(0,ti)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Highlight</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const isYT=v.type==='youtube',isHLS=!isYT&&v.src?.includes('.m3u8'),poster=v.poster?` poster="${v.poster}"`:'';const vtag=isYT?`<iframe data-yt-src="${v.src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`:isHLS?`<video playsinline preload="none"${poster} data-hls="${v.src}" loop controls></video>`:`<video playsinline preload="none"${poster} loop controls><source src="${v.src}" type="video/mp4"></video>`;const videoId='hl-'+league+'-'+(v.id||v._idx);h+=buildTikTokSlide({vtag,title:v.title,badge:'HL',badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:v.link||'',extraBtn:`<button class="tiktok-right-btn" onclick="event.stopPropagation();this.closest('.tiktok-slide').classList.toggle('ratio-wide')"><div class="icon">⬜</div><div class="count">16:9</div></button>`});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
 
404
  async function openYTShortsFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=_shortsData.length?_shortsData:await fetch('/api/shorts').then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có shorts</div>';return}const ordered=startIdx>0?[...arts.slice(startIdx),...arts.slice(0,startIdx)]:arts;let h=`<button class="back-btn" onclick="switchCat('home')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const id=v.id||'';const src=`https://www.youtube.com/embed/${id}?autoplay=1&rel=0&playsinline=1`;const vtag=`<iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`;const badge=v.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';const videoId='yt-'+id;h+=buildTikTokSlide({vtag,title:v.title,badge,badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:'https://youtube.com/watch?v='+id});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
 
405
  async function openShortAIFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const wall=(await fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];const vids=wall.filter(p=>p.video);if(!vids.length){el.innerHTML='<div class="loading">Chưa có Short AI</div>';return}const ordered=startIdx>0?[...vids.slice(startIdx),...vids.slice(0,startIdx)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((p,i)=>{const vtag=`<video src="${p.video}" playsinline loop controls></video>`;const videoId='ai-'+(p.id||i);h+=buildTikTokSlide({vtag,title:p.title,badge:'AI',badgeClass:'badge-ai',videoId,idx:i,total:ordered.length,shareUrl:SPACE});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
 
406
  async function readArticle(url){showView('view-article');const el=document.getElementById('view-article');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/article?url='+encodeURIComponent(url));const data=await r.json();if(data&&!data.error&&data.body&&data.body.length){_currentArticle={url,data};let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><h1 class="article-title">${esc(data.title)}</h1>`;if(data.summary)h+=`<div class="article-summary">${esc(data.summary)}</div>`;const seen={};data.body.forEach(b=>{if(b.type==='p')h+=`<p class="article-p">${b.text}</p>`;else if(b.type==='img'&&b.src&&!seen[b.src]){seen[b.src]=1;h+=`<img class="article-img" src="${esc(b.src)}" onerror="this.style.display='none'">`}else if(b.type==='heading')h+=`<h2 class="article-h2">${esc(b.text)}</h2>`});h+=`<div class="article-actions"><button class="primary" onclick="rewriteArticle()">🤖 Rewrite AI đăng tường</button><button onclick="doShare('${esc(data.title)}','${esc(url)}','${esc(data.og_image||'')}')">📤</button><button onclick="window.open('${esc(url)}','_blank')">🔗 Gốc</button></div><div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="ask-q" placeholder="Hỏi về bài viết..."></textarea><button onclick="askAI()">Hỏi</button><div id="ask-a" class="article-ai-answer"></div></div></div>`;el.innerHTML=h;window.scrollTo(0,0);return;}}catch(e){}el.innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="loading"><p>Không đọc được.</p><a href="${esc(url)}" target="_blank" style="color:#5cb87a">Mở gốc →</a></div>`;}
407
  async function rewriteArticle(){const url=_currentArticle?.url;if(!url)return;toast('⏳ Đang rewrite...');try{const r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,context:document.querySelector('.article-view')?.innerText?.slice(0,14000)||''})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng Tường AI!');if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
408
  async function rewriteUrl(){const url=document.getElementById('url-input')?.value.trim();if(!url)return alert('Dán URL');toast('⏳ Đang rewrite...');try{const r=await fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng!');document.getElementById('url-input').value='';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
409
  async function askAI(){const q=document.getElementById('ask-q')?.value.trim();if(!q)return alert('Nhập câu hỏi');const a=document.getElementById('ask-a');a.textContent='Đang hỏi...';try{const r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:_currentArticle?.url||'',question:q,context:document.querySelector('.article-view')?.innerText?.slice(0,12000)||''})});const j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}}
 
410
  async function readWallPost(i){const p=_wallPosts[i];if(!p)return;showView('view-article');
411
  const images = p.images || [];
412
  let imgGallery = '';
413
  if(images.length > 0){
414
  imgGallery = '<div class="article-image-gallery">';
415
  images.forEach((imgUrl, idx) => {
416
+ if(idx === 0){ imgGallery += `<img class="article-img article-hero-img" src="${esc(imgUrl)}" onerror="this.style.display='none" loading="eager">`; }
417
+ else { if(idx === 1) imgGallery += '<div class="gallery-thumbs">'; imgGallery += `<div class="gallery-thumb"><img src="${esc(imgUrl)}" onerror="this.parentElement.style.display='none'" loading="lazy"></div>`; }
 
 
 
 
418
  });
419
  if(images.length > 1) imgGallery += '</div>';
420
  imgGallery += '</div>';
421
  }
422
  const hasVideo = p.video && p.video.length > 0;
423
+ const voiceOptions = [{id:'hoaimy',label:'🎙️ Nữ — Hoài My'},{id:'namminh',label:'🎙️ Nam — Nam Minh'}];
 
 
 
424
  let voiceSelector = '';
425
  if(!hasVideo){
426
  voiceSelector = `<div class="tts-selector"><div class="tts-selector-label">🎙️ Chọn giọng đọc:</div><div class="tts-voice-btns">`;
427
+ voiceOptions.forEach(v=>{ voiceSelector += `<button class="tts-voice-btn" onclick="document.querySelectorAll('.tts-voice-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('selected-voice').value='${v.id}'">${v.label}</button>`; });
 
 
428
  voiceSelector += `</div><div class="tts-speed-row"><span>Tốc độ:</span><select id="selected-speed"><option value="1.0">1.0x — Bình thường</option><option value="1.2" selected>1.2x — Nhanh</option><option value="1.5">1.5x — Rất nhanh</option><option value="0.8">0.8x — Chậm</option></select></div>`;
429
  voiceSelector += `<input type="hidden" id="selected-voice" value="hoaimy"></div>`;
430
  }
 
432
  const firstVoiceBtn = document.querySelector('.tts-voice-btn');
433
  if(firstVoiceBtn) firstVoiceBtn.classList.add('active');
434
  window.scrollTo(0,0)}
 
435
  async function loadNewsTab(){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/homepage');const news=await r.json();if(!news.length){el.innerHTML='<div class="loading">Không có tin</div>';return}const groups={};news.forEach(a=>{if(!groups[a.group])groups[a.group]=[];groups[a.group].push(a)});let h='';for(const[g,arts] of Object.entries(groups)){h+=`<div class="section-title">${g}</div><div class="grid">`;arts.slice(0,6).forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'VnE')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>'}el.innerHTML=h}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
436
  async function loadCat(id){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=await fetch('/api/category/'+id).then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có tin</div>';return}let h='<div class="grid">';arts.forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>';el.innerHTML=h}
437
+ fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!persistent){const home=document.getElementById('view-home');if(home){const w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ Persistent Storage chưa bật.';home.prepend(w)}}}).catch(()=>{});
438
 
439
+ (function(){
440
+ try{
441
+ const pa=localStorage.getItem('pending_article');
442
+ const pv=localStorage.getItem('pending_video');
443
+ if(pa){ localStorage.removeItem('pending_article'); setTimeout(()=>{ if(typeof readArticle==='function') readArticle(pa); },1500); }
444
+ if(pv){ localStorage.removeItem('pending_video'); try{ const v=JSON.parse(pv); if(v&&v.url) setTimeout(()=>{window.open(v.url,'_blank')},1500); }catch(e){} }
445
+ }catch(e){}
446
+ })();
static/core_1781056782.js ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // === VNEWS Frontend v2 - Full Functions ===
2
+ // Updated: Voice selector + speed control + image gallery + auto voice detect
3
+
4
+ // === LOAD HOME ===
5
+ async function loadHome(){
6
+ const[featured,sh,wall,hlLeagues,ai,wcData]=await Promise.all([
7
+ fetch('/api/livescore/featured').then(r=>r.json()).catch(()=>null),
8
+ fetch('/api/shorts').then(r=>r.json()).catch(()=>[]),
9
+ fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]})),
10
+ fetch('/api/highlights/leagues').then(r=>r.json()).catch(()=>({})),
11
+ fetch('/api/genk_ai').then(r=>r.json()).catch(()=>[]),
12
+ fetch('/api/wc2026').then(r=>r.json()).catch(()=>null)
13
+ ]);
14
+ _hlLeagueData=hlLeagues;
15
+ _wc2026Data=wcData;
16
+ _shortsData=interleaveShorts(sh||[]);
17
+ _wallPosts=(wall&&wall.posts)||[];
18
+ let h='';
19
+ if(featured&&featured.home){
20
+ const sc=featured.status==='live'?'':'upcoming';
21
+ const st=featured.status==='live'?`🔴 ${featured.minute||'LIVE'}`:`⏰ ${featured.time}`;
22
+ // Safely encode for HTML attribute: escape quotes, angle brackets, ampersands
23
+ const eid = String(featured.event_id||'').replace(/[<>&"']/g,'');
24
+ const mUrl = String(featured.url||'').replace(/[<>&"']/g,'');
25
+ const fHome = String(featured.home||'').replace(/[<>&"']/g,'');
26
+ const fAway = String(featured.away||'').replace(/[<>&"']/g,'');
27
+ const fLeague = String(featured.league||'').replace(/[<>&"']/g,'');
28
+ const fScore = String(featured.score||'VS').replace(/[<>&"']/g,'');
29
+ const fHomeLogo = String(featured.home_logo||'').replace(/[<>&"']/g,'');
30
+ const fAwayLogo = String(featured.away_logo||'').replace(/[<>&"']/g,'');
31
+ const safeTitle = `${fHome} vs ${fAway} — ${fLeague}`;
32
+ h+=`<div class="featured-match" data-event-id="${eid}" data-url="${mUrl}" onclick="openMatch('${eid}','${mUrl}')" title="${safeTitle}">`+
33
+ `<div class="fm-league">${fLeague}</div>`+
34
+ `<div class="fm-teams">`+
35
+ `<div class="fm-team"><img src="${fHomeLogo}" onerror="this.style.display='none'"><span>${fHome}</span></div>`+
36
+ `<div class="fm-score">${fScore}</div>`+
37
+ `<div class="fm-team"><img src="${fAwayLogo}" onerror="this.style.display='none'"><span>${fAway}</span></div>`+
38
+ `</div>`+
39
+ `<div class="fm-status ${sc}">${st}</div>`+
40
+ `</div>`;
41
+ }
42
+ h+=`<div class="ai-compose"><div class="ai-compose-title">🤖 AI viết bài</div><div class="ai-compose-row"><input id="topic-input" placeholder="Nhập chủ đề..."><button onclick="searchTopic()">Tìm nguồn</button></div><div class="ai-compose-row"><input id="url-input" placeholder="Dán URL bài viết..."><button class="secondary" onclick="rewriteUrl()">Rewrite</button></div><div id="hot-topics" class="hot-topic-row"></div></div>`;
43
+ h+='<div id="hashtag-box"></div>';
44
+ h+=`<div class="ls-section"><div class="ls-header"><h3>⚽ Livescore</h3></div><div class="ls-tabs"><span class="ls-tab active" data-tab="today" onclick="loadLivescore('today')">📅 Hôm nay</span><span class="ls-tab" data-tab="live" onclick="loadLivescore('live')">🔴 Live</span><span class="ls-tab" data-tab="incoming" onclick="loadLivescore('incoming')">⏰ Sắp tới</span><span class="ls-tab" data-tab="results" onclick="loadLivescore('results')">✅ Kết quả</span><span class="ls-tab" data-tab="bxh_nha" onclick="loadLivescore('bxh_nha')">🏆 NHA</span><span class="ls-tab" data-tab="bxh_laliga" onclick="loadLivescore('bxh_laliga')">🏆 La Liga</span></div><div class="ls-content" id="ls-content"><div class="loading">Đang tải...</div></div></div>`;
45
+ h+=`<div id="wc2026-live-section" class="wc2026-section"><div class="wc-header"><h2>🏆 World Cup 2026</h2><span class="wc-live-badge">● LIVE</span></div><div class="wc-tabs"><span class="wc-tab active" onclick="switchWCTab('news')">📰 Tin tức</span><span class="wc-tab" onclick="switchWCTab('fixtures')">📅 Lịch thi đấu</span><span class="wc-tab" onclick="switchWCTab('standings')">🏆 BXH</span><span class="wc-tab" onclick="switchWCTab('highlights')">🎬 Highlight</span><span class="wc-tab" onclick="switchWCTab('stats')">📊 Thống kê</span></div><div class="wc-content" id="wc-content"><div class="loading">Đang tải World Cup 2026...</div></div></div>`;
46
+ const wallPosts=_wallPosts;
47
+ const aiShorts=wallPosts.filter(p=>p.video);
48
+ if(aiShorts.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🎬 Short AI</span></div><div class="slider-track">';aiShorts.slice(0,20).forEach((p,i)=>{h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${p.video}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div></div>';}
49
+ if(_shortsData.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS</span><span class="slider-note">Mới nhất · xen kẽ</span></div><div class="slider-track">';_shortsData.slice(0,30).forEach((a,i)=>{const badge=a.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';h+=`<div class="slider-item shorts-item" onclick="openYTShortsFeed(${i})"><div class="slider-thumb shorts-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title"><span style="color:#f0c040;font-size:8px">${badge}</span> ${esc(a.title)}</div></div>`});h+='</div></div>';}
50
+ if(wallPosts.length){h+=`<div class="slider-wrap" id="ai-wall-wrap"><div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">`;wallPosts.slice(0,20).forEach((p,i)=>{h+=makeWallItem(p,i)});h+='</div></div>';}
51
+ const HL_CONFIG={"world-cup":{name:"World Cup 2026",emoji:"🌍"},"premier-league":{name:"Premier League",emoji:"🏴"},"champions-league":{name:"Champions League",emoji:"⭐"},"la-liga":{name:"La Liga",emoji:"🇪🇸"},"serie-a":{name:"Serie A",emoji:"🇮🇹"},"bundesliga":{name:"Bundesliga",emoji:"🇩🇪"},"friendly":{name:"Giao hữu",emoji:"🤝"}};
52
+ for(const[key,cfg] of Object.entries(HL_CONFIG)){const vids=hlLeagues[key];if(!vids||!vids.length)continue;h+=`<div class="slider-wrap"><div class="slider-header"><span class="slider-label">${cfg.emoji} ${cfg.name}</span></div><div class="slider-track">`;vids.slice(0,8).forEach((a,i)=>{h+=`<div class="slider-item" onclick="openHighlightFeed('${key}',${i})"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
53
+ if(ai&&ai.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🤖 Ứng dụng AI</span></div><div class="slider-track">';ai.slice(0,12).forEach(a=>{h+=`<div class="slider-item" onclick="readArticle('${esc(a.link)}')"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}</div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
54
+ document.getElementById('view-home').innerHTML=h;
55
+ loadLivescore('today');loadHotTopics();
56
+ if(_wc2026Data)switchWCTab('news');
57
+ }
58
+
59
+ // === WALL POST HELPERS ===
60
+ function makeWallItem(p,i){
61
+ const hasVideo = p.video && p.video.length > 0;
62
+ const thumbContent = p.img
63
+ ? `<img src="${esc(p.img)}" onerror="this.style.display='none'">`
64
+ : (hasVideo ? `<video src="${esc(p.video)}" muted></video>` : '');
65
+ const videoBadge = hasVideo
66
+ ? `<div class="wall-video-badge">🎬</div>`
67
+ : '';
68
+ const videoBtn = hasVideo
69
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(${i})">▶ Xem Short</button>`
70
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(p.id||i)}',this)">🎬 Tạo Video</button>`;
71
+
72
+ return `<div class="wall-item" id="wall-item-${esc(p.id||i)}">
73
+ <div class="wall-thumb">
74
+ ${thumbContent}
75
+ ${videoBadge}
76
+ </div>
77
+ <div class="wall-title">${esc(p.title)}</div>
78
+ <div class="wall-text">${esc((p.text||'').slice(0,180))}</div>
79
+ <div class="wall-actions">
80
+ <button class="primary" onclick="readWallPost(${i})">Xem</button>
81
+ ${videoBtn}
82
+ </div>
83
+ </div>`;
84
+ }
85
+
86
+ // === GENERATE SHORT VIDEO FOR A WALL POST ===
87
+ async function makeShortVideo(postId, btn, voice, speed){
88
+ if(!postId)return;
89
+ const origText = btn ? btn.textContent : '🎬 Tạo Video';
90
+ if(btn){btn.disabled=true;btn.textContent='⏳ Đang tạo...';}
91
+ toast('⏳ Đang tạo video shorts...');
92
+ try{
93
+ let url = '/api/ai/short/'+encodeURIComponent(postId);
94
+ const params = [];
95
+ if(voice) params.push('voice='+encodeURIComponent(voice));
96
+ if(speed) params.push('speed='+encodeURIComponent(speed));
97
+ if(params.length) url += '?' + params.join('&');
98
+ const r = await fetch(url, {method:'POST'});
99
+ const j = await r.json();
100
+ if(!r.ok || j.error) throw new Error(j.error||'Lỗi tạo video');
101
+ toast('✅ Đã tạo video shorts!');
102
+ const p = _wallPosts.find(x => String(x.id) === String(postId));
103
+ if(p){
104
+ p.video = j.video;
105
+ const itemId = 'wall-item-'+postId;
106
+ const el = document.getElementById(itemId);
107
+ if(el){
108
+ const idx = _wallPosts.indexOf(p);
109
+ el.outerHTML = makeWallItem(p, idx);
110
+ const newEl = document.getElementById(itemId);
111
+ if(newEl) newEl.className = 'wall-item wall-item-new';
112
+ }
113
+ }
114
+ refreshShortAISlider();
115
+ }catch(e){
116
+ toast('❌ '+e.message);
117
+ if(btn){btn.disabled=false;btn.textContent=origText;}
118
+ }
119
+ }
120
+
121
+ // Refresh Short AI slider after video generation
122
+ function refreshShortAISlider(){
123
+ const aiShorts = _wallPosts.filter(p=>p.video);
124
+ let shortAISection = document.getElementById('short-ai-section');
125
+ if(aiShorts.length === 0){
126
+ if(shortAISection) shortAISection.remove();
127
+ return;
128
+ }
129
+ if(shortAISection){
130
+ const track = shortAISection.querySelector('.slider-track');
131
+ if(track){
132
+ let h = '';
133
+ aiShorts.slice(0,20).forEach((p,i)=>{
134
+ h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${esc(p.video)}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`;
135
+ });
136
+ track.innerHTML = h;
137
+ }
138
+ }
139
+ }
140
+
141
+ function prependWallPost(post){
142
+ _wallPosts.unshift(post);
143
+ const track=document.getElementById('ai-wall-track');
144
+ const wrap=document.getElementById('ai-wall-wrap');
145
+ const homeEl=document.getElementById('view-home');
146
+ if(!track||!wrap){
147
+ if(homeEl){
148
+ let insertBefore=homeEl.querySelector('.slider-wrap');
149
+ const newWrap=document.createElement('div');
150
+ newWrap.className='slider-wrap';
151
+ newWrap.id='ai-wall-wrap';
152
+ newWrap.innerHTML=`<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">${makeWallItem(post,0)}</div>`;
153
+ if(insertBefore){
154
+ homeEl.insertBefore(newWrap,insertBefore);
155
+ }else{
156
+ homeEl.appendChild(newWrap);
157
+ }
158
+ const firstItem=newWrap.querySelector('.wall-item');
159
+ if(firstItem)firstItem.className='wall-item wall-item-new';
160
+ }
161
+ return;
162
+ }
163
+ const div=document.createElement('div');
164
+ div.className='wall-item wall-item-new';
165
+ div.id='wall-item-'+(post.id||'new-'+Date.now());
166
+ const hasVideo = post.video && post.video.length > 0;
167
+ const thumbContent = post.img
168
+ ? `<img src="${esc(post.img)}" onerror="this.style.display='none'">`
169
+ : (hasVideo ? `<video src="${esc(post.video)}" muted></video>` : '');
170
+ const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
171
+ const videoBtn = hasVideo
172
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(0)">▶ Xem Short</button>`
173
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(post.id)}',this)">🎬 Tạo Video</button>`;
174
+ div.innerHTML=`<div class="wall-thumb">${thumbContent}${videoBadge}</div><div class="wall-title">${esc(post.title)}</div><div class="wall-text">${esc((post.text||'').slice(0,180))}</div><div class="wall-actions"><button class="primary" onclick="readWallPost(0)">Xem</button>${videoBtn}</div>`;
175
+ track.prepend(div);
176
+ track.scrollTo({left:0,behavior:'smooth'});
177
+ if(hasVideo) refreshShortAISlider();
178
+ }
179
+
180
+ // === REST OF FUNCTIONS ===
181
+ let _shortsData=[];
182
+ let _wallPosts=[];
183
+ let _currentView='home';
184
+ let _currentEventId=null;
185
+ let _currentMatchUrl=null;
186
+ function interleaveShorts(shorts){const dt=shorts.filter(s=>s.channel==='baodantri7941');const sk=shorts.filter(s=>s.channel==='baosuckhoedoisongboyte');const result=[];let i=0,j=0;while(i<dt.length||j<sk.length){if(i<dt.length)result.push(dt[i++]);if(j<sk.length)result.push(sk[j++]);}return result;}
187
+ let _htPage=0,_htTopic='';
188
+ async function loadHotTopics(){const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));const el=document.getElementById('hot-topics');if(!el)return;el.innerHTML=(j.topics||[]).slice(0,18).map(t=>{const topicText=t.topic||t.label.replace(/^#/,'');return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;}).join('');if(j.topics&&j.topics[0]){const firstTopic=j.topics[0].topic||j.topics[0].label.replace(/^#/,'');setTimeout(()=>searchTopic(firstTopic),800);}}
189
+ function searchTopic(topic){if(!topic){topic=document.getElementById('topic-input')?.value.trim();if(!topic){alert('Nhập chủ đề');return;}}document.getElementById('topic-input').value='';_htTopic=topic;_htPage=0;showHashtagSources(topic,0);}
190
+ async function showHashtagSources(topic,page){const box=document.getElementById('hashtag-box');if(!box)return;if(page===0)box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Đang tìm...</div></div>`;try{const r=await fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=${page}`);const j=await r.json();const sources=j.sources||[];if(!sources.length&&page===0){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#888;padding:8px">Không tìm được bài viết liên quan</div></div>`;return;}let h='';if(page===0)h=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)} <span style="font-size:10px;color:#888">(${j.total} bài từ 8 nguồn)</span></h3><div id="ht-list">`;sources.forEach((s,i)=>{const idx=page*8+i;h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${idx}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')}</div></div></div>`;});if(page===0){h+=`</div><button class="hashtag-rewrite-btn" onclick="rewriteHashtag('${esc(topic).replace(/'/g,"\\'")}')">🤖 Rewrite AI tổng hợp & đăng tường</button>`;if(j.has_more)h+=`<button class="hashtag-load-more" id="ht-more" onclick="loadMoreHashtag()">Tải thêm ▼</button>`;h+=`</div>`;box.innerHTML=h;}else{document.getElementById('ht-list')?.insertAdjacentHTML('beforeend',h);const btn=document.getElementById('ht-more');if(btn){if(!j.has_more)btn.remove();else{btn.disabled=false;btn.textContent='Tải thêm ▼';}}}sources.forEach((s,i)=>{const idx=page*8+i;if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+idx);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#e74c3c;padding:8px">Lỗi: ${esc(e.message)}</div></div>`;}}
191
+ function loadMoreHashtag(){_htPage++;const btn=document.getElementById('ht-more');if(btn){btn.disabled=true;btn.textContent='Đang tải...';}showHashtagSources(_htTopic,_htPage);}
192
+ async function rewriteHashtag(topic){const btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}try{const r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');toast('✅ Đã đăng Tường AI!');if(btn)btn.textContent='✅ Đăng thành công!';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message);if(btn){btn.disabled=false;btn.textContent='🤖 Rewrite AI';}}}
193
+ async function loadLivescore(tab){document.querySelectorAll('.ls-tab').forEach(t=>t.classList.remove('active'));document.querySelector(`.ls-tab[data-tab="${tab}"]`)?.classList.add('active');const el=document.getElementById('ls-content');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';let ep='/api/livescore/'+tab;if(tab.startsWith('bxh_'))ep='/api/livescore/standings/'+tab.replace('bxh_','');try{const r=await fetch(ep);const d=await r.json();el.innerHTML=d.html&&d.html.length>50?d.html:'<div class="loading">Không có dữ liệu</div>';bindMatchClicks(el);}catch(e){el.innerHTML='<div class="loading">Lỗi</div>';}}
194
+ function bindMatchClicks(el){
195
+ if(!el) return;
196
+ el.querySelectorAll('.match-detail').forEach(md=>{
197
+ md.style.cursor='pointer';
198
+ // Remove old listeners to avoid duplicates (mark as bound)
199
+ if(md._bound) return;
200
+ md._bound = true;
201
+ md.addEventListener('click',function(e){
202
+ // Don't intercept clicks on interactive elements inside the row
203
+ const tag = e.target.tagName?.toLowerCase();
204
+ if(tag === 'a' || tag === 'button' || tag === 'input') {
205
+ e.preventDefault();
206
+ e.stopPropagation();
207
+ }
208
+ // Find ANY link with /tran-dau/ inside this match-detail row
209
+ const links = this.querySelectorAll('a[href*="/tran-dau/"]');
210
+ let bestA = null;
211
+ links.forEach(a => {
212
+ const href = a.getAttribute('href') || '';
213
+ // Prefer links with both event_id AND slug (fuller URL)
214
+ if(href.match(/\/tran-dau\/\d+\/(centre|preview|quan-cau|video)\//)) {
215
+ bestA = a;
216
+ } else if(!bestA && href.match(/\/tran-dau\/\d+\//)) {
217
+ bestA = a;
218
+ }
219
+ });
220
+ if(!bestA) return;
221
+ e.preventDefault();
222
+ e.stopPropagation();
223
+ const href = bestA.getAttribute('href') || '';
224
+ const m = href.match(/\/tran-dau\/(\d+)\//);
225
+ if(m){
226
+ const fullUrl = href.startsWith('http') ? href : 'https://bongda.com.vn' + href;
227
+ openMatch(m[1], fullUrl);
228
+ }
229
+ });
230
+ });
231
+ // Prevent default navigation on all links inside livescore (but let match-detail click handler work)
232
+ el.querySelectorAll('a').forEach(a=>{
233
+ a.addEventListener('click',e=>{
234
+ e.preventDefault();
235
+ e.stopPropagation();
236
+ });
237
+ });
238
+ }
239
+ function openMatch(id,url){if(!id)return;_currentEventId=id;if(url)_currentMatchUrl=url;document.getElementById('match-overlay').classList.add('active');document.body.style.overflow='hidden';loadMatchTab('detail')}
240
+ function closeMatch(){document.getElementById('match-overlay').classList.remove('active');document.body.style.overflow=''}
241
+ async function loadMatchTab(tab){document.querySelectorAll('.mo-tab').forEach(t=>t.classList.remove('active'));document.querySelectorAll('.mo-tab').forEach(t=>{if((tab==='comm'&&t.textContent==='Diễn biến')||(tab==='stats'&&t.textContent==='Thống kê')||(tab==='detail'&&t.textContent.includes('Chi tiết')))t.classList.add('active')});const el=document.getElementById('mo-body');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';try{let apiUrl;if(tab==='stats')apiUrl=`/api/match/${_currentEventId}/stats`;else if(tab==='comm')apiUrl=`/api/match/${_currentEventId}/commentaries`;else{apiUrl=`/api/match/${_currentEventId}/detail`;if(_currentMatchUrl)apiUrl+='?url='+encodeURIComponent(_currentMatchUrl)}const r=await fetch(apiUrl);if(!r.ok){el.innerHTML='<div class="loading">Lỗi máy chủ ('+r.status+')</div>';return}const d=await r.json();if(d.error){el.innerHTML='<div class="loading">'+esc(d.error)+'</div>';return}if(tab==='detail'&&typeof renderMatchDetail==='function'){renderMatchDetail(el,d);return}el.innerHTML=d.html||'<div class="loading">Không có dữ liệu</div>'}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
242
+ async function doInteract(videoId,type){try{const r=await fetch('/api/v2/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,type})});return await r.json();}catch(e){return null;}}
243
+ async function getInteractions(videoId){try{return await fetch('/api/v2/interactions?id='+encodeURIComponent(videoId)).then(r=>r.json());}catch(e){return{views:0,likes:0,comments:0};}}
244
+ async function getComments(videoId){try{const j=await fetch('/api/v2/comments?id='+encodeURIComponent(videoId)).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
245
+ async function postComment(videoId,text){try{const j=await fetch('/api/v2/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,text})}).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
246
+ function buildTikTokSlide(opts){return`<div class="tiktok-slide" data-vid="${esc(opts.videoId)}">${opts.vtag}<div class="tiktok-bottom"><span class="badge ${opts.badgeClass||'badge-fpt'}">${opts.badge||''}</span><p class="tiktok-title">${esc(opts.title)}</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation();doView('${esc(opts.videoId)}',this)"><div class="icon">👁</div><div class="count" id="vc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doLike('${esc(opts.videoId)}',this)"><div class="icon">❤️</div><div class="count" id="lc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();toggleComments('${esc(opts.videoId)}',${opts.idx})"><div class="icon">💬</div><div class="count" id="cc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doShare('${esc(opts.title)}','${esc(opts.shareUrl||'')}','')"><div class="icon">📤</div></button>${opts.extraBtn||''}</div><span class="tiktok-counter">${opts.idx+1}/${opts.total}</span><div class="inline-comments" id="cmt-inline-${opts.idx}" style="display:none"></div></div>`;}
247
+ async function doView(videoId,btn){const j=await doInteract(videoId,'view');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.views);}}
248
+ async function doLike(videoId,btn){const j=await doInteract(videoId,'like');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.likes);}}
249
+ function fmtNum(n){if(!n)return'0';if(n>=1000000)return(n/1000000).toFixed(1)+'M';if(n>=1000)return(n/1000).toFixed(1)+'K';return String(n);}
250
+ async function loadCounters(videoIds){for(let i=0;i<videoIds.length;i++){const id=videoIds[i];if(!id)continue;const j=await getInteractions(id);const vc=document.getElementById('vc-'+i);if(vc)vc.textContent=fmtNum(j.views);const lc=document.getElementById('lc-'+i);if(lc)lc.textContent=fmtNum(j.likes);const cc=document.getElementById('cc-'+i);if(cc)cc.textContent=fmtNum(j.comments);}}
251
+ async function toggleComments(videoId,idx){const panel=document.getElementById('cmt-inline-'+idx);if(!panel)return;if(panel.style.display!=='none'){panel.style.display='none';return;}panel.style.display='block';panel.innerHTML='<div style="padding:8px;color:#888;font-size:11px">Đang tải...</div>';const cmts=await getComments(videoId);renderInlineComments(panel,videoId,idx,cmts);}
252
+ function renderInlineComments(panel,videoId,idx,cmts){let h='<div class="inline-cmt-header"><span>💬 Bình luận</span><button onclick="document.getElementById(\'cmt-inline-'+idx+'\').style.display=\'none\'">✕</button></div><div class="inline-cmt-list">';if(cmts.length){cmts.slice(-30).forEach(c=>{h+=`<div class="inline-cmt-item"><span class="inline-cmt-time">${c.time||''}</span>${esc(c.text)}</div>`;});}else{h+='<div style="color:#777;font-size:11px;padding:4px">Chưa có bình luận</div>';}h+=`</div><div class="inline-cmt-input"><input id="cmt-input-${idx}" placeholder="Viết bình luận..." onkeydown="if(event.key==='Enter')submitInlineCmt('${esc(videoId)}',${idx})"><button onclick="submitInlineCmt('${esc(videoId)}',${idx})">Gửi</button></div>`;panel.innerHTML=h;const list=panel.querySelector('.inline-cmt-list');if(list)list.scrollTop=list.scrollHeight;}
253
+ async function submitInlineCmt(videoId,idx){const inp=document.getElementById('cmt-input-'+idx);if(!inp)return;const text=inp.value.trim();if(!text)return;inp.value='';inp.disabled=true;const cmts=await postComment(videoId,text);inp.disabled=false;const panel=document.getElementById('cmt-inline-'+idx);if(panel)renderInlineComments(panel,videoId,idx,cmts);const cc=document.getElementById('cc-'+idx);if(cc)cc.textContent=fmtNum(cmts.length);}
254
+ function initTikTokFeed(){const feed=document.getElementById('tiktok-feed');if(!feed)return;const slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{const v=sl.querySelector('video');const fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls&&!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){const hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,()=>v.play().catch(()=>{}));v._hls=hls}else if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;const vid=sl.dataset.vid;if(vid&&!sl._viewed){sl._viewed=true;doInteract(vid,'view').then(j=>{if(j){const vc=document.getElementById('vc-'+idx);if(vc)vc.textContent=fmtNum(j.views);}});}}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null}}if(fr&&fr.src)fr.src=''}});cur=i}let sT;feed.addEventListener('scroll',()=>{clearTimeout(sT);sT=setTimeout(()=>{const rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2;let best=-1,bestD=1e9;slides.forEach((sl,i)=>{const d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i}});if(best>=0)act(best)},150)});setTimeout(()=>act(0),400);slides.forEach(sl=>{const v=sl.querySelector('video');if(v)v.addEventListener('click',e=>{e.preventDefault();v.paused?v.play().catch(()=>{}):v.pause()})});const ids=[...slides].map(sl=>sl.dataset.vid||'');loadCounters(ids)}
255
+ async function openHighlightFeed(league,idx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';let articles=(_hlLeagueData||{})[league]||[];if(!articles.length){try{articles=await fetch('/api/highlights/'+league).then(r=>r.json())}catch(e){articles=[]}}if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return}const vids=[];const results=await Promise.all(articles.map(async(a,i)=>{try{const r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));const v=await r.json();if(v&&v.src)return{...a,...v,_idx:i}}catch(e){}return null}));results.forEach(r=>{if(r)vids.push(r)});vids.sort((a,b)=>a._idx-b._idx);if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return}let ti=vids.findIndex(v=>v._idx===idx);if(ti<0)ti=0;const ordered=ti>0?[...vids.slice(ti),...vids.slice(0,ti)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Highlight</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const isYT=v.type==='youtube',isHLS=!isYT&&v.src?.includes('.m3u8'),poster=v.poster?` poster="${v.poster}"`:'';const vtag=isYT?`<iframe data-yt-src="${v.src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`:isHLS?`<video playsinline preload="none"${poster} data-hls="${v.src}" loop controls></video>`:`<video playsinline preload="none"${poster} loop controls><source src="${v.src}" type="video/mp4"></video>`;const videoId='hl-'+league+'-'+(v.id||v._idx);h+=buildTikTokSlide({vtag,title:v.title,badge:'HL',badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:v.link||'',extraBtn:`<button class="tiktok-right-btn" onclick="event.stopPropagation();this.closest('.tiktok-slide').classList.toggle('ratio-wide')"><div class="icon">⬜</div><div class="count">16:9</div></button>`});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
256
+ async function openYTShortsFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=_shortsData.length?_shortsData:await fetch('/api/shorts').then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có shorts</div>';return}const ordered=startIdx>0?[...arts.slice(startIdx),...arts.slice(0,startIdx)]:arts;let h=`<button class="back-btn" onclick="switchCat('home')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const id=v.id||'';const src=`https://www.youtube.com/embed/${id}?autoplay=1&rel=0&playsinline=1`;const vtag=`<iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`;const badge=v.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';const videoId='yt-'+id;h+=buildTikTokSlide({vtag,title:v.title,badge,badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:'https://youtube.com/watch?v='+id});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
257
+ async function openShortAIFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const wall=(await fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];const vids=wall.filter(p=>p.video);if(!vids.length){el.innerHTML='<div class="loading">Chưa có Short AI</div>';return}const ordered=startIdx>0?[...vids.slice(startIdx),...vids.slice(0,startIdx)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((p,i)=>{const vtag=`<video src="${p.video}" playsinline loop controls></video>`;const videoId='ai-'+(p.id||i);h+=buildTikTokSlide({vtag,title:p.title,badge:'AI',badgeClass:'badge-ai',videoId,idx:i,total:ordered.length,shareUrl:SPACE});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
258
+ async function readArticle(url){showView('view-article');const el=document.getElementById('view-article');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/article?url='+encodeURIComponent(url));const data=await r.json();if(data&&!data.error&&data.body&&data.body.length){_currentArticle={url,data};let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><h1 class="article-title">${esc(data.title)}</h1>`;if(data.summary)h+=`<div class="article-summary">${esc(data.summary)}</div>`;const seen={};data.body.forEach(b=>{if(b.type==='p')h+=`<p class="article-p">${b.text}</p>`;else if(b.type==='img'&&b.src&&!seen[b.src]){seen[b.src]=1;h+=`<img class="article-img" src="${esc(b.src)}" onerror="this.style.display='none'">`}else if(b.type==='heading')h+=`<h2 class="article-h2">${esc(b.text)}</h2>`});h+=`<div class="article-actions"><button class="primary" onclick="rewriteArticle()">🤖 Rewrite AI đăng tường</button><button onclick="doShare('${esc(data.title)}','${esc(url)}','${esc(data.og_image||'')}')">📤</button><button onclick="window.open('${esc(url)}','_blank')">🔗 Gốc</button></div><div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="ask-q" placeholder="Hỏi về bài viết..."></textarea><button onclick="askAI()">Hỏi</button><div id="ask-a" class="article-ai-answer"></div></div></div>`;el.innerHTML=h;window.scrollTo(0,0);return;}}catch(e){}el.innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="loading"><p>Không đọc được.</p><a href="${esc(url)}" target="_blank" style="color:#5cb87a">Mở gốc →</a></div>`;}
259
+ async function rewriteArticle(){const url=_currentArticle?.url;if(!url)return;toast('⏳ Đang rewrite...');try{const r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,context:document.querySelector('.article-view')?.innerText?.slice(0,14000)||''})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng Tường AI!');if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
260
+ async function rewriteUrl(){const url=document.getElementById('url-input')?.value.trim();if(!url)return alert('Dán URL');toast('⏳ Đang rewrite...');try{const r=await fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng!');document.getElementById('url-input').value='';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
261
+ async function askAI(){const q=document.getElementById('ask-q')?.value.trim();if(!q)return alert('Nhập câu hỏi');const a=document.getElementById('ask-a');a.textContent='Đang hỏi...';try{const r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:_currentArticle?.url||'',question:q,context:document.querySelector('.article-view')?.innerText?.slice(0,12000)||''})});const j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}}
262
+ async function readWallPost(i){const p=_wallPosts[i];if(!p)return;showView('view-article');
263
+ const images = p.images || [];
264
+ let imgGallery = '';
265
+ if(images.length > 0){
266
+ imgGallery = '<div class="article-image-gallery">';
267
+ images.forEach((imgUrl, idx) => {
268
+ if(idx === 0){
269
+ imgGallery += `<img class="article-img article-hero-img" src="${esc(imgUrl)}" onerror="this.style.display='none" loading="eager">`;
270
+ } else {
271
+ if(idx === 1) imgGallery += '<div class="gallery-thumbs">';
272
+ imgGallery += `<div class="gallery-thumb"><img src="${esc(imgUrl)}" onerror="this.parentElement.style.display='none'" loading="lazy"></div>`;
273
+ }
274
+ });
275
+ if(images.length > 1) imgGallery += '</div>';
276
+ imgGallery += '</div>';
277
+ }
278
+ const hasVideo = p.video && p.video.length > 0;
279
+ const voiceOptions = [
280
+ {id:'hoaimy', label:'🎙️ Nữ — Hoài My'},
281
+ {id:'namminh', label:'🎙️ Nam — Nam Minh'},
282
+ ];
283
+ let voiceSelector = '';
284
+ if(!hasVideo){
285
+ voiceSelector = `<div class="tts-selector"><div class="tts-selector-label">🎙️ Chọn giọng đọc:</div><div class="tts-voice-btns">`;
286
+ voiceOptions.forEach(v=>{
287
+ voiceSelector += `<button class="tts-voice-btn" onclick="document.querySelectorAll('.tts-voice-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('selected-voice').value='${v.id}'">${v.label}</button>`;
288
+ });
289
+ voiceSelector += `</div><div class="tts-speed-row"><span>Tốc độ:</span><select id="selected-speed"><option value="1.0">1.0x — Bình thường</option><option value="1.2" selected>1.2x — Nhanh</option><option value="1.5">1.5x — Rất nhanh</option><option value="0.8">0.8x — Chậm</option></select></div>`;
290
+ voiceSelector += `<input type="hidden" id="selected-voice" value="hoaimy"></div>`;
291
+ }
292
+ document.getElementById('view-article').innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${imgGallery}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>${hasVideo?`<video class="article-img" src="${esc(p.video)}" controls playsinline style="max-height:400px"></video>`:''}<div class="article-actions">${hasVideo?`<button onclick="openShortAIFeed(${i})">🎬 Xem Short</button>${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🔄 Tạo lại Short</button>`:`${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🎬 Tạo Video Shorts</button>`}<button onclick="doShare('${esc(p.title)}','${SPACE}','${esc(p.img||'')}')">📤</button></div></div>`;
293
+ const firstVoiceBtn = document.querySelector('.tts-voice-btn');
294
+ if(firstVoiceBtn) firstVoiceBtn.classList.add('active');
295
+ window.scrollTo(0,0)}
296
+ async function loadNewsTab(){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/homepage');const news=await r.json();if(!news.length){el.innerHTML='<div class="loading">Không có tin</div>';return}const groups={};news.forEach(a=>{if(!groups[a.group])groups[a.group]=[];groups[a.group].push(a)});let h='';for(const[g,arts] of Object.entries(groups)){h+=`<div class="section-title">${g}</div><div class="grid">`;arts.slice(0,6).forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'VnE')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>'}el.innerHTML=h}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
297
+ async function loadCat(id){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=await fetch('/api/category/'+id).then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có tin</div>';return}let h='<div class="grid">';arts.forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>';el.innerHTML=h}
298
+ fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){const home=document.getElementById('view-home');if(home){const w=document.createElement('div');w.className='storage-warn';w.innerHTML='⚠️ Persistent Storage chưa bật.';home.prepend(w)}}}).catch(()=>{});
299
+
300
+ // === AUTO-OPEN SHARE LINKS (/s?url=... sets pending_article) ===
301
+ (function(){
302
+ try{
303
+ const pa=localStorage.getItem('pending_article');
304
+ const pv=localStorage.getItem('pending_video');
305
+ if(pa){
306
+ localStorage.removeItem('pending_article');
307
+ setTimeout(()=>{
308
+ if(typeof readArticle==='function') readArticle(pa);
309
+ },1500);
310
+ }
311
+ if(pv){
312
+ localStorage.removeItem('pending_video');
313
+ try{
314
+ const v=JSON.parse(pv);
315
+ if(v&&v.url) setTimeout(()=>{window.open(v.url,'_blank')},1500);
316
+ }catch(e){}
317
+ }
318
+ }catch(e){}
319
+ })();
static/index.html CHANGED
@@ -11,7 +11,6 @@
11
  <link rel="stylesheet" href="/static/wc2026.css">
12
  <script src="https://cdn.jsdelivr.net/npm/hls.js@1/dist/hls.min.js"></script>
13
  <style>
14
-
15
  *{box-sizing:border-box;margin:0;padding:0}body{background:#111;color:#eee;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;overflow-x:hidden}
16
  .header{background:linear-gradient(135deg,#0d1117,#1a3a2a 50%,#8b7500);padding:12px;text-align:center}.header h1{font-size:18px;color:#fff}.header p{font-size:10px;color:#aaa}
17
  .cats{display:flex;overflow-x:auto;background:#1a1a1a;border-bottom:1px solid #333;padding:0 4px;position:sticky;top:0;z-index:50;scrollbar-width:none}.cats::-webkit-scrollbar{display:none}
@@ -67,7 +66,6 @@
67
  .vtv-epg-item.now .epg-t{color:#aaccee}
68
  .vtv-epg-item .epg-n{color:#ccc;font-size:8px}
69
  .vtv-epg-item.now .epg-n{color:#fff}
70
-
71
  </style>
72
  </head>
73
  <body>
@@ -84,7 +82,6 @@
84
  <div class="mo-body" id="mo-body"><div class="loading">Đang tải...</div></div>
85
  </div>
86
  <div id="progress-toast"></div>
87
- <div id="progress-toast"></div>
88
  <script>
89
  var _cats=[],_hlLeagueData={},_currentArticle=null;window._currentEventId='';
90
  function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]))}
@@ -94,10 +91,679 @@ function toast(msg){let t=document.getElementById('progress-toast');if(t){t.text
94
  function doShare(title,url,img){const shareUrl=SPACE+'/s?url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title)+'&img='+encodeURIComponent(img||'');if(navigator.share)navigator.share({title,url:shareUrl}).catch(()=>{});else navigator.clipboard.writeText(shareUrl).then(()=>alert('Đã sao chép!')).catch(()=>{})}
95
  var SPACE=location.origin;
96
  </script>
97
- <script src="/static/app_v2.js?v=21"></script>
98
- <script src="/static/match_detail_v6.js"></script>
99
- <script src="/static/live_mode.js?v=21"></script>
100
- <script src="/static/hot_multi.js?v=21"></script>
101
- <script src="/static/wc2026_v2.js"></script>
102
- </body>
103
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  <link rel="stylesheet" href="/static/wc2026.css">
12
  <script src="https://cdn.jsdelivr.net/npm/hls.js@1/dist/hls.min.js"></script>
13
  <style>
 
14
  *{box-sizing:border-box;margin:0;padding:0}body{background:#111;color:#eee;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;overflow-x:hidden}
15
  .header{background:linear-gradient(135deg,#0d1117,#1a3a2a 50%,#8b7500);padding:12px;text-align:center}.header h1{font-size:18px;color:#fff}.header p{font-size:10px;color:#aaa}
16
  .cats{display:flex;overflow-x:auto;background:#1a1a1a;border-bottom:1px solid #333;padding:0 4px;position:sticky;top:0;z-index:50;scrollbar-width:none}.cats::-webkit-scrollbar{display:none}
 
66
  .vtv-epg-item.now .epg-t{color:#aaccee}
67
  .vtv-epg-item .epg-n{color:#ccc;font-size:8px}
68
  .vtv-epg-item.now .epg-n{color:#fff}
 
69
  </style>
70
  </head>
71
  <body>
 
82
  <div class="mo-body" id="mo-body"><div class="loading">Đang tải...</div></div>
83
  </div>
84
  <div id="progress-toast"></div>
 
85
  <script>
86
  var _cats=[],_hlLeagueData={},_currentArticle=null;window._currentEventId='';
87
  function esc(s){return String(s||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]))}
 
91
  function doShare(title,url,img){const shareUrl=SPACE+'/s?url='+encodeURIComponent(url)+'&title='+encodeURIComponent(title)+'&img='+encodeURIComponent(img||'');if(navigator.share)navigator.share({title,url:shareUrl}).catch(()=>{});else navigator.clipboard.writeText(shareUrl).then(()=>alert('Đã sao chép!')).catch(()=>{})}
92
  var SPACE=location.origin;
93
  </script>
94
+ <script>
95
+ // === VNEWS Frontend v2 - Full Functions ===
96
+ // Updated: Voice selector + speed control + image gallery + auto voice detect
97
+
98
+ // === LOAD HOME ===
99
+ async function loadHome(){
100
+ const[featured,sh,wall,hlLeagues,ai,wcData]=await Promise.all([
101
+ fetch('/api/livescore/featured').then(r=>r.json()).catch(()=>null),
102
+ fetch('/api/shorts').then(r=>r.json()).catch(()=>[]),
103
+ fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]})),
104
+ fetch('/api/highlights/leagues').then(r=>r.json()).catch(()=>({})),
105
+ fetch('/api/genk_ai').then(r=>r.json()).catch(()=>[]),
106
+ fetch('/api/wc2026').then(r=>r.json()).catch(()=>null)
107
+ ]);
108
+ _hlLeagueData=hlLeagues;
109
+ _wc2026Data=wcData;
110
+ _shortsData=interleaveShorts(sh||[]);
111
+ _wallPosts=(wall&&wall.posts)||[];
112
+ let h='';
113
+ if(featured&&featured.home){
114
+ const sc=featured.status==='live'?'':'upcoming';
115
+ const st=featured.status==='live'?`🔴 ${featured.minute||'LIVE'}`:`⏰ ${featured.time}`;
116
+ // Safely encode for HTML attribute: escape quotes, angle brackets, ampersands
117
+ const eid = String(featured.event_id||'').replace(/[<>&"']/g,'');
118
+ const mUrl = String(featured.url||'').replace(/[<>&"']/g,'');
119
+ const fHome = String(featured.home||'').replace(/[<>&"']/g,'');
120
+ const fAway = String(featured.away||'').replace(/[<>&"']/g,'');
121
+ const fLeague = String(featured.league||'').replace(/[<>&"']/g,'');
122
+ const fScore = String(featured.score||'VS').replace(/[<>&"']/g,'');
123
+ const fHomeLogo = String(featured.home_logo||'').replace(/[<>&"']/g,'');
124
+ const fAwayLogo = String(featured.away_logo||'').replace(/[<>&"']/g,'');
125
+ const safeTitle = `${fHome} vs ${fAway} — ${fLeague}`;
126
+ h+=`<div class="featured-match" data-event-id="${eid}" data-url="${mUrl}" onclick="openMatch('${eid}','${mUrl}')" title="${safeTitle}">`+
127
+ `<div class="fm-league">${fLeague}</div>`+
128
+ `<div class="fm-teams">`+
129
+ `<div class="fm-team"><img src="${fHomeLogo}" onerror="this.style.display='none'"><span>${fHome}</span></div>`+
130
+ `<div class="fm-score">${fScore}</div>`+
131
+ `<div class="fm-team"><img src="${fAwayLogo}" onerror="this.style.display='none'"><span>${fAway}</span></div>`+
132
+ `</div>`+
133
+ `<div class="fm-status ${sc}">${st}</div>`+
134
+ `</div>`;
135
+ }
136
+ h+=`<div class="ai-compose"><div class="ai-compose-title">🤖 AI viết bài</div><div class="ai-compose-row"><input id="topic-input" placeholder="Nhập chủ đề..."><button onclick="searchTopic()">Tìm nguồn</button></div><div class="ai-compose-row"><input id="url-input" placeholder="Dán URL bài viết..."><button class="secondary" onclick="rewriteUrl()">Rewrite</button></div><div id="hot-topics" class="hot-topic-row"></div></div>`;
137
+ h+='<div id="hashtag-box"></div>';
138
+ h+=`<div class="ls-section"><div class="ls-header"><h3>⚽ Livescore</h3></div><div class="ls-tabs"><span class="ls-tab active" data-tab="today" onclick="loadLivescore('today')">📅 Hôm nay</span><span class="ls-tab" data-tab="live" onclick="loadLivescore('live')">🔴 Live</span><span class="ls-tab" data-tab="incoming" onclick="loadLivescore('incoming')">⏰ Sắp tới</span><span class="ls-tab" data-tab="results" onclick="loadLivescore('results')">✅ Kết quả</span><span class="ls-tab" data-tab="bxh_nha" onclick="loadLivescore('bxh_nha')">🏆 NHA</span><span class="ls-tab" data-tab="bxh_laliga" onclick="loadLivescore('bxh_laliga')">🏆 La Liga</span></div><div class="ls-content" id="ls-content"><div class="loading">Đang tải...</div></div></div>`;
139
+ h+=`<div id="wc2026-live-section" class="wc2026-section"><div class="wc-header"><h2>🏆 World Cup 2026</h2><span class="wc-live-badge">● LIVE</span></div><div class="wc-tabs"><span class="wc-tab active" onclick="switchWCTab('news')">📰 Tin tức</span><span class="wc-tab" onclick="switchWCTab('fixtures')">📅 Lịch thi đấu</span><span class="wc-tab" onclick="switchWCTab('standings')">🏆 BXH</span><span class="wc-tab" onclick="switchWCTab('highlights')">🎬 Highlight</span><span class="wc-tab" onclick="switchWCTab('stats')">📊 Thống kê</span></div><div class="wc-content" id="wc-content"><div class="loading">Đang tải World Cup 2026...</div></div></div>`;
140
+ const wallPosts=_wallPosts;
141
+ const aiShorts=wallPosts.filter(p=>p.video);
142
+ if(aiShorts.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🎬 Short AI</span></div><div class="slider-track">';aiShorts.slice(0,20).forEach((p,i)=>{h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${p.video}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`});h+='</div></div>';}
143
+ if(_shortsData.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">📱 Shorts Dân trí & SKĐS</span><span class="slider-note">Mới nhất · xen kẽ</span></div><div class="slider-track">';_shortsData.slice(0,30).forEach((a,i)=>{const badge=a.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';h+=`<div class="slider-item shorts-item" onclick="openYTShortsFeed(${i})"><div class="slider-thumb shorts-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title"><span style="color:#f0c040;font-size:8px">${badge}</span> ${esc(a.title)}</div></div>`});h+='</div></div>';}
144
+ if(wallPosts.length){h+=`<div class="slider-wrap" id="ai-wall-wrap"><div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">`;wallPosts.slice(0,20).forEach((p,i)=>{h+=makeWallItem(p,i)});h+='</div></div>';}
145
+ const HL_CONFIG={"world-cup":{name:"World Cup 2026",emoji:"🌍"},"premier-league":{name:"Premier League",emoji:"🏴"},"champions-league":{name:"Champions League",emoji:"⭐"},"la-liga":{name:"La Liga",emoji:"🇪🇸"},"serie-a":{name:"Serie A",emoji:"🇮🇹"},"bundesliga":{name:"Bundesliga",emoji:"🇩🇪"},"friendly":{name:"Giao hữu",emoji:"🤝"}};
146
+ for(const[key,cfg] of Object.entries(HL_CONFIG)){const vids=hlLeagues[key];if(!vids||!vids.length)continue;h+=`<div class="slider-wrap"><div class="slider-header"><span class="slider-label">${cfg.emoji} ${cfg.name}</span></div><div class="slider-track">`;vids.slice(0,8).forEach((a,i)=>{h+=`<div class="slider-item" onclick="openHighlightFeed('${key}',${i})"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}<div class="card-play">▶</div></div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
147
+ if(ai&&ai.length){h+='<div class="slider-wrap"><div class="slider-header"><span class="slider-label">🤖 Ứng dụng AI</span></div><div class="slider-track">';ai.slice(0,12).forEach(a=>{h+=`<div class="slider-item" onclick="readArticle('${esc(a.link)}')"><div class="slider-thumb">${a.img?`<img src="${a.img}">`:''}</div><div class="slider-title">${esc(a.title)}</div></div>`});h+='</div></div>';}
148
+ document.getElementById('view-home').innerHTML=h;
149
+ loadLivescore('today');loadHotTopics();
150
+ if(_wc2026Data)switchWCTab('news');
151
+ }
152
+
153
+ // === WALL POST HELPERS ===
154
+ function makeWallItem(p,i){
155
+ const hasVideo = p.video && p.video.length > 0;
156
+ const thumbContent = p.img
157
+ ? `<img src="${esc(p.img)}" onerror="this.style.display='none'">`
158
+ : (hasVideo ? `<video src="${esc(p.video)}" muted></video>` : '');
159
+ const videoBadge = hasVideo
160
+ ? `<div class="wall-video-badge">🎬</div>`
161
+ : '';
162
+ const videoBtn = hasVideo
163
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(${i})">▶ Xem Short</button>`
164
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(p.id||i)}',this)">🎬 Tạo Video</button>`;
165
+
166
+ return `<div class="wall-item" id="wall-item-${esc(p.id||i)}">
167
+ <div class="wall-thumb">
168
+ ${thumbContent}
169
+ ${videoBadge}
170
+ </div>
171
+ <div class="wall-title">${esc(p.title)}</div>
172
+ <div class="wall-text">${esc((p.text||'').slice(0,180))}</div>
173
+ <div class="wall-actions">
174
+ <button class="primary" onclick="readWallPost(${i})">Xem</button>
175
+ ${videoBtn}
176
+ </div>
177
+ </div>`;
178
+ }
179
+
180
+ // === GENERATE SHORT VIDEO FOR A WALL POST ===
181
+ async function makeShortVideo(postId, btn, voice, speed){
182
+ if(!postId)return;
183
+ const origText = btn ? btn.textContent : '🎬 Tạo Video';
184
+ if(btn){btn.disabled=true;btn.textContent='⏳ Đang tạo...';}
185
+ toast('⏳ Đang tạo video shorts...');
186
+ try{
187
+ let url = '/api/ai/short/'+encodeURIComponent(postId);
188
+ const params = [];
189
+ if(voice) params.push('voice='+encodeURIComponent(voice));
190
+ if(speed) params.push('speed='+encodeURIComponent(speed));
191
+ if(params.length) url += '?' + params.join('&');
192
+ const r = await fetch(url, {method:'POST'});
193
+ const j = await r.json();
194
+ if(!r.ok || j.error) throw new Error(j.error||'Lỗi tạo video');
195
+ toast('✅ Đã tạo video shorts!');
196
+ const p = _wallPosts.find(x => String(x.id) === String(postId));
197
+ if(p){
198
+ p.video = j.video;
199
+ const itemId = 'wall-item-'+postId;
200
+ const el = document.getElementById(itemId);
201
+ if(el){
202
+ const idx = _wallPosts.indexOf(p);
203
+ el.outerHTML = makeWallItem(p, idx);
204
+ const newEl = document.getElementById(itemId);
205
+ if(newEl) newEl.className = 'wall-item wall-item-new';
206
+ }
207
+ }
208
+ refreshShortAISlider();
209
+ }catch(e){
210
+ toast('❌ '+e.message);
211
+ if(btn){btn.disabled=false;btn.textContent=origText;}
212
+ }
213
+ }
214
+
215
+ // Refresh Short AI slider after video generation
216
+ function refreshShortAISlider(){
217
+ const aiShorts = _wallPosts.filter(p=>p.video);
218
+ let shortAISection = document.getElementById('short-ai-section');
219
+ if(aiShorts.length === 0){
220
+ if(shortAISection) shortAISection.remove();
221
+ return;
222
+ }
223
+ if(shortAISection){
224
+ const track = shortAISection.querySelector('.slider-track');
225
+ if(track){
226
+ let h = '';
227
+ aiShorts.slice(0,20).forEach((p,i)=>{
228
+ h+=`<div class="slider-item shorts-item" onclick="openShortAIFeed(${i})"><div class="slider-thumb shorts-thumb"><video src="${esc(p.video)}" muted preload="metadata"></video><div class="card-play">▶</div></div><div class="slider-title">${esc(p.title)}</div></div>`;
229
+ });
230
+ track.innerHTML = h;
231
+ }
232
+ }
233
+ }
234
+
235
+ function prependWallPost(post){
236
+ _wallPosts.unshift(post);
237
+ const track=document.getElementById('ai-wall-track');
238
+ const wrap=document.getElementById('ai-wall-wrap');
239
+ const homeEl=document.getElementById('view-home');
240
+ if(!track||!wrap){
241
+ if(homeEl){
242
+ let insertBefore=homeEl.querySelector('.slider-wrap');
243
+ const newWrap=document.createElement('div');
244
+ newWrap.className='slider-wrap';
245
+ newWrap.id='ai-wall-wrap';
246
+ newWrap.innerHTML=`<div class="slider-header"><span class="slider-label">🧱 Tường AI</span></div><div class="slider-track" id="ai-wall-track">${makeWallItem(post,0)}</div>`;
247
+ if(insertBefore){
248
+ homeEl.insertBefore(newWrap,insertBefore);
249
+ }else{
250
+ homeEl.appendChild(newWrap);
251
+ }
252
+ const firstItem=newWrap.querySelector('.wall-item');
253
+ if(firstItem)firstItem.className='wall-item wall-item-new';
254
+ }
255
+ return;
256
+ }
257
+ const div=document.createElement('div');
258
+ div.className='wall-item wall-item-new';
259
+ div.id='wall-item-'+(post.id||'new-'+Date.now());
260
+ const hasVideo = post.video && post.video.length > 0;
261
+ const thumbContent = post.img
262
+ ? `<img src="${esc(post.img)}" onerror="this.style.display='none'">`
263
+ : (hasVideo ? `<video src="${esc(post.video)}" muted></video>` : '');
264
+ const videoBadge = hasVideo ? `<div class="wall-video-badge">🎬</div>` : '';
265
+ const videoBtn = hasVideo
266
+ ? `<button class="wall-btn-video" onclick="event.stopPropagation();openShortAIFeed(0)">▶ Xem Short</button>`
267
+ : `<button class="wall-btn-make" onclick="event.stopPropagation();makeShortVideo('${esc(post.id)}',this)">🎬 Tạo Video</button>`;
268
+ div.innerHTML=`<div class="wall-thumb">${thumbContent}${videoBadge}</div><div class="wall-title">${esc(post.title)}</div><div class="wall-text">${esc((post.text||'').slice(0,180))}</div><div class="wall-actions"><button class="primary" onclick="readWallPost(0)">Xem</button>${videoBtn}</div>`;
269
+ track.prepend(div);
270
+ track.scrollTo({left:0,behavior:'smooth'});
271
+ if(hasVideo) refreshShortAISlider();
272
+ }
273
+
274
+ // === REST OF FUNCTIONS ===
275
+ let _shortsData=[];
276
+ let _wallPosts=[];
277
+ let _currentView='home';
278
+ let _currentEventId=null;
279
+ let _currentMatchUrl=null;
280
+ function interleaveShorts(shorts){const dt=shorts.filter(s=>s.channel==='baodantri7941');const sk=shorts.filter(s=>s.channel==='baosuckhoedoisongboyte');const result=[];let i=0,j=0;while(i<dt.length||j<sk.length){if(i<dt.length)result.push(dt[i++]);if(j<sk.length)result.push(sk[j++]);}return result;}
281
+ let _htPage=0,_htTopic='';
282
+ async function loadHotTopics(){const j=await fetch('/api/hot_topics').then(r=>r.json()).catch(()=>({topics:[]}));const el=document.getElementById('hot-topics');if(!el)return;el.innerHTML=(j.topics||[]).slice(0,18).map(t=>{const topicText=t.topic||t.label.replace(/^#/,'');return`<button class="hot-chip" onclick="searchTopic('${topicText.replace(/'/g,"\\'")}')">${esc(t.label)}</button>`;}).join('');if(j.topics&&j.topics[0]){const firstTopic=j.topics[0].topic||j.topics[0].label.replace(/^#/,'');setTimeout(()=>searchTopic(firstTopic),800);}}
283
+ function searchTopic(topic){if(!topic){topic=document.getElementById('topic-input')?.value.trim();if(!topic){alert('Nhập chủ đề');return;}}document.getElementById('topic-input').value='';_htTopic=topic;_htPage=0;showHashtagSources(topic,0);}
284
+ async function showHashtagSources(topic,page){const box=document.getElementById('hashtag-box');if(!box)return;if(page===0)box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div class="hashtag-loading"><div class="hashtag-spinner"></div>Đang tìm...</div></div>`;try{const r=await fetch(`/api/hashtag/sources?topic=${encodeURIComponent(topic)}&page=${page}`);const j=await r.json();const sources=j.sources||[];if(!sources.length&&page===0){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#888;padding:8px">Không tìm được bài viết liên quan</div></div>`;return;}let h='';if(page===0)h=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)} <span style="font-size:10px;color:#888">(${j.total} bài từ 8 nguồn)</span></h3><div id="ht-list">`;sources.forEach((s,i)=>{const idx=page*8+i;h+=`<div class="hashtag-src-item" onclick="readArticle('${esc(s.url)}')"><div class="hashtag-src-img" id="ht-img-${idx}"></div><div class="hashtag-src-text"><div class="hashtag-src-title">${esc(s.title)}</div><div class="hashtag-src-via">${esc(s.via||'')}</div></div></div>`;});if(page===0){h+=`</div><button class="hashtag-rewrite-btn" onclick="rewriteHashtag('${esc(topic).replace(/'/g,"\\'")}')">🤖 Rewrite AI tổng hợp & đăng tường</button>`;if(j.has_more)h+=`<button class="hashtag-load-more" id="ht-more" onclick="loadMoreHashtag()">Tải thêm ▼</button>`;h+=`</div>`;box.innerHTML=h;}else{document.getElementById('ht-list')?.insertAdjacentHTML('beforeend',h);const btn=document.getElementById('ht-more');if(btn){if(!j.has_more)btn.remove();else{btn.disabled=false;btn.textContent='Tải thêm ▼';}}}sources.forEach((s,i)=>{const idx=page*8+i;if(!s.url)return;fetch('/api/article?url='+encodeURIComponent(s.url)).then(r=>r.json()).then(d=>{if(d&&(d.og_image||d.img)){const el=document.getElementById('ht-img-'+idx);if(el)el.innerHTML=`<img src="${esc(d.og_image||d.img)}" onerror="this.style.display='none'">`;}}).catch(()=>{});});}catch(e){box.innerHTML=`<div class="hashtag-sources"><h3>🔍 ${esc(topic)}</h3><div style="color:#e74c3c;padding:8px">Lỗi: ${esc(e.message)}</div></div>`;}}
285
+ function loadMoreHashtag(){_htPage++;const btn=document.getElementById('ht-more');if(btn){btn.disabled=true;btn.textContent='Đang tải...';}showHashtagSources(_htTopic,_htPage);}
286
+ async function rewriteHashtag(topic){const btn=event?.target;if(btn){btn.disabled=true;btn.textContent='Đang tổng hợp...';}try{const r=await fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error||'Lỗi');toast('✅ Đã đăng Tường AI!');if(btn)btn.textContent='✅ Đăng thành công!';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message);if(btn){btn.disabled=false;btn.textContent='🤖 Rewrite AI';}}}
287
+ async function loadLivescore(tab){document.querySelectorAll('.ls-tab').forEach(t=>t.classList.remove('active'));document.querySelector(`.ls-tab[data-tab="${tab}"]`)?.classList.add('active');const el=document.getElementById('ls-content');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';let ep='/api/livescore/'+tab;if(tab.startsWith('bxh_'))ep='/api/livescore/standings/'+tab.replace('bxh_','');try{const r=await fetch(ep);const d=await r.json();el.innerHTML=d.html&&d.html.length>50?d.html:'<div class="loading">Không có dữ liệu</div>';bindMatchClicks(el);}catch(e){el.innerHTML='<div class="loading">Lỗi</div>';}}
288
+ function bindMatchClicks(el){
289
+ if(!el) return;
290
+ el.querySelectorAll('.match-detail').forEach(md=>{
291
+ md.style.cursor='pointer';
292
+ // Remove old listeners to avoid duplicates (mark as bound)
293
+ if(md._bound) return;
294
+ md._bound = true;
295
+ md.addEventListener('click',function(e){
296
+ // Don't intercept clicks on interactive elements inside the row
297
+ const tag = e.target.tagName?.toLowerCase();
298
+ if(tag === 'a' || tag === 'button' || tag === 'input') {
299
+ e.preventDefault();
300
+ e.stopPropagation();
301
+ }
302
+ // Find ANY link with /tran-dau/ inside this match-detail row
303
+ const links = this.querySelectorAll('a[href*="/tran-dau/"]');
304
+ let bestA = null;
305
+ links.forEach(a => {
306
+ const href = a.getAttribute('href') || '';
307
+ // Prefer links with both event_id AND slug (fuller URL)
308
+ if(href.match(/\/tran-dau\/\d+\/(centre|preview|quan-cau|video)\//)) {
309
+ bestA = a;
310
+ } else if(!bestA && href.match(/\/tran-dau\/\d+\//)) {
311
+ bestA = a;
312
+ }
313
+ });
314
+ if(!bestA) return;
315
+ e.preventDefault();
316
+ e.stopPropagation();
317
+ const href = bestA.getAttribute('href') || '';
318
+ const m = href.match(/\/tran-dau\/(\d+)\//);
319
+ if(m){
320
+ const fullUrl = href.startsWith('http') ? href : 'https://bongda.com.vn' + href;
321
+ openMatch(m[1], fullUrl);
322
+ }
323
+ });
324
+ });
325
+ // Prevent default navigation on all links inside livescore (but let match-detail click handler work)
326
+ el.querySelectorAll('a').forEach(a=>{
327
+ a.addEventListener('click',e=>{
328
+ e.preventDefault();
329
+ e.stopPropagation();
330
+ });
331
+ });
332
+ }
333
+ function openMatch(id,url){if(!id)return;_currentEventId=id;if(url)_currentMatchUrl=url;document.getElementById('match-overlay').classList.add('active');document.body.style.overflow='hidden';loadMatchTab('detail')}
334
+ function closeMatch(){document.getElementById('match-overlay').classList.remove('active');document.body.style.overflow=''}
335
+ async function loadMatchTab(tab){document.querySelectorAll('.mo-tab').forEach(t=>t.classList.remove('active'));document.querySelectorAll('.mo-tab').forEach(t=>{if((tab==='comm'&&t.textContent==='Diễn biến')||(tab==='stats'&&t.textContent==='Thống kê')||(tab==='detail'&&t.textContent.includes('Chi tiết')))t.classList.add('active')});const el=document.getElementById('mo-body');if(!el)return;el.innerHTML='<div class="loading">Đang tải...</div>';try{let apiUrl;if(tab==='stats')apiUrl=`/api/match/${_currentEventId}/stats`;else if(tab==='comm')apiUrl=`/api/match/${_currentEventId}/commentaries`;else{apiUrl=`/api/match/${_currentEventId}/detail`;if(_currentMatchUrl)apiUrl+='?url='+encodeURIComponent(_currentMatchUrl)}const r=await fetch(apiUrl);if(!r.ok){el.innerHTML='<div class="loading">Lỗi máy chủ ('+r.status+')</div>';return}const d=await r.json();if(d.error){el.innerHTML='<div class="loading">'+esc(d.error)+'</div>';return}if(tab==='detail'&&typeof renderMatchDetail==='function'){renderMatchDetail(el,d);return}el.innerHTML=d.html||'<div class="loading">Không có dữ liệu</div>'}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
336
+ async function doInteract(videoId,type){try{const r=await fetch('/api/v2/interact',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,type})});return await r.json();}catch(e){return null;}}
337
+ async function getInteractions(videoId){try{return await fetch('/api/v2/interactions?id='+encodeURIComponent(videoId)).then(r=>r.json());}catch(e){return{views:0,likes:0,comments:0};}}
338
+ async function getComments(videoId){try{const j=await fetch('/api/v2/comments?id='+encodeURIComponent(videoId)).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
339
+ async function postComment(videoId,text){try{const j=await fetch('/api/v2/comment',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({id:videoId,text})}).then(r=>r.json());return j.comments||[];}catch(e){return[];}}
340
+ function buildTikTokSlide(opts){return`<div class="tiktok-slide" data-vid="${esc(opts.videoId)}">${opts.vtag}<div class="tiktok-bottom"><span class="badge ${opts.badgeClass||'badge-fpt'}">${opts.badge||''}</span><p class="tiktok-title">${esc(opts.title)}</p></div><div class="tiktok-right"><button class="tiktok-right-btn" onclick="event.stopPropagation();doView('${esc(opts.videoId)}',this)"><div class="icon">👁</div><div class="count" id="vc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doLike('${esc(opts.videoId)}',this)"><div class="icon">❤️</div><div class="count" id="lc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();toggleComments('${esc(opts.videoId)}',${opts.idx})"><div class="icon">💬</div><div class="count" id="cc-${opts.idx}">0</div></button><button class="tiktok-right-btn" onclick="event.stopPropagation();doShare('${esc(opts.title)}','${esc(opts.shareUrl||'')}','')"><div class="icon">📤</div></button>${opts.extraBtn||''}</div><span class="tiktok-counter">${opts.idx+1}/${opts.total}</span><div class="inline-comments" id="cmt-inline-${opts.idx}" style="display:none"></div></div>`;}
341
+ async function doView(videoId,btn){const j=await doInteract(videoId,'view');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.views);}}
342
+ async function doLike(videoId,btn){const j=await doInteract(videoId,'like');if(j){const c=btn.querySelector('.count');if(c)c.textContent=fmtNum(j.likes);}}
343
+ function fmtNum(n){if(!n)return'0';if(n>=1000000)return(n/1000000).toFixed(1)+'M';if(n>=1000)return(n/1000).toFixed(1)+'K';return String(n);}
344
+ async function loadCounters(videoIds){for(let i=0;i<videoIds.length;i++){const id=videoIds[i];if(!id)continue;const j=await getInteractions(id);const vc=document.getElementById('vc-'+i);if(vc)vc.textContent=fmtNum(j.views);const lc=document.getElementById('lc-'+i);if(lc)lc.textContent=fmtNum(j.likes);const cc=document.getElementById('cc-'+i);if(cc)cc.textContent=fmtNum(j.comments);}}
345
+ async function toggleComments(videoId,idx){const panel=document.getElementById('cmt-inline-'+idx);if(!panel)return;if(panel.style.display!=='none'){panel.style.display='none';return;}panel.style.display='block';panel.innerHTML='<div style="padding:8px;color:#888;font-size:11px">Đang tải...</div>';const cmts=await getComments(videoId);renderInlineComments(panel,videoId,idx,cmts);}
346
+ function renderInlineComments(panel,videoId,idx,cmts){let h='<div class="inline-cmt-header"><span>💬 Bình luận</span><button onclick="document.getElementById(\'cmt-inline-'+idx+'\').style.display=\'none\'">✕</button></div><div class="inline-cmt-list">';if(cmts.length){cmts.slice(-30).forEach(c=>{h+=`<div class="inline-cmt-item"><span class="inline-cmt-time">${c.time||''}</span>${esc(c.text)}</div>`;});}else{h+='<div style="color:#777;font-size:11px;padding:4px">Chưa có bình luận</div>';}h+=`</div><div class="inline-cmt-input"><input id="cmt-input-${idx}" placeholder="Viết bình luận..." onkeydown="if(event.key==='Enter')submitInlineCmt('${esc(videoId)}',${idx})"><button onclick="submitInlineCmt('${esc(videoId)}',${idx})">Gửi</button></div>`;panel.innerHTML=h;const list=panel.querySelector('.inline-cmt-list');if(list)list.scrollTop=list.scrollHeight;}
347
+ async function submitInlineCmt(videoId,idx){const inp=document.getElementById('cmt-input-'+idx);if(!inp)return;const text=inp.value.trim();if(!text)return;inp.value='';inp.disabled=true;const cmts=await postComment(videoId,text);inp.disabled=false;const panel=document.getElementById('cmt-inline-'+idx);if(panel)renderInlineComments(panel,videoId,idx,cmts);const cc=document.getElementById('cc-'+idx);if(cc)cc.textContent=fmtNum(cmts.length);}
348
+ function initTikTokFeed(){const feed=document.getElementById('tiktok-feed');if(!feed)return;const slides=feed.querySelectorAll('.tiktok-slide');let cur=-1;function act(i){if(i===cur)return;slides.forEach((sl,idx)=>{const v=sl.querySelector('video');const fr=sl.querySelector('iframe');if(idx===i){if(v&&v.dataset.hls&&!v._hls&&typeof Hls!=='undefined'&&Hls.isSupported()){const hls=new Hls();hls.loadSource(v.dataset.hls);hls.attachMedia(v);hls.on(Hls.Events.MANIFEST_PARSED,()=>v.play().catch(()=>{}));v._hls=hls}else if(v)v.play().catch(()=>{});if(fr&&!fr.src&&fr.dataset.ytSrc)fr.src=fr.dataset.ytSrc;const vid=sl.dataset.vid;if(vid&&!sl._viewed){sl._viewed=true;doInteract(vid,'view').then(j=>{if(j){const vc=document.getElementById('vc-'+idx);if(vc)vc.textContent=fmtNum(j.views);}});}}else{if(v){v.pause();if(v._hls){v._hls.destroy();v._hls=null}}if(fr&&fr.src)fr.src=''}});cur=i}let sT;feed.addEventListener('scroll',()=>{clearTimeout(sT);sT=setTimeout(()=>{const rect=feed.getBoundingClientRect(),ctr=rect.top+rect.height/2;let best=-1,bestD=1e9;slides.forEach((sl,i)=>{const d=Math.abs(sl.getBoundingClientRect().top+sl.getBoundingClientRect().height/2-ctr);if(d<bestD){bestD=d;best=i}});if(best>=0)act(best)},150)});setTimeout(()=>act(0),400);slides.forEach(sl=>{const v=sl.querySelector('video');if(v)v.addEventListener('click',e=>{e.preventDefault();v.paused?v.play().catch(()=>{}):v.pause()})});const ids=[...slides].map(sl=>sl.dataset.vid||'');loadCounters(ids)}
349
+ async function openHighlightFeed(league,idx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';let articles=(_hlLeagueData||{})[league]||[];if(!articles.length){try{articles=await fetch('/api/highlights/'+league).then(r=>r.json())}catch(e){articles=[]}}if(!articles.length){el.innerHTML='<div class="loading">Không có video</div>';return}const vids=[];const results=await Promise.all(articles.map(async(a,i)=>{try{const r=await fetch('/api/video_url?url='+encodeURIComponent(a.link));const v=await r.json();if(v&&v.src)return{...a,...v,_idx:i}}catch(e){}return null}));results.forEach(r=>{if(r)vids.push(r)});vids.sort((a,b)=>a._idx-b._idx);if(!vids.length){el.innerHTML='<div class="loading">Không tìm thấy video</div>';return}let ti=vids.findIndex(v=>v._idx===idx);if(ti<0)ti=0;const ordered=ti>0?[...vids.slice(ti),...vids.slice(0,ti)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Highlight</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const isYT=v.type==='youtube',isHLS=!isYT&&v.src?.includes('.m3u8'),poster=v.poster?` poster="${v.poster}"`:'';const vtag=isYT?`<iframe data-yt-src="${v.src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`:isHLS?`<video playsinline preload="none"${poster} data-hls="${v.src}" loop controls></video>`:`<video playsinline preload="none"${poster} loop controls><source src="${v.src}" type="video/mp4"></video>`;const videoId='hl-'+league+'-'+(v.id||v._idx);h+=buildTikTokSlide({vtag,title:v.title,badge:'HL',badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:v.link||'',extraBtn:`<button class="tiktok-right-btn" onclick="event.stopPropagation();this.closest('.tiktok-slide').classList.toggle('ratio-wide')"><div class="icon">⬜</div><div class="count">16:9</div></button>`});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
350
+ async function openYTShortsFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=_shortsData.length?_shortsData:await fetch('/api/shorts').then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có shorts</div>';return}const ordered=startIdx>0?[...arts.slice(startIdx),...arts.slice(0,startIdx)]:arts;let h=`<button class="back-btn" onclick="switchCat('home')">← Shorts</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((v,i)=>{const id=v.id||'';const src=`https://www.youtube.com/embed/${id}?autoplay=1&rel=0&playsinline=1`;const vtag=`<iframe data-yt-src="${src}" allowfullscreen allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture"></iframe>`;const badge=v.channel==='baosuckhoedoisongboyte'?'SKĐS':'Dân trí';const videoId='yt-'+id;h+=buildTikTokSlide({vtag,title:v.title,badge,badgeClass:'badge-fpt',videoId,idx:i,total:ordered.length,shareUrl:'https://youtube.com/watch?v='+id});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
351
+ async function openShortAIFeed(startIdx){showView('view-tiktok');const el=document.getElementById('view-tiktok');el.innerHTML='<div class="loading">Đang tải...</div>';const wall=(await fetch('/api/wall').then(r=>r.json()).catch(()=>({posts:[]}))).posts||[];const vids=wall.filter(p=>p.video);if(!vids.length){el.innerHTML='<div class="loading">Chưa có Short AI</div>';return}const ordered=startIdx>0?[...vids.slice(startIdx),...vids.slice(0,startIdx)]:vids;let h=`<button class="back-btn" onclick="switchCat('home')">← Short AI</button><div class="tiktok-container"><div class="tiktok-feed" id="tiktok-feed">`;ordered.forEach((p,i)=>{const vtag=`<video src="${p.video}" playsinline loop controls></video>`;const videoId='ai-'+(p.id||i);h+=buildTikTokSlide({vtag,title:p.title,badge:'AI',badgeClass:'badge-ai',videoId,idx:i,total:ordered.length,shareUrl:SPACE});});h+='</div></div>';el.innerHTML=h;initTikTokFeed();}
352
+ async function readArticle(url){showView('view-article');const el=document.getElementById('view-article');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/article?url='+encodeURIComponent(url));const data=await r.json();if(data&&!data.error&&data.body&&data.body.length){_currentArticle={url,data};let h=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><h1 class="article-title">${esc(data.title)}</h1>`;if(data.summary)h+=`<div class="article-summary">${esc(data.summary)}</div>`;const seen={};data.body.forEach(b=>{if(b.type==='p')h+=`<p class="article-p">${b.text}</p>`;else if(b.type==='img'&&b.src&&!seen[b.src]){seen[b.src]=1;h+=`<img class="article-img" src="${esc(b.src)}" onerror="this.style.display='none'">`}else if(b.type==='heading')h+=`<h2 class="article-h2">${esc(b.text)}</h2>`});h+=`<div class="article-actions"><button class="primary" onclick="rewriteArticle()">🤖 Rewrite AI đăng tường</button><button onclick="doShare('${esc(data.title)}','${esc(url)}','${esc(data.og_image||'')}')">📤</button><button onclick="window.open('${esc(url)}','_blank')">🔗 Gốc</button></div><div class="article-ai-ask"><h3 style="font-size:14px;color:#5cb87a">🤖 Hỏi AI</h3><textarea id="ask-q" placeholder="Hỏi về bài viết..."></textarea><button onclick="askAI()">Hỏi</button><div id="ask-a" class="article-ai-answer"></div></div></div>`;el.innerHTML=h;window.scrollTo(0,0);return;}}catch(e){}el.innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="loading"><p>Không đọc được.</p><a href="${esc(url)}" target="_blank" style="color:#5cb87a">Mở gốc →</a></div>`;}
353
+ async function rewriteArticle(){const url=_currentArticle?.url;if(!url)return;toast('⏳ Đang rewrite...');try{const r=await fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url,context:document.querySelector('.article-view')?.innerText?.slice(0,14000)||''})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng Tường AI!');if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
354
+ async function rewriteUrl(){const url=document.getElementById('url-input')?.value.trim();if(!url)return alert('Dán URL');toast('⏳ Đang rewrite...');try{const r=await fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})});const j=await r.json();if(!r.ok||j.error)throw new Error(j.error);toast('✅ Đã đăng!');document.getElementById('url-input').value='';if(j.post)prependWallPost(j.post);}catch(e){toast('❌ '+e.message)}}
355
+ async function askAI(){const q=document.getElementById('ask-q')?.value.trim();if(!q)return alert('Nhập câu hỏi');const a=document.getElementById('ask-a');a.textContent='Đang hỏi...';try{const r=await fetch('/api/article/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:_currentArticle?.url||'',question:q,context:document.querySelector('.article-view')?.innerText?.slice(0,12000)||''})});const j=await r.json();a.textContent=j.answer||'Không trả lời được';}catch(e){a.textContent='Lỗi: '+e.message}}
356
+ async function readWallPost(i){const p=_wallPosts[i];if(!p)return;showView('view-article');
357
+ const images = p.images || [];
358
+ let imgGallery = '';
359
+ if(images.length > 0){
360
+ imgGallery = '<div class="article-image-gallery">';
361
+ images.forEach((imgUrl, idx) => {
362
+ if(idx === 0){
363
+ imgGallery += `<img class="article-img article-hero-img" src="${esc(imgUrl)}" onerror="this.style.display='none" loading="eager">`;
364
+ } else {
365
+ if(idx === 1) imgGallery += '<div class="gallery-thumbs">';
366
+ imgGallery += `<div class="gallery-thumb"><img src="${esc(imgUrl)}" onerror="this.parentElement.style.display='none'" loading="lazy"></div>`;
367
+ }
368
+ });
369
+ if(images.length > 1) imgGallery += '</div>';
370
+ imgGallery += '</div>';
371
+ }
372
+ const hasVideo = p.video && p.video.length > 0;
373
+ const voiceOptions = [
374
+ {id:'hoaimy', label:'🎙️ Nữ — Hoài My'},
375
+ {id:'namminh', label:'🎙️ Nam — Nam Minh'},
376
+ ];
377
+ let voiceSelector = '';
378
+ if(!hasVideo){
379
+ voiceSelector = `<div class="tts-selector"><div class="tts-selector-label">🎙️ Chọn giọng đọc:</div><div class="tts-voice-btns">`;
380
+ voiceOptions.forEach(v=>{
381
+ voiceSelector += `<button class="tts-voice-btn" onclick="document.querySelectorAll('.tts-voice-btn').forEach(b=>b.classList.remove('active'));this.classList.add('active');document.getElementById('selected-voice').value='${v.id}'">${v.label}</button>`;
382
+ });
383
+ voiceSelector += `</div><div class="tts-speed-row"><span>Tốc độ:</span><select id="selected-speed"><option value="1.0">1.0x — Bình thường</option><option value="1.2" selected>1.2x — Nhanh</option><option value="1.5">1.5x — Rất nhanh</option><option value="0.8">0.8x — Chậm</option></select></div>`;
384
+ voiceSelector += `<input type="hidden" id="selected-voice" value="hoaimy"></div>`;
385
+ }
386
+ document.getElementById('view-article').innerHTML=`<button class="back-btn" onclick="switchCat('home')">← Quay lại</button><div class="article-view"><span class="badge badge-ai">AI</span><h1 class="article-title">${esc(p.title)}</h1>${imgGallery}<p class="article-p" style="white-space:pre-wrap">${esc(p.text)}</p>${hasVideo?`<video class="article-img" src="${esc(p.video)}" controls playsinline style="max-height:400px"></video>`:''}<div class="article-actions">${hasVideo?`<button onclick="openShortAIFeed(${i})">🎬 Xem Short</button>${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🔄 Tạo lại Short</button>`:`${voiceSelector}<button class="primary" onclick="makeShortVideo('${esc(p.id)}',this,document.getElementById('selected-voice')?.value,parseFloat(document.getElementById('selected-speed')?.value)||1.2)">🎬 Tạo Video Shorts</button>`}<button onclick="doShare('${esc(p.title)}','${SPACE}','${esc(p.img||'')}')">📤</button></div></div>`;
387
+ const firstVoiceBtn = document.querySelector('.tts-voice-btn');
388
+ if(firstVoiceBtn) firstVoiceBtn.classList.add('active');
389
+ window.scrollTo(0,0)}
390
+ async function loadNewsTab(){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';try{const r=await fetch('/api/homepage');const news=await r.json();if(!news.length){el.innerHTML='<div class="loading">Không có tin</div>';return}const groups={};news.forEach(a=>{if(!groups[a.group])groups[a.group]=[];groups[a.group].push(a)});let h='';for(const[g,arts] of Object.entries(groups)){h+=`<div class="section-title">${g}</div><div class="grid">`;arts.slice(0,6).forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'VnE')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>'}el.innerHTML=h}catch(e){el.innerHTML='<div class="loading">Lỗi</div>'}}
391
+ async function loadCat(id){const el=document.getElementById('view-cat');el.innerHTML='<div class="loading">Đang tải...</div>';const arts=await fetch('/api/category/'+id).then(r=>r.json()).catch(()=>[]);if(!arts.length){el.innerHTML='<div class="loading">Không có tin</div>';return}let h='<div class="grid">';arts.forEach(a=>{h+=`<div class="card" onclick="readArticle('${esc(a.link)}')"><div class="card-img">${a.img?`<img src="${a.img}">`:''}</div><div class="card-body"><span class="badge badge-vne">${esc(a.source||'')}</span><div class="card-title">${esc(a.title)}</div></div></div>`});h+='</div>';el.innerHTML=h}
392
+ fetch('/api/storage_status').then(r=>r.json()).then(j=>{if(!j.persistent){const home=document.getElementById('view-home');if(home){const w=document.createElement('div');w.className='storage-warn';w.innerHTML='��️ Persistent Storage chưa bật.';home.prepend(w)}}}).catch(()=>{});
393
+
394
+ // === AUTO-OPEN SHARE LINKS (/s?url=... sets pending_article) ===
395
+ (function(){
396
+ try{
397
+ const pa=localStorage.getItem('pending_article');
398
+ const pv=localStorage.getItem('pending_video');
399
+ if(pa){
400
+ localStorage.removeItem('pending_article');
401
+ setTimeout(()=>{
402
+ if(typeof readArticle==='function') readArticle(pa);
403
+ },1500);
404
+ }
405
+ if(pv){
406
+ localStorage.removeItem('pending_video');
407
+ try{
408
+ const v=JSON.parse(pv);
409
+ if(v&&v.url) setTimeout(()=>{window.open(v.url,'_blank')},1500);
410
+ }catch(e){}
411
+ }
412
+ }catch(e){}
413
+ })();
414
+ </script>
415
+ <script>
416
+ // === VNEWS — VTV1-VTV10 + VTVPrime LIVE CHANNELS + EPG ===
417
+ // Uses backend /api/vtv/streams for stream URLs
418
+ // Default channel: VTV6 | No double-load | EPG schedule
419
+
420
+ (function(){
421
+ if(window._ytLiveLoaded) return;
422
+ window._ytLiveLoaded = true;
423
+
424
+ const CHANNELS = [
425
+ {id:'vtv1', name:'VTV1', badge:'Tin tức'},
426
+ {id:'vtv2', name:'VTV2', badge:'Khoa học'},
427
+ {id:'vtv3', name:'VTV3', badge:'Giải trí'},
428
+ {id:'vtv4', name:'VTV4', badge:'Quốc tế'},
429
+ {id:'vtv5', name:'VTV5', badge:'Miền Nam'},
430
+ {id:'vtv6', name:'VTV6', badge:'Thanh niên'},
431
+ {id:'vtv7', name:'VTV7', badge:'Giáo dục'},
432
+ {id:'vtv8', name:'VTV8', badge:'Miền Trung'},
433
+ {id:'vtv9', name:'VTV9', badge:'Miền Bắc'},
434
+ {id:'vtv10', name:'VTV10', badge:'VTV10'},
435
+ {id:'vtvprime', name:'VTVPrime', badge:'Prime'},
436
+ ];
437
+
438
+ // ===== EPG — Lịch phát sóng mẫu cho từng kênh =====
439
+ const EPG = {
440
+ vtv1: [
441
+ {t:'06:00',n:'Nhật ký ngày mai'},{t:'07:00',n:'Thời sự sáng'},{t:'09:00',n:'Thời sự'},
442
+ {t:'12:00',n:'Thời sự trưa'},{t:'15:00',n:'Thời sự chiều'},{t:'19:00',n:'Thời sự tối'},
443
+ {t:'21:00',n:'Thời sự đêm'},{t:'23:00',n:'Nhật ký ngày mai'},
444
+ ],
445
+ vtv2: [
446
+ {t:'06:00',n:'Khoa học & Công nghệ'},{t:'08:00',n:'Thế giới tự nhiên'},{t:'10:00',n:'Khoa học 360'},
447
+ {t:'12:00',n:'Đi tìm giải pháp'},{t:'14:00',n:'Sức khỏe & Cuộc sống'},{t:'16:00',n:'Khoa học cho mọi nhà'},
448
+ {t:'18:00',n:'Thế giới động vật'},{t:'20:00',n:'Khoa học & Tương lai'},{t:'22:00',n:'Tài liệu khoa học'},
449
+ ],
450
+ vtv3: [
451
+ {t:'06:00',n:'Sáng vui'},{t:'08:00',n:'Phim truyện'},{t:'10:00',n:'Gameshow'},
452
+ {t:'12:00',n:'Âm nhạc'},{t:'14:00',n:'Phim truyện'},{t:'16:00',n:'Giải trí chiều'},
453
+ {t:'18:00',n:'Tạp kỹ thuật số'},{t:'20:00',n:'Phim truyện đặc biệt'},{t:'22:00',n:'Đêm giải trí'},
454
+ ],
455
+ vtv4: [
456
+ {t:'06:00',n:'News'},{t:'08:00',n:'World News'},{t:'10:00',n:'Culture'},
457
+ {t:'12:00',n:'Midday News'},{t:'14:00',n:'Documentary'},{t:'16:00',n:'Sports'},
458
+ {t:'18:00',n:'Evening News'},{t:'20:00',n:'World Today'},{t:'22:00',n:'Nightline'},
459
+ ],
460
+ vtv5: [
461
+ {t:'06:00',n:'Thời sự miền Nam'},{t:'08:00',n:'Chương trình thiếu nhi'},{t:'10:00',n:'Phim truyện'},
462
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Nam'},
463
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
464
+ ],
465
+ vtv6: [
466
+ {t:'06:00',n:'Khởi động ngày mới'},{t:'08:00',n:'Thanh niên & Sáng tạo'},{t:'10:00',n:'Thế giới trẻ'},
467
+ {t:'12:00',n:'Nhịp sống trẻ'},{t:'14:00',n:'Thể thao tuổi trẻ'},{t:'16:00',n:'Giải trí thanh niên'},
468
+ {t:'18:00',n:'Thời sự trẻ'},{t:'20:00',n:'Đêm nhạc'},{t:'22:00',n:'Thanh niên & Đêm'},
469
+ ],
470
+ vtv7: [
471
+ {t:'06:00',n:'Giáo dục sáng'},{t:'08:00',n:'Học mọi lúc'},{t:'10:00',n:'Kỹ năng sống'},
472
+ {t:'12:00',n:'Giáo dục trưa'},{t:'14:00',n:'Học trực tuyến'},{t:'16:00',n:'Thiếu nhi'},
473
+ {t:'18:00',n:'Giáo dục chiều'},{t:'20:00',n:'Tài liệu giáo dục'},{t:'22:00',n:'Học suốt đời'},
474
+ ],
475
+ vtv8: [
476
+ {t:'06:00',n:'Thời sự miền Trung'},{t:'08:00',n:'Văn hóa miền Trung'},{t:'10:00',n:'Phim truyện'},
477
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Trung'},
478
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
479
+ ],
480
+ vtv9: [
481
+ {t:'06:00',n:'Thời sự miền Bắc'},{t:'08:00',n:'Văn hóa miền Bắc'},{t:'10:00',n:'Phim truyện'},
482
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Bắc'},
483
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
484
+ ],
485
+ vtv10: [
486
+ {t:'06:00',n:'Thời sự Tây Nam Bộ'},{t:'08:00',n:'Văn hóa đồng bằng'},{t:'10:00',n:'Phim truyện'},
487
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao Tây Nam Bộ'},
488
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
489
+ ],
490
+ vtvprime: [
491
+ {t:'06:00',n:'Prime Morning'},{t:'08:00',n:'Prime Cinema'},{t:'10:00',n:'Prime Sports'},
492
+ {t:'12:00',n:'Prime News'},{t:'14:00',n:'Prime Drama'},{t:'16:00',n:'Prime Entertainment'},
493
+ {t:'18:00',n:'Prime Evening'},{t:'20:00',n:'Prime Night'},{t:'22:00',n:'Prime Late'},
494
+ ],
495
+ };
496
+
497
+ // ALL external streams need proxy — VTVGo/fptplay CDNs don't send CORS headers
498
+ const STREAMS = {};
499
+ let _currentCh = null;
500
+ let _hls = null;
501
+ let _loading = false;
502
+ let _epgVisible = false;
503
+
504
+ const s = document.createElement('style');
505
+ s.textContent = `
506
+ .vtv-wrap{margin:6px 4px;background:#111;border:1px solid #0066cc;border-radius:10px;overflow:hidden}
507
+ .vtv-head{display:flex;align-items:center;gap:8px;padding:8px 10px;background:linear-gradient(90deg,#003366,#1a1a1a)}
508
+ .vtv-title{font-size:13px;font-weight:800;color:#00ccff}
509
+ .vtv-badge{font-size:10px;font-weight:800;color:#00ccff;animation:vtvp 1.3s infinite}
510
+ @keyframes vtvp{0%,100%{opacity:1}50%{opacity:.3}}
511
+ .vtv-tabs{display:flex;gap:3px;padding:6px 8px;overflow-x:auto;scrollbar-width:none;background:#0d1a2a}
512
+ .vtv-tabs::-webkit-scrollbar{display:none}
513
+ .vtv-tab{padding:4px 8px;background:#1a2a3a;border:1px solid #2a3a4a;border-radius:10px;color:#8ab4d8;font-size:9px;cursor:pointer;white-space:nowrap;flex-shrink:0;transition:all .2s}
514
+ .vtv-tab:hover{background:#0b4a7a;color:#fff}
515
+ .vtv-tab.on{background:#0066cc;border-color:#00ccff;color:#fff;font-weight:700}
516
+ .vtv-tab.off{opacity:.35;pointer-events:none}
517
+ .vtv-frame{position:relative;width:100%;aspect-ratio:16/9;background:#000;min-height:180px}
518
+ .vtv-frame video{position:absolute;inset:0;width:100%;height:100%;object-fit:contain}
519
+ .vtv-err{display:flex;align-items:center;justify-content:center;height:180px;color:#888;font-size:12px;text-align:center;padding:20px;flex-direction:column;gap:8px}
520
+ .vtv-err button{background:#0066cc;border:none;color:#fff;padding:6px 14px;border-radius:8px;font-size:11px;cursor:pointer}
521
+ .vtv-load{display:flex;align-items:center;justify-content:center;height:180px;color:#00ccff;font-size:12px;flex-direction:column;gap:8px}
522
+ .vtv-spinner{width:24px;height:24px;border:2px solid #333;border-top-color:#00ccff;border-radius:50%;animation:vtvspin .8s linear infinite}
523
+ @keyframes vtvspin{to{transform:rotate(360deg)}}
524
+ .vtv-epg{margin:0;padding:6px 10px;background:#0a1628;border-top:1px solid #1a2a3a}
525
+ .vtv-epg-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:4px}
526
+ .vtv-epg-title{font-size:10px;font-weight:700;color:#00ccff}
527
+ .vtv-epg-toggle{background:none;border:1px solid #2a3a4a;color:#8ab4d8;font-size:9px;padding:2px 8px;border-radius:6px;cursor:pointer}
528
+ .vtv-epg-list{display:flex;gap:4px;overflow-x:auto;scrollbar-width:none;padding-bottom:4px}
529
+ .vtv-epg-list::-webkit-scrollbar{display:none}
530
+ .vtv-epg-item{flex:0 0 auto;padding:3px 6px;background:#1a2a3a;border-radius:4px;font-size:8px;color:#8ab4d8;white-space:nowrap}
531
+ .vtv-epg-item.now{background:#0066cc;color:#fff;font-weight:700}
532
+ .vtv-epg-item .epg-t{font-size:7px;color:#6a8aaa}
533
+ .vtv-epg-item.now .epg-t{color:#aaccee}
534
+ .vtv-epg-item .epg-n{color:#ccc;font-size:8px}
535
+ .vtv-epg-item.now .epg-n{color:#fff}
536
+ `;
537
+ document.head.appendChild(s);
538
+
539
+ function getCurrentHour(){
540
+ return new Date().getHours();
541
+ }
542
+
543
+ function buildEPGHTML(chId){
544
+ const epg = EPG[chId] || [];
545
+ if(!epg.length) return '';
546
+ const curH = getCurrentHour();
547
+ let items = '';
548
+ epg.forEach(item => {
549
+ const itemH = parseInt(item.t.split(':')[0], 10);
550
+ const isNow = itemH <= curH && (itemH + 2) > curH;
551
+ items += `<div class="vtv-epg-item${isNow?' now':''}"><div class="epg-t">${item.t}</div><div class="epg-n">${item.n}</div></div>`;
552
+ });
553
+ return `<div class="vtv-epg" id="vtv-epg">' +
554
+ '<div class="vtv-epg-header"><span class="vtv-epg-title">📋 Lịch phát sóng</span>' +
555
+ '<button class="vtv-epg-toggle" onclick="window._vtvToggleEPG()">Ẩn/Hiện</button></div>' +
556
+ '<div class="vtv-epg-list" id="vtv-epg-list">' + items + '</div></div>';
557
+ }
558
+
559
+ window._vtvToggleEPG = function(){
560
+ const list = document.getElementById('vtv-epg-list');
561
+ if(list) list.style.display = list.style.display === 'none' ? 'flex' : 'none';
562
+ };
563
+
564
+ async function loadAllStreams(){
565
+ if(_loading) return;
566
+ _loading = true;
567
+ const loadEl = document.getElementById('vtv-load');
568
+ if(loadEl) loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang tải danh sách kênh...';
569
+
570
+ try {
571
+ const r = await fetch('/api/vtv/streams', {signal: AbortSignal.timeout(10000)});
572
+ if(r.ok){
573
+ const data = await r.json();
574
+ CHANNELS.forEach(ch => {
575
+ const info = data[ch.id];
576
+ if(info && info.stream_url){
577
+ // Always proxy through backend to avoid CORS issues
578
+ const url = '/api/proxy/m3u8/vtv?url=' + encodeURIComponent(info.stream_url);
579
+ STREAMS[ch.id] = [url];
580
+ } else {
581
+ STREAMS[ch.id] = [];
582
+ }
583
+ });
584
+ }
585
+ } catch(e) {
586
+ console.warn('VTV API error:', e);
587
+ }
588
+
589
+ CHANNELS.forEach(ch => {
590
+ const tab = document.getElementById('vtvt-'+ch.id);
591
+ if(tab){
592
+ if(STREAMS[ch.id] && STREAMS[ch.id].length > 0){
593
+ tab.classList.remove('off');
594
+ tab.textContent = ch.name;
595
+ } else {
596
+ tab.style.opacity = '0.35';
597
+ tab.textContent = ch.name + ' ✕';
598
+ }
599
+ }
600
+ });
601
+ _loading = false;
602
+ }
603
+
604
+ function buildBlock(){
605
+ const w = document.createElement('div');
606
+ w.className = 'vtv-wrap';
607
+ w.id = 'vtv-block';
608
+ let tabs = '';
609
+ CHANNELS.forEach(ch => {
610
+ tabs += '<button class="vtv-tab off" id="vtvt-'+ch.id+'" onclick="window._vtvPlay(\''+ch.id+'\')">'+ch.name+'</button>';
611
+ });
612
+ w.innerHTML =
613
+ '<div class="vtv-head"><span class="vtv-title">📺 VTV Trực Tuyến</span><span class="vtv-badge">● LIVE</span></div>' +
614
+ '<div class="vtv-tabs">' + tabs + '</div>' +
615
+ '<div class="vtv-frame">' +
616
+ '<div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div>' +
617
+ '<video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video>' +
618
+ '<div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="window._vtvRetry()">Thử lại</button></div>' +
619
+ '</div>';
620
+ return w;
621
+ }
622
+
623
+ // ===== PIN BLOCK — called only once via loadHome wrapper =====
624
+ function pinBlock(){
625
+ const h = document.getElementById('view-home');
626
+ if(!h || document.getElementById('vtv-block')) return;
627
+ h.insertBefore(buildBlock(), h.firstChild);
628
+ loadAllStreams().then(() => {
629
+ // Default to VTV6 if available, otherwise first available channel
630
+ const tryOrder = ['vtv6','vtv1','vtv2','vtv3','vtv4','vtv5','vtv7','vtv8','vtv9','vtv10'];
631
+ for(const chId of tryOrder){
632
+ if(STREAMS[chId] && STREAMS[chId].length > 0){
633
+ setTimeout(() => window._vtvPlay(chId), 300);
634
+ return;
635
+ }
636
+ }
637
+ });
638
+ }
639
+
640
+ window._vtvRetry = function(){
641
+ if(_currentCh) window._vtvPlay(_currentCh);
642
+ };
643
+
644
+ window._vtvPlay = function(chId){
645
+ const ch = CHANNELS.find(c => c.id === chId);
646
+ if(!ch) return;
647
+ _currentCh = chId;
648
+ document.querySelectorAll('.vtv-tab').forEach(t => t.classList.remove('on'));
649
+ const tab = document.getElementById('vtvt-'+chId);
650
+ if(tab) tab.classList.add('on');
651
+ const video = document.getElementById('vtv-player');
652
+ const errEl = document.getElementById('vtv-err');
653
+ const loadEl = document.getElementById('vtv-load');
654
+ const errMsg = document.getElementById('vtv-err-msg');
655
+ video.style.display = 'none';
656
+ errEl.style.display = 'none';
657
+ loadEl.style.display = 'flex';
658
+ loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + ch.name + '...';
659
+ if(_hls){ _hls.destroy(); _hls = null; }
660
+ const urls = STREAMS[chId] || [];
661
+ if(urls.length === 0){
662
+ loadEl.style.display = 'none';
663
+ errEl.style.display = 'flex';
664
+ if(chId === 'vtvprime'){
665
+ errMsg.textContent = 'VTVPrime: Kênh trả phí, không có luồng miễn phí.';
666
+ } else {
667
+ errMsg.textContent = ch.name + ': Không tìm thấy luồng. Thử lại sau.';
668
+ }
669
+ return;
670
+ }
671
+ // Update EPG
672
+ const epgEl = document.getElementById('vtv-epg');
673
+ if(epgEl) epgEl.remove();
674
+ const frame = document.querySelector('.vtv-frame');
675
+ if(frame){
676
+ const epgDiv = document.createElement('div');
677
+ epgDiv.innerHTML = buildEPGHTML(chId);
678
+ frame.appendChild(epgDiv.firstElementChild);
679
+ }
680
+ _tryPlay(video, urls, 0, ch.name, loadEl, errEl, errMsg);
681
+ };
682
+
683
+ function _tryPlay(video, urls, idx, name, loadEl, errEl, errMsg){
684
+ if(idx >= urls.length){
685
+ loadEl.style.display = 'none';
686
+ errEl.style.display = 'flex';
687
+ errMsg.textContent = name + ': Tất cả nguồn đều lỗi. Thử lại sau.';
688
+ return;
689
+ }
690
+ const src = urls[idx];
691
+ const sourceLabel = ' (' + (idx+1) + '/' + urls.length + ')';
692
+ loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + name + sourceLabel + '...';
693
+ if(typeof Hls !== 'undefined' && Hls.isSupported()){
694
+ const hls = new Hls({
695
+ enableWorker: true,
696
+ lowLatencyMode: true,
697
+ startLevel: -1,
698
+ capLevelToPlayerSize: true,
699
+ maxBufferLength: 20,
700
+ xhrSetup: function(xhr, url){
701
+ if(url.includes('fptplay')){
702
+ xhr.setRequestHeader('Referer', 'https://fptplay.vn/');
703
+ xhr.setRequestHeader('Origin', 'https://fptplay.vn');
704
+ }
705
+ }
706
+ });
707
+ _hls = hls;
708
+ hls.loadSource(src);
709
+ hls.attachMedia(video);
710
+ hls.on(Hls.Events.MANIFEST_PARSED, () => {
711
+ video.play().catch(() => {});
712
+ loadEl.style.display = 'none';
713
+ video.style.display = 'block';
714
+ });
715
+ let recoverAttempts = 0;
716
+ hls.on(Hls.Events.ERROR, (ev, data) => {
717
+ if(data.fatal){
718
+ if(data.type === Hls.ErrorTypes.NETWORK_ERROR){
719
+ recoverAttempts++;
720
+ if(recoverAttempts <= 3){
721
+ setTimeout(() => hls.startLoad(), 2000);
722
+ } else {
723
+ hls.destroy();
724
+ _hls = null;
725
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
726
+ }
727
+ } else if(data.type === Hls.ErrorTypes.MEDIA_ERROR){
728
+ try { hls.recoverMediaError(); } catch(e) {}
729
+ } else {
730
+ hls.destroy();
731
+ _hls = null;
732
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
733
+ }
734
+ }
735
+ });
736
+ } else if(video.canPlayType('application/vnd.apple.mpegurl')){
737
+ video.src = src;
738
+ video.addEventListener('loadedmetadata', () => {
739
+ video.play().catch(() => {});
740
+ loadEl.style.display = 'none';
741
+ video.style.display = 'block';
742
+ }, {once: true});
743
+ video.addEventListener('error', () => {
744
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
745
+ }, {once: true});
746
+ } else {
747
+ loadEl.style.display = 'none';
748
+ errEl.style.display = 'flex';
749
+ errMsg.textContent = 'Trình duyệt không hỗ trợ HLS';
750
+ }
751
+ }
752
+
753
+ // ===== ONLY wrap loadHome — no DOMContentLoaded listener to avoid double-load =====
754
+ const orig = window.loadHome;
755
+ if(orig && !orig.__vtvWrapped){
756
+ window.loadHome = async function(){
757
+ const r = await orig.apply(this, arguments);
758
+ try{ pinBlock(); }catch(e){}
759
+ return r;
760
+ };
761
+ window.loadHome.__vtvWrapped = true;
762
+ }
763
+ })();
764
+ </script>
765
+ <!-- hot_multi.js --><script src="/static/hot_multi.js"></script>
766
+ <!-- wc2026_v2.js --><script src="/static/wc2026_v2.js"></script>
767
+ <!-- live_mode.js --><script src="/static/live_mode.js"></script>
768
+ <!-- match_detail_v6.js --><script src="/static/match_detail_v6.js"></script>
769
+ <script>init();</script><!-- v1781058532 --></body></html>
static/{index_v2.html → index_v3.html} RENAMED
@@ -36,12 +36,11 @@ function doShare(title,url,img){const shareUrl=SPACE+'/s?url='+encodeURIComponen
36
  async function init(){_cats=await fetch('/api/categories').then(r=>r.json()).catch(()=>[]);let bar='<div class="cat active" data-cat="home">🏠</div><div class="cat" data-cat="news-all">📰 Tin tức</div>';_cats.forEach(c=>{bar+=`<div class="cat" data-cat="${c.id}">${c.name}</div>`});document.getElementById('cat-bar').innerHTML=bar;document.querySelectorAll('.cat').forEach(t=>{t.onclick=()=>switchCat(t.dataset.cat)});await loadHome()}
37
  var SPACE=location.origin;
38
  </script>
39
- <script src="/static/app_v3.js?v=2026061002"></script>
40
- <script src="/static/yt_live_v2.js?v=2026061002"></script>
41
- <script src="/static/hot_multi.js?v=2026061002"></script>
42
- <script src="/static/wc2026_v2.js?v=2026061002"></script>
43
- <script src="/static/live_mode.js?v=2026061002"></script>
44
- <script src="/static/match_detail_v6.js?v=2026061002"></script>
45
  <script>init();</script>
46
  </body>
47
  </html>
 
36
  async function init(){_cats=await fetch('/api/categories').then(r=>r.json()).catch(()=>[]);let bar='<div class="cat active" data-cat="home">🏠</div><div class="cat" data-cat="news-all">📰 Tin tức</div>';_cats.forEach(c=>{bar+=`<div class="cat" data-cat="${c.id}">${c.name}</div>`});document.getElementById('cat-bar').innerHTML=bar;document.querySelectorAll('.cat').forEach(t=>{t.onclick=()=>switchCat(t.dataset.cat)});await loadHome()}
37
  var SPACE=location.origin;
38
  </script>
39
+ <script src="/static/app_v4.js?v=1781060367"></script>
40
+ <script src="/static/hot_multi.js?v=1781059323"></script>
41
+ <script src="/static/wc2026_v2.js?v=1781059323"></script>
42
+ <script src="/static/live_mode.js?v=1781059323"></script>
43
+ <script src="/static/match_detail_v6.js?v=1781059323"></script>
 
44
  <script>init();</script>
45
  </body>
46
  </html>
static/rewrite_fix.js ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Fix rewriteArticle - call correct endpoint
2
+ // This file patches the rewriteArticle function to use /api/rewrite_slide instead of /api/rewrite_share
3
+
4
+ (function(){
5
+ // Override rewriteArticle to call /api/rewrite_slide
6
+ const origRewrite = window.rewriteArticle;
7
+ window.rewriteArticle = async function(){
8
+ const url = _currentArticle?.url;
9
+ if(!url) return;
10
+ toast('⏳ Đang tạo slide tóm tắt...');
11
+ try {
12
+ const r = await fetch('/api/rewrite_slide', {
13
+ method: 'POST',
14
+ headers: {'Content-Type': 'application/json'},
15
+ body: JSON.stringify({url, context: document.querySelector('.article-view')?.innerText?.slice(0,14000) || ''})
16
+ });
17
+ const j = await r.json();
18
+ if (!r.ok || j.error) throw new Error(j.error);
19
+ toast('✅ Đã đăng Tường AI!');
20
+ if (j.post) prependWallPost(j.post);
21
+ // Navigate to the new post on Tường AI (home). Slide overlay (if any) stays on top.
22
+ if (j.post && typeof goToWallPost === 'function') goToWallPost(j.post.id);
23
+ // Show slides preview
24
+ if (j.slides && j.slides.length) {
25
+ showSlidePreview(j.slides, j.post?.title || '');
26
+ }
27
+ } catch(e) {
28
+ // Fallback: try /api/rewrite_share (old endpoint from ai_ext)
29
+ try {
30
+ const r2 = await fetch('/api/rewrite_share', {
31
+ method: 'POST',
32
+ headers: {'Content-Type': 'application/json'},
33
+ body: JSON.stringify({url, context: document.querySelector('.article-view')?.innerText?.slice(0,14000) || ''})
34
+ });
35
+ const j2 = await r2.json();
36
+ if (r2.ok && !j2.error) {
37
+ toast('✅ Đã đăng Tường AI!');
38
+ if (j2.post) prependWallPost(j2.post);
39
+ if (j2.post && typeof goToWallPost === 'function') goToWallPost(j2.post.id);
40
+ return;
41
+ }
42
+ } catch(e2) {}
43
+ toast('❌ ' + e.message);
44
+ }
45
+ };
46
+
47
+ // Show slides as fullscreen overlay
48
+ window.showSlidePreview = function(slides, title) {
49
+ if (!slides || !slides.length) return;
50
+ const overlay = document.createElement('div');
51
+ overlay.id = 'slide-preview';
52
+ overlay.style.cssText = 'position:fixed;inset:0;background:#000;z-index:99999;display:flex;flex-direction:column;overflow:hidden';
53
+
54
+ let currentSlide = 0;
55
+ function renderSlide(idx) {
56
+ const s = slides[idx];
57
+ overlay.innerHTML = `
58
+ <div style="position:absolute;top:10px;left:10px;right:10px;display:flex;justify-content:space-between;align-items:center;z-index:2">
59
+ <button onclick="document.getElementById('slide-preview').remove()" style="background:rgba(0,0,0,.6);border:0;color:#fff;padding:8px 14px;border-radius:20px;font-size:12px;cursor:pointer">✕ Đóng</button>
60
+ <span style="color:#fff;font-size:11px;background:rgba(0,0,0,.6);padding:4px 10px;border-radius:10px">${idx+1}/${slides.length}</span>
61
+ </div>
62
+ <div style="flex:1;display:flex;align-items:center;justify-content:center;padding:20px">
63
+ ${s.image ? `<img src="${esc(s.image)}" style="max-width:100%;max-height:60vh;border-radius:10px;object-fit:contain" onerror="this.style.display='none'">` : ''}
64
+ </div>
65
+ <div style="padding:16px 20px;background:linear-gradient(transparent,rgba(0,0,0,.9));min-height:100px">
66
+ <p style="color:#fff;font-size:14px;line-height:1.6">${esc(s.text)}</p>
67
+ </div>
68
+ <div style="display:flex;gap:10px;padding:10px 20px 20px;justify-content:center">
69
+ <button onclick="prevSlide()" style="background:#333;border:0;color:#fff;padding:10px 20px;border-radius:20px;font-size:12px;cursor:pointer" ${idx===0?'disabled style="opacity:.3"':''}>← Trước</button>
70
+ <button onclick="nextSlide()" style="background:#2d8659;border:0;color:#fff;padding:10px 20px;border-radius:20px;font-size:12px;cursor:pointer" ${idx===slides.length-1?'disabled style="opacity:.3"':''}>Tiếp →</button>
71
+ </div>
72
+ `;
73
+ }
74
+
75
+ window.nextSlide = function() { if (currentSlide < slides.length - 1) { currentSlide++; renderSlide(currentSlide); } };
76
+ window.prevSlide = function() { if (currentSlide > 0) { currentSlide--; renderSlide(currentSlide); } };
77
+
78
+ renderSlide(0);
79
+ document.body.appendChild(overlay);
80
+
81
+ // Swipe support
82
+ let startX = 0;
83
+ overlay.addEventListener('touchstart', e => { startX = e.touches[0].clientX; });
84
+ overlay.addEventListener('touchend', e => {
85
+ const diff = e.changedTouches[0].clientX - startX;
86
+ if (diff < -50) nextSlide();
87
+ else if (diff > 50) prevSlide();
88
+ });
89
+ };
90
+ })();
static/tv_player.js ADDED
@@ -0,0 +1,348 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // === VNEWS — VTV1-VTV10 + VTVPrime LIVE CHANNELS + EPG ===
2
+ // Uses backend /api/vtv/streams for stream URLs
3
+ // Default channel: VTV6 | No double-load | EPG schedule
4
+
5
+ (function(){
6
+ if(window._ytLiveLoaded) return;
7
+ window._ytLiveLoaded = true;
8
+
9
+ const CHANNELS = [
10
+ {id:'vtv1', name:'VTV1', badge:'Tin tức'},
11
+ {id:'vtv2', name:'VTV2', badge:'Khoa học'},
12
+ {id:'vtv3', name:'VTV3', badge:'Giải trí'},
13
+ {id:'vtv4', name:'VTV4', badge:'Quốc tế'},
14
+ {id:'vtv5', name:'VTV5', badge:'Miền Nam'},
15
+ {id:'vtv6', name:'VTV6', badge:'Thanh niên'},
16
+ {id:'vtv7', name:'VTV7', badge:'Giáo dục'},
17
+ {id:'vtv8', name:'VTV8', badge:'Miền Trung'},
18
+ {id:'vtv9', name:'VTV9', badge:'Miền Bắc'},
19
+ {id:'vtv10', name:'VTV10', badge:'VTV10'},
20
+ {id:'vtvprime', name:'VTVPrime', badge:'Prime'},
21
+ ];
22
+
23
+ // ===== EPG — Lịch phát sóng mẫu cho từng kênh =====
24
+ const EPG = {
25
+ vtv1: [
26
+ {t:'06:00',n:'Nhật ký ngày mai'},{t:'07:00',n:'Thời sự sáng'},{t:'09:00',n:'Thời sự'},
27
+ {t:'12:00',n:'Thời sự trưa'},{t:'15:00',n:'Thời sự chiều'},{t:'19:00',n:'Thời sự tối'},
28
+ {t:'21:00',n:'Thời sự đêm'},{t:'23:00',n:'Nhật ký ngày mai'},
29
+ ],
30
+ vtv2: [
31
+ {t:'06:00',n:'Khoa học & Công nghệ'},{t:'08:00',n:'Thế giới tự nhiên'},{t:'10:00',n:'Khoa học 360'},
32
+ {t:'12:00',n:'Đi tìm giải pháp'},{t:'14:00',n:'Sức khỏe & Cuộc sống'},{t:'16:00',n:'Khoa học cho mọi nhà'},
33
+ {t:'18:00',n:'Thế giới động vật'},{t:'20:00',n:'Khoa học & Tương lai'},{t:'22:00',n:'Tài liệu khoa học'},
34
+ ],
35
+ vtv3: [
36
+ {t:'06:00',n:'Sáng vui'},{t:'08:00',n:'Phim truyện'},{t:'10:00',n:'Gameshow'},
37
+ {t:'12:00',n:'Âm nhạc'},{t:'14:00',n:'Phim truyện'},{t:'16:00',n:'Giải trí chiều'},
38
+ {t:'18:00',n:'Tạp kỹ thuật số'},{t:'20:00',n:'Phim truyện đặc biệt'},{t:'22:00',n:'Đêm giải trí'},
39
+ ],
40
+ vtv4: [
41
+ {t:'06:00',n:'News'},{t:'08:00',n:'World News'},{t:'10:00',n:'Culture'},
42
+ {t:'12:00',n:'Midday News'},{t:'14:00',n:'Documentary'},{t:'16:00',n:'Sports'},
43
+ {t:'18:00',n:'Evening News'},{t:'20:00',n:'World Today'},{t:'22:00',n:'Nightline'},
44
+ ],
45
+ vtv5: [
46
+ {t:'06:00',n:'Thời sự miền Nam'},{t:'08:00',n:'Chương trình thiếu nhi'},{t:'10:00',n:'Phim truyện'},
47
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Nam'},
48
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
49
+ ],
50
+ vtv6: [
51
+ {t:'06:00',n:'Khởi động ngày mới'},{t:'08:00',n:'Thanh niên & Sáng tạo'},{t:'10:00',n:'Thế giới trẻ'},
52
+ {t:'12:00',n:'Nhịp sống trẻ'},{t:'14:00',n:'Thể thao tuổi trẻ'},{t:'16:00',n:'Giải trí thanh niên'},
53
+ {t:'18:00',n:'Thời sự trẻ'},{t:'20:00',n:'Đêm nhạc'},{t:'22:00',n:'Thanh niên & Đêm'},
54
+ ],
55
+ vtv7: [
56
+ {t:'06:00',n:'Giáo dục sáng'},{t:'08:00',n:'Học mọi lúc'},{t:'10:00',n:'Kỹ năng sống'},
57
+ {t:'12:00',n:'Giáo dục trưa'},{t:'14:00',n:'Học trực tuyến'},{t:'16:00',n:'Thiếu nhi'},
58
+ {t:'18:00',n:'Giáo dục chiều'},{t:'20:00',n:'Tài liệu giáo dục'},{t:'22:00',n:'Học suốt đời'},
59
+ ],
60
+ vtv8: [
61
+ {t:'06:00',n:'Thời sự miền Trung'},{t:'08:00',n:'Văn hóa miền Trung'},{t:'10:00',n:'Phim truyện'},
62
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Trung'},
63
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
64
+ ],
65
+ vtv9: [
66
+ {t:'06:00',n:'Thời sự miền Bắc'},{t:'08:00',n:'Văn hóa miền Bắc'},{t:'10:00',n:'Phim truyện'},
67
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Bắc'},
68
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
69
+ ],
70
+ vtv10: [
71
+ {t:'06:00',n:'Thời sự Tây Nam Bộ'},{t:'08:00',n:'Văn hóa đồng bằng'},{t:'10:00',n:'Phim truyện'},
72
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao Tây Nam Bộ'},
73
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
74
+ ],
75
+ vtvprime: [
76
+ {t:'06:00',n:'Prime Morning'},{t:'08:00',n:'Prime Cinema'},{t:'10:00',n:'Prime Sports'},
77
+ {t:'12:00',n:'Prime News'},{t:'14:00',n:'Prime Drama'},{t:'16:00',n:'Prime Entertainment'},
78
+ {t:'18:00',n:'Prime Evening'},{t:'20:00',n:'Prime Night'},{t:'22:00',n:'Prime Late'},
79
+ ],
80
+ };
81
+
82
+ // ALL external streams need proxy — VTVGo/fptplay CDNs don't send CORS headers
83
+ const STREAMS = {};
84
+ let _currentCh = null;
85
+ let _hls = null;
86
+ let _loading = false;
87
+ let _epgVisible = false;
88
+
89
+ const s = document.createElement('style');
90
+ s.textContent = `
91
+ .vtv-wrap{margin:6px 4px;background:#111;border:1px solid #0066cc;border-radius:10px;overflow:hidden}
92
+ .vtv-head{display:flex;align-items:center;gap:8px;padding:8px 10px;background:linear-gradient(90deg,#003366,#1a1a1a)}
93
+ .vtv-title{font-size:13px;font-weight:800;color:#00ccff}
94
+ .vtv-badge{font-size:10px;font-weight:800;color:#00ccff;animation:vtvp 1.3s infinite}
95
+ @keyframes vtvp{0%,100%{opacity:1}50%{opacity:.3}}
96
+ .vtv-tabs{display:flex;gap:3px;padding:6px 8px;overflow-x:auto;scrollbar-width:none;background:#0d1a2a}
97
+ .vtv-tabs::-webkit-scrollbar{display:none}
98
+ .vtv-tab{padding:4px 8px;background:#1a2a3a;border:1px solid #2a3a4a;border-radius:10px;color:#8ab4d8;font-size:9px;cursor:pointer;white-space:nowrap;flex-shrink:0;transition:all .2s}
99
+ .vtv-tab:hover{background:#0b4a7a;color:#fff}
100
+ .vtv-tab.on{background:#0066cc;border-color:#00ccff;color:#fff;font-weight:700}
101
+ .vtv-tab.off{opacity:.35;pointer-events:none}
102
+ .vtv-frame{position:relative;width:100%;aspect-ratio:16/9;background:#000;min-height:180px}
103
+ .vtv-frame video{position:absolute;inset:0;width:100%;height:100%;object-fit:contain}
104
+ .vtv-err{display:flex;align-items:center;justify-content:center;height:180px;color:#888;font-size:12px;text-align:center;padding:20px;flex-direction:column;gap:8px}
105
+ .vtv-err button{background:#0066cc;border:none;color:#fff;padding:6px 14px;border-radius:8px;font-size:11px;cursor:pointer}
106
+ .vtv-load{display:flex;align-items:center;justify-content:center;height:180px;color:#00ccff;font-size:12px;flex-direction:column;gap:8px}
107
+ .vtv-spinner{width:24px;height:24px;border:2px solid #333;border-top-color:#00ccff;border-radius:50%;animation:vtvspin .8s linear infinite}
108
+ @keyframes vtvspin{to{transform:rotate(360deg)}}
109
+ .vtv-epg{margin:0;padding:6px 10px;background:#0a1628;border-top:1px solid #1a2a3a}
110
+ .vtv-epg-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:4px}
111
+ .vtv-epg-title{font-size:10px;font-weight:700;color:#00ccff}
112
+ .vtv-epg-toggle{background:none;border:1px solid #2a3a4a;color:#8ab4d8;font-size:9px;padding:2px 8px;border-radius:6px;cursor:pointer}
113
+ .vtv-epg-list{display:flex;gap:4px;overflow-x:auto;scrollbar-width:none;padding-bottom:4px}
114
+ .vtv-epg-list::-webkit-scrollbar{display:none}
115
+ .vtv-epg-item{flex:0 0 auto;padding:3px 6px;background:#1a2a3a;border-radius:4px;font-size:8px;color:#8ab4d8;white-space:nowrap}
116
+ .vtv-epg-item.now{background:#0066cc;color:#fff;font-weight:700}
117
+ .vtv-epg-item .epg-t{font-size:7px;color:#6a8aaa}
118
+ .vtv-epg-item.now .epg-t{color:#aaccee}
119
+ .vtv-epg-item .epg-n{color:#ccc;font-size:8px}
120
+ .vtv-epg-item.now .epg-n{color:#fff}
121
+ `;
122
+ document.head.appendChild(s);
123
+
124
+ function getCurrentHour(){
125
+ return new Date().getHours();
126
+ }
127
+
128
+ function buildEPGHTML(chId){
129
+ const epg = EPG[chId] || [];
130
+ if(!epg.length) return '';
131
+ const curH = getCurrentHour();
132
+ let items = '';
133
+ epg.forEach(item => {
134
+ const itemH = parseInt(item.t.split(':')[0], 10);
135
+ const isNow = itemH <= curH && (itemH + 2) > curH;
136
+ items += `<div class="vtv-epg-item${isNow?' now':''}"><div class="epg-t">${item.t}</div><div class="epg-n">${item.n}</div></div>`;
137
+ });
138
+ return `<div class="vtv-epg" id="vtv-epg">' +
139
+ '<div class="vtv-epg-header"><span class="vtv-epg-title">📋 Lịch phát sóng</span>' +
140
+ '<button class="vtv-epg-toggle" onclick="window._vtvToggleEPG()">Ẩn/Hiện</button></div>' +
141
+ '<div class="vtv-epg-list" id="vtv-epg-list">' + items + '</div></div>';
142
+ }
143
+
144
+ window._vtvToggleEPG = function(){
145
+ const list = document.getElementById('vtv-epg-list');
146
+ if(list) list.style.display = list.style.display === 'none' ? 'flex' : 'none';
147
+ };
148
+
149
+ async function loadAllStreams(){
150
+ if(_loading) return;
151
+ _loading = true;
152
+ const loadEl = document.getElementById('vtv-load');
153
+ if(loadEl) loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang tải danh sách kênh...';
154
+
155
+ try {
156
+ const r = await fetch('/api/vtv/streams', {signal: AbortSignal.timeout(10000)});
157
+ if(r.ok){
158
+ const data = await r.json();
159
+ CHANNELS.forEach(ch => {
160
+ const info = data[ch.id];
161
+ if(info && info.stream_url){
162
+ // Always proxy through backend to avoid CORS issues
163
+ const url = '/api/proxy/m3u8/vtv?url=' + encodeURIComponent(info.stream_url);
164
+ STREAMS[ch.id] = [url];
165
+ } else {
166
+ STREAMS[ch.id] = [];
167
+ }
168
+ });
169
+ }
170
+ } catch(e) {
171
+ console.warn('VTV API error:', e);
172
+ }
173
+
174
+ CHANNELS.forEach(ch => {
175
+ const tab = document.getElementById('vtvt-'+ch.id);
176
+ if(tab){
177
+ if(STREAMS[ch.id] && STREAMS[ch.id].length > 0){
178
+ tab.classList.remove('off');
179
+ tab.textContent = ch.name;
180
+ } else {
181
+ tab.style.opacity = '0.35';
182
+ tab.textContent = ch.name + ' ✕';
183
+ }
184
+ }
185
+ });
186
+ _loading = false;
187
+ }
188
+
189
+ function buildBlock(){
190
+ const w = document.createElement('div');
191
+ w.className = 'vtv-wrap';
192
+ w.id = 'vtv-block';
193
+ let tabs = '';
194
+ CHANNELS.forEach(ch => {
195
+ tabs += '<button class="vtv-tab off" id="vtvt-'+ch.id+'" onclick="window._vtvPlay(\''+ch.id+'\')">'+ch.name+'</button>';
196
+ });
197
+ w.innerHTML =
198
+ '<div class="vtv-head"><span class="vtv-title">📺 VTV Trực Tuyến</span><span class="vtv-badge">● LIVE</span></div>' +
199
+ '<div class="vtv-tabs">' + tabs + '</div>' +
200
+ '<div class="vtv-frame">' +
201
+ '<div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div>' +
202
+ '<video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video>' +
203
+ '<div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="window._vtvRetry()">Thử lại</button></div>' +
204
+ '</div>';
205
+ return w;
206
+ }
207
+
208
+ // ===== PIN BLOCK — called only once via loadHome wrapper =====
209
+ function pinBlock(){
210
+ const h = document.getElementById('view-home');
211
+ if(!h || document.getElementById('vtv-block')) return;
212
+ h.insertBefore(buildBlock(), h.firstChild);
213
+ loadAllStreams().then(() => {
214
+ // Default to VTV6 if available, otherwise first available channel
215
+ const tryOrder = ['vtv6','vtv1','vtv2','vtv3','vtv4','vtv5','vtv7','vtv8','vtv9','vtv10'];
216
+ for(const chId of tryOrder){
217
+ if(STREAMS[chId] && STREAMS[chId].length > 0){
218
+ setTimeout(() => window._vtvPlay(chId), 300);
219
+ return;
220
+ }
221
+ }
222
+ });
223
+ }
224
+
225
+ window._vtvRetry = function(){
226
+ if(_currentCh) window._vtvPlay(_currentCh);
227
+ };
228
+
229
+ window._vtvPlay = function(chId){
230
+ const ch = CHANNELS.find(c => c.id === chId);
231
+ if(!ch) return;
232
+ _currentCh = chId;
233
+ document.querySelectorAll('.vtv-tab').forEach(t => t.classList.remove('on'));
234
+ const tab = document.getElementById('vtvt-'+chId);
235
+ if(tab) tab.classList.add('on');
236
+ const video = document.getElementById('vtv-player');
237
+ const errEl = document.getElementById('vtv-err');
238
+ const loadEl = document.getElementById('vtv-load');
239
+ const errMsg = document.getElementById('vtv-err-msg');
240
+ video.style.display = 'none';
241
+ errEl.style.display = 'none';
242
+ loadEl.style.display = 'flex';
243
+ loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + ch.name + '...';
244
+ if(_hls){ _hls.destroy(); _hls = null; }
245
+ const urls = STREAMS[chId] || [];
246
+ if(urls.length === 0){
247
+ loadEl.style.display = 'none';
248
+ errEl.style.display = 'flex';
249
+ if(chId === 'vtvprime'){
250
+ errMsg.textContent = 'VTVPrime: Kênh trả phí, không có luồng miễn phí.';
251
+ } else {
252
+ errMsg.textContent = ch.name + ': Không tìm thấy luồng. Thử lại sau.';
253
+ }
254
+ return;
255
+ }
256
+ // Update EPG
257
+ const epgEl = document.getElementById('vtv-epg');
258
+ if(epgEl) epgEl.remove();
259
+ const frame = document.querySelector('.vtv-frame');
260
+ if(frame){
261
+ const epgDiv = document.createElement('div');
262
+ epgDiv.innerHTML = buildEPGHTML(chId);
263
+ frame.appendChild(epgDiv.firstElementChild);
264
+ }
265
+ _tryPlay(video, urls, 0, ch.name, loadEl, errEl, errMsg);
266
+ };
267
+
268
+ function _tryPlay(video, urls, idx, name, loadEl, errEl, errMsg){
269
+ if(idx >= urls.length){
270
+ loadEl.style.display = 'none';
271
+ errEl.style.display = 'flex';
272
+ errMsg.textContent = name + ': Tất cả nguồn đều lỗi. Thử lại sau.';
273
+ return;
274
+ }
275
+ const src = urls[idx];
276
+ const sourceLabel = ' (' + (idx+1) + '/' + urls.length + ')';
277
+ loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + name + sourceLabel + '...';
278
+ if(typeof Hls !== 'undefined' && Hls.isSupported()){
279
+ const hls = new Hls({
280
+ enableWorker: true,
281
+ lowLatencyMode: true,
282
+ startLevel: -1,
283
+ capLevelToPlayerSize: true,
284
+ maxBufferLength: 20,
285
+ xhrSetup: function(xhr, url){
286
+ if(url.includes('fptplay')){
287
+ xhr.setRequestHeader('Referer', 'https://fptplay.vn/');
288
+ xhr.setRequestHeader('Origin', 'https://fptplay.vn');
289
+ }
290
+ }
291
+ });
292
+ _hls = hls;
293
+ hls.loadSource(src);
294
+ hls.attachMedia(video);
295
+ hls.on(Hls.Events.MANIFEST_PARSED, () => {
296
+ video.play().catch(() => {});
297
+ loadEl.style.display = 'none';
298
+ video.style.display = 'block';
299
+ });
300
+ let recoverAttempts = 0;
301
+ hls.on(Hls.Events.ERROR, (ev, data) => {
302
+ if(data.fatal){
303
+ if(data.type === Hls.ErrorTypes.NETWORK_ERROR){
304
+ recoverAttempts++;
305
+ if(recoverAttempts <= 3){
306
+ setTimeout(() => hls.startLoad(), 2000);
307
+ } else {
308
+ hls.destroy();
309
+ _hls = null;
310
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
311
+ }
312
+ } else if(data.type === Hls.ErrorTypes.MEDIA_ERROR){
313
+ try { hls.recoverMediaError(); } catch(e) {}
314
+ } else {
315
+ hls.destroy();
316
+ _hls = null;
317
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
318
+ }
319
+ }
320
+ });
321
+ } else if(video.canPlayType('application/vnd.apple.mpegurl')){
322
+ video.src = src;
323
+ video.addEventListener('loadedmetadata', () => {
324
+ video.play().catch(() => {});
325
+ loadEl.style.display = 'none';
326
+ video.style.display = 'block';
327
+ }, {once: true});
328
+ video.addEventListener('error', () => {
329
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
330
+ }, {once: true});
331
+ } else {
332
+ loadEl.style.display = 'none';
333
+ errEl.style.display = 'flex';
334
+ errMsg.textContent = 'Trình duyệt không hỗ trợ HLS';
335
+ }
336
+ }
337
+
338
+ // ===== ONLY wrap loadHome — no DOMContentLoaded listener to avoid double-load =====
339
+ const orig = window.loadHome;
340
+ if(orig && !orig.__vtvWrapped){
341
+ window.loadHome = async function(){
342
+ const r = await orig.apply(this, arguments);
343
+ try{ pinBlock(); }catch(e){}
344
+ return r;
345
+ };
346
+ window.loadHome.__vtvWrapped = true;
347
+ }
348
+ })();
static/yt_live_v2.js CHANGED
@@ -1,72 +1,348 @@
1
  // === VNEWS — VTV1-VTV10 + VTVPrime LIVE CHANNELS + EPG ===
 
 
 
2
  (function(){
3
  if(window._ytLiveLoaded) return;
4
  window._ytLiveLoaded = true;
 
5
  const CHANNELS = [
6
- {id:'vtv1',name:'VTV1',badge:'Tin tức'},{id:'vtv2',name:'VTV2',badge:'Khoa học'},{id:'vtv3',name:'VTV3',badge:'Giải trí'},
7
- {id:'vtv4',name:'VTV4',badge:'Quốc tế'},{id:'vtv5',name:'VTV5',badge:'Miền Nam'},{id:'vtv6',name:'VTV6',badge:'Thanh niên'},
8
- {id:'vtv7',name:'VTV7',badge:'Giáo dục'},{id:'vtv8',name:'VTV8',badge:'Miền Trung'},{id:'vtv9',name:'VTV9',badge:'Miền Bắc'},
9
- {id:'vtv10',name:'VTV10',badge:'VTV10'},{id:'vtvprime',name:'VTVPrime',badge:'Prime'},
 
 
 
 
 
 
 
10
  ];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  const STREAMS = {};
12
- let _currentCh = null, _hls = null, _loading = false;
 
 
 
 
13
  const s = document.createElement('style');
14
- s.textContent = '.vtv-wrap{margin:6px 4px;background:#111;border:1px solid #0066cc;border-radius:10px;overflow:hidden}.vtv-head{display:flex;align-items:center;gap:8px;padding:8px 10px;background:linear-gradient(90deg,#003366,#1a1a1a)}.vtv-title{font-size:13px;font-weight:800;color:#00ccff}.vtv-badge{font-size:10px;font-weight:800;color:#00ccff;animation:vtvp 1.3s infinite}@keyframes vtvp{0%,100%{opacity:1}50%{opacity:.3}}.vtv-tabs{display:flex;gap:3px;padding:6px 8px;overflow-x:auto;scrollbar-width:none;background:#0d1a2a}.vtv-tab{padding:4px 8px;background:#1a2a3a;border:1px solid #2a3a4a;border-radius:10px;color:#8ab4d8;font-size:9px;cursor:pointer;white-space:nowrap;flex-shrink:0}.vtv-tab.on{background:#0066cc;border-color:#00ccff;color:#fff;font-weight:700}.vtv-tab.off{opacity:.35;pointer-events:none}.vtv-frame{position:relative;width:100%;aspect-ratio:16/9;background:#000;min-height:180px}.vtv-frame video{position:absolute;inset:0;width:100%;height:100%;object-fit:contain}.vtv-err{display:flex;align-items:center;justify-content:center;height:180px;color:#888;font-size:12px;flex-direction:column;gap:8px}.vtv-err button{background:#0066cc;border:none;color:#fff;padding:6px 14px;border-radius:8px;font-size:11px;cursor:pointer}.vtv-load{display:flex;align-items:center;justify-content:center;height:180px;color:#00ccff;font-size:12px;flex-direction:column;gap:8px}.vtv-spinner{width:24px;height:24px;border:2px solid #333;border-top-color:#00ccff;border-radius:50%;animation:vtvspin .8s linear infinite}@keyframes vtvspin{to{transform:rotate(360deg)}}';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  document.head.appendChild(s);
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  async function loadAllStreams(){
18
- if(_loading) return; _loading = true;
 
19
  const loadEl = document.getElementById('vtv-load');
20
  if(loadEl) loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang tải danh sách kênh...';
21
- try { const r = await fetch('/api/vtv/streams',{signal:AbortSignal.timeout(10000)}); if(r.ok){ const data = await r.json(); CHANNELS.forEach(ch => { const info = data[ch.id]; STREAMS[ch.id] = (info && info.stream_url) ? ['/api/proxy/m3u8/vtv?url='+encodeURIComponent(info.stream_url)] : []; }); } } catch(e){}
22
- CHANNELS.forEach(ch => { const tab = document.getElementById('vtvt-'+ch.id); if(tab){ if(STREAMS[ch.id]&&STREAMS[ch.id].length>0){tab.classList.remove('off');tab.textContent=ch.name;}else{tab.style.opacity='0.35';tab.textContent=ch.name+' ✕';} } });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  _loading = false;
24
  }
25
 
26
  function buildBlock(){
27
- const w = document.createElement('div'); w.className='vtv-wrap'; w.id='vtv-block';
 
 
28
  let tabs = '';
29
- CHANNELS.forEach(ch => { tabs += '<button class="vtv-tab off" id="vtvt-'+ch.id+'" onclick="window._vtvPlay(\''+ch.id+'\')">'+ch.name+'</button>'; });
30
- w.innerHTML = '<div class="vtv-head"><span class="vtv-title">📺 VTV Trực Tuyến</span><span class="vtv-badge">● LIVE</span></div><div class="vtv-tabs">'+tabs+'</div><div class="vtv-frame"><div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div><video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video><div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="window._vtvRetry()">Thử lại</button></div></div>';
 
 
 
 
 
 
 
 
 
31
  return w;
32
  }
33
 
 
34
  function pinBlock(){
35
  const h = document.getElementById('view-home');
36
  if(!h || document.getElementById('vtv-block')) return;
37
  h.insertBefore(buildBlock(), h.firstChild);
38
- loadAllStreams().then(() => { const order=['vtv6','vtv1','vtv2','vtv3','vtv4','vtv5','vtv7','vtv8','vtv9','vtv10']; for(const c of order){if(STREAMS[c]&&STREAMS[c].length>0){setTimeout(()=>window._vtvPlay(c),300);return;}} });
 
 
 
 
 
 
 
 
 
39
  }
40
 
41
- window._vtvRetry = function(){ if(_currentCh) window._vtvPlay(_currentCh); };
 
 
42
 
43
  window._vtvPlay = function(chId){
44
- const ch = CHANNELS.find(c => c.id === chId); if(!ch) return;
45
- _currentCh = chId; document.querySelectorAll('.vtv-tab').forEach(t=>t.classList.remove('on'));
46
- const tab = document.getElementById('vtvt-'+chId); if(tab) tab.classList.add('on');
47
- const video = document.getElementById('vtv-player'), errEl = document.getElementById('vtv-err'), loadEl = document.getElementById('vtv-load'), errMsg = document.getElementById('vtv-err-msg');
48
- video.style.display='none'; errEl.style.display='none'; loadEl.style.display='flex';
49
- loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối '+ch.name+'...';
50
- if(_hls){_hls.destroy();_hls=null;}
 
 
 
 
 
 
 
 
51
  const urls = STREAMS[chId] || [];
52
- if(!urls.length){loadEl.style.display='none';errEl.style.display='flex';errMsg.textContent=ch.name+': Không tìm thấy luồng.';return;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  _tryPlay(video, urls, 0, ch.name, loadEl, errEl, errMsg);
54
  };
55
 
56
  function _tryPlay(video, urls, idx, name, loadEl, errEl, errMsg){
57
- if(idx>=urls.length){loadEl.style.display='none';errEl.style.display='flex';errMsg.textContent=name+': Tất cả nguồn lỗi.';return;}
58
- const src=urls[idx]; loadEl.innerHTML='<div class="vtv-spinner"></div>Đang kết nối '+name+' ('+(idx+1)+'/'+urls.length+')...';
59
- if(typeof Hls!=='undefined'&&Hls.isSupported()){
60
- const hls=new Hls({enableWorker:true,lowLatencyMode:true,startLevel:-1,capLevelToPlayerSize:true,maxBufferLength:20,xhrSetup:function(xhr,url){if(url.includes('fptplay')){xhr.setRequestHeader('Referer','https://fptplay.vn/');xhr.setRequestHeader('Origin','https://fptplay.vn');}}});
61
- _hls=hls; hls.loadSource(src); hls.attachMedia(video);
62
- hls.on(Hls.Events.MANIFEST_PARSED,()=>{video.play().catch(()=>{});loadEl.style.display='none';video.style.display='block';});
63
- let ra=0; hls.on(Hls.Events.ERROR,(ev,data)=>{if(data.fatal){if(data.type===Hls.ErrorTypes.NETWORK_ERROR){ra++;if(ra<=3)setTimeout(()=>hls.startLoad(),2000);else{hls.destroy();_hls=null;_tryPlay(video,urls,idx+1,name,loadEl,errEl,errMsg);}}else if(data.type===Hls.ErrorTypes.MEDIA_ERROR){try{hls.recoverMediaError();}catch(e){}}else{hls.destroy();_hls=null;_tryPlay(video,urls,idx+1,name,loadEl,errEl,errMsg);}}});
64
- }else if(video.canPlayType('application/vnd.apple.mpegurl')){
65
- video.src=src; video.addEventListener('loadedmetadata',()=>{video.play().catch(()=>{});loadEl.style.display='none';video.style.display='block';},{once:true});
66
- video.addEventListener('error',()=>{_tryPlay(video,urls,idx+1,name,loadEl,errEl,errMsg);},{once:true});
67
- }else{loadEl.style.display='none';errEl.style.display='flex';errMsg.textContent='Trình duyệt không hỗ trợ HLS';}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  }
69
 
70
- const orig=window.loadHome;
71
- if(orig&&!orig.__vtvWrapped){window.loadHome=async function(){const r=await orig.apply(this,arguments);try{pinBlock();}catch(e){}return r;};window.loadHome.__vtvWrapped=true;}
72
- })();
 
 
 
 
 
 
 
 
 
1
  // === VNEWS — VTV1-VTV10 + VTVPrime LIVE CHANNELS + EPG ===
2
+ // Uses backend /api/vtv/streams for stream URLs
3
+ // Default channel: VTV6 | No double-load | EPG schedule
4
+
5
  (function(){
6
  if(window._ytLiveLoaded) return;
7
  window._ytLiveLoaded = true;
8
+
9
  const CHANNELS = [
10
+ {id:'vtv1', name:'VTV1', badge:'Tin tức'},
11
+ {id:'vtv2', name:'VTV2', badge:'Khoa học'},
12
+ {id:'vtv3', name:'VTV3', badge:'Giải trí'},
13
+ {id:'vtv4', name:'VTV4', badge:'Quốc tế'},
14
+ {id:'vtv5', name:'VTV5', badge:'Miền Nam'},
15
+ {id:'vtv6', name:'VTV6', badge:'Thanh niên'},
16
+ {id:'vtv7', name:'VTV7', badge:'Giáo dục'},
17
+ {id:'vtv8', name:'VTV8', badge:'Miền Trung'},
18
+ {id:'vtv9', name:'VTV9', badge:'Miền Bắc'},
19
+ {id:'vtv10', name:'VTV10', badge:'VTV10'},
20
+ {id:'vtvprime', name:'VTVPrime', badge:'Prime'},
21
  ];
22
+
23
+ // ===== EPG — Lịch phát sóng mẫu cho từng kênh =====
24
+ const EPG = {
25
+ vtv1: [
26
+ {t:'06:00',n:'Nhật ký ngày mai'},{t:'07:00',n:'Thời sự sáng'},{t:'09:00',n:'Thời sự'},
27
+ {t:'12:00',n:'Thời sự trưa'},{t:'15:00',n:'Thời sự chiều'},{t:'19:00',n:'Thời sự tối'},
28
+ {t:'21:00',n:'Thời sự đêm'},{t:'23:00',n:'Nhật ký ngày mai'},
29
+ ],
30
+ vtv2: [
31
+ {t:'06:00',n:'Khoa học & Công nghệ'},{t:'08:00',n:'Thế giới tự nhiên'},{t:'10:00',n:'Khoa học 360'},
32
+ {t:'12:00',n:'Đi tìm giải pháp'},{t:'14:00',n:'Sức khỏe & Cuộc sống'},{t:'16:00',n:'Khoa học cho mọi nhà'},
33
+ {t:'18:00',n:'Thế giới động vật'},{t:'20:00',n:'Khoa học & Tương lai'},{t:'22:00',n:'Tài liệu khoa học'},
34
+ ],
35
+ vtv3: [
36
+ {t:'06:00',n:'Sáng vui'},{t:'08:00',n:'Phim truyện'},{t:'10:00',n:'Gameshow'},
37
+ {t:'12:00',n:'Âm nhạc'},{t:'14:00',n:'Phim truyện'},{t:'16:00',n:'Giải trí chiều'},
38
+ {t:'18:00',n:'Tạp kỹ thuật số'},{t:'20:00',n:'Phim truyện đặc biệt'},{t:'22:00',n:'Đêm giải trí'},
39
+ ],
40
+ vtv4: [
41
+ {t:'06:00',n:'News'},{t:'08:00',n:'World News'},{t:'10:00',n:'Culture'},
42
+ {t:'12:00',n:'Midday News'},{t:'14:00',n:'Documentary'},{t:'16:00',n:'Sports'},
43
+ {t:'18:00',n:'Evening News'},{t:'20:00',n:'World Today'},{t:'22:00',n:'Nightline'},
44
+ ],
45
+ vtv5: [
46
+ {t:'06:00',n:'Thời sự miền Nam'},{t:'08:00',n:'Chương trình thiếu nhi'},{t:'10:00',n:'Phim truyện'},
47
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Nam'},
48
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
49
+ ],
50
+ vtv6: [
51
+ {t:'06:00',n:'Khởi động ngày mới'},{t:'08:00',n:'Thanh niên & Sáng tạo'},{t:'10:00',n:'Thế giới trẻ'},
52
+ {t:'12:00',n:'Nhịp sống trẻ'},{t:'14:00',n:'Thể thao tuổi trẻ'},{t:'16:00',n:'Giải trí thanh niên'},
53
+ {t:'18:00',n:'Thời sự trẻ'},{t:'20:00',n:'Đêm nhạc'},{t:'22:00',n:'Thanh niên & Đêm'},
54
+ ],
55
+ vtv7: [
56
+ {t:'06:00',n:'Giáo dục sáng'},{t:'08:00',n:'Học mọi lúc'},{t:'10:00',n:'Kỹ năng sống'},
57
+ {t:'12:00',n:'Giáo dục trưa'},{t:'14:00',n:'Học trực tuyến'},{t:'16:00',n:'Thiếu nhi'},
58
+ {t:'18:00',n:'Giáo dục chiều'},{t:'20:00',n:'Tài liệu giáo dục'},{t:'22:00',n:'Học suốt đời'},
59
+ ],
60
+ vtv8: [
61
+ {t:'06:00',n:'Thời sự miền Trung'},{t:'08:00',n:'Văn hóa miền Trung'},{t:'10:00',n:'Phim truyện'},
62
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Trung'},
63
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
64
+ ],
65
+ vtv9: [
66
+ {t:'06:00',n:'Thời sự miền Bắc'},{t:'08:00',n:'Văn hóa miền Bắc'},{t:'10:00',n:'Phim truyện'},
67
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao miền Bắc'},
68
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
69
+ ],
70
+ vtv10: [
71
+ {t:'06:00',n:'Thời sự Tây Nam Bộ'},{t:'08:00',n:'Văn hóa đồng bằng'},{t:'10:00',n:'Phim truyện'},
72
+ {t:'12:00',n:'Thời sự trưa'},{t:'14:00',n:'Giải trí'},{t:'16:00',n:'Thể thao Tây Nam Bộ'},
73
+ {t:'18:00',n:'Thời sự chiều'},{t:'20:00',n:'Phim truyện'},{t:'22:00',n:'Thời sự tối'},
74
+ ],
75
+ vtvprime: [
76
+ {t:'06:00',n:'Prime Morning'},{t:'08:00',n:'Prime Cinema'},{t:'10:00',n:'Prime Sports'},
77
+ {t:'12:00',n:'Prime News'},{t:'14:00',n:'Prime Drama'},{t:'16:00',n:'Prime Entertainment'},
78
+ {t:'18:00',n:'Prime Evening'},{t:'20:00',n:'Prime Night'},{t:'22:00',n:'Prime Late'},
79
+ ],
80
+ };
81
+
82
+ // ALL external streams need proxy — VTVGo/fptplay CDNs don't send CORS headers
83
  const STREAMS = {};
84
+ let _currentCh = null;
85
+ let _hls = null;
86
+ let _loading = false;
87
+ let _epgVisible = false;
88
+
89
  const s = document.createElement('style');
90
+ s.textContent = `
91
+ .vtv-wrap{margin:6px 4px;background:#111;border:1px solid #0066cc;border-radius:10px;overflow:hidden}
92
+ .vtv-head{display:flex;align-items:center;gap:8px;padding:8px 10px;background:linear-gradient(90deg,#003366,#1a1a1a)}
93
+ .vtv-title{font-size:13px;font-weight:800;color:#00ccff}
94
+ .vtv-badge{font-size:10px;font-weight:800;color:#00ccff;animation:vtvp 1.3s infinite}
95
+ @keyframes vtvp{0%,100%{opacity:1}50%{opacity:.3}}
96
+ .vtv-tabs{display:flex;gap:3px;padding:6px 8px;overflow-x:auto;scrollbar-width:none;background:#0d1a2a}
97
+ .vtv-tabs::-webkit-scrollbar{display:none}
98
+ .vtv-tab{padding:4px 8px;background:#1a2a3a;border:1px solid #2a3a4a;border-radius:10px;color:#8ab4d8;font-size:9px;cursor:pointer;white-space:nowrap;flex-shrink:0;transition:all .2s}
99
+ .vtv-tab:hover{background:#0b4a7a;color:#fff}
100
+ .vtv-tab.on{background:#0066cc;border-color:#00ccff;color:#fff;font-weight:700}
101
+ .vtv-tab.off{opacity:.35;pointer-events:none}
102
+ .vtv-frame{position:relative;width:100%;aspect-ratio:16/9;background:#000;min-height:180px}
103
+ .vtv-frame video{position:absolute;inset:0;width:100%;height:100%;object-fit:contain}
104
+ .vtv-err{display:flex;align-items:center;justify-content:center;height:180px;color:#888;font-size:12px;text-align:center;padding:20px;flex-direction:column;gap:8px}
105
+ .vtv-err button{background:#0066cc;border:none;color:#fff;padding:6px 14px;border-radius:8px;font-size:11px;cursor:pointer}
106
+ .vtv-load{display:flex;align-items:center;justify-content:center;height:180px;color:#00ccff;font-size:12px;flex-direction:column;gap:8px}
107
+ .vtv-spinner{width:24px;height:24px;border:2px solid #333;border-top-color:#00ccff;border-radius:50%;animation:vtvspin .8s linear infinite}
108
+ @keyframes vtvspin{to{transform:rotate(360deg)}}
109
+ .vtv-epg{margin:0;padding:6px 10px;background:#0a1628;border-top:1px solid #1a2a3a}
110
+ .vtv-epg-header{display:flex;align-items:center;justify-content:space-between;margin-bottom:4px}
111
+ .vtv-epg-title{font-size:10px;font-weight:700;color:#00ccff}
112
+ .vtv-epg-toggle{background:none;border:1px solid #2a3a4a;color:#8ab4d8;font-size:9px;padding:2px 8px;border-radius:6px;cursor:pointer}
113
+ .vtv-epg-list{display:flex;gap:4px;overflow-x:auto;scrollbar-width:none;padding-bottom:4px}
114
+ .vtv-epg-list::-webkit-scrollbar{display:none}
115
+ .vtv-epg-item{flex:0 0 auto;padding:3px 6px;background:#1a2a3a;border-radius:4px;font-size:8px;color:#8ab4d8;white-space:nowrap}
116
+ .vtv-epg-item.now{background:#0066cc;color:#fff;font-weight:700}
117
+ .vtv-epg-item .epg-t{font-size:7px;color:#6a8aaa}
118
+ .vtv-epg-item.now .epg-t{color:#aaccee}
119
+ .vtv-epg-item .epg-n{color:#ccc;font-size:8px}
120
+ .vtv-epg-item.now .epg-n{color:#fff}
121
+ `;
122
  document.head.appendChild(s);
123
 
124
+ function getCurrentHour(){
125
+ return new Date().getHours();
126
+ }
127
+
128
+ function buildEPGHTML(chId){
129
+ const epg = EPG[chId] || [];
130
+ if(!epg.length) return '';
131
+ const curH = getCurrentHour();
132
+ let items = '';
133
+ epg.forEach(item => {
134
+ const itemH = parseInt(item.t.split(':')[0], 10);
135
+ const isNow = itemH <= curH && (itemH + 2) > curH;
136
+ items += `<div class="vtv-epg-item${isNow?' now':''}"><div class="epg-t">${item.t}</div><div class="epg-n">${item.n}</div></div>`;
137
+ });
138
+ return `<div class="vtv-epg" id="vtv-epg">' +
139
+ '<div class="vtv-epg-header"><span class="vtv-epg-title">📋 Lịch phát sóng</span>' +
140
+ '<button class="vtv-epg-toggle" onclick="window._vtvToggleEPG()">Ẩn/Hiện</button></div>' +
141
+ '<div class="vtv-epg-list" id="vtv-epg-list">' + items + '</div></div>';
142
+ }
143
+
144
+ window._vtvToggleEPG = function(){
145
+ const list = document.getElementById('vtv-epg-list');
146
+ if(list) list.style.display = list.style.display === 'none' ? 'flex' : 'none';
147
+ };
148
+
149
  async function loadAllStreams(){
150
+ if(_loading) return;
151
+ _loading = true;
152
  const loadEl = document.getElementById('vtv-load');
153
  if(loadEl) loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang tải danh sách kênh...';
154
+
155
+ try {
156
+ const r = await fetch('/api/vtv/streams', {signal: AbortSignal.timeout(10000)});
157
+ if(r.ok){
158
+ const data = await r.json();
159
+ CHANNELS.forEach(ch => {
160
+ const info = data[ch.id];
161
+ if(info && info.stream_url){
162
+ // Always proxy through backend to avoid CORS issues
163
+ const url = '/api/proxy/m3u8/vtv?url=' + encodeURIComponent(info.stream_url);
164
+ STREAMS[ch.id] = [url];
165
+ } else {
166
+ STREAMS[ch.id] = [];
167
+ }
168
+ });
169
+ }
170
+ } catch(e) {
171
+ console.warn('VTV API error:', e);
172
+ }
173
+
174
+ CHANNELS.forEach(ch => {
175
+ const tab = document.getElementById('vtvt-'+ch.id);
176
+ if(tab){
177
+ if(STREAMS[ch.id] && STREAMS[ch.id].length > 0){
178
+ tab.classList.remove('off');
179
+ tab.textContent = ch.name;
180
+ } else {
181
+ tab.style.opacity = '0.35';
182
+ tab.textContent = ch.name + ' ✕';
183
+ }
184
+ }
185
+ });
186
  _loading = false;
187
  }
188
 
189
  function buildBlock(){
190
+ const w = document.createElement('div');
191
+ w.className = 'vtv-wrap';
192
+ w.id = 'vtv-block';
193
  let tabs = '';
194
+ CHANNELS.forEach(ch => {
195
+ tabs += '<button class="vtv-tab off" id="vtvt-'+ch.id+'" onclick="window._vtvPlay(\''+ch.id+'\')">'+ch.name+'</button>';
196
+ });
197
+ w.innerHTML =
198
+ '<div class="vtv-head"><span class="vtv-title">📺 VTV Trực Tuyến</span><span class="vtv-badge">● LIVE</span></div>' +
199
+ '<div class="vtv-tabs">' + tabs + '</div>' +
200
+ '<div class="vtv-frame">' +
201
+ '<div class="vtv-load" id="vtv-load"><div class="vtv-spinner"></div>Đang tải danh sách kênh...</div>' +
202
+ '<video id="vtv-player" playsinline muted controls preload="auto" style="display:none"></video>' +
203
+ '<div class="vtv-err" id="vtv-err" style="display:none"><span id="vtv-err-msg">Không thể tải kênh</span><button onclick="window._vtvRetry()">Thử lại</button></div>' +
204
+ '</div>';
205
  return w;
206
  }
207
 
208
+ // ===== PIN BLOCK — called only once via loadHome wrapper =====
209
  function pinBlock(){
210
  const h = document.getElementById('view-home');
211
  if(!h || document.getElementById('vtv-block')) return;
212
  h.insertBefore(buildBlock(), h.firstChild);
213
+ loadAllStreams().then(() => {
214
+ // Default to VTV6 if available, otherwise first available channel
215
+ const tryOrder = ['vtv6','vtv1','vtv2','vtv3','vtv4','vtv5','vtv7','vtv8','vtv9','vtv10'];
216
+ for(const chId of tryOrder){
217
+ if(STREAMS[chId] && STREAMS[chId].length > 0){
218
+ setTimeout(() => window._vtvPlay(chId), 300);
219
+ return;
220
+ }
221
+ }
222
+ });
223
  }
224
 
225
+ window._vtvRetry = function(){
226
+ if(_currentCh) window._vtvPlay(_currentCh);
227
+ };
228
 
229
  window._vtvPlay = function(chId){
230
+ const ch = CHANNELS.find(c => c.id === chId);
231
+ if(!ch) return;
232
+ _currentCh = chId;
233
+ document.querySelectorAll('.vtv-tab').forEach(t => t.classList.remove('on'));
234
+ const tab = document.getElementById('vtvt-'+chId);
235
+ if(tab) tab.classList.add('on');
236
+ const video = document.getElementById('vtv-player');
237
+ const errEl = document.getElementById('vtv-err');
238
+ const loadEl = document.getElementById('vtv-load');
239
+ const errMsg = document.getElementById('vtv-err-msg');
240
+ video.style.display = 'none';
241
+ errEl.style.display = 'none';
242
+ loadEl.style.display = 'flex';
243
+ loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + ch.name + '...';
244
+ if(_hls){ _hls.destroy(); _hls = null; }
245
  const urls = STREAMS[chId] || [];
246
+ if(urls.length === 0){
247
+ loadEl.style.display = 'none';
248
+ errEl.style.display = 'flex';
249
+ if(chId === 'vtvprime'){
250
+ errMsg.textContent = 'VTVPrime: Kênh trả phí, không có luồng miễn phí.';
251
+ } else {
252
+ errMsg.textContent = ch.name + ': Không tìm thấy luồng. Thử lại sau.';
253
+ }
254
+ return;
255
+ }
256
+ // Update EPG
257
+ const epgEl = document.getElementById('vtv-epg');
258
+ if(epgEl) epgEl.remove();
259
+ const frame = document.querySelector('.vtv-frame');
260
+ if(frame){
261
+ const epgDiv = document.createElement('div');
262
+ epgDiv.innerHTML = buildEPGHTML(chId);
263
+ frame.appendChild(epgDiv.firstElementChild);
264
+ }
265
  _tryPlay(video, urls, 0, ch.name, loadEl, errEl, errMsg);
266
  };
267
 
268
  function _tryPlay(video, urls, idx, name, loadEl, errEl, errMsg){
269
+ if(idx >= urls.length){
270
+ loadEl.style.display = 'none';
271
+ errEl.style.display = 'flex';
272
+ errMsg.textContent = name + ': Tất cả nguồn đều lỗi. Thử lại sau.';
273
+ return;
274
+ }
275
+ const src = urls[idx];
276
+ const sourceLabel = ' (' + (idx+1) + '/' + urls.length + ')';
277
+ loadEl.innerHTML = '<div class="vtv-spinner"></div>Đang kết nối ' + name + sourceLabel + '...';
278
+ if(typeof Hls !== 'undefined' && Hls.isSupported()){
279
+ const hls = new Hls({
280
+ enableWorker: true,
281
+ lowLatencyMode: true,
282
+ startLevel: -1,
283
+ capLevelToPlayerSize: true,
284
+ maxBufferLength: 20,
285
+ xhrSetup: function(xhr, url){
286
+ if(url.includes('fptplay')){
287
+ xhr.setRequestHeader('Referer', 'https://fptplay.vn/');
288
+ xhr.setRequestHeader('Origin', 'https://fptplay.vn');
289
+ }
290
+ }
291
+ });
292
+ _hls = hls;
293
+ hls.loadSource(src);
294
+ hls.attachMedia(video);
295
+ hls.on(Hls.Events.MANIFEST_PARSED, () => {
296
+ video.play().catch(() => {});
297
+ loadEl.style.display = 'none';
298
+ video.style.display = 'block';
299
+ });
300
+ let recoverAttempts = 0;
301
+ hls.on(Hls.Events.ERROR, (ev, data) => {
302
+ if(data.fatal){
303
+ if(data.type === Hls.ErrorTypes.NETWORK_ERROR){
304
+ recoverAttempts++;
305
+ if(recoverAttempts <= 3){
306
+ setTimeout(() => hls.startLoad(), 2000);
307
+ } else {
308
+ hls.destroy();
309
+ _hls = null;
310
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
311
+ }
312
+ } else if(data.type === Hls.ErrorTypes.MEDIA_ERROR){
313
+ try { hls.recoverMediaError(); } catch(e) {}
314
+ } else {
315
+ hls.destroy();
316
+ _hls = null;
317
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
318
+ }
319
+ }
320
+ });
321
+ } else if(video.canPlayType('application/vnd.apple.mpegurl')){
322
+ video.src = src;
323
+ video.addEventListener('loadedmetadata', () => {
324
+ video.play().catch(() => {});
325
+ loadEl.style.display = 'none';
326
+ video.style.display = 'block';
327
+ }, {once: true});
328
+ video.addEventListener('error', () => {
329
+ _tryPlay(video, urls, idx + 1, name, loadEl, errEl, errMsg);
330
+ }, {once: true});
331
+ } else {
332
+ loadEl.style.display = 'none';
333
+ errEl.style.display = 'flex';
334
+ errMsg.textContent = 'Trình duyệt không hỗ trợ HLS';
335
+ }
336
  }
337
 
338
+ // ===== ONLY wrap loadHome — no DOMContentLoaded listener to avoid double-load =====
339
+ const orig = window.loadHome;
340
+ if(orig && !orig.__vtvWrapped){
341
+ window.loadHome = async function(){
342
+ const r = await orig.apply(this, arguments);
343
+ try{ pinBlock(); }catch(e){}
344
+ return r;
345
+ };
346
+ window.loadHome.__vtvWrapped = true;
347
+ }
348
+ })();
vtv_api.py CHANGED
@@ -1,125 +1,107 @@
1
  """
2
- VTV Channels API v4 Fast, reliable, multi-source
3
- Strategy:
4
- - Return static sources immediately (< 100ms)
5
- - Try multiple CDN sources in parallel for fresh URLs
6
- - Short timeouts, no blocking
7
  """
8
  import re, time, threading
9
  import requests
10
  from fastapi import APIRouter, Query
11
  from fastapi.responses import JSONResponse, Response
12
- from concurrent.futures import ThreadPoolExecutor, as_completed
13
 
14
  router = APIRouter()
15
 
16
  UA = {
17
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
18
  "Accept-Language": "vi-VN,vi;q=0.9",
19
  "Referer": "https://hd.xemtv.net/",
20
  }
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  CHANNEL_NAMES = {
23
- "vtv1": "VTV1", "vtv2": "VTV2", "vtv3": "VTV3", "vtv4": "VTV4",
24
- "vtv5": "VTV5", "vtv6": "VTV6", "vtv7": "VTV7", "vtv8": "VTV8",
25
- "vtv9": "VTV9", "vtv10": "VTV10", "vtvprime": "VTVPrime",
 
 
 
 
 
 
 
 
26
  }
27
 
28
- # ===== STATIC SOURCE LISTS (primary + fallbacks) =====
29
- # Multiple CDN sources per channel for fast failover
30
- STATIC_SOURCES = {
31
- "vtv1": [
32
- "https://live-a.fptplay53.net/live/media/vtv1/live247-hls-avc/index.m3u8",
33
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv1-manifest.m3u8",
34
- "https://live.fptplay53.net/fnxch2/vtv1hd_abr.smil/chunklist.m3u8",
35
- ],
36
- "vtv2": [
37
- "https://live-a.fptplay53.net/live/media/vtv2/live247-hls-avc/index.m3u8",
38
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv2-manifest.m3u8",
39
- "https://live.fptplay53.net/fnxch2/vtv2hd_abr.smil/chunklist.m3u8",
40
- ],
41
- "vtv3": [
42
- "https://live-a.fptplay53.net/live/media/vtv3/live247-hls-avc/index.m3u8",
43
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv3-manifest.m3u8",
44
- "https://live.fptplay53.net/fnxch2/vtv3hd_abr.smil/chunklist.m3u8",
45
- ],
46
- "vtv4": [
47
- "https://live-a.fptplay53.net/live/media/vtv4/live247-hls-avc/index.m3u8",
48
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv4-manifest.m3u8",
49
- ],
50
- "vtv5": [
51
- "https://live-a.fptplay53.net/live/media/vtv5/live247-hls-avc/index.m3u8",
52
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv5-manifest.m3u8",
53
- "https://live-a.fptplay53.net/live/media/VTV5HD/live_hls_avc/index.m3u8",
54
- ],
55
- "vtv6": [
56
- "https://live-a.fptplay53.net/live/media/vtv6/live247-hls-avc/index.m3u8",
57
- "https://live-a.fptplay53.net/live/media/vtv6/live247-hls-avc/vtv6-avc1_5600000=10000-mp4a_131600=20000.m3u8",
58
- ],
59
- "vtv7": [
60
- "https://live-a.fptplay53.net/live/media/vtv7/live247-hls-avc/index.m3u8",
61
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv7-manifest.m3u8",
62
- "https://live.fptplay53.net/fnxhd1/vtv7hd_vhls.smil/chunklist_b5000000.m3u8",
63
- ],
64
- "vtv8": [
65
- "https://live.fptplay53.net/epzhd1/vtv8hd_vhls.smil/chunklist.m3u8",
66
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv8-manifest.m3u8",
67
- ],
68
- "vtv9": [
69
- "https://live.fptplay53.net/fnxhd1/vtv9hd_vhls.smil/chunklist.m3u8",
70
- "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv9-manifest.m3u8",
71
- ],
72
- "vtv10": [
73
- "https://live-a.fptplay53.net/live/media/vtv10/live247-hls-avc/index.m3u8",
74
- "https://live-a.fptplay53.net/live/media/vtv10/live247-hls-avc/vtv10-avc1_5600000=10000-mp4a_131600=20000.m3u8",
75
- ],
76
- "vtvprime": [
77
- "https://live.fptplay53.net/fnxch2/vtvprime_abr.smil/chunklist.m3u8",
78
- ],
79
  }
80
 
81
- # xemtv.net PHP endpoints (for fresh signed URLs, scraped in background)
82
- XEMTV_PHP = {
83
- "vtv1": "https://hd.xemtv.net/kenh/vtv1.php",
84
- "vtv2": "https://hd.xemtv.net/kenh/vtv2.php",
85
- "vtv3": "https://hd.xemtv.net/kenh/vtv3.php",
86
- "vtv4": "https://hd.xemtv.net/kenh/vtv4.php",
87
- "vtv5": "https://hd.xemtv.net/kenh/vtv5.php",
88
- "vtv6": "https://hd.xemtv.net/kenh/vtv6.php",
89
- "vtv7": "https://hd.xemtv.net/kenh/vtv7.php",
90
- "vtv8": "https://hd.xemtv.net/kenh/vtv8.php",
91
- "vtv9": "https://hd.xemtv.net/kenh/vtv9.php",
92
- "vtv10": "https://hd.xemtv.net/kenh/vtv10.php",
93
- "vtvprime": "https://hd.xemtv.net/kenh/vtvprime.php",
 
 
 
94
  }
95
 
96
- _vtv_xemtv_cache = {}
97
  _vtv_lock = threading.Lock()
98
- _XEMTV_CACHE_TTL = 300
99
 
100
 
101
- def _get_cached_xemtv(ch_id):
102
  with _vtv_lock:
103
- entry = _vtv_xemtv_cache.get(ch_id)
104
- if entry and time.time() - entry['t'] < _XEMTV_CACHE_TTL:
105
- return entry['url']
106
  return None
107
 
108
 
109
- def _set_cached_xemtv(ch_id, url):
110
  with _vtv_lock:
111
- _vtv_xemtv_cache[ch_id] = {'t': time.time(), 'url': url}
112
 
113
 
114
  def extract_m3u8_from_html(html):
 
115
  if not html:
116
  return None
117
- m = re.search(r"file\s*:\s*['\"]([^'\"]+\.(?:m3u8|smil)[^'\"]*)['\"]", html, re.IGNORECASE)
118
  if m:
119
  url = m.group(1).strip()
120
  if len(url) > 20:
121
  return url
122
- m = re.search(r"(https?://[^\s\"'<>\\]+\.(?:m3u8|smil)[^\s\"'<>\\]*)", html, re.IGNORECASE)
123
  if m:
124
  url = m.group(1).strip()
125
  if len(url) > 20:
@@ -127,88 +109,171 @@ def extract_m3u8_from_html(html):
127
  return None
128
 
129
 
130
- def scrape_xemtv_one(ch_id):
131
- php_url = XEMTV_PHP.get(ch_id)
 
132
  if not php_url:
133
  return None
134
  try:
135
- r = requests.get(php_url, headers=UA, timeout=5, allow_redirects=True)
136
  if r.status_code == 200:
137
- url = extract_m3u8_from_html(r.text)
138
- if url:
139
- _set_cached_xemtv(ch_id, url)
140
- return url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  except:
142
  pass
143
  return None
144
 
145
 
146
- def scrape_xemtv_all_parallel():
147
- """Scrape xemtv.net in background (non-blocking)."""
148
- with ThreadPoolExecutor(max_workers=4) as ex:
149
- futs = {ex.submit(scrape_xemtv_one, ch): ch for ch in XEMTV_PHP}
150
- for f in as_completed(futs, timeout=12):
151
- try:
152
- f.result()
153
- except:
154
- pass
155
-
156
-
157
- def get_stream_url(ch_id):
158
- ch_id = ch_id.lower().strip()
159
- name_map = {'vtvct': 'vtv10', 'vtv-can-tho': 'vtv10', 'cantho': 'vtv10'}
160
- ch_id = name_map.get(ch_id, ch_id)
161
- all_sources = STATIC_SOURCES.get(ch_id, [])
162
- if not all_sources:
163
- return None, "none", []
164
- xemtv_url = _get_cached_xemtv(ch_id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  if xemtv_url:
166
- sources = [xemtv_url] + [s for s in all_sources if s != xemtv_url]
167
- return xemtv_url, "xemtv", sources
168
- return all_sources[0], "static", all_sources
 
 
 
 
 
 
 
 
169
 
170
 
171
  @router.get("/api/vtv/streams")
172
  def api_vtv_streams():
173
- """Return all VTV channel streams. Fast response (< 100ms) using static sources."""
174
  result = {}
175
  for ch_id in CHANNEL_NAMES:
176
- url, source, all_sources = get_stream_url(ch_id)
177
  result[ch_id] = {
178
  "name": CHANNEL_NAMES[ch_id],
179
- "stream_url": url,
180
- "source": source,
181
- "all_sources": all_sources,
182
- "status": "ok" if url else "offline",
183
  }
184
- # Background: try to refresh xemtv cache (non-blocking)
185
- threading.Thread(target=scrape_xemtv_all_parallel, daemon=True).start()
186
  return JSONResponse(result)
187
 
188
 
189
  @router.get("/api/vtv/stream/{channel_id}")
190
  def api_vtv_stream(channel_id: str):
191
- url, source, all_sources = get_stream_url(channel_id)
192
- if url:
193
- return JSONResponse({"stream_url": url, "source": source, "all_sources": all_sources, "status": "ok"})
 
194
  return JSONResponse({"error": "stream not found", "status": "offline"}, status_code=404)
195
 
196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
  @router.get("/api/proxy/m3u8/vtv")
198
  def proxy_vtv_m3u8(url: str = Query(...)):
199
- """Proxy m3u8 manifest, rewrite segment URLs to go through our proxy."""
200
  try:
201
  headers = {"User-Agent": UA["User-Agent"], "Accept": "*/*"}
202
  if "fptplay" in url:
203
  headers["Referer"] = "https://fptplay.vn/"
204
  headers["Origin"] = "https://fptplay.vn"
205
- elif "vtvgolive" in url or "vtvdigital" in url:
206
- headers["Referer"] = "https://vtvgo.vn/"
207
  elif "xemtv" in url:
208
  headers["Referer"] = "https://hd.xemtv.net/"
209
- r = requests.get(url, headers=headers, timeout=5, allow_redirects=True)
210
  if r.status_code != 200:
211
- return Response(status_code=502, content=f"upstream error: {r.status_code}")
212
  content = r.text
213
  lines = content.split('\n')
214
  rewritten = []
@@ -222,27 +287,33 @@ def proxy_vtv_m3u8(url: str = Query(...)):
222
  if not seg_url.startswith('http'):
223
  seg_url = base_url + seg_url
224
  rewritten.append("/api/proxy/seg/vtv?url=" + requests.utils.quote(seg_url, safe=""))
225
- return Response(content='\n'.join(rewritten).encode('utf-8'), media_type="application/vnd.apple.mpegurl", headers={"Access-Control-Allow-Origin": "*", "Cache-Control": "no-cache"})
 
 
 
 
226
  except Exception as e:
227
  return Response(status_code=502, content="proxy error: " + str(e))
228
 
229
 
230
  @router.get("/api/proxy/seg/vtv")
231
  def proxy_vtv_segment(url: str = Query(...)):
232
- """Proxy individual TS segments."""
233
  try:
234
  headers = {"User-Agent": UA["User-Agent"], "Accept": "*/*"}
235
  if "fptplay" in url:
236
  headers["Referer"] = "https://fptplay.vn/"
237
  headers["Origin"] = "https://fptplay.vn"
238
- elif "vtvgolive" in url or "vtvdigital" in url:
239
- headers["Referer"] = "https://vtvgo.vn/"
240
- r = requests.get(url, headers=headers, timeout=8, allow_redirects=True, stream=True)
241
  if r.status_code != 200:
242
- return Response(status_code=502, content=f"upstream error: {r.status_code}")
243
  data = r.content
244
  if len(data) > 188 and data[0:4] == b'\x89PNG' and data[188] == 0x47:
245
  data = data[188:]
246
- return Response(content=data, media_type="video/mp2t", headers={"Access-Control-Allow-Origin": "*", "Cache-Control": "public, max-age=3600"})
 
 
 
 
247
  except:
248
  return Response(status_code=502, content="proxy error")
 
1
  """
2
+ VTV Channels API - Backend endpoints for VTV1-VTV10 + VTVPrime
3
+ Fetches stream URLs from hd.xemtv.net PHP endpoints and VTVPrime
 
 
 
4
  """
5
  import re, time, threading
6
  import requests
7
  from fastapi import APIRouter, Query
8
  from fastapi.responses import JSONResponse, Response
 
9
 
10
  router = APIRouter()
11
 
12
  UA = {
13
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
14
  "Accept-Language": "vi-VN,vi;q=0.9",
15
  "Referer": "https://hd.xemtv.net/",
16
  }
17
 
18
+ # Channel ID -> xemtv.net PHP endpoint mapping
19
+ XEMTV_PHP_ENDPOINTS = {
20
+ "vtv1": "https://hd.xemtv.net/kenh/vtv1.php",
21
+ "vtv2": "https://hd.xemtv.net/kenh/vtv2.php",
22
+ "vtv3": "https://hd.xemtv.net/kenh/vtv3.php",
23
+ "vtv4": "https://hd.xemtv.net/kenh/vtv4.php",
24
+ "vtv5": "https://hd.xemtv.net/kenh/vtv5.php",
25
+ "vtv6": "https://hd.xemtv.net/kenh/vtv6.php",
26
+ "vtv7": "https://hd.xemtv.net/kenh/vtv7.php",
27
+ "vtv8": "https://hd.xemtv.net/kenh/vtv8.php",
28
+ "vtv9": "https://hd.xemtv.net/kenh/vtv9.php",
29
+ "vtv10": "https://hd.xemtv.net/kenh/vtv10.php",
30
+ "vtvprime": "https://hd.xemtv.net/kenh/vtvprime.php",
31
+ }
32
+
33
+ # Channel display names
34
  CHANNEL_NAMES = {
35
+ "vtv1": "VTV1",
36
+ "vtv2": "VTV2",
37
+ "vtv3": "VTV3",
38
+ "vtv4": "VTV4",
39
+ "vtv5": "VTV5",
40
+ "vtv6": "VTV6",
41
+ "vtv7": "VTV7",
42
+ "vtv8": "VTV8",
43
+ "vtv9": "VTV9",
44
+ "vtv10": "VTV10",
45
+ "vtvprime": "VTVPrime",
46
  }
47
 
48
+ # ===== PRIMARY: VTVGo failover CDN (verified working 2026) =====
49
+ VTVGO_FAILOVER = {
50
+ "vtv1": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv1-manifest.m3u8",
51
+ "vtv2": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv2-manifest.m3u8",
52
+ "vtv3": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv3-manifest.m3u8",
53
+ "vtv4": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv4-manifest.m3u8",
54
+ "vtv5": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv5-manifest.m3u8",
55
+ "vtv7": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv7-manifest.m3u8",
56
+ "vtv8": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv8-manifest.m3u8",
57
+ "vtv9": "https://vtvgolive-failover.vtvdigital.vn/vtvgo/vtv9-manifest.m3u8",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  }
59
 
60
+ # ===== Channels that should use xemtv.net scraping (not VTVGo failover) =====
61
+ # VTV6, VTV10: xemtv.net scraping (returns fptplay CDN URLs that need proxying)
62
+ XEMTV_ONLY_CHANNELS = {"vtv6", "vtv10"}
63
+
64
+ # ===== LAST RESORT: fptplay CDN URLs (need proxy for referer) =====
65
+ FPTPLAY_URLS = {
66
+ "vtv1": "https://live.fptplay53.net/fnxch2/vtv1hd_abr.smil/chunklist.m3u8",
67
+ "vtv2": "https://live.fptplay53.net/fnxch2/vtv2hd_abr.smil/chunklist.m3u8",
68
+ "vtv3": "https://live.fptplay53.net/fnxch2/vtv3hd_abr.smil/chunklist.m3u8",
69
+ "vtv4": "https://live.fptplay53.net/fnxch2/vtv4hd_abr.smil/chunklist.m3u8",
70
+ "vtv5": "https://live-a.fptplay53.net/live/media/VTV5HD/live_hls_avc/index.m3u8",
71
+ "vtv6": "https://live-a.fptplay53.net/live/media/vtv6/live247-hls-avc/vtv6-avc1_5600000=10000-mp4a_131600=20000.m3u8",
72
+ "vtv7": "https://live.fptplay53.net/fnxhd1/vtv7hd_vhls.smil/chunklist_b5000000.m3u8",
73
+ "vtv8": "https://live.fptplay53.net/epzhd1/vtv8hd_vhls.smil/chunklist.m3u8",
74
+ "vtv9": "https://live.fptplay53.net/fnxhd1/vtv9hd_vhls.smil/chunklist.m3u8",
75
+ "vtv10": "https://live-a.fptplay53.net/live/media/vtv10/live247-hls-avc/vtv10-avc1_5600000=10000-mp4a_131600=20000.m3u8",
76
  }
77
 
78
+ _vtv_cache = {}
79
  _vtv_lock = threading.Lock()
80
+ _CACHE_TTL = 180
81
 
82
 
83
+ def _cached(key):
84
  with _vtv_lock:
85
+ if key in _vtv_cache and time.time() - _vtv_cache[key]['t'] < _CACHE_TTL:
86
+ return _vtv_cache[key]['d']
 
87
  return None
88
 
89
 
90
+ def _set_cache(key, data):
91
  with _vtv_lock:
92
+ _vtv_cache[key] = {'t': time.time(), 'd': data}
93
 
94
 
95
  def extract_m3u8_from_html(html):
96
+ """Extract m3u8 URL from xemtv PHP page (jwplayer config)."""
97
  if not html:
98
  return None
99
+ m = re.search(r"file\s*:\s*['\"]([^'\"]*\.m3u8[^'\"]*)['\"]", html, re.IGNORECASE)
100
  if m:
101
  url = m.group(1).strip()
102
  if len(url) > 20:
103
  return url
104
+ m = re.search(r"(https?://[^\s\"'<>\\]+\.m3u8[^\s\"'<>\\]*)", html, re.IGNORECASE)
105
  if m:
106
  url = m.group(1).strip()
107
  if len(url) > 20:
 
109
  return None
110
 
111
 
112
+ def fetch_xemtv_stream(channel_id):
113
+ """Scrape xemtv.net PHP page for fresh stream URL."""
114
+ php_url = XEMTV_PHP_ENDPOINTS.get(channel_id)
115
  if not php_url:
116
  return None
117
  try:
118
+ r = requests.get(php_url, headers=UA, timeout=15, allow_redirects=True)
119
  if r.status_code == 200:
120
+ m3u8 = extract_m3u8_from_html(r.text)
121
+ if m3u8:
122
+ return m3u8
123
+ except:
124
+ pass
125
+ return None
126
+
127
+
128
+ def fetch_fptplay_stream(channel_id):
129
+ """Fetch fptplay CDN stream URL with proper Referer header."""
130
+ url = FPTPLAY_URLS.get(channel_id)
131
+ if not url:
132
+ return None
133
+ try:
134
+ headers = {
135
+ "User-Agent": UA["User-Agent"],
136
+ "Referer": "https://fptplay.vn/",
137
+ "Origin": "https://fptplay.vn",
138
+ }
139
+ r = requests.get(url, headers=headers, timeout=15, allow_redirects=True)
140
+ if r.status_code == 200:
141
+ return url
142
  except:
143
  pass
144
  return None
145
 
146
 
147
+ def fetch_vtv_stream(channel_id):
148
+ """
149
+ Fetch m3u8 stream URL for a VTV channel.
150
+ Priority:
151
+ 1. VTV1-VTV5, VTV7-VTV9: VTVGo failover CDN (no referer needed)
152
+ 2. VTV6, VTV10: xemtv.net scraping (user requirement) -> fptplay URL (proxied)
153
+ 3. VTVPrime: try xemtv, else None (paid channel)
154
+ """
155
+ channel_id = channel_id.lower().strip()
156
+ name_map = {
157
+ 'vtvct': 'vtv10', 'vtv-can-tho': 'vtv10', 'vtv can tho': 'vtv10',
158
+ 'vtv_can_tho': 'vtv10', 'cantho': 'vtv10',
159
+ 'vietnam_vtv1': 'vtv1', 'vietnam_vtv2': 'vtv2', 'vietnam_vtv3': 'vtv3',
160
+ 'vietnam_vtv4': 'vtv4', 'vietnam_vtv5': 'vtv5', 'vietnam_vtv6': 'vtv6',
161
+ 'vietnam_vtv7': 'vtv7', 'vietnam_vtv8': 'vtv8', 'vietnam_vtv9': 'vtv9',
162
+ }
163
+ channel_id = name_map.get(channel_id, channel_id)
164
+
165
+ cached = _cached(channel_id)
166
+ if cached is not None:
167
+ return cached
168
+
169
+ # --- VTV6: xemtv.net scraping (returns working fptplay URL) ---
170
+ # --- VTV10: xemtv returns vtvcantho (403), skip to fptplay CDN directly ---
171
+ if channel_id in XEMTV_ONLY_CHANNELS:
172
+ if channel_id == "vtv10":
173
+ # VTV10: xemtv.net returns dead vtvcantho URL (403), use fptplay CDN directly
174
+ fpt_url = FPTPLAY_URLS.get(channel_id)
175
+ if fpt_url:
176
+ _set_cache(channel_id, fpt_url)
177
+ return fpt_url
178
+ _set_cache(channel_id, None)
179
+ return None
180
+ xemtv_url = fetch_xemtv_stream(channel_id)
181
+ if xemtv_url:
182
+ _set_cache(channel_id, xemtv_url)
183
+ return xemtv_url
184
+ fpt_url = FPTPLAY_URLS.get(channel_id)
185
+ if fpt_url:
186
+ _set_cache(channel_id, fpt_url)
187
+ return fpt_url
188
+ _set_cache(channel_id, None)
189
+ return None
190
+
191
+ # --- VTV1-VTV5, VTV7-VTV9: VTVGo failover ---
192
+ if channel_id in VTVGO_FAILOVER:
193
+ result = VTVGO_FAILOVER[channel_id]
194
+ _set_cache(channel_id, result)
195
+ return result
196
+
197
+ # --- VTVPrime ---
198
+ if channel_id == 'vtvprime':
199
+ xemtv_url = fetch_xemtv_stream('vtvprime')
200
+ if xemtv_url:
201
+ _set_cache(channel_id, xemtv_url)
202
+ return xemtv_url
203
+ _set_cache(channel_id, None)
204
+ return None
205
+
206
+ # --- Fallback: xemtv scraping ---
207
+ xemtv_url = fetch_xemtv_stream(channel_id)
208
  if xemtv_url:
209
+ _set_cache(channel_id, xemtv_url)
210
+ return xemtv_url
211
+
212
+ # --- Last resort: fptplay ---
213
+ fpt_url = fetch_fptplay_stream(channel_id)
214
+ if fpt_url:
215
+ _set_cache(channel_id, fpt_url)
216
+ return fpt_url
217
+
218
+ _set_cache(channel_id, None)
219
+ return None
220
 
221
 
222
  @router.get("/api/vtv/streams")
223
  def api_vtv_streams():
224
+ """Get all VTV channel stream URLs."""
225
  result = {}
226
  for ch_id in CHANNEL_NAMES:
227
+ stream_url = fetch_vtv_stream(ch_id)
228
  result[ch_id] = {
229
  "name": CHANNEL_NAMES[ch_id],
230
+ "stream_url": stream_url,
231
+ "status": "ok" if stream_url else "offline",
 
 
232
  }
 
 
233
  return JSONResponse(result)
234
 
235
 
236
  @router.get("/api/vtv/stream/{channel_id}")
237
  def api_vtv_stream(channel_id: str):
238
+ """Get stream URL for a specific VTV channel."""
239
+ stream_url = fetch_vtv_stream(channel_id)
240
+ if stream_url:
241
+ return JSONResponse({"stream_url": stream_url, "status": "ok"})
242
  return JSONResponse({"error": "stream not found", "status": "offline"}, status_code=404)
243
 
244
 
245
+ @router.get("/api/proxy/page")
246
+ def proxy_page(url: str = Query(...)):
247
+ """Proxy a web page."""
248
+ try:
249
+ headers = {**UA}
250
+ if "xemtv.net" in url:
251
+ headers["Referer"] = "https://hd.xemtv.net/"
252
+ r = requests.get(url, headers=headers, timeout=15, allow_redirects=True)
253
+ if r.status_code != 200:
254
+ return Response(status_code=502, content="upstream error")
255
+ return Response(
256
+ content=r.text.encode("utf-8"),
257
+ media_type="text/html; charset=utf-8",
258
+ headers={"Access-Control-Allow-Origin": "*"}
259
+ )
260
+ except:
261
+ return Response(status_code=502, content="proxy error")
262
+
263
+
264
  @router.get("/api/proxy/m3u8/vtv")
265
  def proxy_vtv_m3u8(url: str = Query(...)):
266
+ """Proxy an m3u8 stream URL with proper headers for fptplay CDN."""
267
  try:
268
  headers = {"User-Agent": UA["User-Agent"], "Accept": "*/*"}
269
  if "fptplay" in url:
270
  headers["Referer"] = "https://fptplay.vn/"
271
  headers["Origin"] = "https://fptplay.vn"
 
 
272
  elif "xemtv" in url:
273
  headers["Referer"] = "https://hd.xemtv.net/"
274
+ r = requests.get(url, headers=headers, timeout=15, allow_redirects=True)
275
  if r.status_code != 200:
276
+ return Response(status_code=502, content="upstream error")
277
  content = r.text
278
  lines = content.split('\n')
279
  rewritten = []
 
287
  if not seg_url.startswith('http'):
288
  seg_url = base_url + seg_url
289
  rewritten.append("/api/proxy/seg/vtv?url=" + requests.utils.quote(seg_url, safe=""))
290
+ return Response(
291
+ content='\n'.join(rewritten).encode('utf-8'),
292
+ media_type="application/vnd.apple.mpegurl",
293
+ headers={"Access-Control-Allow-Origin": "*", "Cache-Control": "no-cache"}
294
+ )
295
  except Exception as e:
296
  return Response(status_code=502, content="proxy error: " + str(e))
297
 
298
 
299
  @router.get("/api/proxy/seg/vtv")
300
  def proxy_vtv_segment(url: str = Query(...)):
301
+ """Proxy a video segment with proper headers for fptplay CDN."""
302
  try:
303
  headers = {"User-Agent": UA["User-Agent"], "Accept": "*/*"}
304
  if "fptplay" in url:
305
  headers["Referer"] = "https://fptplay.vn/"
306
  headers["Origin"] = "https://fptplay.vn"
307
+ r = requests.get(url, headers=headers, timeout=30, allow_redirects=True)
 
 
308
  if r.status_code != 200:
309
+ return Response(status_code=502, content="upstream error")
310
  data = r.content
311
  if len(data) > 188 and data[0:4] == b'\x89PNG' and data[188] == 0x47:
312
  data = data[188:]
313
+ return Response(
314
+ content=data,
315
+ media_type="video/mp2t",
316
+ headers={"Access-Control-Allow-Origin": "*", "Cache-Control": "public, max-age=3600"}
317
+ )
318
  except:
319
  return Response(status_code=502, content="proxy error")
vtv_scraper.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ VTV Channels Scraper
3
+ Fetches stream URLs from hd.xemtv.net PHP endpoints for VTV1-VTV10 + VTV Cần Thơ
4
+ The PHP endpoints return jwplayer config with fresh stream URLs (important for VTV6 which has expiring signatures)
5
+ """
6
+ import requests, re, time, threading
7
+
8
+ UA = {
9
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
10
+ "Accept-Language": "vi-VN,vi;q=0.9",
11
+ "Referer": "https://hd.xemtv.net/",
12
+ }
13
+
14
+ # Channel ID -> xemtv.net PHP endpoint mapping
15
+ # These PHP pages return jwplayer config with fresh stream URLs
16
+ XEMTV_PHP_ENDPOINTS = {
17
+ "vtv1": "https://hd.xemtv.net/kenh/vtv1.php",
18
+ "vtv2": "https://hd.xemtv.net/kenh/vtv2.php",
19
+ "vtv3": "https://hd.xemtv.net/kenh/vtv3.php",
20
+ "vtv4": "https://hd.xemtv.net/kenh/vtv4.php",
21
+ "vtv5": "https://hd.xemtv.net/kenh/vtv5.php",
22
+ "vtv6": "https://hd.xemtv.net/kenh/vtv6.php",
23
+ "vtv7": "https://hd.xemtv.net/kenh/vtv7.php",
24
+ "vtv8": "https://hd.xemtv.net/kenh/vtv8.php",
25
+ "vtv9": "https://hd.xemtv.net/kenh/vtv9.php",
26
+ "vtv10": "https://hd.xemtv.net/kenh/vtv10.php", # VTV Cần Thơ
27
+ }
28
+
29
+ # Channel display names
30
+ CHANNEL_NAMES = {
31
+ "vtv1": "VTV1",
32
+ "vtv2": "VTV2",
33
+ "vtv3": "VTV3",
34
+ "vtv4": "VTV4",
35
+ "vtv5": "VTV5",
36
+ "vtv6": "VTV6",
37
+ "vtv7": "VTV7",
38
+ "vtv8": "VTV8",
39
+ "vtv9": "VTV9",
40
+ "vtv10": "VTV Cần Thơ",
41
+ }
42
+
43
+ # Fallback CDN streams (fptplay) — used when xemtv scraping fails
44
+ CDN_FALLBACK = {
45
+ "vtv1": "https://live.fptplay53.net/fnxch2/vtv1hd_abr.smil/chunklist.m3u8",
46
+ "vtv2": "https://live.fptplay53.net/fnxch2/vtv2hd_abr.smil/chunklist.m3u8",
47
+ "vtv3": "https://live.fptplay53.net/fnxch2/vtv3hd_abr.smil/chunklist.m3u8",
48
+ "vtv4": "https://live.fptplay53.net/fnxch2/vtv4hd_abr.smil/chunklist.m3u8",
49
+ "vtv5": "https://live-a.fptplay53.net/live/media/VTV5HD/live_hls_avc/index.m3u8",
50
+ "vtv7": "https://live.fptplay53.net/fnxhd1/vtv7hd_vhls.smil/chunklist_b5000000.m3u8",
51
+ "vtv8": "https://live.fptplay53.net/epzhd1/vtv8hd_vhls.smil/chunklist.m3u8",
52
+ "vtv9": "https://live.fptplay53.net/fnxhd1/vtv9hd_vhls.smil/chunklist.m3u8",
53
+ "vtv10": "https://live.fptplay53.net/fnxch2/vtvcantho_abr.smil/chunklist.m3u8",
54
+ # VTV6 fallback: try canthotv as alternative
55
+ "vtv6": "https://live.canthotv.vn/live/tv/chunklist.m3u8",
56
+ }
57
+
58
+ _vtv_cache = {}
59
+ _vtv_lock = threading.Lock()
60
+ _CACHE_TTL = 180 # 3 minutes — refresh frequently for VTV6 sign URLs
61
+
62
+
63
+ def _cached(key):
64
+ with _vtv_lock:
65
+ if key in _vtv_cache and time.time() - _vtv_cache[key]['t'] < _CACHE_TTL:
66
+ return _vtv_cache[key]['d']
67
+ return None
68
+
69
+
70
+ def _set_cache(key, data):
71
+ with _vtv_lock:
72
+ _vtv_cache[key] = {'t': time.time(), 'd': data}
73
+
74
+
75
+ def extract_m3u8_from_html(html):
76
+ """Extract m3u8 URL from xemtv PHP page (jwplayer config)."""
77
+ if not html:
78
+ return None
79
+ # jwplayer config: file: 'URL'
80
+ m = re.search(r"file\s*:\s*['\"]([^'\"]*\.m3u8[^'\"]*)['\"]", html, re.IGNORECASE)
81
+ if m:
82
+ url = m.group(1).strip()
83
+ if len(url) > 20:
84
+ return url
85
+ # Generic m3u8 pattern
86
+ m = re.search(r"(https?://[^\s\"'<>\\]+\.m3u8[^\s\"'<>\\]*)", html, re.IGNORECASE)
87
+ if m:
88
+ url = m.group(1).strip()
89
+ if len(url) > 20:
90
+ return url
91
+ return None
92
+
93
+
94
+ def fetch_vtv_stream(channel_id):
95
+ """Fetch m3u8 stream URL for a VTV channel by scraping xemtv.net PHP endpoint."""
96
+ channel_id = channel_id.lower().strip()
97
+
98
+ # Normalize name
99
+ name_map = {
100
+ 'vtvct': 'vtv10', 'vtv-can-tho': 'vtv10', 'vtv can tho': 'vtv10',
101
+ 'vtv_can_tho': 'vtv10', 'cantho': 'vtv10', 'cần thơ': 'vtv10',
102
+ 'vietnam_vtv1': 'vtv1', 'vietnam_vtv2': 'vtv2', 'vietnam_vtv3': 'vtv3',
103
+ 'vietnam_vtv4': 'vtv4', 'vietnam_vtv5': 'vtv5', 'vietnam_vtv6': 'vtv6',
104
+ 'vietnam_vtv7': 'vtv7', 'vietnam_vtv8': 'vtv8', 'vietnam_vtv9': 'vtv9',
105
+ }
106
+ channel_id = name_map.get(channel_id, channel_id)
107
+
108
+ # Check cache
109
+ cached = _cached(channel_id)
110
+ if cached:
111
+ return cached
112
+
113
+ php_url = XEMTV_PHP_ENDPOINTS.get(channel_id)
114
+ if not php_url:
115
+ # Fallback
116
+ fallback = CDN_FALLBACK.get(channel_id)
117
+ if fallback:
118
+ _set_cache(channel_id, fallback)
119
+ return fallback
120
+ return None
121
+
122
+ try:
123
+ r = requests.get(php_url, headers=UA, timeout=15, allow_redirects=True)
124
+ if r.status_code == 200:
125
+ m3u8 = extract_m3u8_from_html(r.text)
126
+ if m3u8:
127
+ _set_cache(channel_id, m3u8)
128
+ return m3u8
129
+ except:
130
+ pass
131
+
132
+ # Fallback to CDN
133
+ fallback = CDN_FALLBACK.get(channel_id)
134
+ if fallback:
135
+ _set_cache(channel_id, fallback)
136
+ return fallback
137
+
138
+ return None
139
+
140
+
141
+ def get_all_vtv_streams():
142
+ """Fetch all VTV channel streams. Returns list of {id, name, stream_url}."""
143
+ channels = []
144
+ for ch_id, php_url in XEMTV_PHP_ENDPOINTS.items():
145
+ stream_url = fetch_vtv_stream(ch_id)
146
+ channels.append({
147
+ 'id': ch_id,
148
+ 'name': CHANNEL_NAMES.get(ch_id, ch_id.upper()),
149
+ 'stream_url': stream_url,
150
+ })
151
+ return channels
152
+
153
+
154
+ # Legacy compatibility
155
+ XEMTV_CHANNELS = {v: k for k, v in CHANNEL_NAMES.items()}
156
+ CDN_STREAMS = {v: k for k, v in CDN_FALLBACK.items()}