-
Notifications
You must be signed in to change notification settings - Fork 18
/
course_3_project.py
277 lines (169 loc) · 9.09 KB
/
course_3_project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# This project will take you through the process of mashing up data from two different APIs to make movie recommendations. The TasteDive API lets you provide a movie (or bands, TV shows, etc.) as a query input, and returns a set of related items. The OMDB API lets you provide a movie title as a query input and get back data about the movie, including scores from various review sites (Rotten Tomatoes, IMDB, etc.).
# You will put those two together. You will use TasteDive to get related movies for a whole list of titles. You’ll combine the resulting lists of related movies, and sort them according to their Rotten Tomatoes scores (which will require making API calls to the OMDB API.)
# To avoid problems with rate limits and site accessibility, we have provided a cache file with results for all the queries you need to make to both OMDB and TasteDive. Just use requests_with_caching.get() rather than requests.get(). If you’re having trouble, you may not be formatting your queries properly, or you may not be asking for data that exists in our cache. We will try to provide as much information as we can to help guide you to form queries for which data exists in the cache.
# Your first task will be to fetch data from TasteDive. The documentation for the API is at https://tastedive.com/read/api.
# Define a function, called get_movies_from_tastedive. It should take one input parameter, a string that is the name of a movie or music artist. The function should return the 5 TasteDive results that are associated with that string; be sure to only get movies, not other kinds of media. It will be a python dictionary with just one key, ‘Similar’.
# Try invoking your function with the input “Black Panther”.
# HINT: Be sure to include only q, type, and limit as parameters in order to extract data from the cache. If any other parameters are included, then the function will not be able to recognize the data that you’re attempting to pull from the cache. Remember, you will not need an api key in order to complete the project, because all data will be found in the cache.
The cache includes data for the following queries:
q
type
limit
Black Panther
<omitted>
<omitted>
Black Panther
<omitted>
5
Black Panther
movies
<omitted>
Black Panther
movies
5
Tony Bennett
<omitted>
5
Tony Bennett
movies
5
Captain Marvel
movies
5
Bridesmaids
movies
5
Sherlock Holmes
movies
5
import requests_with_caching
import json
def get_movies_from_tastedive(title):
url = 'https://tastedive.com/api/similar'
param = {}
param['q']= title
param['type']= 'movies'
param['limit']= 5
this_page_cache = requests_with_caching.get(url, params=param)
return json.loads(this_page_cache.text)
get_movies_from_tastedive('Captain Marvel')
get_movies_from_tastedive('Sherlock Holmes')
#Please copy the completed function from above into this active code window. Next, you will need to write a function that extracts just the list of movie titles from a dictionary returned by get_movies_from_tastedive. Call it extract_movie_titles.
import requests_with_caching
import json
def get_movies_from_tastedive(title):
endpoint = 'https://tastedive.com/api/similar'
param = {}
param['q'] = title
param['limit'] = 5
param['type'] = 'movies'
this_page_cache = requests_with_caching.get(endpoint, params=param)
return json.loads(this_page_cache.text)
def extract_movie_titles(dic):
return ([i['Name'] for i in dic['Similar']['Results']])
# some invocations that we use in the automated tests; uncomment these if you are getting errors and want better error messages
extract_movie_titles(get_movies_from_tastedive("Tony Bennett"))
extract_movie_titles(get_movies_from_tastedive("Black Panther"))
####Please copy the completed functions from the two code windows above into this active code window. Next, you’ll write a function, called get_related_titles. It takes a list of movie titles as input. It gets five related movies for each from TasteDive, extracts the titles for all of them, and combines them all into a single list. Don’t include the same movie twice.
import requests_with_caching
import json
def get_movies_from_tastedive(title):
endpoint = 'https://tastedive.com/api/similar'
param = {}
param['q'] = title
param['limit'] = 5
param['type'] = 'movies'
this_page_cache = requests_with_caching.get(endpoint, params=param)
return json.loads(this_page_cache.text)
def extract_movie_titles(dic):
return ([i['Name'] for i in dic['Similar']['Results']])
def get_related_titles(movie_list):
li = []
for movie in movie_list:
li.extend(extract_movie_titles(get_movies_from_tastedive(movie)))
return list(set(li))
get_related_titles(["Black Panther", "Captain Marvel"])
##Your next task will be to fetch data from OMDB. The documentation for the API is at https://www.omdbapi.com/
#Define a function called get_movie_data. It takes in one parameter which is a string that should represent the title of a movie you want to search. The function should return a dictionary with information about that movie.
#Again, use requests_with_caching.get(). For the queries on movies that are already in the cache, you won’t need an api key. You will need to provide the following keys: t and r. As with the TasteDive cache, be sure to only include those two parameters in order to extract existing data from the cache.
import requests_with_caching
import json
def get_movie_data(title):
endpoint = 'http://www.omdbapi.com/'
param = {}
param['t'] = title
param['r'] = 'json'
this_page_cache = requests_with_caching.get(endpoint, params=param)
return json.loads(this_page_cache.text)
get_movie_data("Venom")
get_movie_data("Baby Mama")
##Please copy the completed function from above into this active code window. Now write a function called get_movie_rating. It takes an OMDB dictionary result for one movie and extracts the Rotten Tomatoes rating as an integer. For example, if given the OMDB dictionary for “Black Panther”, it would return 97. If there is no Rotten Tomatoes rating, return 0.
import requests_with_caching
import json
def get_movie_data(title):
endpoint = 'http://www.omdbapi.com/'
param = {}
param['t'] = title
param['r'] = 'json'
this_page_cache = requests_with_caching.get(endpoint, params=param)
return json.loads(this_page_cache.text)
print(get_movie_data("Black Panther")['Ratings'][1])
def get_movie_rating(dic):
ranking = dic['Ratings']
for dic_item in ranking:
if dic_item['Source'] == 'Rotten Tomatoes':
return int(dic_item['Value'][:-1])
return 0
get_movie_rating(get_movie_data("Deadpool 2"))
###Now, you’ll put it all together. Don’t forget to copy all of the functions that you have previously defined into this code window. Define a function get_sorted_recommendations. It takes a list of movie titles as an input. It returns a sorted list of related movie titles as output, up to five related movies for each input movie title. The movies should be sorted in descending order by their Rotten Tomatoes rating, as returned by the get_movie_rating function. Break ties in reverse alphabetic order, so that ‘Yahşi Batı’ comes before ‘Eyyvah Eyvah’.
import requests_with_caching
import json
def get_movies_from_tastedive(title):
endpoint = 'https://tastedive.com/api/similar'
param = {}
param['q'] = title
param['limit'] = 5
param['type'] = 'movies'
this_page_cache = requests_with_caching.get(endpoint, params=param)
return json.loads(this_page_cache.text)
def extract_movie_titles(dic):
list = []
for i in dic['Similar']['Results']:
list.append(i['Name'])
return(list)
def get_related_titles(titles_list):
list = []
for i in titles_list:
new_list = extract_movie_titles(get_movies_from_tastedive(i))
for i in new_list:
if i not in list:
list.append(i)
print(list)
return list
def get_movie_data(title):
endpoint = 'http://www.omdbapi.com/'
param = {}
param['t'] = title
param['r'] = 'json'
this_page_cache = requests_with_caching.get(endpoint, params=param)
return json.loads(this_page_cache.text)
# some invocations that we use in the automated tests; uncomment these if you are getting errors and want better error messages
# get_movie_rating(get_movie_data("Deadpool 2"))
def get_movie_rating(data):
rating = 0
for i in data['Ratings']:
if i['Source'] == 'Rotten Tomatoes':
rating = int(i['Value'][:-1])
#print(rating)
return rating
def get_sorted_recommendations(list):
new_list = get_related_titles(list)
new_dict = {}
for i in new_list:
rating = get_movie_rating(get_movie_data(i))
new_dict[i] = rating
print(new_dict)
#print(sorted(new_dict, reverse=True))
return [i[0] for i in sorted(new_dict.items(), key=lambda item: (item[1], item[0]), reverse=True)]
# some invocations that we use in the automated tests; uncomment these if you are getting errors and want better error messages
# get_sorted_recommendations(["Bridesmaids", "Sherlock Holmes"])