Karan Adhikari commited on
Commit
30463fe
·
unverified ·
1 Parent(s): 07f63a8
Files changed (1) hide show
  1. Backend/helper/metadata.py +27 -13
Backend/helper/metadata.py CHANGED
@@ -16,7 +16,21 @@ from Backend.helper.encrypt import encode_string
16
  # ----------------- Configuration -----------------
17
 
18
  DELAY = 0
19
- tmdb = aioTMDb(key=SettingsManager.current().tmdb_api, language="en-US", region="US")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Cache dictionaries (per run)
22
  IMDB_CACHE: dict = {}
@@ -285,16 +299,16 @@ async def safe_tmdb_search(title: str, type_: str, year: Optional[int] = None):
285
  async with API_SEMAPHORE:
286
  if type_ == "movie":
287
  results = (
288
- await tmdb.search().movies(query=title, year=year)
289
  if year
290
- else await tmdb.search().movies(query=title)
291
  )
292
  # Year-constrained search sometimes returns nothing for new/limited releases
293
  if not results and year:
294
  async with API_SEMAPHORE:
295
- results = await tmdb.search().movies(query=title)
296
  else:
297
- results = await tmdb.search().tv(query=title)
298
 
299
  best = _pick_best_tmdb_result(results, title, year, type_)
300
 
@@ -357,10 +371,10 @@ async def _tmdb_movie_details(movie_id):
357
  return TMDB_DETAILS_CACHE[movie_id]
358
  try:
359
  async with API_SEMAPHORE:
360
- details = await tmdb.movie(movie_id).details(
361
  append_to_response="external_ids,credits"
362
  )
363
- images = await tmdb.movie(movie_id).images()
364
  details.images = images
365
 
366
  TMDB_DETAILS_CACHE[movie_id] = details
@@ -376,10 +390,10 @@ async def _tmdb_tv_details(tv_id):
376
  return TMDB_DETAILS_CACHE[tv_id]
377
  try:
378
  async with API_SEMAPHORE:
379
- details = await tmdb.tv(tv_id).details(
380
  append_to_response="external_ids,credits"
381
  )
382
- images = await tmdb.tv(tv_id).images()
383
  details.images = images
384
  TMDB_DETAILS_CACHE[tv_id] = details
385
  return details
@@ -395,7 +409,7 @@ async def _tmdb_episode_details(tv_id, season, episode):
395
  return EPISODE_CACHE[key]
396
  try:
397
  async with API_SEMAPHORE:
398
- details = await tmdb.episode(tv_id, season, episode).details()
399
  EPISODE_CACHE[key] = details
400
  return details
401
  except Exception:
@@ -826,9 +840,9 @@ async def search_movie_candidates(query: str, year: int | None = None, limit: in
826
  try:
827
  async with API_SEMAPHORE:
828
  tmdb_results = (
829
- await tmdb.search().movies(query=query, year=year)
830
  if year
831
- else await tmdb.search().movies(query=query)
832
  )
833
 
834
  for item in (tmdb_results or [])[:limit]:
@@ -897,7 +911,7 @@ async def search_tv_candidates(query: str, limit: int = 8) -> list[dict]:
897
 
898
  try:
899
  async with API_SEMAPHORE:
900
- tmdb_results = await tmdb.search().tv(query=query)
901
 
902
  for item in (tmdb_results or [])[:limit]:
903
  tmdb_id = getattr(item, "id", None)
 
16
  # ----------------- Configuration -----------------
17
 
18
  DELAY = 0
19
+
20
+ _tmdb_client: aioTMDb | None = None
21
+ _tmdb_client_key: str | None = None
22
+
23
+
24
+ def get_tmdb_client() -> aioTMDb:
25
+ global _tmdb_client, _tmdb_client_key
26
+
27
+ current_key = SettingsManager.current().tmdb_api
28
+ if _tmdb_client is None or _tmdb_client_key != current_key:
29
+ _tmdb_client = aioTMDb(key=current_key, language="en-US", region="US")
30
+ _tmdb_client_key = current_key
31
+
32
+ return _tmdb_client
33
+
34
 
35
  # Cache dictionaries (per run)
36
  IMDB_CACHE: dict = {}
 
299
  async with API_SEMAPHORE:
300
  if type_ == "movie":
301
  results = (
302
+ await get_tmdb_client().search().movies(query=title, year=year)
303
  if year
304
+ else await get_tmdb_client().search().movies(query=title)
305
  )
306
  # Year-constrained search sometimes returns nothing for new/limited releases
307
  if not results and year:
308
  async with API_SEMAPHORE:
309
+ results = await get_tmdb_client().search().movies(query=title)
310
  else:
311
+ results = await get_tmdb_client().search().tv(query=title)
312
 
313
  best = _pick_best_tmdb_result(results, title, year, type_)
314
 
 
371
  return TMDB_DETAILS_CACHE[movie_id]
372
  try:
373
  async with API_SEMAPHORE:
374
+ details = await get_tmdb_client().movie(movie_id).details(
375
  append_to_response="external_ids,credits"
376
  )
377
+ images = await get_tmdb_client().movie(movie_id).images()
378
  details.images = images
379
 
380
  TMDB_DETAILS_CACHE[movie_id] = details
 
390
  return TMDB_DETAILS_CACHE[tv_id]
391
  try:
392
  async with API_SEMAPHORE:
393
+ details = await get_tmdb_client().tv(tv_id).details(
394
  append_to_response="external_ids,credits"
395
  )
396
+ images = await get_tmdb_client().tv(tv_id).images()
397
  details.images = images
398
  TMDB_DETAILS_CACHE[tv_id] = details
399
  return details
 
409
  return EPISODE_CACHE[key]
410
  try:
411
  async with API_SEMAPHORE:
412
+ details = await get_tmdb_client().episode(tv_id, season, episode).details()
413
  EPISODE_CACHE[key] = details
414
  return details
415
  except Exception:
 
840
  try:
841
  async with API_SEMAPHORE:
842
  tmdb_results = (
843
+ await get_tmdb_client().search().movies(query=query, year=year)
844
  if year
845
+ else await get_tmdb_client().search().movies(query=query)
846
  )
847
 
848
  for item in (tmdb_results or [])[:limit]:
 
911
 
912
  try:
913
  async with API_SEMAPHORE:
914
+ tmdb_results = await get_tmdb_client().search().tv(query=query)
915
 
916
  for item in (tmdb_results or [])[:limit]:
917
  tmdb_id = getattr(item, "id", None)