bep40 commited on
Commit
353b5e0
·
verified ·
1 Parent(s): df939f4

v6.4: Tối ưu MyTV scraper — bỏ HEAD verify, chỉ dùng URL pattern

Browse files
Files changed (1) hide show
  1. main.py +10 -20
main.py CHANGED
@@ -443,14 +443,12 @@ def _slugify_mytv(s):
443
 
444
  def scrape_mytv_highlights(limit=20):
445
  """Lấy video từ mytv.com.vn cho các trận đấu đã kết thúc.
446
- mytv Next.js SPA nên không scrape trực tiếp được.
447
- Thay vào đó, dùng URL pattern từ các trận đấu trong VNEWS.
448
  """
449
  videos = []
450
  seen_urls = set()
451
 
452
  try:
453
- # Lấy trận đấu đã kết thúc trong 7 ngày qua
454
  from datetime import datetime as _dt, timedelta as _td
455
  now = _dt.now()
456
 
@@ -465,7 +463,6 @@ def scrape_mytv_highlights(limit=20):
465
  for li in soup.select("li.match-detail"):
466
  status_el = li.select_one('.status .label')
467
  status_text = status_el.get_text(strip=True) if status_el else ""
468
- # Chỉ lấy trận đã kết thúc
469
  if status_text not in ('KT', 'FT', 'Finished'):
470
  continue
471
 
@@ -477,7 +474,6 @@ def scrape_mytv_highlights(limit=20):
477
  if not home or not away:
478
  continue
479
 
480
- # Build mytv URL
481
  home_s = _slugify_mytv(home)
482
  away_s = _slugify_mytv(away)
483
  mytv_url = f"https://mytv.com.vn/{home_s}-vs-{away_s}"
@@ -486,21 +482,15 @@ def scrape_mytv_highlights(limit=20):
486
  continue
487
  seen_urls.add(mytv_url)
488
 
489
- # Verify URL exists (HEAD request, non-blocking)
490
- try:
491
- head_r = requests.head(mytv_url, headers=HEADERS, timeout=5, allow_redirects=True)
492
- if head_r.status_code == 200 and "anonymous" not in head_r.url:
493
- videos.append({
494
- "title": f"{home} vs {away}",
495
- "url": mytv_url,
496
- "source": "mytv",
497
- "type": "full_match",
498
- "home": home,
499
- "away": away,
500
- "date": ds
501
- })
502
- except:
503
- pass
504
 
505
  if len(videos) >= limit:
506
  break
 
443
 
444
  def scrape_mytv_highlights(limit=20):
445
  """Lấy video từ mytv.com.vn cho các trận đấu đã kết thúc.
446
+ Dùng URL pattern từ các trận đấu trong VNEWS (không verify HEAD request).
 
447
  """
448
  videos = []
449
  seen_urls = set()
450
 
451
  try:
 
452
  from datetime import datetime as _dt, timedelta as _td
453
  now = _dt.now()
454
 
 
463
  for li in soup.select("li.match-detail"):
464
  status_el = li.select_one('.status .label')
465
  status_text = status_el.get_text(strip=True) if status_el else ""
 
466
  if status_text not in ('KT', 'FT', 'Finished'):
467
  continue
468
 
 
474
  if not home or not away:
475
  continue
476
 
 
477
  home_s = _slugify_mytv(home)
478
  away_s = _slugify_mytv(away)
479
  mytv_url = f"https://mytv.com.vn/{home_s}-vs-{away_s}"
 
482
  continue
483
  seen_urls.add(mytv_url)
484
 
485
+ videos.append({
486
+ "title": f"{home} vs {away}",
487
+ "url": mytv_url,
488
+ "source": "mytv",
489
+ "type": "full_match",
490
+ "home": home,
491
+ "away": away,
492
+ "date": ds
493
+ })
 
 
 
 
 
 
494
 
495
  if len(videos) >= limit:
496
  break