tobiasaurer commited on
Commit
144bcd1
1 Parent(s): 73cb8e3

adds errorhandling

Browse files
pages/1 - Popularity-Based Recommender.py CHANGED
@@ -66,9 +66,10 @@ def get_popular_recommendations_streaming(n, genres, time_range, country, url, h
66
  # create new column for streaming links
67
  recommendations_ids['Streaming Availability'] = ""
68
 
69
- # loop through imdb_ids to make one api call for each to get available streaming links
70
- for id in imdb_ids:
71
 
 
72
  # make api call
73
  try:
74
  querystring = {"country":country,"imdb_id":id,"output_language":"en"}
@@ -77,12 +78,21 @@ def get_popular_recommendations_streaming(n, genres, time_range, country, url, h
77
 
78
  for streaming_service in streaming_info['streamingInfo']:
79
  recommendations_ids.loc[recommendations_ids['imdbId'] == id, 'Streaming Availability'] += f"{streaming_service}: {streaming_info['streamingInfo'][streaming_service][country]['link']} \n"
 
 
 
80
  except:
81
  continue
82
 
83
  recommendations_ids.rename(columns= {'title': 'Movie Title', 'genres': 'Genres'}, inplace = True)
84
 
85
- return recommendations_ids[['Movie Title', 'Genres', 'Streaming Availability']]
 
 
 
 
 
 
86
 
87
  def transform_genre_to_regex(genres):
88
  regex = ""
@@ -133,7 +143,7 @@ if st.button("Get Recommendations"):
133
  else:
134
  try:
135
  recommendations = get_popular_recommendations_streaming(number_of_recommendations, genres_regex, time_range, streaming_country, url, headers)
136
- st.write("Double-click on a Streaming-Availability cell to see all options.", recommendations)
137
  except:
138
  recommendations = get_popular_recommendations(number_of_recommendations, genres_regex, time_range)
139
  st.write('Error: Streaming information could not be gathered. Providing output without streaming availability instead.', recommendations)
 
66
  # create new column for streaming links
67
  recommendations_ids['Streaming Availability'] = ""
68
 
69
+ # track successful calls to provide errormessage if all calls fail
70
+ successful_calls = 0
71
 
72
+ for id in imdb_ids:
73
  # make api call
74
  try:
75
  querystring = {"country":country,"imdb_id":id,"output_language":"en"}
 
78
 
79
  for streaming_service in streaming_info['streamingInfo']:
80
  recommendations_ids.loc[recommendations_ids['imdbId'] == id, 'Streaming Availability'] += f"{streaming_service}: {streaming_info['streamingInfo'][streaming_service][country]['link']} \n"
81
+
82
+ successful_calls += 1
83
+
84
  except:
85
  continue
86
 
87
  recommendations_ids.rename(columns= {'title': 'Movie Title', 'genres': 'Genres'}, inplace = True)
88
 
89
+ if successful_calls == 0:
90
+ st.write("Error: Streaming information could not be gathered. Providing output without streaming availability instead.")
91
+ return recommendations_ids[['Movie Title', 'Genres']]
92
+
93
+ else:
94
+ st.write("Double-click on a Streaming-Availability cell to see all options.")
95
+ return recommendations_ids[['Movie Title', 'Genres', 'Streaming Availability']]
96
 
97
  def transform_genre_to_regex(genres):
98
  regex = ""
 
143
  else:
144
  try:
145
  recommendations = get_popular_recommendations_streaming(number_of_recommendations, genres_regex, time_range, streaming_country, url, headers)
146
+ st.write(recommendations)
147
  except:
148
  recommendations = get_popular_recommendations(number_of_recommendations, genres_regex, time_range)
149
  st.write('Error: Streaming information could not be gathered. Providing output without streaming availability instead.', recommendations)
pages/2 - Movie-Based Recommender.py CHANGED
@@ -84,6 +84,7 @@ def get_similar_recommendations_streaming(movie_title, n, genres, time_range, co
84
  recommendations_ids['Streaming Availability'] = ""
85
 
86
  # loop through imdb_ids to make one api call for each to get available streaming links
 
87
  for id in imdb_ids:
88
 
89
  # make api call
@@ -94,12 +95,20 @@ def get_similar_recommendations_streaming(movie_title, n, genres, time_range, co
94
 
95
  for streaming_service in streaming_info['streamingInfo']:
96
  recommendations_ids.loc[recommendations_ids['imdbId'] == id, 'Streaming Availability'] += f"{streaming_service}: {streaming_info['streamingInfo'][streaming_service][country]['link']} \n"
 
 
97
  except:
98
  continue
99
-
100
  recommendations_ids.rename(columns= {'title': 'Movie Title', 'genres': 'Genres'}, inplace = True)
101
 
102
- return recommendations_ids[['Movie Title', 'Genres', 'Streaming Availability']]
 
 
 
 
 
 
103
 
104
 
105
  def transform_genre_to_regex(genres):
@@ -173,7 +182,7 @@ if st.button("Get Recommendations"):
173
  else:
174
  try:
175
  recommendations = get_similar_recommendations_streaming(movie_title, number_of_recommendations, genres_regex, time_range, streaming_country, url, headers)
176
- st.write("Double-click on a Streaming-Availability cell to see all options.", recommendations)
177
  except:
178
  recommendations = get_similar_recommendations(movie_title, number_of_recommendations, genres_regex, time_range)
179
  st.write('Error: Streaming information could not be gathered. Providing output without streaming availability instead.', recommendations)
 
84
  recommendations_ids['Streaming Availability'] = ""
85
 
86
  # loop through imdb_ids to make one api call for each to get available streaming links
87
+ successful_calls = 0
88
  for id in imdb_ids:
89
 
90
  # make api call
 
95
 
96
  for streaming_service in streaming_info['streamingInfo']:
97
  recommendations_ids.loc[recommendations_ids['imdbId'] == id, 'Streaming Availability'] += f"{streaming_service}: {streaming_info['streamingInfo'][streaming_service][country]['link']} \n"
98
+
99
+ successful_calls += 1
100
  except:
101
  continue
102
+
103
  recommendations_ids.rename(columns= {'title': 'Movie Title', 'genres': 'Genres'}, inplace = True)
104
 
105
+ if successful_calls == 0:
106
+ st.write("Error: Streaming information could not be gathered. Providing output without streaming availability instead.")
107
+ return recommendations_ids[['Movie Title', 'Genres']]
108
+
109
+ else:
110
+ st.write("Double-click on a Streaming-Availability cell to see all options.")
111
+ return recommendations_ids[['Movie Title', 'Genres', 'Streaming Availability']]
112
 
113
 
114
  def transform_genre_to_regex(genres):
 
182
  else:
183
  try:
184
  recommendations = get_similar_recommendations_streaming(movie_title, number_of_recommendations, genres_regex, time_range, streaming_country, url, headers)
185
+ st.write(recommendations)
186
  except:
187
  recommendations = get_similar_recommendations(movie_title, number_of_recommendations, genres_regex, time_range)
188
  st.write('Error: Streaming information could not be gathered. Providing output without streaming availability instead.', recommendations)