fix(cache): enhance tmdb match_web rate-limiting and caching

This commit is contained in:
InfinityPacer
2025-01-22 14:58:56 +08:00
parent 70c4509682
commit 5c7bd95f6b
+13 -5
View File
@@ -8,8 +8,10 @@ from lxml import etree
from app.core.cache import cached
from app.core.config import settings
from app.log import logger
from app.schemas import APIRateLimitException
from app.schemas.types import MediaType
from app.utils.http import RequestUtils
from app.utils.limit import rate_limit_exponential
from app.utils.string import StringUtils
from .tmdbv3api import TMDb, Search, Movie, TV, Season, Episode, Discover, Trending, Person, Collection
from .tmdbv3api.exceptions import TMDbException
@@ -492,6 +494,7 @@ class TmdbApi:
return ret_info
@cached(maxsize=settings.CACHE_CONF["tmdb"], ttl=settings.CACHE_CONF["meta"])
@rate_limit_exponential(source="match_tmdb_web", max_wait=1800, enable_logging=True)
def match_web(self, name: str, mtype: MediaType) -> Optional[dict]:
"""
搜索TMDB网站,直接抓取结果,结果只有一条时才返回
@@ -504,11 +507,16 @@ class TmdbApi:
return {}
logger.info("正在从TheDbMovie网站查询:%s ..." % name)
tmdb_url = "https://www.themoviedb.org/search?query=%s" % quote(name)
res = RequestUtils(timeout=5, ua=settings.USER_AGENT).get_res(url=tmdb_url)
if res and res.status_code == 200:
res = RequestUtils(timeout=5, ua=settings.USER_AGENT, proxies=settings.PROXY).get_res(url=tmdb_url)
if res is None:
return None
if res.status_code == 429:
raise APIRateLimitException("触发TheDbMovie网站限流,获取媒体信息失败")
if res.status_code != 200:
return {}
html_text = res.text
if not html_text:
return None
return {}
try:
tmdb_links = []
html = etree.HTML(html_text)
@@ -547,8 +555,8 @@ class TmdbApi:
logger.info("%s TMDB网站未查询到媒体信息!" % name)
except Exception as err:
logger.error(f"从TheDbMovie网站查询出错:{str(err)}")
return None
return None
return {}
return {}
def get_info(self,
mtype: MediaType,