forked from Mujju-palaan/Python3Programming--Coursera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse_3_project.py
More file actions
179 lines (131 loc) · 6.7 KB
/
course_3_project.py
File metadata and controls
179 lines (131 loc) · 6.7 KB
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
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"])