-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcourse3-assess-3.py
63 lines (52 loc) · 1.94 KB
/
course3-assess-3.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
#Assessment: Project: OMDB and TasteDive Mashup
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"])