|
1 | | -import aiohttp |
2 | | - |
3 | | -TMDB_API_KEY = '1f7708bb9a218ab891a5d438b1b63992' |
4 | | -TMDB_SEARCH_URL = 'https://api.themoviedb.org/3/search/{media_type}?api_key={api_key}&query={query}' |
5 | | -TMDB_DETAILS_URL = 'https://api.themoviedb.org/3/{media_type}/{tmdb_id}?api_key={api_key}&append_to_response=videos' |
6 | | - |
7 | | -async def get_tmdb_trailer_url(hass, title, media_type): |
8 | | - if media_type == 'show': |
9 | | - media_type = 'tv' |
10 | | - elif media_type == 'movie': |
11 | | - media_type = 'movie' |
12 | | - else: |
13 | | - return None |
14 | | - |
15 | | - async with aiohttp.ClientSession() as session: |
16 | | - # Search for the movie or TV show |
17 | | - search_url = TMDB_SEARCH_URL.format(media_type=media_type, api_key=TMDB_API_KEY, query=title) |
18 | | - async with session.get(search_url) as response: |
19 | | - search_data = await response.json() |
20 | | - if not search_data.get('results'): |
21 | | - return None |
22 | | - |
23 | | - tmdb_id = search_data['results'][0]['id'] |
24 | | - |
25 | | - # Get details including videos |
26 | | - details_url = TMDB_DETAILS_URL.format(media_type=media_type, tmdb_id=tmdb_id, api_key=TMDB_API_KEY) |
27 | | - async with session.get(details_url) as response: |
28 | | - details_data = await response.json() |
29 | | - |
30 | | - videos = details_data.get('videos', {}).get('results', []) |
31 | | - for video in videos: |
32 | | - if video['type'] == 'Trailer' and video['site'] == 'YouTube': |
33 | | - return f'https://www.youtube.com/watch?v={video["key"]}' |
34 | | - |
35 | | - return None |
| 1 | +import aiohttp |
| 2 | + |
| 3 | +TMDB_API_KEY = '1f7708bb9a218ab891a5d438b1b63992' |
| 4 | +TMDB_SEARCH_URL = 'https://api.themoviedb.org/3/search/{media_type}?api_key={api_key}&query={query}' |
| 5 | +TMDB_DETAILS_URL = 'https://api.themoviedb.org/3/{media_type}/{tmdb_id}?api_key={api_key}&append_to_response=videos' |
| 6 | + |
| 7 | +EMPTY_TMDB_RESULT = {'trailer': None, 'tmdb_rating': None, 'tmdb_genres': None} |
| 8 | + |
| 9 | +async def get_tmdb_trailer_url(hass, title, media_type): |
| 10 | + if media_type == 'show': |
| 11 | + media_type = 'tv' |
| 12 | + elif media_type == 'movie': |
| 13 | + media_type = 'movie' |
| 14 | + else: |
| 15 | + return EMPTY_TMDB_RESULT.copy() |
| 16 | + |
| 17 | + async with aiohttp.ClientSession() as session: |
| 18 | + # Search for the movie or TV show |
| 19 | + search_url = TMDB_SEARCH_URL.format(media_type=media_type, api_key=TMDB_API_KEY, query=title) |
| 20 | + async with session.get(search_url) as response: |
| 21 | + search_data = await response.json() |
| 22 | + if not search_data.get('results'): |
| 23 | + return EMPTY_TMDB_RESULT.copy() |
| 24 | + |
| 25 | + tmdb_id = search_data['results'][0]['id'] |
| 26 | + |
| 27 | + # Get details including videos |
| 28 | + details_url = TMDB_DETAILS_URL.format(media_type=media_type, tmdb_id=tmdb_id, api_key=TMDB_API_KEY) |
| 29 | + async with session.get(details_url) as response: |
| 30 | + details_data = await response.json() |
| 31 | + |
| 32 | + tmdb_rating = details_data.get('vote_average', 0) |
| 33 | + tmdb_genres = [g['name'] for g in details_data.get('genres', [])] |
| 34 | + |
| 35 | + trailer_url = None |
| 36 | + videos = details_data.get('videos', {}).get('results', []) |
| 37 | + for video in videos: |
| 38 | + if video['type'] == 'Trailer' and video['site'] == 'YouTube': |
| 39 | + trailer_url = f'https://www.youtube.com/watch?v={video["key"]}' |
| 40 | + break |
| 41 | + |
| 42 | + return {'trailer': trailer_url, 'tmdb_rating': tmdb_rating, 'tmdb_genres': tmdb_genres} |
| 43 | + |
| 44 | + return EMPTY_TMDB_RESULT.copy() |
0 commit comments