bep40 commited on
Commit
e072bab
·
verified ·
1 Parent(s): b136659

Fix EPG _parse_time: always use current calendar date (not yesterday for hours < 5)

Browse files
Files changed (1) hide show
  1. vtv_api.py +7 -29
vtv_api.py CHANGED
@@ -351,7 +351,7 @@ def proxy_vtv_segment(url: str = Query(...)):
351
 
352
  _epg_cache = {}
353
  _epg_cache_time = 0
354
- _EPG_CACHE_TTL = 600 # Giảm từ 30 phút xuống 10 phút, đảm bảo dữ liệu mới
355
 
356
  VTV_CHANNEL_MAP = {
357
  "vtv1": "vtv1", "vtv2": "vtv2", "vtv3": "vtv3", "vtv4": "vtv4",
@@ -366,7 +366,6 @@ def _fetch_epg_from_vtv():
366
  if _epg_cache and now_ts - _epg_cache_time < _EPG_CACHE_TTL:
367
  return _epg_cache
368
  epg_data = {}
369
- # Lấy thời gian VN hiện tại để truyền vào parse_time
370
  now_vn = datetime.now(VN_TZ)
371
  try:
372
  headers = {
@@ -411,7 +410,6 @@ def _fetch_epg_from_vtv():
411
  title = title_span.get_text(strip=True)
412
  if not time_str or not title:
413
  continue
414
- # Truyền reference_date để xử lý quy tắc ngày truyền hình VTV
415
  start_dt = _parse_time(time_str, reference_date=now_vn)
416
  if not start_dt:
417
  continue
@@ -439,9 +437,9 @@ def _parse_time(time_str, reference_date=None):
439
  VTV hiển thị lịch theo ngày dương lịch (không phải ngày truyền hình).
440
  Ví dụ: Lịch ngày 17/06 sẽ hiển thị tất cả chương trình từ 00:00 đến 23:59 ngày 17/06.
441
 
442
- Logic:
443
- - Giờ 00:00-04:59: thể đêm khuya của ngày hôm trước HOẶC sáng sớm của ngày mới
444
- - Giờ 05:00-23:59: Luôn thuộc ngày hiện tại
445
  """
446
  if not time_str:
447
  return None
@@ -454,31 +452,11 @@ def _parse_time(time_str, reference_date=None):
454
  if reference_date:
455
  base_date = reference_date
456
  else:
457
- # Lấy ngày hiện tại theo giờ VN (UTC+7)
458
- now_vn = datetime.now(VN_TZ)
459
- base_date = now_vn
460
 
461
- from datetime import timedelta
 
462
 
463
- # Xác định ngày cho giờ program
464
- if hour < 5:
465
- # Giờ 00:00-04:59: Cần xem giờ hiện tại để quyết định
466
- if base_date.hour < 5:
467
- # Nếu hiện tại cũng < 5:00 (đang trong khoảng sáng sớm)
468
- # → Giờ program thuộc CÙNG NGÀY với giờ hiện tại
469
- tv_date = base_date
470
- else:
471
- # Nếu hiện tại >= 5:00 (đã qua sáng sớm)
472
- # → Giờ 00:00-04:59 thuộc NGÀY HÔM TRƯỚC
473
- tv_date = base_date - timedelta(days=1)
474
- else:
475
- # Giờ 05:00-23:59: Luôn thuộc ngày hiện tại
476
- tv_date = base_date
477
-
478
- # Tạo datetime với giờ từ lịch
479
- result = tv_date.replace(hour=hour, minute=minute, second=0, microsecond=0)
480
-
481
- # Đảm bảo result có timezone
482
  if result.tzinfo is None:
483
  result = result.replace(tzinfo=VN_TZ)
484
  return result
 
351
 
352
  _epg_cache = {}
353
  _epg_cache_time = 0
354
+ _EPG_CACHE_TTL = 600
355
 
356
  VTV_CHANNEL_MAP = {
357
  "vtv1": "vtv1", "vtv2": "vtv2", "vtv3": "vtv3", "vtv4": "vtv4",
 
366
  if _epg_cache and now_ts - _epg_cache_time < _EPG_CACHE_TTL:
367
  return _epg_cache
368
  epg_data = {}
 
369
  now_vn = datetime.now(VN_TZ)
370
  try:
371
  headers = {
 
410
  title = title_span.get_text(strip=True)
411
  if not time_str or not title:
412
  continue
 
413
  start_dt = _parse_time(time_str, reference_date=now_vn)
414
  if not start_dt:
415
  continue
 
437
  VTV hiển thị lịch theo ngày dương lịch (không phải ngày truyền hình).
438
  Ví dụ: Lịch ngày 17/06 sẽ hiển thị tất cả chương trình từ 00:00 đến 23:59 ngày 17/06.
439
 
440
+ FIX: Luôn dùng ngày hiện tại (calendar date) cho mọi giờ 00:00-23:59.
441
+ Trước đây, giờ 00:00-04:59 bị gán vào ngày hôm trước khi giờ hiện tại >= 5AM,
442
+ gây ra lệch ngày EPG.
443
  """
444
  if not time_str:
445
  return None
 
452
  if reference_date:
453
  base_date = reference_date
454
  else:
455
+ base_date = datetime.now(VN_TZ)
 
 
456
 
457
+ # Luôn dùng ngày hiện tại — VTV hiển thị lịch theo ngày dương lịch
458
+ result = base_date.replace(hour=hour, minute=minute, second=0, microsecond=0)
459
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460
  if result.tzinfo is None:
461
  result = result.replace(tzinfo=VN_TZ)
462
  return result