-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecommender.py
More file actions
32 lines (24 loc) · 861 Bytes
/
Copy pathrecommender.py
File metadata and controls
32 lines (24 loc) · 861 Bytes
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
import pickle
import requests
movies = pickle.load(open('movies.pkl', 'rb'))
similarity = pickle.load(open('similarity.pkl', 'rb'))
API_KEY = "YOUR-API-KEY"
def fetch_poster(movie_id):
url = f"https://api.themoviedb.org/3/movie/{movie_id}?api_key={API_KEY}"
data = requests.get(url).json()
return "https://image.tmdb.org/t/p/w500/" + data['poster_path']
def recommend(movie):
movie_index = movies[movies['title'].str.lower() == movie.lower()].index[0]
distances = similarity[movie_index]
movies_list = sorted(
list(enumerate(distances)),
reverse=True,
key=lambda x: x[1]
)[1:6]
names = []
posters = []
for i in movies_list:
movie_id = movies.iloc[i[0]].movie_id
names.append(movies.iloc[i[0]].title)
posters.append(fetch_poster(movie_id))
return names, posters