Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import pandas as pd
|
|
| 3 |
import requests
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
# TMDB API Configuration (Environment Variables)
|
| 8 |
TMDB_API_KEY = os.environ.get("TMDB_API_KEY", "YOUR_API_KEY")
|
|
@@ -10,14 +11,36 @@ TMDB_API_TOKEN = os.environ.get("TMDB_API_TOKEN", "YOUR_API_TOKEN")
|
|
| 10 |
TMDB_BASE_URL = "https://api.themoviedb.org/3"
|
| 11 |
TMDB_IMAGE_BASE_URL = "https://image.tmdb.org/t/p/w500"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def fetch_genres() -> List[Dict[str, Any]]:
|
| 14 |
"""Fetch movie genres from TMDB"""
|
| 15 |
headers = {
|
| 16 |
"Authorization": f"Bearer {TMDB_API_TOKEN}",
|
| 17 |
"accept": "application/json"
|
| 18 |
}
|
| 19 |
-
response =
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def fetch_movies_by_genre(genre_id: str = None) -> pd.DataFrame:
|
| 23 |
"""Fetch movies from TMDB, optionally filtered by genre"""
|
|
@@ -37,9 +60,12 @@ def fetch_movies_by_genre(genre_id: str = None) -> pd.DataFrame:
|
|
| 37 |
if genre_id and genre_id != "All":
|
| 38 |
params["with_genres"] = genre_id
|
| 39 |
|
| 40 |
-
response =
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
# Convert to DataFrame
|
| 44 |
movie_data = {
|
| 45 |
"title": [],
|
|
@@ -67,12 +93,16 @@ def get_movie_trailer(movie_id: int) -> str:
|
|
| 67 |
"accept": "application/json"
|
| 68 |
}
|
| 69 |
|
| 70 |
-
response =
|
| 71 |
f"{TMDB_BASE_URL}/movie/{movie_id}/videos",
|
| 72 |
headers=headers
|
| 73 |
)
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
trailer = next(
|
| 77 |
(v for v in videos if v["type"] == "Trailer" and v["site"] == "YouTube"),
|
| 78 |
None
|
|
|
|
| 3 |
import requests
|
| 4 |
from typing import List, Dict, Any
|
| 5 |
import os
|
| 6 |
+
import time # For rate limiting
|
| 7 |
|
| 8 |
# TMDB API Configuration (Environment Variables)
|
| 9 |
TMDB_API_KEY = os.environ.get("TMDB_API_KEY", "YOUR_API_KEY")
|
|
|
|
| 11 |
TMDB_BASE_URL = "https://api.themoviedb.org/3"
|
| 12 |
TMDB_IMAGE_BASE_URL = "https://image.tmdb.org/t/p/w500"
|
| 13 |
|
| 14 |
+
def make_api_request(url, headers, params=None, max_retries=3, delay=2):
|
| 15 |
+
"""Makes a request to the TMDb API with retries and rate limiting."""
|
| 16 |
+
retries = 0
|
| 17 |
+
while retries < max_retries:
|
| 18 |
+
try:
|
| 19 |
+
response = requests.get(url, headers=headers, params=params)
|
| 20 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 21 |
+
return response
|
| 22 |
+
except requests.exceptions.RequestException as e:
|
| 23 |
+
print(f"API request failed: {e}")
|
| 24 |
+
retries += 1
|
| 25 |
+
if retries < max_retries:
|
| 26 |
+
print(f"Retrying in {delay} seconds...")
|
| 27 |
+
time.sleep(delay)
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
def fetch_genres() -> List[Dict[str, Any]]:
|
| 31 |
"""Fetch movie genres from TMDB"""
|
| 32 |
headers = {
|
| 33 |
"Authorization": f"Bearer {TMDB_API_TOKEN}",
|
| 34 |
"accept": "application/json"
|
| 35 |
}
|
| 36 |
+
response = make_api_request(f"{TMDB_BASE_URL}/genre/movie/list", headers=headers)
|
| 37 |
+
if response:
|
| 38 |
+
try:
|
| 39 |
+
return response.json()["genres"] # Handle potential errors
|
| 40 |
+
except KeyError:
|
| 41 |
+
print("Error: 'genres' key not found in the TMDb response.")
|
| 42 |
+
return []
|
| 43 |
+
return []
|
| 44 |
|
| 45 |
def fetch_movies_by_genre(genre_id: str = None) -> pd.DataFrame:
|
| 46 |
"""Fetch movies from TMDB, optionally filtered by genre"""
|
|
|
|
| 60 |
if genre_id and genre_id != "All":
|
| 61 |
params["with_genres"] = genre_id
|
| 62 |
|
| 63 |
+
response = make_api_request(f"{TMDB_BASE_URL}/discover/movie", headers=headers, params=params)
|
| 64 |
+
if response:
|
| 65 |
+
movies = response.json()["results"]
|
| 66 |
+
else:
|
| 67 |
+
movies = []
|
| 68 |
+
|
| 69 |
# Convert to DataFrame
|
| 70 |
movie_data = {
|
| 71 |
"title": [],
|
|
|
|
| 93 |
"accept": "application/json"
|
| 94 |
}
|
| 95 |
|
| 96 |
+
response = make_api_request(
|
| 97 |
f"{TMDB_BASE_URL}/movie/{movie_id}/videos",
|
| 98 |
headers=headers
|
| 99 |
)
|
| 100 |
|
| 101 |
+
if response:
|
| 102 |
+
videos = response.json().get("results", [])
|
| 103 |
+
else:
|
| 104 |
+
videos = []
|
| 105 |
+
|
| 106 |
trailer = next(
|
| 107 |
(v for v in videos if v["type"] == "Trailer" and v["site"] == "YouTube"),
|
| 108 |
None
|