-
-
Save cibofdevs/4ca80f401792e7728e71565b4b3a4fdf to your computer and use it in GitHub Desktop.
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"]) |
Thanks!
thanks
it show an errror
thanks
Please anyone can help me with the last question!
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’.
it shown an error below
Error
TimeLimitError: Program exceeded run time limit. on line 60
Description
Your program is running too long. Most programs in this book should run in less than 10 seconds easily. This probably indicates your program is in an infinite loop.
To Fix
Add some print statements to figure out if your program is in an infinte loop. If it is not you can increase the run time with sys.setExecutionLimit(msecs)
This is another error shown by last question codes
Error
An error occurred after the end of your code.
One possible reason is that you have an unclosed parenthesis or string.
Another possibility is that there is an error in the hidden test code.
Yet another is that there is an internal error. The internal error message is: undefined
Please anyone can help me with the last question!!!!
Error
TimeLimitError: Program exceeded run time limit. on line undefined
Description
Your program is running too long. Most programs in this book should run in less than 10 seconds easily. This probably indicates your program is in an infinite loop.
To Fix
Add some print statements to figure out if your program is in an infinte loop. If it is not you can increase the run time with sys.setExecutionLimit(msecs)
Please anyone can help me with the last question! 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’.
it shown an error below Error TimeLimitError: Program exceeded run time limit. on line 60 Description Your program is running too long. Most programs in this book should run in less than 10 seconds easily. This probably indicates your program is in an infinite loop.
To Fix Add some print statements to figure out if your program is in an infinte loop. If it is not you can increase the run time with sys.setExecutionLimit(msecs)
hi did you solve the last question? did you find the problem?
its good