Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,31 +18,39 @@ def get_recommendations(title, cosine_sim=cosine_sim):
|
|
| 18 |
|
| 19 |
# Fetch movie poster and additional details using append_to_response
|
| 20 |
def fetch_movie_data(movie_id):
|
| 21 |
-
api_key = "5c1c27e14a6a61a873db79d82528056f"
|
| 22 |
url = f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}&append_to_response=videos,images'
|
| 23 |
|
| 24 |
try:
|
| 25 |
response = requests.get(url)
|
| 26 |
-
response.raise_for_status()
|
| 27 |
data = response.json()
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
poster_path = data.get('poster_path'
|
| 31 |
poster_url = f"https://image.tmdb.org/t/p/w500{poster_path}" if poster_path else \
|
| 32 |
"https://via.placeholder.com/500x750?text=No+Image+Available"
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return {
|
| 39 |
'poster': poster_url,
|
| 40 |
-
'trailer_key': trailer
|
| 41 |
-
'backdrop':
|
| 42 |
-
'overview':
|
| 43 |
}
|
| 44 |
|
| 45 |
-
except requests.exceptions.RequestException:
|
|
|
|
| 46 |
return {
|
| 47 |
'poster': "https://via.placeholder.com/500x750?text=No+Image+Available",
|
| 48 |
'trailer_key': None,
|
|
|
|
| 18 |
|
| 19 |
# Fetch movie poster and additional details using append_to_response
|
| 20 |
def fetch_movie_data(movie_id):
|
| 21 |
+
api_key = "5c1c27e14a6a61a873db79d82528056f"
|
| 22 |
url = f'https://api.themoviedb.org/3/movie/{movie_id}?api_key={api_key}&append_to_response=videos,images'
|
| 23 |
|
| 24 |
try:
|
| 25 |
response = requests.get(url)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
data = response.json()
|
| 28 |
+
|
| 29 |
+
# Handle poster path
|
| 30 |
+
poster_path = data.get('poster_path')
|
| 31 |
poster_url = f"https://image.tmdb.org/t/p/w500{poster_path}" if poster_path else \
|
| 32 |
"https://via.placeholder.com/500x750?text=No+Image+Available"
|
| 33 |
+
|
| 34 |
+
# Handle trailer safely
|
| 35 |
+
videos = data.get('videos', {}).get('results', [])
|
| 36 |
+
trailer = next((v for v in videos if v.get('type') == 'Trailer'), None)
|
| 37 |
+
|
| 38 |
+
# Handle backdrop safely
|
| 39 |
+
backdrops = data.get('images', {}).get('backdrops', [])
|
| 40 |
+
backdrop_path = backdrops[0].get('file_path') if backdrops else ''
|
| 41 |
+
|
| 42 |
+
# Handle overview
|
| 43 |
+
overview = data.get('overview', 'No description available')[:100] + "..."
|
| 44 |
+
|
| 45 |
return {
|
| 46 |
'poster': poster_url,
|
| 47 |
+
'trailer_key': trailer.get('key') if trailer else None,
|
| 48 |
+
'backdrop': backdrop_path,
|
| 49 |
+
'overview': overview
|
| 50 |
}
|
| 51 |
|
| 52 |
+
except requests.exceptions.RequestException as e:
|
| 53 |
+
st.error(f"API Error: {str(e)}")
|
| 54 |
return {
|
| 55 |
'poster': "https://via.placeholder.com/500x750?text=No+Image+Available",
|
| 56 |
'trailer_key': None,
|