Abs6187 commited on
Commit
ce69630
·
verified ·
1 Parent(s): 8c68bab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
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" # Updated API key
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() # Raise exception for 4xx/5xx errors
27
  data = response.json()
28
-
29
- # Get poster path with fallback
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
- # Extract additional information
35
- trailer = next((v for v in data.get('videos', {}).get('results', [])
36
- if v['type'] == 'Trailer'), None)
37
-
 
 
 
 
 
 
 
38
  return {
39
  'poster': poster_url,
40
- 'trailer_key': trailer['key'] if trailer else None,
41
- 'backdrop': data.get('images', {}).get('backdrops', [{}])[0].get('file_path', ''),
42
- 'overview': data.get('overview', 'No description available')
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,