-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie_recommendations.py
93 lines (74 loc) · 2.73 KB
/
movie_recommendations.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
import requests_with_caching
import json
def get_tastedive_results(name):
"""
Fetches TasteDive results for a given movie or title.
Parameters:
- name (str): The name of the movie or title.
Returns:
dict: A dictionary containing TasteDive results.
"""
parameters = {"q": name, "type": "movies", "limit": 5}
tastedive_response = requests_with_caching.get("https://tastedive.com/api/similar", params=parameters)
return json.loads(tastedive_response.text)
def extract_movie_titles(tastedive_data):
"""
Extracts movie titles from TasteDive data.
Parameters:
- tastedive_data (dict): TasteDive data dictionary.
Returns:
list: A list of movie titles.
"""
return [movie["Name"] for movie in tastedive_data.get("Similar", {}).get("Results", [])]
def get_related_titles(movie_titles):
"""
Retrieves related titles for a list of movie titles.
Parameters:
- movie_titles (list): A list of movie titles.
Returns:
list: A list of related movie titles.
"""
related_titles = set()
for title in movie_titles:
tastedive_data = get_tastedive_results(title)
related_titles.update(extract_movie_titles(tastedive_data))
return list(related_titles)
def get_movie_data(movie_name):
"""
Fetches movie data from the OMDB API.
Parameters:
- movie_name (str): The name of the movie.
Returns:
dict: A dictionary containing movie data.
"""
parameters = {'t': movie_name, 'r': 'json'}
omdbapi_response = requests_with_caching.get('http://www.omdbapi.com/', params=parameters)
return json.loads(omdbapi_response.text)
def get_rotten_tomatoes_rating(movie_dict):
"""
Retrieves the Rotten Tomatoes rating from movie data.
Parameters:
- movie_dict (dict): A dictionary containing movie data.
Returns:
int: The Rotten Tomatoes rating (percentage).
"""
ratings = movie_dict.get('Ratings', [])
for rating in ratings:
if rating['Source'] == 'Rotten Tomatoes':
return int(rating['Value'][:2])
return 0
def get_sorted_recommendations(movie_list):
"""
Generates sorted movie recommendations based on Rotten Tomatoes ratings.
Parameters:
- movie_list (list): A list of movie titles.
Returns:
list: A list of recommended movie titles sorted by Rotten Tomatoes ratings.
"""
related_titles = get_related_titles(movie_list)
ratings = [get_rotten_tomatoes_rating(get_movie_data(movie)) for movie in related_titles]
sorted_movies = [title for _, title in sorted(zip(ratings, related_titles), key=lambda x: x[0], reverse=True)]
return sorted_movies
# Example usage:
movies = get_sorted_recommendations(["Bridesmaids", "Sherlock Holmes"])
print(movies)