-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtmdb_utils.py
More file actions
468 lines (374 loc) · 16.4 KB
/
Copy pathtmdb_utils.py
File metadata and controls
468 lines (374 loc) · 16.4 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import os
import requests
from dotenv import load_dotenv
import sonarr_utils # Import your existing sonarr_utils module
load_dotenv()
TMDB_API_KEY = os.getenv('TMDB_API_KEY')
MAX_TMDB_MOVIES = int(os.getenv('MAX_TMDB_MOVIES', 24))
MAX_TMDB_SHOWS = int(os.getenv('MAX_TMDB_SHOWS', 24))
def get_tmdb_endpoint(endpoint, params=None):
"""Make a request to any TMDB endpoint with the given parameters."""
base_url = f"https://api.themoviedb.org/3/{endpoint}"
if params is None:
params = {}
# Check if we have a v3 or v4 token
auth_token = TMDB_API_KEY.strip('"\'')
try:
headers = {}
# If it's a long token (v4), use it as a bearer token
if len(auth_token) > 40:
headers["Authorization"] = f"Bearer {auth_token}"
else:
# Otherwise use as API key in params (v3)
params['api_key'] = auth_token
response = requests.get(base_url, params=params, headers=headers)
if response.status_code == 200:
return response.json()
else:
print(f"Error fetching {endpoint}: {response.status_code}")
return {'results': []}
except Exception as e:
print(f"Exception during API request: {str(e)}")
return {'results': []}
def get_movie_details(tmdb_id):
"""
Get detailed movie information from TMDB by its ID
:param tmdb_id: TMDB movie ID
:return: Dictionary of movie details
"""
return get_tmdb_endpoint(f"movie/{tmdb_id}")
def get_tv_show_details(tmdb_id):
"""
Get detailed TV show information from TMDB by its ID
:param tmdb_id: TMDB TV show ID
:return: Dictionary of TV show details
"""
return get_tmdb_endpoint(f"tv/{tmdb_id}")
def is_talk_show(title):
"""Check if a show title appears to be a talk show or similar format."""
keywords = [
'tonight show', 'late show', 'late night', 'daily show',
'talk show', 'with seth meyers', 'with james corden',
'with jimmy', 'with stephen', 'with trevor', 'news',
'live with', 'watch what happens live', 'the view',
'good morning', 'today show', 'kimmel', 'colbert',
'fallon', 'ellen', 'conan', 'graham norton', 'meet the press',
'face the nation', 'last week tonight', 'real time',
'kelly and', 'kelly &', 'jeopardy', 'wheel of fortune',
'daily mail', 'entertainment tonight', 'zeiten', 'schlechte'
]
title_lower = title.lower()
return any(keyword in title_lower for keyword in keywords)
def get_quality_movies():
"""Get quality movies avoiding duplicates with your Radarr library."""
# Import radarr_utils here to avoid circular imports
import radarr_utils
# Get existing movies from Radarr
radarr_preferences = radarr_utils.load_preferences()
radarr_movies = radarr_utils.get_movie_list(radarr_preferences)
existing_tmdb_ids = []
# Extract TMDB IDs from Radarr movies
for movie in radarr_movies:
if 'tmdbId' in movie and movie['tmdbId']:
existing_tmdb_ids.append(movie['tmdbId'])
print(f"Found {len(existing_tmdb_ids)} movies with TMDB IDs in Radarr")
# Get movies from multiple sources
all_movies = []
# Get trending movies (weekly and daily)
trending_weekly = get_tmdb_endpoint("trending/movie/week", {'language': 'en-US'})
all_movies.extend(trending_weekly.get('results', []))
trending_daily = get_tmdb_endpoint("trending/movie/day", {'language': 'en-US'})
all_movies.extend(trending_daily.get('results', []))
# Get popular movies
popular = get_tmdb_endpoint("movie/popular", {'language': 'en-US'})
all_movies.extend(popular.get('results', []))
# Get top rated movies
top_rated = get_tmdb_endpoint("movie/top_rated", {'language': 'en-US'})
all_movies.extend(top_rated.get('results', []))
# Filter and deduplicate
filtered_results = []
seen_ids = set()
for movie in all_movies:
movie_id = movie.get('id')
# Skip if already processed or in Radarr
if movie_id in seen_ids or movie_id in existing_tmdb_ids:
continue
seen_ids.add(movie_id)
# Get movie details
title = movie.get('title', '')
original_language = movie.get('original_language', '')
# Skip non-English movies
if original_language != 'en':
continue
# Add to filtered results
filtered_results.append(movie)
print(f"After filtering, returning {len(filtered_results)} movies")
return {'results': filtered_results[:MAX_TMDB_MOVIES]}
def get_quality_tv_shows():
"""Get quality TV shows avoiding duplicates with your Sonarr library."""
# Load Sonarr preferences
sonarr_preferences = sonarr_utils.load_preferences()
# Get existing series from Sonarr
sonarr_series = sonarr_utils.get_series_list(sonarr_preferences)
existing_tmdb_ids = []
# Extract TMDB IDs from Sonarr series
for series in sonarr_series:
if 'tmdbId' in series and series['tmdbId']:
existing_tmdb_ids.append(series['tmdbId'])
print(f"Found {len(existing_tmdb_ids)} shows with TMDB IDs in Sonarr")
# Get shows from multiple sources for variety
all_shows = []
# Get trending TV shows for the week (2 pages)
for page in [1, 2]:
trending = get_tmdb_endpoint("trending/tv/week", {
'language': 'en-US',
'page': page
})
all_shows.extend(trending.get('results', []))
# Get top rated TV shows (2 pages)
for page in [1, 2]:
top_rated = get_tmdb_endpoint("tv/top_rated", {
'language': 'en-US',
'page': page
})
all_shows.extend(top_rated.get('results', []))
# Get popular shows (2 pages)
for page in [1, 2]:
popular = get_tmdb_endpoint("tv/popular", {
'language': 'en-US',
'page': page
})
all_shows.extend(popular.get('results', []))
# Filter and deduplicate
filtered_results = []
seen_ids = set()
for show in all_shows:
show_id = show.get('id')
# Skip if we've already processed this show
if show_id in seen_ids:
continue
seen_ids.add(show_id)
# Skip if already in Sonarr
if show_id in existing_tmdb_ids:
continue
title = show.get('name', '').lower()
year = 0
# Get the year if available
if show.get('first_air_date'):
try:
year = int(show.get('first_air_date', '').split('-')[0])
except:
pass
# Skip shows older than 2010
if year > 0 and year < 2010:
continue
# Skip talk shows and similar formats
if is_talk_show(title):
continue
# Skip news, reality, talk shows by genre
if any(genre_id in [10763, 10764, 10767] for genre_id in show.get('genre_ids', [])):
continue
# Skip non-English shows (original_language should be 'en')
if show.get('original_language') != 'en':
continue
# Add the show to our filtered results
filtered_results.append(show)
print(f"After filtering, returning {len(filtered_results)} shows")
# Return more shows for better display
return {'results': filtered_results[:MAX_TMDB_SHOWS]}
def get_recommendations_by_genre(genre_ids, media_type='movie'):
"""Get recommendations based on genres."""
try:
params = {
'api_key': TMDB_API_KEY,
'language': 'en-US',
'sort_by': 'popularity.desc',
'with_genres': ','.join(str(g) for g in genre_ids),
'page': 1
}
endpoint = 'discover/movie' if media_type == 'movie' else 'discover/tv'
response = requests.get(f"{TMDB_BASE_URL}/{endpoint}", params=params)
if response.ok:
return response.json()
else:
logger.error(f"Failed to get {media_type} recommendations: {response.status_code}")
return {'results': []}
except Exception as e:
logger.error(f"Error getting {media_type} recommendations: {str(e)}")
return {'results': []}
def search_tv_shows(query):
"""Search for TV shows using TMDB API."""
return get_tmdb_endpoint("search/tv", {
'query': query,
'language': 'en-US',
'page': 1
})
def search_movies(query):
"""Search for movies using TMDB API."""
return get_tmdb_endpoint("search/movie", {
'query': query,
'language': 'en-US',
'page': 1
})
def get_external_ids(tmdb_id, media_type='tv'):
"""Get external IDs for a TV show or movie."""
endpoint = f"{media_type}/{tmdb_id}/external_ids"
return get_tmdb_endpoint(endpoint)
def get_or_add_series_to_sonarr(show_data):
"""Add a series to Sonarr if it doesn't exist, or return the ID if it does."""
try:
tmdb_id = show_data.get('id')
# Get TVDB ID from TMDB
details = tmdb_utils.get_external_ids(tmdb_id, 'tv')
tvdb_id = details.get('tvdb_id')
if not tvdb_id:
app.logger.error(f"Could not find TVDB ID for {show_data.get('name')}")
return None
# Check if show exists in Sonarr
sonarr_preferences = sonarr_utils.load_preferences()
headers = {
'X-Api-Key': sonarr_preferences['SONARR_API_KEY'],
'Content-Type': 'application/json'
}
sonarr_url = sonarr_preferences['SONARR_URL']
# Check if show already exists in Sonarr
existing_series = sonarr_utils.get_series_list(sonarr_preferences)
for series in existing_series:
if series.get('tvdbId') == tvdb_id:
return series.get('id')
# If not, look up the show
response = requests.get(
f"{sonarr_url}/api/v3/series/lookup",
headers=headers,
params={"term": f"tvdb:{tvdb_id}"}
)
if not response.ok or not response.json():
app.logger.error(f"Failed to look up show {show_data.get('name')}")
return None
lookup_results = response.json()
# Get the first root folder path
root_folder_response = requests.get(f"{sonarr_url}/api/v3/rootfolder", headers=headers)
if not root_folder_response.ok or not root_folder_response.json():
app.logger.error("Failed to get root folders from Sonarr")
return None
root_folder = root_folder_response.json()[0].get('path')
# Get quality profile
profile_response = requests.get(f"{sonarr_url}/api/v3/qualityprofile", headers=headers)
if not profile_response.ok or not profile_response.json():
app.logger.error("Failed to get quality profiles from Sonarr")
return None
# Use the first quality profile
quality_profile_id = profile_response.json()[0].get('id')
# Prepare series for adding
series_to_add = lookup_results[0]
series_to_add['rootFolderPath'] = root_folder
series_to_add['qualityProfileId'] = quality_profile_id
series_to_add['monitored'] = True
series_to_add['addOptions'] = {
'searchForMissingEpisodes': False,
'monitor': 'none' # Start with nothing monitored
}
# Add to Sonarr
add_response = requests.post(
f"{sonarr_url}/api/v3/series",
headers=headers,
json=series_to_add
)
if not add_response.ok:
app.logger.error(f"Failed to add show to Sonarr: {add_response.text}")
return None
return add_response.json().get('id')
except Exception as e:
app.logger.error(f"Error in get_or_add_series_to_sonarr: {str(e)}")
return None
def monitor_and_search_season(series_id, season_number):
"""Monitor and search for a full season."""
try:
sonarr_preferences = sonarr_utils.load_preferences()
headers = {
'X-Api-Key': sonarr_preferences['SONARR_API_KEY'],
'Content-Type': 'application/json'
}
sonarr_url = sonarr_preferences['SONARR_URL']
# Get episodes for the season
episodes_response = requests.get(
f"{sonarr_url}/api/v3/episode?seriesId={series_id}&seasonNumber={season_number}",
headers=headers
)
if not episodes_response.ok or not episodes_response.json():
app.logger.error(f"Failed to get episodes for season {season_number}")
return False
episodes = episodes_response.json()
episode_ids = [ep.get('id') for ep in episodes]
# Monitor all episodes
if episode_ids:
monitor_response = requests.put(
f"{sonarr_url}/api/v3/episode/monitor",
headers=headers,
json={"episodeIds": episode_ids, "monitored": True}
)
if not monitor_response.ok:
app.logger.error(f"Failed to monitor episodes for season {season_number}")
return False
# Search for the season
search_response = requests.post(
f"{sonarr_url}/api/v3/command",
headers=headers,
json={"name": "SeasonSearch", "seriesId": series_id, "seasonNumber": season_number}
)
if not search_response.ok:
app.logger.error(f"Failed to search for season {season_number}")
return False
return True
except Exception as e:
app.logger.error(f"Error in monitor_and_search_season: {str(e)}")
return False
def monitor_and_search_episode(series_id, season_number, episode_number):
"""Monitor and search for a specific episode."""
try:
sonarr_preferences = sonarr_utils.load_preferences()
headers = {
'X-Api-Key': sonarr_preferences['SONARR_API_KEY'],
'Content-Type': 'application/json'
}
sonarr_url = sonarr_preferences['SONARR_URL']
# Get episodes for the season
episodes_response = requests.get(
f"{sonarr_url}/api/v3/episode?seriesId={series_id}&seasonNumber={season_number}",
headers=headers
)
if not episodes_response.ok:
app.logger.error(f"Failed to get episodes for season {season_number}")
return False
episodes = episodes_response.json()
# Find the specific episode
target_episode = None
for ep in episodes:
if ep.get('episodeNumber') == episode_number:
target_episode = ep
break
if not target_episode:
app.logger.error(f"Episode {episode_number} not found in season {season_number}")
return False
# Monitor just this episode
monitor_response = requests.put(
f"{sonarr_url}/api/v3/episode/monitor",
headers=headers,
json={"episodeIds": [target_episode.get('id')], "monitored": True}
)
if not monitor_response.ok:
app.logger.error(f"Failed to monitor episode {episode_number}")
return False
# Search for the episode
search_response = requests.post(
f"{sonarr_url}/api/v3/command",
headers=headers,
json={"name": "EpisodeSearch", "episodeIds": [target_episode.get('id')]}
)
if not search_response.ok:
app.logger.error(f"Failed to search for episode {episode_number}")
return False
return True
except Exception as e:
app.logger.error(f"Error in monitor_and_search_episode: {str(e)}")
return False