Spaces:
Running
Running
Restrict hashtags and topic sources to selected Vietnamese sources
Browse files- ai_runtime_final6.py +137 -0
ai_runtime_final6.py
CHANGED
|
@@ -1004,3 +1004,140 @@ 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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]
|