bep40 commited on
Commit
42bb703
·
verified ·
1 Parent(s): 2eb76dd

Auto scheduler: 3 topics HOT khác nhau ko trùng, mỗi topic 1 bài

Browse files
Files changed (1) hide show
  1. app_v2_entry.py +43 -23
app_v2_entry.py CHANGED
@@ -1326,10 +1326,16 @@ async def _auto_fetch_short(post_id):
1326
  except: pass
1327
  return False
1328
 
1329
- async def _auto_rewrite_one(topic, slot_label):
1330
- """Rewrite one topic: find articles, summarize, post to wall, trigger short."""
 
1331
  from urllib.parse import quote as _q
1332
  items = _search_all(topic, limit=6)
 
 
 
 
 
1333
  if not items:
1334
  # fallback: Google RSS
1335
  try:
@@ -1348,6 +1354,8 @@ async def _auto_rewrite_one(topic, slot_label):
1348
  item = items[0] # best match
1349
  url = item.get('url', '')
1350
  title = item.get('title', topic)
 
 
1351
  if not url.startswith('http'):
1352
  return False
1353
 
@@ -1399,34 +1407,46 @@ async def _auto_rewrite_one(topic, slot_label):
1399
  return True
1400
 
1401
  async def _do_scheduled_run(slot_label):
1402
- """Main scheduled run: top 3 hot topics + latest articles."""
1403
  print(f"[auto] Starting scheduled rewrite for {slot_label}")
1404
 
1405
- # 1) Get top 3 hot topics
1406
- topics = _get_hot_topics()[:3]
1407
- job_topics = [t['topic'] for t in topics if t.get('topic')]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1408
 
1409
- # 2) Add latest articles from RSS
1410
- for feed_url in ['https://vnexpress.net/rss/tin-moi-nhat.rss',
1411
- 'https://dantri.com.vn/rss/home.rss',
1412
- 'https://vietnamnet.vn/rss/tin-moi-nhat.rss']:
1413
- try:
1414
- r = req.get(feed_url, headers={'User-Agent':'Mozilla/5.0'}, timeout=5)
1415
- r.encoding = 'utf-8'
1416
- soup = BeautifulSoup(r.text, 'xml')
1417
- for it in soup.find_all('item')[:3]:
1418
- t = _clean(it.find('title').get_text(' ',strip=True) if it.find('title') else '')
1419
- if t and len(t) > 10 and t[:50] not in ' '.join(job_topics):
1420
- job_topics.append(t[:60])
1421
- except: pass
1422
 
1423
- # 3) Execute jobs
 
1424
  results = []
1425
- for jt in job_topics[:6]:
 
1426
  try:
1427
- ok = await asyncio.wait_for(_auto_rewrite_one(jt, slot_label), timeout=90)
1428
  results.append((jt, ok))
1429
- except: results.append((jt, False))
 
 
1430
  await asyncio.sleep(2)
1431
 
1432
  # Log
 
1326
  except: pass
1327
  return False
1328
 
1329
+ async def _auto_rewrite_one(topic, slot_label, used_urls=None):
1330
+ """Rewrite one topic: find articles, summarize, post to wall, trigger short.
1331
+ used_urls: shared set to avoid duplicate articles across topics."""
1332
  from urllib.parse import quote as _q
1333
  items = _search_all(topic, limit=6)
1334
+ # Skip URLs already used by another topic
1335
+ if used_urls is not None:
1336
+ filtered = [it for it in items if it.get('url') not in used_urls]
1337
+ if filtered:
1338
+ items = filtered
1339
  if not items:
1340
  # fallback: Google RSS
1341
  try:
 
1354
  item = items[0] # best match
1355
  url = item.get('url', '')
1356
  title = item.get('title', topic)
1357
+ if url and used_urls is not None:
1358
+ used_urls.add(url)
1359
  if not url.startswith('http'):
1360
  return False
1361
 
 
1407
  return True
1408
 
1409
  async def _do_scheduled_run(slot_label):
1410
+ """Main scheduled run: exactly 3 posts from 3 different HOT topics, no duplicates."""
1411
  print(f"[auto] Starting scheduled rewrite for {slot_label}")
1412
 
1413
+ # Get top hot topics, skip duplicates
1414
+ all_topics = _get_hot_topics()
1415
+ seen_topics = set()
1416
+ unique_topics = []
1417
+ for t in all_topics:
1418
+ kw = t.get('topic', '').lower().strip()
1419
+ if kw and len(kw) > 5 and kw not in seen_topics:
1420
+ is_dup = False
1421
+ for s in seen_topics:
1422
+ # Check if one topic is substring of another
1423
+ if kw in s or s in kw:
1424
+ is_dup = True
1425
+ break
1426
+ if not is_dup:
1427
+ seen_topics.add(kw)
1428
+ unique_topics.append(t)
1429
+ if len(unique_topics) >= 3:
1430
+ break
1431
 
1432
+ job_topics = [t['topic'] for t in unique_topics[:3] if t.get('topic')]
1433
+ if not job_topics:
1434
+ print(f"[auto] No hot topics found, skipping")
1435
+ return
1436
+
1437
+ print(f"[auto] Running 3 topics: {job_topics}")
 
 
 
 
 
 
 
1438
 
1439
+ # Track used URLs to avoid cross-topic duplicates
1440
+ _used_urls = set()
1441
  results = []
1442
+
1443
+ for jt in job_topics:
1444
  try:
1445
+ ok = await asyncio.wait_for(_auto_rewrite_one(jt, slot_label, _used_urls), timeout=120)
1446
  results.append((jt, ok))
1447
+ except Exception as e:
1448
+ print(f"[auto] Error on '{jt}': {e}")
1449
+ results.append((jt, False))
1450
  await asyncio.sleep(2)
1451
 
1452
  # Log