This commit is contained in:
2025-09-12 13:54:29 -04:00
parent 77295cc400
commit 4ef28c3021
5 changed files with 103 additions and 10 deletions
+67
View File
@@ -42,6 +42,64 @@ def _parse_date_to_iso(date_str: str) -> Optional[str]:
return None
class TVDBClient:
"""The TV Database API client for IMDB to TVDB ID conversion"""
def __init__(self, api_key: str = None):
self.api_key = api_key or os.environ.get("TVDB_API_KEY", "")
self.base_url = "https://api4.thetvdb.com/v4"
self._token = None
self._token_expires = 0
def _get_token(self) -> Optional[str]:
"""Get TVDB auth token (cached)"""
if not self.api_key:
return None
if time.time() < self._token_expires and self._token:
return self._token
try:
req = UrlRequest(
f"{self.base_url}/login",
data=json.dumps({"apikey": self.api_key}).encode('utf-8'),
headers={"Content-Type": "application/json"}
)
with urlopen(req, timeout=10) as resp:
data = json.loads(resp.read().decode("utf-8"))
if data.get("status") == "success":
self._token = data["data"]["token"]
self._token_expires = time.time() + 3600 # 1 hour
return self._token
except Exception as e:
_log("WARNING", f"TVDB login failed: {e}")
return None
def imdb_to_tvdb_series_id(self, imdb_id: str) -> Optional[str]:
"""Convert IMDB ID to TVDB series ID"""
token = self._get_token()
if not token:
return None
try:
# Search by remote ID (IMDB)
url = f"{self.base_url}/search/remoteid?remoteId={imdb_id}&type=series"
headers = {"Authorization": f"Bearer {token}"}
data = _get_json(url, headers=headers)
if data and data.get("status") == "success" and data.get("data"):
series_list = data["data"]
if series_list and len(series_list) > 0:
tvdb_id = series_list[0].get("id")
if tvdb_id:
_log("INFO", f"TVDB: Converted {imdb_id}{tvdb_id}")
return str(tvdb_id)
except Exception as e:
_log("WARNING", f"TVDB IMDB conversion failed for {imdb_id}: {e}")
return None
class TMDBClient:
"""The Movie Database API client"""
@@ -346,6 +404,7 @@ class ExternalClientManager:
self.tmdb = TMDBClient(primary_country=tmdb_country)
self.omdb = OMDbClient()
self.jellyseerr = JellyseerrClient()
self.tvdb = TVDBClient()
def get_release_date_by_priority(self, imdb_id: str, priority_order: List[str], enable_smart_validation: bool = True) -> Optional[Tuple[str, str]]:
"""Get release date using configurable priority order with smart date validation"""
@@ -461,6 +520,14 @@ class ExternalClientManager:
"""Get the earliest digital release date (legacy method)"""
candidates = self.get_digital_release_candidates(imdb_id)
return candidates[0] if candidates else None
def get_tvdb_series_id(self, imdb_id: str) -> Optional[str]:
"""Get TVDB series ID from IMDB ID"""
if not self.tvdb.api_key:
_log("WARNING", "TVDB API key not configured, skipping TVDB ID lookup")
return None
return self.tvdb.imdb_to_tvdb_series_id(imdb_id)
if __name__ == "__main__":