asif-coder commited on
Commit
c2d1c97
·
verified ·
1 Parent(s): 5f20e13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -71,7 +71,6 @@ similarity_matrix = cosine_similarity(embeddings)
71
  # Fetch movie poster
72
  # ---------------------------
73
 
74
- import os
75
 
76
  def fetch_poster(movie_title):
77
 
@@ -80,27 +79,31 @@ def fetch_poster(movie_title):
80
  if not api_key:
81
  return None
82
 
83
- movie_title = movie_title.replace(" ", "%20")
84
-
85
  url = f"https://api.themoviedb.org/3/search/movie?api_key={api_key}&query={movie_title}"
86
 
87
  try:
88
- response = requests.get(url)
 
 
 
 
89
  data = response.json()
90
 
91
- # Safe check
92
- if "results" in data and len(data["results"]) > 0:
93
 
94
- poster_path = data["results"][0].get("poster_path")
 
95
 
96
- if poster_path:
97
- return "https://image.tmdb.org/t/p/w500/" + poster_path
98
 
99
- return None
 
100
 
101
- except:
102
- return None
103
 
 
 
104
 
105
  # ---------------------------
106
  # Recommendation function
 
71
  # Fetch movie poster
72
  # ---------------------------
73
 
 
74
 
75
  def fetch_poster(movie_title):
76
 
 
79
  if not api_key:
80
  return None
81
 
 
 
82
  url = f"https://api.themoviedb.org/3/search/movie?api_key={api_key}&query={movie_title}"
83
 
84
  try:
85
+ response = requests.get(url, timeout=5)
86
+
87
+ if response.status_code != 200:
88
+ return None
89
+
90
  data = response.json()
91
 
92
+ # Safe validation
93
+ results = data.get("results", [])
94
 
95
+ if len(results) == 0:
96
+ return None
97
 
98
+ poster_path = results[0].get("poster_path")
 
99
 
100
+ if not poster_path:
101
+ return None
102
 
103
+ return f"https://image.tmdb.org/t/p/w500/{poster_path}"
 
104
 
105
+ except Exception as e:
106
+ return None
107
 
108
  # ---------------------------
109
  # Recommendation function