-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtmdb_api.py
More file actions
64 lines (50 loc) · 2.21 KB
/
tmdb_api.py
File metadata and controls
64 lines (50 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import requests
import logging
from typing import Optional, Dict, List
logger = logging.getLogger(__name__)
class TMDBAPI:
def __init__(self, api_key: Optional[str] = None):
self.api_key = api_key
self.base_url = "https://api.themoviedb.org/3"
self.enabled = bool(api_key)
def _make_request(self, endpoint: str, params: Optional[Dict] = None) -> Optional[Dict]:
if not self.enabled:
return None
if params is None:
params = {}
params['api_key'] = self.api_key
try:
response = requests.get(f"{self.base_url}/{endpoint}", params=params, timeout=10)
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
logger.error(f"TMDB API request failed: {e}")
return None
def search_movie(self, title: str, year: Optional[int] = None) -> Optional[Dict]:
params = {'query': title}
if year:
params['year'] = year
data = self._make_request('search/movie', params)
if data and data.get('results'):
return data['results'][0]
return None
def get_movie_details(self, tmdb_id: int) -> Optional[Dict]:
return self._make_request(f'movie/{tmdb_id}', {'append_to_response': 'keywords'})
def get_movie_keywords(self, tmdb_id: int) -> List[str]:
data = self._make_request(f'movie/{tmdb_id}/keywords')
if data and 'keywords' in data:
return [kw['name'].lower() for kw in data['keywords']]
return []
def get_collection(self, collection_id: int) -> Optional[Dict]:
return self._make_request(f'collection/{collection_id}')
def get_movie_by_plex_metadata(self, title: str, year: Optional[int] = None) -> Optional[Dict]:
movie_data = self.search_movie(title, year)
if not movie_data:
return None
tmdb_id = movie_data.get('id')
if not tmdb_id:
return None
details = self.get_movie_details(tmdb_id)
if details:
details['tmdb_keywords'] = self.get_movie_keywords(tmdb_id)
return details