Alptraum commited on
Commit
d1e8586
·
verified ·
1 Parent(s): 3d56fc2

Update app.py

Browse files

Fixed repetative answer

Files changed (1) hide show
  1. app.py +33 -6
app.py CHANGED
@@ -18,6 +18,10 @@ def get_movie_recommendation(mood: str, location: str) -> str:
18
  mood: The user's current mood (e.g., 'happy', 'sad', 'excited', 'relaxed', 'nostalgic')
19
  location: The user's location to check weather (e.g., 'London', 'New York')
20
  """
 
 
 
 
21
  try:
22
  # Get weather data using wttr.in (free weather API)
23
  weather_url = f"https://wttr.in/{location}?format=%C"
@@ -51,10 +55,14 @@ def get_movie_recommendation(mood: str, location: str) -> str:
51
  selected_genres.extend(weather_genres[weather_key])
52
  break
53
 
54
- # Use OMDB API to fetch a movie recommendation
55
  omdb_api_key = "59341a1e" # Free tier API key
56
  primary_genre = selected_genres[0]
57
- movie_url = f"http://www.omdbapi.com/?apikey={omdb_api_key}&s={primary_genre}&type=movie"
 
 
 
 
58
 
59
  movie_response = requests.get(movie_url)
60
  if movie_response.status_code != 200:
@@ -64,9 +72,28 @@ def get_movie_recommendation(mood: str, location: str) -> str:
64
  if movie_data.get("Response") == "False":
65
  return f"No movies found for your current mood and weather"
66
 
67
- # Get first movie's details
68
- first_movie = movie_data["Search"][0]
69
- detail_url = f"http://www.omdbapi.com/?apikey={omdb_api_key}&i={first_movie['imdbID']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  detail_response = requests.get(detail_url)
71
  movie_details = detail_response.json()
72
 
@@ -78,7 +105,7 @@ def get_movie_recommendation(mood: str, location: str) -> str:
78
  f"Year: {movie_details['Year']}\n"
79
  f"Genre: {movie_details['Genre']}\n"
80
  f"Plot: {movie_details['Plot']}\n"
81
- f"IMDb Rating: {movie_details['imdbRating']}"
82
  )
83
 
84
  return recommendation
 
18
  mood: The user's current mood (e.g., 'happy', 'sad', 'excited', 'relaxed', 'nostalgic')
19
  location: The user's location to check weather (e.g., 'London', 'New York')
20
  """
21
+ # Using a class to maintain state of previously recommended movies
22
+ if not hasattr(get_movie_recommendation, '_previous_recommendations'):
23
+ get_movie_recommendation._previous_recommendations = set()
24
+
25
  try:
26
  # Get weather data using wttr.in (free weather API)
27
  weather_url = f"https://wttr.in/{location}?format=%C"
 
55
  selected_genres.extend(weather_genres[weather_key])
56
  break
57
 
58
+ # Use OMDB API to fetch movie recommendations
59
  omdb_api_key = "59341a1e" # Free tier API key
60
  primary_genre = selected_genres[0]
61
+
62
+ # Add page parameter for pagination (1-100)
63
+ import random
64
+ page = random.randint(1, 10) # Randomly select a page
65
+ movie_url = f"http://www.omdbapi.com/?apikey={omdb_api_key}&s={primary_genre}&type=movie&page={page}"
66
 
67
  movie_response = requests.get(movie_url)
68
  if movie_response.status_code != 200:
 
72
  if movie_data.get("Response") == "False":
73
  return f"No movies found for your current mood and weather"
74
 
75
+ # Get all movies from the search result
76
+ movies = movie_data.get("Search", [])
77
+ if not movies:
78
+ return f"No movies available for the current criteria"
79
+
80
+ # Filter out previously recommended movies
81
+ new_movies = [movie for movie in movies
82
+ if movie['imdbID'] not in get_movie_recommendation._previous_recommendations]
83
+
84
+ # If all movies on this page have been recommended, clear history and use all movies
85
+ if not new_movies:
86
+ get_movie_recommendation._previous_recommendations.clear()
87
+ new_movies = movies
88
+
89
+ # Randomly select a movie from the available ones
90
+ selected_movie = random.choice(new_movies)
91
+
92
+ # Add the selected movie to previous recommendations
93
+ get_movie_recommendation._previous_recommendations.add(selected_movie['imdbID'])
94
+
95
+ # Get detailed information for the selected movie
96
+ detail_url = f"http://www.omdbapi.com/?apikey={omdb_api_key}&i={selected_movie['imdbID']}"
97
  detail_response = requests.get(detail_url)
98
  movie_details = detail_response.json()
99
 
 
105
  f"Year: {movie_details['Year']}\n"
106
  f"Genre: {movie_details['Genre']}\n"
107
  f"Plot: {movie_details['Plot']}\n"
108
+ f"IMDb Rating: {movie_details.get('imdbRating', 'N/A')}"
109
  )
110
 
111
  return recommendation