-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathTMDB.py
More file actions
479 lines (389 loc) · 18.5 KB
/
Copy pathTMDB.py
File metadata and controls
479 lines (389 loc) · 18.5 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
469
470
471
472
473
474
475
476
477
478
479
import requests
from PIL import Image, ImageDraw, ImageFont, ImageFilter
from io import BytesIO
import os
import shutil
from urllib.request import urlopen
import textwrap
from datetime import datetime, timedelta
from dotenv import load_dotenv
load_dotenv(verbose=True)
TMDB_BEARER_TOKEN = os.getenv('TMDB_BEARER_TOKEN')
TMDB_BASE_URL = os.getenv('TMDB_BASE_URL')
LANGUAGE = os.getenv("TMDB_LANGUAGE")
# Set your TMDB API Read Access Token key here
headers = {
"accept": "application/json",
"Authorization": f"Bearer {TMDB_BEARER_TOKEN}"
}
# Get current date in YYYYMMDD format
date_str = datetime.now().strftime("%Y%m%d")
# Set the number of movies and tvshwos to get
numberofmovies = 5
numberoftvshows = 5
# TV Exclusion list - this filter will exclude TV shows from chosen countries that have a specific genre
tv_excluded_countries = ['XX', 'XX', 'XX', 'XX', 'XX', 'XX'] # ISO 3166-1 alpha-2 codes lowercas, jp, kr, us for Japan, Korea, and the US
tv_excluded_genres = {
'XX': ['XXXX', 'XXXX', 'XXXX'], # Exclude Animation and Drama from Japan
'XX': ['XXXX', 'XXX', 'XXXX', 'XXXX'], # Exclude Animation and Drama from Korea
'XX': ['XXXX','XXXX'], # Exclude Animation from the US
'XX': ['*'],
'XX': ['*'],
'X': ['*']
}
# Movie Exclusion list - this filter will exclude movies from chosen countries that have a specific genre
movie_excluded_countries = ['XX', 'XX', 'XX', 'XX', 'XX', 'XX'] # ISO 3166-1 alpha-2 codes lowercas, jp, kr, us for Japan, Korea, and the US
movie_excluded_genres = {
'XX': ['XX'], # Exclude Animation and Drama from Japan
'kr': ['XX'], # Exclude Animation and Drama from Korea
'XX': ['XX'], # Exclude Animation and Drama from US
'XX': ['*'],
'XX': ['*'],
'XX': ['*']
}
# Keyword exclusion list - this filter will exclude movies or TV shows that contain a specific keyword in their TMDB profile
excluded_keywords = ['XX', 'XX', 'XX', 'XX', 'XX', 'XX', 'XX','XX'] # like ['adult']
# Filter movies by release date and TV shows by last air date
# Filter movies by release date and TV shows by last air date
max_air_date = datetime.now() - timedelta(days=90) # specify the number of days since the movie release or the TV show last air date, shows before this date will be excluded
# Save font locally
truetype_url = 'https://github.com/googlefonts/roboto/raw/main/src/hinted/Roboto-Light.ttf'
truetype_path = 'Roboto-Light.ttf'
if not os.path.exists(truetype_path):
try:
response = requests.get(truetype_url, timeout=10)
if response.status_code == 200:
with open(truetype_path, 'wb') as f:
f.write(response.content)
print("Roboto-Light font saved")
else:
print(f"Failed to download Roboto-Light font. Status code: {response.status_code}")
except Exception as e:
print(f"An error occurred while downloading the Roboto-Light font: {e}")
# Fetching genres for movies
genres_url = f'{TMDB_BASE_URL}/genre/movie/list?language={LANGUAGE}'
genres_response = requests.get(genres_url, headers=headers)
genres_data = genres_response.json()
movie_genres = {genre['id']: genre['name'] for genre in genres_data.get('genres', [])}
# Fetching genres for TV shows
genres_url = f'{TMDB_BASE_URL}/genre/tv/list?language={LANGUAGE}'
genres_response = requests.get(genres_url, headers=headers)
genres_data = genres_response.json()
tv_genres = {genre['id']: genre['name'] for genre in genres_data.get('genres', [])}
# Fetching TV show details
def get_tv_show_details(tv_id):
tv_details_url = f'{TMDB_BASE_URL}/tv/{tv_id}?language={LANGUAGE}'
tv_details_response = requests.get(tv_details_url, headers=headers)
return tv_details_response.json()
# Fetching movie details
def get_movie_details(movie_id):
movie_details_url = f'{TMDB_BASE_URL}/movie/{movie_id}?language={LANGUAGE}'
movie_details_response = requests.get(movie_details_url, headers=headers)
return movie_details_response.json()
# Function to fetch keywords for a movie
def get_movie_keywords(movie_id):
keywords_url = f"{TMDB_BASE_URL}/movie/{movie_id}/keywords"
response = requests.get(keywords_url, headers=headers)
if response.status_code == 200:
# Extract and return the names of the keywords
return [keyword['name'].lower() for keyword in response.json().get('keywords', [])]
return []
# Function to fetch keywords for a TV show
def get_tv_keywords(tv_id):
keywords_url = f"{TMDB_BASE_URL}/tv/{tv_id}/keywords"
response = requests.get(keywords_url, headers=headers)
if response.status_code == 200:
return [keyword['name'].lower() for keyword in response.json().get('results', [])]
return []
# Filter criteria for movies
def should_exclude_movie(movie, movie_excluded_countries=movie_excluded_countries, movie_excluded_genres=movie_excluded_genres, excluded_keywords=excluded_keywords):
# Check if the movie's country is in the excluded countries list
origin_countries = [c.lower() for c in movie.get('origin_country', [])]
genres = [movie_genres.get(genre_id, '') for genre_id in movie.get('genre_ids', [])]
# Fetch movie keywords
movie_keywords = get_movie_keywords(movie['id']) if excluded_keywords else []
# Check release date
release_date_str = movie.get('release_date')
release_date = datetime.strptime(release_date_str, "%Y-%m-%d") if release_date_str else None
# Exclusion logic by country and genre
for country in origin_countries:
if country in movie_excluded_countries:
excluded = movie_excluded_genres.get(country, [])
if excluded == ['*'] or any(genre in excluded for genre in genres):
return True
# Exclusion by keyword or date
if any(keyword in movie_keywords for keyword in excluded_keywords):
return True
if release_date and release_date < max_air_date:
return True
return False
# Filter criteria for TV shows
def should_exclude_tvshow(tvshow, tv_excluded_countries=tv_excluded_countries, tv_excluded_genres=tv_excluded_genres, excluded_keywords=excluded_keywords):
# Ensure 'origin_country' is a list or string and get the country (case insensitive)
origin_countries = [c.lower() for c in tvshow.get('origin_country', [])]
genres = [tv_genres.get(genre_id, '') for genre_id in tvshow.get('genre_ids', [])]
for country in origin_countries:
if country in tv_excluded_countries:
excluded = tv_excluded_genres.get(country, [])
if excluded == ['*'] or any(genre in excluded for genre in genres):
return True
# Fetch TV show keywords and check against the exclusion list
tv_keywords = get_tv_keywords(tvshow['id']) if excluded_keywords else []
if any(keyword in tv_keywords for keyword in excluded_keywords):
return True
# Check last air date
last_air_date_str = get_tv_show_details(tvshow['id']).get('last_air_date')
if last_air_date_str:
try:
last_air_date = datetime.strptime(last_air_date_str, "%Y-%m-%d")
except ValueError:
last_air_date = None
else:
last_air_date = None
# Exclude if older than max_air_date
if last_air_date and last_air_date < max_air_date:
return True
# Include future shows
if last_air_date and last_air_date > datetime.now():
return False
return False
# Endpoint for trending shows
trending_movies_url = f'{TMDB_BASE_URL}/trending/movie/week?language={LANGUAGE}'
trending_tvshows_url = f'{TMDB_BASE_URL}/trending/tv/week?language={LANGUAGE}'
# Fetch more than required to allow filtering
initial_fetch_count = numberofmovies + 10 # Fetch 15 to get at least 5 valid ones
trending_movies_url = f'{TMDB_BASE_URL}/trending/movie/week?language={LANGUAGE}'
trending_movies_response = requests.get(trending_movies_url, headers=headers)
all_movies = trending_movies_response.json().get('results', [])[:initial_fetch_count]
# Filter manually
valid_movies = []
for movie in all_movies:
if not should_exclude_movie(movie):
valid_movies.append(movie)
else:
print(f"Excluded Movie: {movie['title']} ({movie.get('origin_country')})")
if len(valid_movies) >= numberofmovies:
break
trending_movies = {'results': valid_movies}
# Fetching trending TV shows
initial_fetch_count = numberoftvshows + 10 # Fetch more than needed
trending_tvshows_url = f'{TMDB_BASE_URL}/trending/tv/week?language={LANGUAGE}'
trending_tvshows_response = requests.get(trending_tvshows_url, headers=headers)
all_tvshows = trending_tvshows_response.json().get('results', [])[:initial_fetch_count]
# Filter manually
valid_tvshows = []
for tvshow in all_tvshows:
if not should_exclude_tvshow(tvshow):
valid_tvshows.append(tvshow)
if len(valid_tvshows) >= numberoftvshows:
break
trending_tvshows = {'results': valid_tvshows}
# Create a directory to save the backgrounds and clear its contents
background_dir = "tmdb_backgrounds"
if os.path.exists(background_dir):
shutil.rmtree(background_dir)
os.makedirs(background_dir, exist_ok=True)
# Truncate overview
def truncate_overview(overview, max_chars):
if len(overview) > max_chars:
return overview[:max_chars]
else:
return overview
# Truncate
def truncate(overview, max_chars):
if len(overview) > max_chars:
return overview[:max_chars-3]
else:
return overview
# Resize image
def resize_image(image, height):
ratio = height / image.height
width = int(image.width * ratio)
return image.resize((width, height))
def resize_logo(image, width, height):
# Get the aspect ratio of the image
aspect_ratio = image.width / image.height
# Calculate new width and height to maintain aspect ratio
new_width = width
new_height = int(new_width / aspect_ratio)
# If the calculated height is greater than the desired height,
# recalculate the width to fit the desired height
if new_height > height:
new_height = height
new_width = int(new_height * aspect_ratio)
# Resize the image
resized_img = image.resize((new_width, new_height))
return resized_img
def clean_filename(filename):
# Remove problematic characters from the filename
cleaned_filename = "".join(c if c.isalnum() or c in "._-" else "_" for c in filename)
return cleaned_filename
# Fetch movie or TV show logo in English
def get_logo(media_type, media_id, language):
# Prepare language code (fr-FR -> fr)
lang_code = language.split("-")[0]
# Build TMDB API URL
url = f"{TMDB_BASE_URL}/{media_type}/{media_id}/images?language={LANGUAGE}"
response = requests.get(url, headers=headers)
if response.status_code != 200:
return None
logos = response.json().get("logos", [])
# If no logos at all, try English fallback
if not logos:
url_en = f"{TMDB_BASE_URL}/{media_type}/{media_id}/images?language=en"
response_en = requests.get(url_en, headers=headers)
if response_en.status_code == 200:
logos_en = response_en.json().get("logos", [])
if logos_en:
return sorted(logos_en, key=lambda x: x.get("vote_average", 0), reverse=True)[0]["file_path"]
return None
# 1. Exact match for requested language
lang_match = [l for l in logos if l.get("iso_639_1") == LANGUAGE]
if lang_match:
# Pick highest rated
return sorted(lang_match, key=lambda x: x.get("vote_average", 0), reverse=True)[0]["file_path"]
# 4. Last fallback: best-rated logo overall
return sorted(logos, key=lambda x: x.get("vote_average", 0), reverse=True)[0]["file_path"]
def process_image(image_url, title, is_movie, genre, year, rating, duration=None, seasons=None):
# Download the background image with a timeout of 10 seconds
response = requests.get(image_url, timeout=10)
if response.status_code == 200:
# Open the image
image = Image.open(BytesIO(response.content))
# Resize the image to have a width of 1500 pixels while preserving aspect ratio
image = resize_image(image, 1500)
# Open overlay images
bckg = Image.open(os.path.join(os.path.dirname(__file__), "bckg.png"))
overlay = Image.open(os.path.join(os.path.dirname(__file__), "overlay.png"))
tmdblogo = Image.open(os.path.join(os.path.dirname(__file__), "tmdblogo.png"))
# Paste images
bckg.paste(image, (1175, 0))
bckg.paste(overlay, (1175, 0), overlay)
bckg.paste(tmdblogo, (680, 890), tmdblogo)
# Add title text with shadow
draw = ImageDraw.Draw(bckg)
# Text font
font_title = ImageFont.truetype(truetype_path, size=190)
font_overview = ImageFont.truetype(truetype_path, size=50)
font_custom = ImageFont.truetype(truetype_path, size=60)
# Text color
shadow_color = "black"
main_color = "white"
overview_color = (150, 150, 150) # Grey color for the summary
metadata_color = "white"
# Text position
title_position = (200, 420)
overview_position = (210, 730)
shadow_offset = 2
info_position = (210, 650) # Adjusted position for logo and info
custom_position = (210, 870)
# Wrap overview text
wrapped_overview = "\n".join(textwrap.wrap(overview, width=70, max_lines=2, placeholder=" ..."))
# Draw Overview for info
draw.text((overview_position[0] + shadow_offset, overview_position[1] + shadow_offset), wrapped_overview, font=font_overview, fill=shadow_color)
draw.text(overview_position, wrapped_overview, font=font_overview, fill=metadata_color)
# Determine genre text and additional info
if is_movie:
genre_text = genre
additional_info = f"{duration}"
else:
genre_text = genre
additional_info = f"{seasons} {'Season' if seasons == 1 else 'Seasons'}"
rating_text = "TMDB: " + str(rating)
year_text = truncate(str(year), 7)
info_text = f"{genre_text} \u2022 {year_text} \u2022 {additional_info} \u2022 {rating_text}"
# Draw metadata
draw.text((info_position[0] + shadow_offset, info_position[1] + shadow_offset), info_text, font=font_overview, fill=shadow_color)
draw.text(info_position, info_text, font=font_overview, fill=overview_color)
# Get logo image URL
if is_movie:
logo_path = get_logo("movie", movie['id'], language="en")
else:
logo_path = get_logo("tv", tvshow['id'], language="en")
logo_drawn = False # Flag to track if logo is drawn
if logo_path:
logo_url = f"https://image.tmdb.org/t/p/original{logo_path}"
logo_response = requests.get(logo_url)
if logo_response.status_code == 200:
try:
logo_image = Image.open(BytesIO(logo_response.content))
# Resize the logo image to fit within a box while maintaining aspect ratio
logo_image = resize_logo(logo_image, 1000, 500)
logo_position = (210, info_position[1] - logo_image.height - 25) # Position for logo
logo_image = logo_image.convert('RGBA')
# Paste the logo onto the image
bckg.paste(logo_image, logo_position, logo_image)
logo_drawn = True # Logo was successfully drawn
except Exception as e:
print(f"Failed to draw logo for {title}: {e}")
if not logo_drawn:
# Draw title text if logo is not available or failed to draw
draw.text((title_position[0] + shadow_offset, title_position[1] + shadow_offset), title, font=font_title, fill=shadow_color)
draw.text(title_position, title, font=font_title, fill=main_color)
# Draw custom text
draw.text((custom_position[0] + shadow_offset, custom_position[1] + shadow_offset), custom_text, font=font_custom, fill=shadow_color)
draw.text(custom_position, custom_text, font=font_custom, fill=metadata_color)
# Save the resized image
filename = os.path.join(background_dir, f"{clean_filename(title)}.jpg")
bckg = bckg.convert('RGB')
bckg.save(filename)
print(f"Image saved: {filename}")
else:
print(f"Failed to download background for {title}")
# Process each trending movie
for movie in trending_movies.get('results', []):
if should_exclude_movie(movie):
print(f"Excluded Movie: {movie['title']} ({movie.get('origin_country')})")
continue
# Extract movie details
title = movie['title']
overview = movie['overview']
year = movie['release_date']
rating = round(movie['vote_average'], 1)
genre = ', '.join([movie_genres[genre_id] for genre_id in movie['genre_ids']])
# Fetch additional movie details
movie_details = get_movie_details(movie['id'])
duration = movie_details.get('runtime', 0)
# Format duration as hours and minutes
if duration:
hours = duration // 60
minutes = duration % 60
duration = f"{hours}h{minutes}min"
else:
duration = "N/A"
# Check if backdrop image is available
backdrop_path = movie['backdrop_path']
custom_text = "Now Trending on"
if backdrop_path:
# Construct image URL
image_url = f"https://image.tmdb.org/t/p/original{backdrop_path}"
# Process the image
process_image(image_url, title, is_movie=True, genre=genre, year=year, rating=rating, duration=duration)
else:
# Print error message if no backdrop image found
print(f"No backdrop image found for {title}")
# Process trending TV shows
for tvshow in trending_tvshows.get('results', []):
if should_exclude_tvshow(tvshow):
print(f"Excluded TV Show: {tvshow['name']} ({tvshow.get('origin_country')})")
continue
# Extract TV show details
title = truncate_overview(tvshow['name'], 38)
overview = tvshow['overview']
year = tvshow['first_air_date']
rating = round(tvshow['vote_average'], 1)
genre = ', '.join([tv_genres[genre_id] for genre_id in tvshow['genre_ids']])
# Fetch additional TV show details
tv_details = get_tv_show_details(tvshow['id'])
seasons = tv_details.get('number_of_seasons', 0)
# Check if backdrop image is available
backdrop_path = tvshow['backdrop_path']
custom_text = "Now Trending on"
if backdrop_path:
# Construct image URL
image_url = f"https://image.tmdb.org/t/p/original{backdrop_path}"
# Process the image
process_image(image_url, title, is_movie=False, genre=genre, year=year, rating=rating, seasons=seasons)
else:
# Print error message if no backdrop image found
print(f"No backdrop image found for {title}")