bep40 commited on
Commit
cccc5d6
·
verified ·
1 Parent(s): 6cb1372

Delete ai_runtime_final*.py, ai_fix2.py, ai_patch.py, ai_runtime.py, app_clean.py, app_final.py, app_main.py, app_patch_unified.py, app_run.py, app_v2_entry*.py, app_v3.js, app_v4.js, app_v5.js, core_*.js, patch_*.py, patch_extra.py, patch_runtime.py, restore_runner.py, rewrite_fix.js, tv_player.js, yt_live_v2.js, TRIGGER_REBUILD_V6.md, _run.py, _static_build_trigger.txt

Browse files
TRIGGER_REBUILD_V6.md DELETED
@@ -1 +0,0 @@
1
- # Rebuild trigger v6 - 1780971280.8544343
 
 
_run.py DELETED
@@ -1 +0,0 @@
1
- from app_v2_entry import app # v5-stable inline bongda proxy
 
 
_static_build_trigger.txt DELETED
@@ -1 +0,0 @@
1
- # Force rebuild - match_detail debug v2
 
 
ai_fix2.py DELETED
@@ -1,366 +0,0 @@
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 DELETED
@@ -1,751 +0,0 @@
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 DELETED
@@ -1,357 +0,0 @@
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 DELETED
@@ -1,315 +0,0 @@
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 DELETED
@@ -1,242 +0,0 @@
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 DELETED
@@ -1,191 +0,0 @@
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 DELETED
@@ -1,185 +0,0 @@
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 DELETED
@@ -1,73 +0,0 @@
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 DELETED
@@ -1,1325 +0,0 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_clean.py DELETED
@@ -1,69 +0,0 @@
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 DELETED
@@ -1,213 +0,0 @@
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 DELETED
@@ -1,283 +0,0 @@
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 DELETED
@@ -1,273 +0,0 @@
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 DELETED
@@ -1,221 +0,0 @@
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 DELETED
@@ -1,736 +0,0 @@
1
- """VNEWS v2 Entry Point - with fast bongda proxy"""
2
- import sys, os
3
- from main import app, HEADERS, BONGDA_HEADERS, fetch_bongda_api, HL_LEAGUES
4
-
5
- try:
6
- import ai_ext
7
- except Exception as e:
8
- print(f"[WARN] ai_ext import failed: {e}")
9
-
10
- from fastapi.responses import HTMLResponse, JSONResponse, FileResponse, Response
11
- from fastapi.staticfiles import StaticFiles
12
- from starlette.routing import Mount
13
- from fastapi import Query, Request
14
- import requests as req
15
- from bs4 import BeautifulSoup
16
- import re, html as html_lib, json, threading, time
17
- from concurrent.futures import ThreadPoolExecutor, as_completed
18
- from urllib.parse import quote
19
-
20
- HL_LEAGUES['friendly'] = {"path": "giai-khac/friendly", "name": "Giao hữu", "emoji": "🤝"}
21
-
22
- STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
23
- app.router.routes=[r for r in app.router.routes if not(getattr(r,'path',None)=='/' and hasattr(r,'methods') and 'GET' in getattr(r,'methods',set()))]
24
- app.routes[:]=[r for r in app.routes if not isinstance(r, Mount)]
25
- app.router.routes=[r for r in app.router.routes if not isinstance(r, Mount)]
26
-
27
- def _clean(s): return re.sub(r"\s+"," ",html_lib.unescape(str(s or""))).strip()
28
-
29
- # Cache for match details (5 min TTL)
30
- _match_cache = {}
31
-
32
- # === FAST BONGDA PROXY ENDPOINT ===
33
- def _get_match_detail(event_id, slug=None):
34
- """Internal function to scrape match detail from bongda.com.vn"""
35
- headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", "Accept": "text/html", "Referer": "https://bongda.com.vn/"}
36
-
37
- if slug:
38
- url = f"https://bongda.com.vn/tran-dau/{event_id}/centre/{slug}"
39
- else:
40
- url = f"https://bongda.com.vn/tran-dau/{event_id}"
41
-
42
- resp = req.get(url, headers=headers, timeout=15, allow_redirects=True)
43
- if resp.status_code != 200:
44
- return None
45
-
46
- soup = BeautifulSoup(resp.text, 'html.parser')
47
- result = {"event_id": event_id, "found": False, "sections": []}
48
- info = {}
49
-
50
- tel = soup.select_one('.teams')
51
- if tel:
52
- he = tel.select_one('.team.home')
53
- if he:
54
- p_tags = [p for p in he.select('p') if not p.get('class') or 'logo' not in p.get('class', [])]
55
- if p_tags: info['home_team'] = _clean(p_tags[0].get_text())
56
- lo = he.select_one('img')
57
- if lo: info['home_logo'] = lo.get('src', '')
58
- ae = tel.select_one('.team.away')
59
- if ae:
60
- p_tags = ae.select('p')
61
- team_ps = [p for p in p_tags if not p.get('class') or 'logo' not in p.get('class', [])]
62
- if team_ps: info['away_team'] = _clean(team_ps[-1].get_text())
63
- lo = ae.select_one('img')
64
- if lo: info['away_logo'] = lo.get('src', '')
65
- sc = tel.select_one('.score')
66
- if sc:
67
- parts = [_clean(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'] = _clean(lb.get_text())
71
-
72
- if info.get('home_team') and info.get('away_team'):
73
- result['info'] = info
74
- result['found'] = True
75
- result['sections'].append('info')
76
-
77
- events = []
78
- for ev in soup.select('.events .period .event'):
79
- ev_cls = ' '.join(ev.get('class', []))
80
- ev_data = {'team': 'home' if 'home' in ev_cls else 'away', 'period': '', 'type': 'unknown', 'time': '', 'players': ''}
81
-
82
- parent = ev.parent
83
- if parent:
84
- h2 = parent.find('h2')
85
- if h2: ev_data['period'] = _clean(h2.get_text())
86
-
87
- if ev.select_one('[class*="goal"]'): ev_data['type'] = 'goal'
88
- elif ev.select_one('[class*="redcard"]'): ev_data['type'] = 'redcard'
89
- elif ev.select_one('[class*="yellowcard"]'): ev_data['type'] = 'yellowcard'
90
- elif ev.select_one('[class*="substitution"]'): ev_data['type'] = 'substitution'
91
-
92
- players_el = ev.select_one('.players')
93
- if players_el:
94
- pl_text = _clean(players_el.get_text(' ', strip=True))
95
- m = re.match(r"(\d+)'(.*)", pl_text)
96
- if m:
97
- ev_data['time'] = f"{m.group(1)}'"
98
- ev_data['players'] = m.group(2)
99
- else:
100
- ev_data['players'] = pl_text
101
- events.append(ev_data)
102
-
103
- if events:
104
- result['events'] = events
105
- result['sections'].append('events')
106
-
107
- pred = soup.select_one('.prediction-card')
108
- if pred:
109
- team_info = pred.select_one('.team-info')
110
- if team_info:
111
- teams = team_info.select('.team')
112
- pred_data = {}
113
- if len(teams) >= 2:
114
- pred_data['home_name'] = _clean(teams[0].select_one('.team-name').get_text()) if teams[0].select_one('.team-name') else ''
115
- pred_data['away_name'] = _clean(teams[1].select_one('.team-name').get_text()) if teams[1].select_one('.team-name') else ''
116
- divider = team_info.select_one('.divider')
117
- if divider: pred_data['result'] = _clean(divider.get_text())
118
- vc = pred.select_one('.vote-count')
119
- if vc: pred_data['vote_count'] = _clean(vc.get_text())
120
- result['prediction'] = pred_data
121
-
122
- recent = []
123
- ml = soup.select_one('.matches-list')
124
- if ml:
125
- for item in ml.select('.match-detail, .match-item, li'):
126
- de = item.select_one('.date, .time')
127
- le = item.select_one('.league')
128
- he_item = item.select_one('.home, .team-home')
129
- ae_item = item.select_one('.away, .team-away')
130
- se = item.select_one('.score, .result')
131
- if he_item or ae_item:
132
- recent.append({'date': _clean(de.get_text()) if de else '', 'league': _clean(le.get_text()) if le else '', 'home': _clean(he_item.get_text()) if he_item else '', 'away': _clean(ae_item.get_text()) if ae_item else '', 'score': _clean(se.get_text()) if se else 'vs'})
133
- if recent:
134
- result['recent_matches'] = recent
135
- result['sections'].append('recent')
136
-
137
- try:
138
- api_h = {"User-Agent": "Mozilla/5.0", "Accept": "application/json", "X-Requested-With": "XMLHttpRequest", "Referer": "https://bongda.com.vn/"}
139
- ar = req.get(f"https://bongda.com.vn/api/fixtures/h2h-stats?event_id={event_id}", headers=api_h, timeout=10)
140
- if ar.status_code == 200:
141
- ad = ar.json()
142
- if ad.get('status') == 'success' and ad.get('html'):
143
- asp = BeautifulSoup(ad['html'], 'html.parser')
144
- ast = {}
145
- for row in asp.select('li, tr'):
146
- cells = row.select('td, span, p')
147
- if len(cells) >= 3:
148
- lb = _clean(cells[0].get_text())
149
- if lb: ast[lb] = {'home': _clean(cells[1].get_text()), 'away': _clean(cells[2].get_text())}
150
- if ast:
151
- result['h2h_stats_parsed'] = ast
152
- result['sections'].append('h2h_stats')
153
- except: pass
154
-
155
- return result
156
-
157
- @app.get('/api/proxy/bongda')
158
- def proxy_bongda(event_id: int = Query(default=None), slug: str = Query(default=None)):
159
- if event_id is None:
160
- return JSONResponse({'error': 'event_id required'}, status_code=400)
161
-
162
- cache_key = f"{event_id}_{slug}"
163
- now = time.time()
164
- cached = _match_cache.get(cache_key)
165
- if cached and now - cached.get('_ts', 0) < 300:
166
- return JSONResponse(cached)
167
-
168
- try:
169
- result = _get_match_detail(event_id, slug)
170
- if result:
171
- result['_ts'] = now
172
- _match_cache[cache_key] = result
173
- return JSONResponse(result)
174
- except Exception as e:
175
- err = {"event_id": event_id, "found": False, "error": str(e), "_ts": now}
176
- _match_cache[cache_key] = err
177
- return JSONResponse(err)
178
-
179
- return JSONResponse({"event_id": event_id, "found": False})
180
-
181
- @app.get('/api/match/{event_id}/detail')
182
- def api_match_detail(event_id: int, url: str = Query(default=None)):
183
- # Try to extract slug from url if provided
184
- slug = None
185
- if url:
186
- m = re.match(r'.+/tran-dau/\d+/(?:centre|preview)/(.+)', url)
187
- if m:
188
- slug = m.group(1)
189
-
190
- cache_key = f"{event_id}_{slug or ''}"
191
- now = time.time()
192
- cached = _match_cache.get(cache_key)
193
- if cached and now - cached.get('_ts', 0) < 300:
194
- return JSONResponse(cached)
195
-
196
- try:
197
- # If no slug, try to find it from homepage
198
- if not slug:
199
- try:
200
- home_r = req.get("https://bongda.com.vn/", headers={"User-Agent": "Mozilla/5.0"}, timeout=10)
201
- if home_r.status_code == 200:
202
- home_soup = BeautifulSoup(home_r.text, 'html.parser')
203
- for a in home_soup.select(f'a[href*="/tran-dau/{event_id}/"]'):
204
- href = a.get('href', '')
205
- m = re.match(r'/tran-dau/\d+/(?:centre|preview)/(.+)', href)
206
- if m:
207
- slug = m.group(1)
208
- cache_key = f"{event_id}_{slug}"
209
- break
210
- except: pass
211
-
212
- result = _get_match_detail(event_id, slug)
213
- if result:
214
- result['_ts'] = now
215
- _match_cache[cache_key] = result
216
- return JSONResponse(result)
217
- except Exception as e:
218
- err = {"event_id": event_id, "found": False, "error": str(e), "_ts": now}
219
- _match_cache[cache_key] = err
220
- return JSONResponse(err)
221
-
222
- return JSONResponse({"event_id": event_id, "found": False})
223
-
224
- # === Rest of endpoints (existing) ===
225
- _STOP=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ì này đã để'.split())
226
-
227
- def _has_kw(topic,title):
228
- tl=topic.lower();tt=(title or'').lower()
229
- if tl in tt:return True
230
- words=[w for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',tl) if len(w)>2 and w not in _STOP]
231
- if not words:return True
232
- return any(w in tt for w in words)
233
-
234
- def _s_vnexpress(topic,limit=8):
235
- items=[]
236
- try:
237
- r=req.get(f"https://timkiem.vnexpress.net/?q={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=10);soup=BeautifulSoup(r.text,'lxml')
238
- for art in soup.select('article.item-news')[:limit]:
239
- a=art.select_one('h2 a, h3 a')
240
- if a and a.get('href'):
241
- t=_clean(a.get('title','') or a.get_text(strip=True))
242
- if _has_kw(topic,t):items.append({'title':t,'url':a['href'],'via':'VnExpress'})
243
- except:pass
244
- return items
245
-
246
- def _s_dantri(topic,limit=8):
247
- items=[]
248
- try:
249
- 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')
250
- for a in soup.select('h3 a[href], .article-title a[href]')[:limit*2]:
251
- t=_clean(a.get_text(strip=True));href=a.get('href','')
252
- if t and len(t)>15 and _has_kw(topic,t):
253
- if not href.startswith('http'):href='https://dantri.com.vn'+href
254
- items.append({'title':t,'url':href,'via':'Dân Trí'})
255
- if len(items)>=limit:break
256
- except:pass
257
- return items
258
-
259
- def _s_vietnamnet(topic,limit=6):
260
- items=[]
261
- try:
262
- 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')
263
- for a in soup.select('h3 a[href], .vnn-title a')[:limit*2]:
264
- t=_clean(a.get_text(strip=True));href=a.get('href','')
265
- if t and len(t)>15 and _has_kw(topic,t):
266
- if not href.startswith('http'):href='https://vietnamnet.vn'+href
267
- items.append({'title':t,'url':href,'via':'VietNamNet'})
268
- if len(items)>=limit:break
269
- except:pass
270
- return items
271
-
272
- def _s_bongda(topic,limit=5):
273
- items=[]
274
- try:
275
- r=req.get(f"https://bongda.com.vn/tim-kiem.html?q={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=8);soup=BeautifulSoup(r.text,'lxml')
276
- for a in soup.select('h3 a[href], .title a[href]')[:limit*2]:
277
- t=_clean(a.get_text(strip=True));href=a.get('href','')
278
- if t and len(t)>15 and _has_kw(topic,t):
279
- if not href.startswith('http'):href='https://bongda.com.vn'+href
280
- items.append({'title':t,'url':href,'via':'Bóng Đá'})
281
- if len(items)>=limit:break
282
- except:pass
283
- return items
284
-
285
- def _s_genk(topic,limit=5):
286
- items=[]
287
- try:
288
- r=req.get(f"https://genk.vn/tim-kiem?q={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=8);soup=BeautifulSoup(r.text,'lxml')
289
- for a in soup.select('a[href$=".chn"]')[:limit*3]:
290
- t=_clean(a.get('title','') or a.get_text(strip=True));href=a.get('href','')
291
- if t and len(t)>15 and _has_kw(topic,t):
292
- if href.startswith('/'):href='https://genk.vn'+href
293
- items.append({'title':t,'url':href,'via':'GenK'})
294
- if len(items)>=limit:break
295
- except:pass
296
- return items
297
-
298
- def _s_thanhnien(topic,limit=6):
299
- items=[]
300
- try:
301
- r=req.get(f"https://thanhnien.vn/tim-kiem?q={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=10);soup=BeautifulSoup(r.text,'lxml')
302
- for a in soup.select('h3 a[href], .box-title a')[:limit*2]:
303
- t=_clean(a.get('title','') or a.get_text(strip=True));href=a.get('href','')
304
- if t and len(t)>15 and _has_kw(topic,t):
305
- if not href.startswith('http'):href='https://thanhnien.vn'+href
306
- items.append({'title':t,'url':href,'via':'Thanh Niên'})
307
- if len(items)>=limit:break
308
- except:pass
309
- return items
310
-
311
- def _s_tuoitre(topic,limit=6):
312
- items=[]
313
- try:
314
- r=req.get(f"https://tuoitre.vn/tim-kiem.htm?keywords={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=10);soup=BeautifulSoup(r.text,'lxml')
315
- for a in soup.select('h3 a[href], .box-title-text a')[:limit*2]:
316
- t=_clean(a.get('title','') or a.get_text(strip=True));href=a.get('href','')
317
- if t and len(t)>15 and _has_kw(topic,t):
318
- if not href.startswith('http'):href='https://tuoitre.vn'+href
319
- items.append({'title':t,'url':href,'via':'Tuổi Trẻ'})
320
- if len(items)>=limit:break
321
- except:pass
322
- return items
323
-
324
- def _s_thethaovanhoa(topic,limit=5):
325
- items=[]
326
- try:
327
- r=req.get(f"https://thethaovanhoa.vn/tim-kiem.htm?keyword={quote(topic)}",headers={'User-Agent':'Mozilla/5.0'},timeout=8);soup=BeautifulSoup(r.text,'lxml')
328
- for a in soup.select('h3 a[href], .title a[href]')[:limit*2]:
329
- t=_clean(a.get('title','') or a.get_text(strip=True));href=a.get('href','')
330
- if t and len(t)>15 and _has_kw(topic,t):
331
- if not href.startswith('http'):href='https://thethaovanhoa.vn'+href
332
- items.append({'title':t,'url':href,'via':'TT&VH'})
333
- if len(items)>=limit:break
334
- except:pass
335
- return items
336
-
337
- def _search_all(topic,limit=36):
338
- results={}
339
- with ThreadPoolExecutor(8) as ex:
340
- futs={ex.submit(_s_vnexpress,topic,8):'vne',ex.submit(_s_dantri,topic,8):'dt',ex.submit(_s_vietnamnet,topic,6):'vnn',ex.submit(_s_bongda,topic,5):'bd',ex.submit(_s_genk,topic,5):'gk',ex.submit(_s_thanhnien,topic,6):'tn',ex.submit(_s_tuoitre,topic,6):'tt',ex.submit(_s_thethaovanhoa,topic,5):'tvh'}
341
- for f in as_completed(futs,timeout=14):
342
- try:results[futs[f]]=f.result()
343
- except:results[futs[f]]=[]
344
- srcs=list(results.values());out=[];seen=set()
345
- for i in range(max((len(s) for s in srcs),default=0)):
346
- for s in srcs:
347
- if i<len(s) and s[i].get('url') and s[i]['url'] not in seen:seen.add(s[i]['url']);out.append(s[i])
348
- return out[:limit]
349
-
350
- app.router.routes=[r for r in app.router.routes if not(getattr(r,'path',None)=='/api/article' and 'GET' in getattr(r,'methods',set()))]
351
-
352
- def _scrape_generic(url):
353
- try:
354
- r=req.get(url,headers={'User-Agent':'Mozilla/5.0','Accept-Language':'vi-VN,vi;q=0.9'},timeout=15,allow_redirects=True);r.encoding='utf-8';soup=BeautifulSoup(r.text,'lxml')
355
- for tag in soup.find_all(['script','style','nav','footer','aside','form','noscript']):tag.decompose()
356
- h1=soup.find('h1');ogt=soup.find('meta',property='og:title');title=(h1.get_text(strip=True) if h1 else '')or(ogt.get('content','') if ogt else '')
357
- ogd=soup.find('meta',property='og:description');summary=ogd.get('content','') if ogd else ''
358
- ogi=soup.find('meta',property='og:image');og_img=ogi.get('content','') if ogi else ''
359
- if og_img and og_img.startswith('//'):og_img='https:'+og_img
360
- block=None
361
- for sel in['article','.singular-content','.detail-content','.fck_detail','.content-detail','.knc-content','main','.cms-body','.article__body']:
362
- el=soup.select_one(sel)
363
- if el and len(el.find_all('p'))>=2:block=el;break
364
- if not block:block=soup.body or soup
365
- body=[]
366
- for el in block.find_all(['p','h2','h3','figure','img'],recursive=True):
367
- if el.name=='p':t=el.get_text(strip=True);(body.append({'type':'p','text':t}) if t and len(t)>30 else None)
368
- elif el.name in('h2','h3'):t=el.get_text(strip=True);(body.append({'type':'heading','text':t}) if t else None)
369
- elif el.name in('figure','img'):
370
- im=el if el.name=='img' else el.find('img')
371
- if im:src=im.get('data-src') or im.get('src') or'';(body.append({'type':'img','src':'https:'+src if src.startswith('//') else src}) if src and'base64' not in src else None)
372
- if not body and summary:body=[{'type':'p','text':summary}]
373
- return{'title':_clean(title),'summary':_clean(summary),'og_image':og_img,'body':body[:50],'source':'generic','url':url}
374
- except:return None
375
-
376
- @app.get('/api/article')
377
- def api_article_v2(url:str=Query(...)):
378
- from main import scrape_vne_article,scrape_bbc_article,scrape_dantri_article,scrape_genk_article,scrape_ttvh_article
379
- if 'vnexpress.net' in url:data=scrape_vne_article(url)
380
- elif 'bbc.com' in url:data=scrape_bbc_article(url)
381
- elif 'dantri.com.vn' in url:data=scrape_dantri_article(url)
382
- elif 'genk.vn' in url:data=scrape_genk_article(url)
383
- elif 'thethaovanhoa.vn' in url:data=scrape_ttvh_article(url)
384
- else:data=_scrape_generic(url)
385
- if data and data.get('body'):return JSONResponse(data)
386
- return JSONResponse(data if data else{'error':'Không đọc được','url':url})
387
-
388
- _hot_cache={'t':0,'d':[]}
389
- def _get_hot_topics():
390
- now=time.time()
391
- if _hot_cache['d'] and now-_hot_cache['t']<600:return _hot_cache['d']
392
- freq={};display={}
393
- feeds=['https://vnexpress.net/rss/tin-moi-nhat.rss','https://dantri.com.vn/rss/home.rss','https://vietnamnet.vn/rss/tin-moi-nhat.rss','https://thanhnien.vn/rss/home.rss','https://tuoitre.vn/rss/tin-moi-nhat.rss','https://genk.vn/rss','https://vnexpress.net/rss/the-thao.rss','https://thethaovanhoa.vn/rss/tin-nong.rss']
394
- for feed_url in feeds:
395
- try:
396
- r=req.get(feed_url,headers={'User-Agent':'Mozilla/5.0'},timeout=6);r.encoding='utf-8';soup=BeautifulSoup(r.text,'xml')
397
- for item in soup.find_all('item')[:12]:
398
- title=_clean(item.find('title').get_text() if item.find('title') else '')
399
- if not title:continue
400
- title=re.sub(r'\s*[-|].*$','',title);words=[w for w in re.findall(r'[A-Za-zÀ-ỹ0-9]+',title) if len(w)>2 and w.lower() not in _STOP]
401
- if len(words)<2:continue
402
- for n in(3,4,2):
403
- for i in range(max(0,len(words)-n+1)):
404
- phrase=' '.join(words[i:i+n])
405
- if 8<=len(phrase)<=45:key=phrase.lower();freq[key]=freq.get(key,0)+1;display[key]=phrase
406
- except:continue
407
- ranked=sorted(freq.items(),key=lambda x:x[1],reverse=True);topics=[];seen=set()
408
- for key,count in ranked:
409
- is_dup=any(len(set(e.split())&set(key.split()))/max(len(set(e.split())),len(set(key.split())),1)>0.6 for e in seen)
410
- if is_dup:continue
411
- seen.add(key);topics.append({'label':'#'+re.sub(r'\s+','',display[key].title()),'topic':display[key],'count':count})
412
- if len(topics)>=20:break
413
- for kw in['World Cup 2026','Kinh tế Việt Nam','Bóng đá châu Âu','Công nghệ AI','Giá vàng','Thời tiết']:
414
- if len(topics)>=24:break
415
- if not any(kw.lower() in s for s in seen):topics.append({'label':'#'+re.sub(r'\s+','',kw.title()),'topic':kw,'count':0})
416
- _hot_cache.update({'t':now,'d':topics[:24]});return topics[:24]
417
-
418
- @app.get('/api/hot_topics')
419
- def api_hot_topics():return JSONResponse({'topics':_get_hot_topics()})
420
- @app.get('/')
421
- async def serve_index():
422
- p=os.path.join(STATIC_DIR,'index_v3.html')
423
- if os.path.exists(p):return FileResponse(p,media_type='text/html')
424
- p2=os.path.join(STATIC_DIR,'index_v2.html')
425
- if os.path.exists(p2):return FileResponse(p2,media_type='text/html')
426
- return HTMLResponse('<h1>VNEWS</h1>')
427
- @app.get('/api/hashtag/sources')
428
- def _ht(topic:str=Query(...),page:int=Query(default=0)):
429
- items=_search_all(topic,36);per_page=8;start=page*per_page;end=start+per_page
430
- return JSONResponse({'sources':items[start:end],'topic':topic,'page':page,'has_more':end<len(items),'total':len(items)})
431
- @app.get('/api/categories')
432
- def _cat():return JSONResponse([])
433
- @app.get('/api/storage_status')
434
- def _st():return JSONResponse({'persistent':os.path.isdir('/data') and os.access('/data',os.W_OK)})
435
- @app.get('/s')
436
- async def _sh(url:str='',title:str='',img:str=''):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></body></html>')
437
-
438
- from wc2026_scraper import(scrape_summary,scrape_fixtures,scrape_standings,scrape_stats,scrape_wc_news,scrape_road_to_wc,get_wc2026_all,scrape_history,scrape_h2h,scrape_lineups,scrape_match_detail)
439
- @app.get('/api/wc2026')
440
- def _w():return JSONResponse(get_wc2026_all())
441
- @app.get('/api/wc2026/fixtures')
442
- def _wf():return JSONResponse(scrape_fixtures())
443
- @app.get('/api/wc2026/standings')
444
- def _ws():return JSONResponse(scrape_standings())
445
- @app.get('/api/wc2026/stats')
446
- def _wst():return JSONResponse(scrape_stats())
447
- @app.get('/api/wc2026/history')
448
- def _whi():return JSONResponse(scrape_history())
449
- @app.get('/api/wc2026/news')
450
- def _wn():return JSONResponse(scrape_wc_news())
451
- @app.get('/api/wc2026/road')
452
- def _wr():return JSONResponse(scrape_road_to_wc())
453
- @app.get('/api/wc2026/h2h/{eid}')
454
- def _wh2(eid:int):return JSONResponse(scrape_h2h(eid))
455
- @app.get('/api/wc2026/lineups/{eid}')
456
- def _wl(eid:int):return JSONResponse(scrape_lineups(eid))
457
- @app.get('/api/wc2026/match/{eid}')
458
- def _wm(eid:int):return JSONResponse(scrape_match_detail(eid))
459
-
460
- DATA_DIR='/data' if os.path.isdir('/data') else os.path.join(os.path.dirname(os.path.abspath(__file__)),'data')
461
- os.makedirs(DATA_DIR,exist_ok=True);IF=os.path.join(DATA_DIR,'interactions_v2.json');CF=os.path.join(DATA_DIR,'comments_v2.json')
462
- _il=threading.Lock();_cl=threading.Lock()
463
- def _lj(p):
464
- try:
465
- if os.path.exists(p):return json.load(open(p,'r',encoding='utf-8'))
466
- except:pass
467
- return{}
468
- def _sj(p,d):
469
- try:open(p+'.tmp','w',encoding='utf-8').write(json.dumps(d,ensure_ascii=False));os.replace(p+'.tmp',p)
470
- except:pass
471
-
472
- @app.post('/api/v2/interact')
473
- async def _int(request:Request):
474
- b=await request.json();v=str(b.get('id','')).strip();t=str(b.get('type','')).strip()
475
- if not v or t not in('view','like'):return JSONResponse({'error':'x'},status_code=400)
476
- with _il:db=_lj(IF);db.setdefault(v,{'views':0,'likes':0,'comments':0});db[v][t+'s']+=1;_sj(IF,db);return JSONResponse(db[v])
477
-
478
- @app.get('/api/v2/interactions')
479
- def _gi(id:str=Query(...)):
480
- with _il:return JSONResponse(_lj(IF).get(id.strip(),{'views':0,'likes':0,'comments':0}))
481
-
482
- @app.get('/api/v2/comments')
483
- def _gc(id:str=Query(...)):
484
- with _cl:return JSONResponse({'comments':_lj(CF).get(id.strip(),[])})
485
-
486
- @app.post('/api/v2/comment')
487
- async def _pc(request:Request):
488
- b=await request.json();v=str(b.get('id','')).strip();tx=str(b.get('text','')).strip()[:500]
489
- if not v or not tx:return JSONResponse({'error':'x'},status_code=400)
490
- c={'text':tx,'time':time.strftime('%H:%M %d/%m',time.localtime()),'ts':int(time.time())}
491
- with _cl:db=_lj(CF);db.setdefault(v,[]);db[v].append(c);db[v]=db[v][-200:];_sj(CF,db);cms=db[v]
492
- with _il:idb=_lj(IF);idb.setdefault(v,{'views':0,'likes':0,'comments':0});idb[v]['comments']=len(cms);_sj(IF,idb)
493
- return JSONResponse({'comments':cms})
494
-
495
- # ============================================================
496
- # TEAM STATS — scrape bongda.com.vn /doi-bong/ page, render inline
497
- # ============================================================
498
- def _scrape_team_page(team_path):
499
- """Scrape team stats from bongda.com.vn team page."""
500
- url = f"https://bongda.com.vn/doi-bong/{team_path}"
501
- headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", "Accept": "text/html", "Referer": "https://bongda.com.vn/"}
502
- try:
503
- r = req.get(url, headers=headers, timeout=15, allow_redirects=True)
504
- if r.status_code != 200:
505
- return None
506
- soup = BeautifulSoup(r.text, 'html.parser')
507
- except:
508
- return None
509
-
510
- team = {"name": "", "logo": "", "league": "", "country": "", "stats": {}, "recent": [], "squad": [], "found": False}
511
-
512
- h1 = soup.select_one('h1')
513
- if h1:
514
- team['name'] = _clean(h1.get_text())
515
- team['found'] = True
516
-
517
- logo_el = soup.select_one('.team-logo img, .club-logo img, img.team-logo, .team-header img')
518
- if logo_el:
519
- team['logo'] = logo_el.get('src', '')
520
-
521
- info_el = soup.select_one('.team-info, .club-info, .team-header')
522
- if info_el:
523
- txt = info_el.get_text()
524
- if 'việt nam' in txt.lower() or 'vietnam' in txt.lower():
525
- team['country'] = 'Việt Nam'
526
-
527
- for li in soup.select('li.match-detail, .match-list .match-item, .recent-matches li'):
528
- home_el = li.select_one('.home-team .name, .team-home')
529
- away_el = li.select_one('.away-team .name, .team-away')
530
- if not home_el or not away_el:
531
- continue
532
- home = _clean(home_el.get_text())
533
- away = _clean(away_el.get_text())
534
- status_el = li.select_one('.status a, .score')
535
- score = ""
536
- event_id = ""
537
- match_url = ""
538
- if status_el:
539
- href = status_el.get('href', '')
540
- if href:
541
- match_url = 'https://bongda.com.vn' + href if href.startswith('/') else href
542
- m = re.search(r'/tran-dau/(\d+)/', href)
543
- if m:
544
- event_id = m.group(1)
545
- spans = status_el.find_all('span')
546
- if len(spans) >= 3:
547
- score = f"{spans[0].get_text(strip=True)} - {spans[2].get_text(strip=True)}"
548
- elif status_el.select_one('.vs'):
549
- score = 'VS'
550
- time_el = li.select_one('.match-time, .time, .date')
551
- mt = _clean(time_el.get_text()) if time_el else ''
552
- team['recent'].append({
553
- 'home': home, 'away': away, 'score': score, 'time': mt,
554
- 'event_id': event_id, 'url': match_url
555
- })
556
- if len(team['recent']) >= 10:
557
- break
558
-
559
- standings_rows = []
560
- for tr in soup.select('.standings-table tr, .league-table tr, table tbody tr'):
561
- cells = tr.select('td')
562
- if len(cells) >= 5:
563
- pos = _clean(cells[0].get_text())
564
- tname = _clean(cells[1].get_text()) if len(cells) > 1 else ''
565
- played = _clean(cells[2].get_text()) if len(cells) > 2 else ''
566
- gd = _clean(cells[3].get_text()) if len(cells) > 3 else ''
567
- pts = _clean(cells[4].get_text()) if len(cells) > 4 else ''
568
- if pos and tname and pos.isdigit():
569
- standings_rows.append({'pos': pos, 'team': tname, 'played': played, 'gd': gd, 'pts': pts})
570
- if standings_rows:
571
- team['standings'] = standings_rows
572
-
573
- for p in soup.select('.player-item, .squad-item, .player-card'):
574
- pname_el = p.select_one('.player-name, .name, h3, h4')
575
- pname = _clean(pname_el.get_text()) if pname_el else ''
576
- if not pname:
577
- continue
578
- ppos_el = p.select_one('.position, .pos, .player-pos')
579
- ppos = _clean(ppos_el.get_text()) if ppos_el else ''
580
- team['squad'].append({'name': pname, 'position': ppos})
581
-
582
- breadcrumb = soup.select_one('.breadcrumb, .breadcrumbs')
583
- if breadcrumb:
584
- for a in breadcrumb.select('a'):
585
- lt = _clean(a.get_text())
586
- if lt and lt.lower() != 'trang chủ':
587
- team['league'] = lt
588
- break
589
-
590
- return team
591
-
592
-
593
- @app.get("/api/team/{path:path}")
594
- async def api_team_stats(path: str):
595
- data = _scrape_team_page(path)
596
- if data and data.get('found'):
597
- return JSONResponse(data)
598
- return JSONResponse({"found": False, "error": "Không tìm thấy đội bóng"})
599
-
600
-
601
- def _render_team_page(path: str):
602
- """Render full HTML team stats page."""
603
- data = _scrape_team_page(path)
604
- if not data or not data.get('found'):
605
- from fastapi.responses import RedirectResponse
606
- return RedirectResponse(f"https://bongda.com.vn/doi-bong/{path}", status_code=302)
607
-
608
- team = data
609
- name = _clean(team.get('name') or 'Đội bóng')
610
- logo = _clean(team.get('logo') or '')
611
- league = _clean(team.get('league') or '')
612
- country = _clean(team.get('country') or '')
613
-
614
- recent_html = ''
615
- if team.get('recent'):
616
- for m in team['recent'][:10]:
617
- eid = _clean(m.get('event_id', ''))
618
- murl = _clean(m.get('url', ''))
619
- sc = _clean(m.get('score', 'VS'))
620
- mt = _clean(m.get('time', ''))
621
- home = _clean(m.get('home', ''))
622
- away = _clean(m.get('away', ''))
623
- onclick = f'onclick="openMatch(\'{eid}\',\'{murl}\')"' if eid else ''
624
- recent_html += f'<div class="tm-match" {onclick} style="cursor:pointer">'
625
- recent_html += f'<span class="tm-match-time">{mt}</span>'
626
- recent_html += f'<span class="tm-match-teams">{home} <span class="tm-score">{sc}</span> {away}</span>'
627
- recent_html += '</div>'
628
-
629
- standings_html = ''
630
- if team.get('standings'):
631
- standings_html = '<table class="tm-standings"><thead><tr><th>#</th><th>Đội</th><th>Trận</th><th>HS</th><th>Điểm</th></tr></thead><tbody>'
632
- for row in team['standings']:
633
- pos = _clean(row.get('pos', ''))
634
- tname = _clean(row.get('team', ''))
635
- played = _clean(row.get('played', ''))
636
- gd = _clean(row.get('gd', ''))
637
- pts = _clean(row.get('pts', ''))
638
- highlight = ' class="tm-highlight"' if name and tname and name.lower()[:6] in tname.lower()[:6] else ''
639
- standings_html += f'<tr{highlight}><td>{pos}</td><td>{tname}</td><td>{played}</td><td>{gd}</td><td>{pts}</td></tr>'
640
- standings_html += '</tbody></table>'
641
-
642
- squad_html = ''
643
- if team.get('squad'):
644
- for p in team['squad'][:30]:
645
- pnm = _clean(p.get('name', ''))
646
- pps = _clean(p.get('position', ''))
647
- if not pnm:
648
- continue
649
- squad_html += f'<div class="tm-player"><span class="tm-player-name">{pnm}</span><span class="tm-player-pos">{pps}</span></div>'
650
-
651
- logo_html = f'<img src="{logo}" class="tm-logo" alt="{name}" onerror="this.style.display=\'none\'">' if logo else ''
652
-
653
- sections_html = ''
654
- sections_html += '<div class="tm-section"><div class="tm-section-title">📋 Trận đấu gần nhất</div>'
655
- sections_html += recent_html or '<div class="tm-nodata">Không có dữ liệu trận đấu</div>'
656
- sections_html += '</div>'
657
-
658
- if standings_html:
659
- sections_html += '<div class="tm-section"><div class="tm-section-title">🏆 Bảng xếp hạng</div>' + standings_html + '</div>'
660
-
661
- if squad_html:
662
- sections_html += '<div class="tm-section"><div class="tm-section-title">👥 Đội hình</div><div>' + squad_html + '</div></div>'
663
-
664
- html = f'''<!DOCTYPE html>
665
- <html lang="vi">
666
- <head>
667
- <meta charset="utf-8">
668
- <meta name="viewport" content="width=device-width,initial-scale=1">
669
- <title>⚽ {name} — Thống kê | VNEWS</title>
670
- <meta property="og:title" content="⚽ {name} — Thống kê | VNEWS">
671
- <style>
672
- *{{margin:0;padding:0;box-sizing:border-box}}body{{background:#0d1117;color:#e0e0e0;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,sans-serif}}
673
- .tm-header{{background:linear-gradient(135deg,#1a2a1f,#0d1117);border-bottom:2px solid #2d8659;padding:16px;text-align:center}}
674
- .tm-top{{display:flex;align-items:center;justify-content:center;gap:12px;flex-wrap:wrap}}
675
- .tm-logo{{width:64px;height:64px;object-fit:contain;border-radius:50%;border:3px solid #2d8659;background:#111}}
676
- .tm-name{{font-size:24px;font-weight:900;color:#fff}}
677
- .tm-badge{{font-size:12px;color:#888;margin-top:4px}}
678
- .tm-badge span{{background:#1a2a1f;color:#5cb87a;padding:2px 8px;border-radius:4px;margin:0 2px}}
679
- .tm-nav{{background:#161b22;padding:8px 16px;display:flex;gap:8px;align-items:center;border-bottom:1px solid #21262d}}
680
- .tm-nav a{{color:#5cb87a;text-decoration:none;font-size:13px}}
681
- .tm-nav a:hover{{text-decoration:underline}}
682
- .tm-container{{max-width:900px;margin:0 auto;padding:16px}}
683
- .tm-section{{margin-bottom:20px}}
684
- .tm-section-title{{font-size:14px;font-weight:800;color:#5cb87a;margin-bottom:8px;padding-bottom:6px;border-bottom:1px solid #21262d}}
685
- .tm-match{{display:flex;align-items:center;gap:10px;padding:10px 12px;background:#161b22;border-radius:6px;margin-bottom:4px;font-size:13px;border:1px solid #21262d;transition:background .2s}}
686
- .tm-match:hover{{background:#1c2128;border-color:#2d8659}}
687
- .tm-match-time{{font-size:11px;color:#888;min-width:60px}}
688
- .tm-match-teams{{flex:1;color:#e0e0e0}}
689
- .tm-score{{color:#f0c040;font-weight:800}}
690
- .tm-standings{{width:100%;border-collapse:collapse;font-size:13px}}
691
- .tm-standings th{{background:#1a2a1f;color:#5cb87a;padding:8px;text-align:left;font-size:11px;border-bottom:1px solid #2d8659}}
692
- .tm-standings td{{padding:8px;border-bottom:1px solid #21262d}}
693
- .tm-standings tr:hover td{{background:#161b22}}
694
- .tm-highlight td{{background:#1a2a1f;border-left:3px solid #5cb87a}}
695
- .tm-player{{display:inline-flex;align-items:center;gap:6px;background:#161b22;padding:5px 10px;border-radius:5px;margin:3px;font-size:12px;border:1px solid #21262d}}
696
- .tm-player-name{{color:#e0e0e0}}
697
- .tm-player-pos{{color:#888;font-size:10px}}
698
- .tm-back{{display:inline-block;margin-bottom:12px;color:#5cb87a;text-decoration:none;font-size:14px}}
699
- .tm-back:hover{{text-decoration:underline}}
700
- .tm-nodata{{text-align:center;color:#666;padding:20px;font-size:13px}}
701
- </style>
702
- </head>
703
- <body>
704
- <div class="tm-header"><div class="tm-top">{logo_html}<div><div class="tm-name">⚽ {name}</div><div class="tm-badge">{league and f"<span>🏆 {league}</span>"}{country and f"<span>📍 {country}</span>"}<span>📡 VNEWS</span></div></div></div></div>
705
- <div class="tm-nav"><a href="/" class="tm-back">← Quay về VNEWS</a> <a href="https://bongda.com.vn/doi-bong/{path}" target="_blank">Xem trên Bongda.com.vn ↗</a></div>
706
- <div class="tm-container">{sections_html}</div>
707
- <script>function openMatch(id,url){{if(!id)return;window.location.href='/?match='+id+(url?'&url='+encodeURIComponent(url):'');}}</script>
708
- </body></html>'''
709
- return HTMLResponse(html)
710
-
711
-
712
- @app.get("/doi-bong/{path:path}")
713
- async def page_team_stats(path: str):
714
- return _render_team_page(path)
715
-
716
-
717
- @app.get("/giai-dau/{path:path}")
718
- async def page_league_internal(path: str):
719
- from fastapi.responses import RedirectResponse
720
- return RedirectResponse(f"https://bongda.com.vn/giai-dau/{path}", status_code=302)
721
-
722
-
723
- @app.get("/cau-thu/{path:path}")
724
- async def redirect_cau_thu(path: str):
725
- from fastapi.responses import RedirectResponse
726
- return RedirectResponse(f"https://bongda.com.vn/cau-thu/{path}", status_code=302)
727
-
728
- def _bg():
729
- time.sleep(15)
730
- while True:
731
- try:get_wc2026_all()
732
- except:pass
733
- time.sleep(90)
734
- threading.Thread(target=_bg,daemon=True).start()
735
-
736
- app.mount('/static',StaticFiles(directory=STATIC_DIR),name='vnews_static')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app_v2_entry_test.py DELETED
@@ -1,16 +0,0 @@
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 DELETED
@@ -1,16 +0,0 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
patch_extra.py DELETED
@@ -1,50 +0,0 @@
1
- """Extra CSS/JS fixes injected AFTER main PATCH_INJECT."""
2
- EXTRA_FIX = r'''
3
- <style>
4
- /* Force correct position for Short AI interaction buttons */
5
- .tiktok-slide{position:relative!important}
6
- .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}
7
- .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;font-size:10px!important;cursor:pointer!important}
8
- .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}
9
- .tiktok-right-btn .count{font-size:10px!important;color:#ddd!important}
10
- #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;white-space:nowrap}
11
- /* Kill ALL duplicate short AI slides from old layers */
12
- #ai-short-home,.ai-short-home,.ai-short-card-final,[id*="ai-shorts-patched"]{display:none!important}
13
- </style>
14
- <div id="short-progress-toast"></div>
15
- <script>
16
- (function(){
17
- // Kill old renderers that create duplicate Short AI slides
18
- window.renderAIShortHome=function(){};
19
- window.renderAIShorts7=function(){};
20
- window.renderTopicWallE=function(){};
21
- window.renderAiShorts=function(){};
22
- // Also remove any already-rendered duplicate slides
23
- setInterval(function(){
24
- document.querySelectorAll('#ai-short-home,.ai-short-home,[id*="ai-shorts-patched"]').forEach(function(el){el.remove()});
25
- },2000);
26
- // Progress toast for short creation
27
- window.showShortProgress=function(msg){var t=document.getElementById('short-progress-toast');if(t){t.textContent=msg;t.style.display='block';}};
28
- window.hideShortProgress=function(){var t=document.getElementById('short-progress-toast');if(t)t.style.display='none';};
29
- // Override makeShortFromPost to use progress toast
30
- var _origMakeShort=window.makeShortFromPost;
31
- window.makeShortFromPost=async function(pid,btn){
32
- showShortProgress('⏳ Đang tạo Short AI...');
33
- if(btn){btn.disabled=true;btn.textContent='Đang tạo...';}
34
- try{
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){
42
- showShortProgress('❌ Lỗi: '+e.message);
43
- setTimeout(hideShortProgress,4000);
44
- }finally{
45
- if(btn){btn.disabled=false;btn.textContent='🎬 Tạo Short AI';}
46
- }
47
- };
48
- })();
49
- </script>
50
- '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
patch_runtime.py DELETED
@@ -1,274 +0,0 @@
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"):
33
- data = _main._article_by_url(url)
34
- except Exception:
35
- data = None
36
- if not data:
37
- try:
38
- data = _main._scrape_generic_article(url) if hasattr(_main, "_scrape_generic_article") else None
39
- except Exception:
40
- data = None
41
- if not data:
42
- data = {"title": "", "summary": "", "og_image": "", "body": [], "url": url, "source": "generic"}
43
- title = _safe_text(data.get("title"))
44
- summary = _safe_text(data.get("summary"))
45
- img = _safe_text(data.get("og_image"))
46
- body = data.get("body") or []
47
- if not title or not summary or not img or not body:
48
- try:
49
- r = requests.get(url, headers=getattr(_main, "HEADERS", {}), timeout=15)
50
- r.encoding = "utf-8"
51
- soup = BeautifulSoup(r.text, "lxml")
52
- if not title:
53
- tag = soup.find("meta", property="og:title") or soup.find("title")
54
- title = tag.get("content", "").strip() if tag and tag.name == "meta" else (tag.get_text(strip=True) if tag else "")
55
- if not summary:
56
- tag = soup.find("meta", property="og:description") or soup.find("meta", attrs={"name": "description"})
57
- summary = tag.get("content", "").strip() if tag else ""
58
- if not img:
59
- tag = soup.find("meta", property="og:image") or soup.find("meta", attrs={"name": "twitter:image"})
60
- img = tag.get("content", "").strip() if tag else ""
61
- if not body:
62
- ps = []
63
- for p in soup.find_all("p"):
64
- t = p.get_text(" ", strip=True)
65
- if len(t) > 40:
66
- ps.append({"type": "p", "text": t})
67
- if len(ps) >= 30:
68
- break
69
- body = ps
70
- except Exception:
71
- pass
72
- if not summary and body:
73
- first = next((b.get("text", "") for b in body if b.get("type") == "p" and b.get("text")), "")
74
- summary = first[:360]
75
- if not title:
76
- title = url
77
- if not img:
78
- img = DEFAULT_IMG
79
- if not body and summary:
80
- body = [{"type": "p", "text": summary}]
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"):
88
- text = _main._ai_rewrite_article(data, tone=tone)
89
- if text and len(text.strip()) > 50:
90
- return text.strip()
91
- except Exception:
92
- pass
93
- title = data.get("title", "")
94
- summary = data.get("summary", "")
95
- ps = [b.get("text", "") for b in data.get("body", []) if b.get("type") == "p" and b.get("text")]
96
- lead = summary or (ps[0] if ps else "")
97
- points = "\n".join(["• " + p[:220] + ("..." if len(p) > 220 else "") for p in ps[:5]])
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"):
105
- return _main._image_for_topic(topic)
106
- except Exception:
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 []
114
- except Exception:
115
- posts = []
116
- posts.insert(0, post)
117
- try:
118
- if hasattr(_main, "_save_wall"):
119
- _main._save_wall(posts)
120
- except Exception:
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:
131
- body = await request.json()
132
- except Exception:
133
- body = {}
134
- url = _safe_text(body.get("url"))
135
- tone = _safe_text(body.get("tone")) or "tu-nhien"
136
- if not url:
137
- return JSONResponse({"error": "missing url"}, status_code=400)
138
- try:
139
- data = _ensure_article(url)
140
- text = _rewrite(data, tone=tone)
141
- post = {
142
- "id": hashlib.md5((url + str(time.time())).encode()).hexdigest()[:12],
143
- "url": url,
144
- "title": data.get("title") or url,
145
- "summary": data.get("summary") or "",
146
- "img": data.get("og_image") or DEFAULT_IMG,
147
- "text": text or (data.get("summary") or data.get("title") or url),
148
- "source": data.get("source", "url"),
149
- "ts": int(time.time()),
150
- }
151
- _save_post(post)
152
- return JSONResponse({"post": post})
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:
165
- body = await request.json()
166
- except Exception:
167
- body = {}
168
- topic = _safe_text(body.get("topic"))
169
- tone = _safe_text(body.get("tone")) or "tu-nhien"
170
- if not topic:
171
- return JSONResponse({"error": "missing topic"}, status_code=400)
172
- try:
173
- context = ""
174
- try:
175
- if hasattr(_main, "_topic_article_context"):
176
- context = _main._topic_article_context(topic)
177
- if not context and hasattr(_main, "_web_context"):
178
- context = _main._web_context(topic)
179
- except Exception:
180
- context = ""
181
- if not context:
182
- context = f"Chủ đề: {topic}"
183
- data = {"title": topic, "summary": context[:420], "og_image": _topic_image(topic), "body": [{"type": "p", "text": context}], "source": "topic", "url": ""}
184
- text = _rewrite(data, tone=tone)
185
- post = {
186
- "id": hashlib.md5((topic + str(time.time())).encode()).hexdigest()[:12],
187
- "url": "",
188
- "title": topic,
189
- "summary": data["summary"],
190
- "img": data["og_image"] or DEFAULT_IMG,
191
- "text": text or context,
192
- "source": "topic",
193
- "ts": int(time.time()),
194
- }
195
- _save_post(post)
196
- return JSONResponse({"post": post})
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(){
204
- async function safeJson(res){
205
- const text = await res.text();
206
- try { return JSON.parse(text); }
207
- catch(e){ return { error: (text || 'Server không trả JSON').slice(0,500) }; }
208
- }
209
- window.safeJson = safeJson;
210
- window.createUrlPost = function(){
211
- let inp=document.getElementById('ai-url-input');
212
- let url=(inp&&inp.value||'').trim();
213
- if(!url){ alert('Dán URL trước'); return; }
214
- fetch('/api/url_wall',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url})})
215
- .then(safeJson).then(j=>{
216
- if(j&&j.post){
217
- if(!j.post.img) j.post.img='https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg';
218
- if(!j.post.text) j.post.text=j.post.summary||j.post.title||'Không lấy được nội dung tóm tắt.';
219
- if(typeof prependWallPost==='function') prependWallPost(j.post);
220
- alert('Đã tóm tắt URL và đăng lên tường');
221
- if(inp) inp.value='';
222
- } else alert((j&&j.error)||'Lỗi URL');
223
- }).catch(e=>alert('Lỗi URL: '+e.message));
224
- };
225
- window.createTopicPost = function(){
226
- let inp=document.getElementById('ai-topic-input');
227
- let topic=(inp&&inp.value||'').trim();
228
- if(!topic){ alert('Nhập chủ đề trước'); return; }
229
- fetch('/api/topic_post',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({topic})})
230
- .then(safeJson).then(j=>{
231
- if(j&&j.post){
232
- if(!j.post.img) j.post.img='https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg';
233
- if(!j.post.text) j.post.text=j.post.summary||j.post.title||'Không lấy được nội dung.';
234
- if(typeof prependWallPost==='function') prependWallPost(j.post);
235
- alert('Đã tạo bài và đăng lên tường');
236
- if(inp) inp.value='';
237
- } else alert((j&&j.error)||'Lỗi tạo bài');
238
- }).catch(e=>alert('Lỗi tạo bài: '+e.message));
239
- };
240
- window.rewriteCurrentArticle = function(){
241
- if(!window._currentArticle && typeof _currentArticle!=='undefined') window._currentArticle=_currentArticle;
242
- let ca = (typeof _currentArticle!=='undefined') ? _currentArticle : window._currentArticle;
243
- if(!ca || !ca.url){ alert('Chưa có bài viết để rewrite'); return; }
244
- let tone=document.getElementById('rewrite-tone')?.value||'nghiem-tuc';
245
- let btn=document.querySelector('.article-actions button.primary');
246
- if(btn){btn.textContent='Đang rewrite...';btn.disabled=true;}
247
- fetch('/api/rewrite_share',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({url:ca.url,tone})})
248
- .then(safeJson).then(j=>{
249
- if(j&&j.post){
250
- if(!j.post.img) j.post.img='https://s1.vnecdn.net/vnexpress/restruct/i/v9505/logo_default.jpg';
251
- if(!j.post.text) j.post.text=j.post.summary||j.post.title||'Không lấy được nội dung.';
252
- let box=document.getElementById('rewrite-result');
253
- if(box) box.innerHTML='<div class="rewrite-box"><div class="rewrite-title">Đã rewrite và đăng lên Tường AI</div><div class="rewrite-text">'+(j.post.text||'').replace(/[&<>"']/g,m=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[m]))+'</div></div>';
254
- if(typeof prependWallPost==='function') prependWallPost(j.post);
255
- alert('Đã đăng lên Tường AI');
256
- } else alert((j&&j.error)||'Không tạo được bài AI');
257
- }).catch(e=>alert('Lỗi tạo bài AI: '+e.message))
258
- .finally(()=>{if(btn){btn.textContent='🤖 AI viết lại & đăng tường';btn.disabled=false;}});
259
- };
260
- })();
261
- </script>
262
- '''
263
-
264
-
265
- @app.get("/")
266
- async def patched_index():
267
- try:
268
- with open("/app/static/index.html", "r", encoding="utf-8") as f:
269
- html = f.read()
270
- if "window.safeJson" not in html:
271
- html = html.replace("</body>", _FRONTEND_PATCH + "</body>")
272
- return HTMLResponse(content=html)
273
- except Exception as e:
274
- return HTMLResponse(content=f"<pre>Index error: {str(e)}</pre>", status_code=500)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
restore_runner.py DELETED
@@ -1,31 +0,0 @@
1
- import os
2
- import sys
3
- import subprocess
4
- from huggingface_hub import snapshot_download
5
-
6
- REVISION = os.environ.get("VNEWS_RESTORE_REVISION", "bcaa2dc")
7
- REPO_ID = os.environ.get("VNEWS_REPO_ID", "bep40/vnews")
8
-
9
- # Download exact Space snapshot from Hugging Face Hub.
10
- # This avoids manually copying huge files from an old commit.
11
- snapshot_dir = snapshot_download(
12
- repo_id=REPO_ID,
13
- repo_type="space",
14
- revision=REVISION,
15
- local_dir="/tmp/vnews_restore",
16
- local_dir_use_symlinks=False,
17
- )
18
-
19
- os.chdir(snapshot_dir)
20
- sys.path.insert(0, snapshot_dir)
21
-
22
- # Commit bcaa2dc Dockerfile ran ai_patch:app.
23
- cmd = [
24
- "uvicorn",
25
- "ai_patch:app",
26
- "--host",
27
- "0.0.0.0",
28
- "--port",
29
- "7860",
30
- ]
31
- os.execvp(cmd[0], cmd)