-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
171 lines (140 loc) · 5.78 KB
/
main.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
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
import numpy as np
import pandas as pd
from flask import Flask, render_template, request
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import json
import bs4 as bs
import urllib.request
import pickle
import requests
from dotenv import load_dotenv
load_dotenv()
import os
from tmdbv3api import TMDb
tmdb = TMDb()
tmdb.api_key = os.getenv('TMDB_API')
from tmdbv3api import Movie
# load the nlp model and tfidf vectorizer from disk
filename = 'nlp_model.pkl'
clf = pickle.load(open(filename, 'rb'))
vectorizer = pickle.load(open('tranform.pkl','rb'))
def create_sim():
data = pd.read_csv('data/main_data.csv')
# creating a count matrix
cv = CountVectorizer()
count_matrix = cv.fit_transform(data['comb'])
# creating a similarity score matrix
sim = cosine_similarity(count_matrix)
return data,sim
def rcmd(m):
m = m.lower()
try:
data.head()
sim.shape
except:
data, sim = create_sim()
if m not in data['movie_title'].unique():
return('Sorry! The movie your searched is not in our database. Please check the spelling or try with some other movies')
else:
i = data.loc[data['movie_title']==m].index[0]
lst = list(enumerate(sim[i]))
lst = sorted(lst, key = lambda x:x[1] ,reverse=True)
lst = lst[1:11]
l = []
for i in range(len(lst)):
a = lst[i][0]
l.append(data['movie_title'][a])
return l
def ListOfGenres(genre_json):
if genre_json:
genres = []
genre_str = ", "
for i in range(0,len(genre_json)):
genres.append(genre_json[i]['name'])
return genre_str.join(genres)
def date_convert(s):
MONTHS = ['January', 'February', 'Match', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December']
y = s[:4]
m = int(s[5:-3])
d = s[8:]
month_name = MONTHS[m-1]
result= month_name + ' ' + d + ' ' + y
return result
def MinsToHours(duration):
if duration%60==0:
return "{:.0f} hours".format(duration/60)
else:
return "{:.0f} hours {} minutes".format(duration/60,duration%60)
def get_suggestions():
data = pd.read_csv('data/main_data.csv')
return list(data['movie_title'].str.capitalize())
app = Flask(__name__)
@app.route("/")
def home():
suggestions = get_suggestions()
return render_template('home.html',suggestions=suggestions)
@app.route("/recommend")
def recommend():
suggestions = get_suggestions()
movie = request.args.get('movie') # get movie name from the URL
r = rcmd(movie)
movie = movie.upper()
if type(r)==type('string'): # no such movie found in the database
return render_template('recommend.html',movie=movie,r=r,t='s')
else:
tmdb_movie = Movie()
result = tmdb_movie.search(movie)
# get movie id and movie title
movie_id = result[0].id
movie_name = result[0].title
# making API call
response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id,tmdb.api_key))
data_json = response.json()
imdb_id = data_json['imdb_id']
poster = data_json['poster_path']
img_path = 'https://image.tmdb.org/t/p/original{}'.format(poster)
# getting list of genres form json
genre = ListOfGenres(data_json['genres'])
# web scraping to get user reviews from IMDB site
sauce = urllib.request.urlopen('https://www.imdb.com/title/{}/reviews?ref_=tt_ov_rt'.format(imdb_id)).read()
soup = bs.BeautifulSoup(sauce,'html.parser')
soup_result = soup.find_all("div",{"class":"text show-more__control"})
reviews_list = [] # list of reviews
reviews_status = [] # list of comments (good or bad)
for reviews in soup_result:
if reviews.string:
reviews_list.append(reviews.string)
# passing the review to our model
movie_review_list = np.array([reviews.string])
movie_vector = vectorizer.transform(movie_review_list)
pred = clf.predict(movie_vector)
reviews_status.append('Good' if pred else 'Bad')
# combining reviews and comments into dictionary
movie_reviews = {reviews_list[i]: reviews_status[i] for i in range(len(reviews_list))}
# getting votes with comma as thousands separators
vote_count = "{:,}".format(result[0].vote_count)
# convert date to readable format (eg. 10-06-2019 to June 10 2019)
rd = date_convert(result[0].release_date)
# getting the status of the movie (released or not)
status = data_json['status']
# convert minutes to hours minutes (eg. 148 minutes to 2 hours 28 mins)
runtime = MinsToHours(data_json['runtime'])
# getting the posters for the recommended movies
poster = []
movie_title_list = []
for movie_title in r:
list_result = tmdb_movie.search(movie_title)
movie_id = list_result[0].id
response = requests.get('https://api.themoviedb.org/3/movie/{}?api_key={}'.format(movie_id,tmdb.api_key))
data_json = response.json()
poster.append('https://image.tmdb.org/t/p/original{}'.format(data_json['poster_path']))
movie_cards = {poster[i]: r[i] for i in range(len(r))}
# get movie names for auto completion
suggestions = get_suggestions()
return render_template('recommend.html',movie=movie,mtitle=r,t='l',cards=movie_cards,
result=result[0],reviews=movie_reviews,img_path=img_path,genres=genre,vote_count=vote_count,
release_date=rd,status=status,runtime=runtime,suggestions=suggestions)
if __name__ == '__main__':
app.run(debug=True)