Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
return "https://image.tmdb.org/t/p/w500/" + poster_path
|
| 98 |
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
-
|
| 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
|