feat(variety): 单集数据缓存 24 小时,避免每次回源 TMDB
- 剧集实体新增 episode_air_dates / episodes_fetched_at 两个字段 - build_index 先查缓存(24h 内直接命中),过期或缺失才逐季拉取并写回 - TTL 定 24 小时:综艺周更,久了会漏新一期 - 实测:哈哈哈哈哈 首次「已写入缓存」,再次「命中缓存(24h 内,189 个日期)」 - 库内 中餐厅 实测:11 个视频补上 -YYYYMMDD.期名 尾巴,nfo/thumb 同步跟随改名
This commit is contained in:
@@ -86,9 +86,76 @@ class VarietyService:
|
|||||||
|
|
||||||
# ---------------------------------------------------------------- TMDB 索引
|
# ---------------------------------------------------------------- TMDB 索引
|
||||||
|
|
||||||
|
# TMDB 单集数据缓存有效期:综艺周更,超过这个时间必须重拉,
|
||||||
|
# 否则新一期不会出现在索引里。
|
||||||
|
CACHE_TTL_HOURS = 24
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _fresh(cls, stamp: str) -> bool:
|
||||||
|
try:
|
||||||
|
when = datetime.fromisoformat(stamp)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return False
|
||||||
|
if when.tzinfo is None:
|
||||||
|
when = when.replace(tzinfo=datetime.now().astimezone().tzinfo)
|
||||||
|
age = datetime.now().astimezone() - when
|
||||||
|
return age.total_seconds() <= cls.CACHE_TTL_HOURS * 3600
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _index_from_cache(cls, payload: dict) -> dict:
|
||||||
|
index: dict[str, list[dict]] = defaultdict(list)
|
||||||
|
for season, episodes in payload.items():
|
||||||
|
for item in episodes:
|
||||||
|
date = item.get("date") or ""
|
||||||
|
if len(date) < 10:
|
||||||
|
continue
|
||||||
|
index[date].append({
|
||||||
|
"season": season,
|
||||||
|
"ep": int(item.get("ep") or 0),
|
||||||
|
"name": item.get("name") or "",
|
||||||
|
"air_year": date[:4],
|
||||||
|
"air_date": date,
|
||||||
|
})
|
||||||
|
return dict(index)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _save_index(cls, show_id: int, index: dict, show: dict) -> None:
|
||||||
|
payload: dict[str, list[dict]] = defaultdict(list)
|
||||||
|
for date, candidates in index.items():
|
||||||
|
for cand in candidates:
|
||||||
|
payload[cand["season"]].append(
|
||||||
|
{"ep": cand["ep"], "date": date, "name": cand["name"]})
|
||||||
|
for season in payload:
|
||||||
|
payload[season].sort(key=lambda item: item["ep"])
|
||||||
|
|
||||||
|
key = store.entity_key("tv", show_id)
|
||||||
|
try:
|
||||||
|
with store.transaction(config.TMDB_CACHE_PATH) as db:
|
||||||
|
entity = db["entities"].get(key)
|
||||||
|
if entity is None:
|
||||||
|
entity = store.build_entity("tv", show_id, show)
|
||||||
|
db["entities"][key] = entity
|
||||||
|
entity["episode_air_dates"] = dict(payload)
|
||||||
|
entity["episodes_fetched_at"] = store.now_iso()
|
||||||
|
except OSError as exc:
|
||||||
|
log.warning(f"⚠️ 单集数据缓存写入失败: {exc}")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def build_index(cls, show_id: int) -> dict:
|
def build_index(cls, show_id: int) -> dict:
|
||||||
"""{date: [{'season','ep','name'}]},含 Season 00。按需拉取,失败静默跳过。"""
|
"""{date: [{'season','ep','name','air_year','air_date'}]},含 Season 00。
|
||||||
|
|
||||||
|
优先用缓存里的单集数据(24 小时有效),过期或没有才回源 TMDB。
|
||||||
|
"""
|
||||||
|
key = store.entity_key("tv", show_id)
|
||||||
|
snapshot = store.snapshot(config.TMDB_CACHE_PATH)
|
||||||
|
entity = snapshot["entities"].get(key)
|
||||||
|
if entity:
|
||||||
|
cached = entity.get("episode_air_dates") or {}
|
||||||
|
if cached and cls._fresh(entity.get("episodes_fetched_at") or ""):
|
||||||
|
index = cls._index_from_cache(cached)
|
||||||
|
log.info(f"📅 TMDB 索引命中缓存({cls.CACHE_TTL_HOURS}h 内,{len(index)} 个播出日期)")
|
||||||
|
return index
|
||||||
|
|
||||||
session = TMDBService._get_session()
|
session = TMDBService._get_session()
|
||||||
show = TMDBService._fetch_details(session, "tv", show_id)
|
show = TMDBService._fetch_details(session, "tv", show_id)
|
||||||
if not show:
|
if not show:
|
||||||
@@ -119,8 +186,10 @@ class VarietyService:
|
|||||||
"air_year": air[:4],
|
"air_year": air[:4],
|
||||||
"air_date": air,
|
"air_date": air,
|
||||||
})
|
})
|
||||||
log.info(f"📅 TMDB 索引: {len(index)} 个播出日期({len(seasons)} 季)")
|
result = dict(index)
|
||||||
return dict(index)
|
log.info(f"📅 TMDB 索引: {len(result)} 个播出日期({len(seasons)} 季,已写入缓存)")
|
||||||
|
cls._save_index(show_id, result, show)
|
||||||
|
return result
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 打分与分配
|
# ---------------------------------------------------------------- 打分与分配
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user