-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
366 lines (296 loc) · 11.1 KB
/
app.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from utilityFunctions import *
from SqlQuery import *
from flask import Flask, redirect, render_template, request, jsonify, url_for, session
import operator
from annoy import AnnoyIndex
from flask_mail import Mail, Message
from random import randint
from flask_cors import CORS, cross_origin
app = Flask(__name__)
app.secret_key = "secret key"
CORS(app, support_credentials=True)
# Flask mail configuration
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USERNAME'] = "[email protected]"
app.config['MAIL_PASSWORD'] = "dleiatmoyykkaafn"
app.config['MAIL_USE_TLS'] = False
app.config['MAIL_USE_SSL'] = True
mail = Mail(app)
# fetch movies data
conn = db_connection("movies")
cursor = conn.cursor()
cursor.execute(FETCH_ALL_MOVIES)
movies_result = cursor.fetchall()
conn.close()
# Home page
@app.route('/')
def index():
return render_template("index.html")
# Signup page
signup_email = ""
signup_password = ""
signup_mobile = ""
@app.route('/signup', methods=['GET','POST'])
def signup():
global signup_email
global signup_password
global signup_mobile
response = render_template("signup.html")
if request.method == 'POST':
conn = db_connection("users")
cursor = conn.cursor()
signup_email = request.form["email"]
signup_mobile = request.form["mobile"]
signup_password = request.form["password"]
sql_query = "Select email from users where email= '"+signup_email+"'"
cursor.execute(sql_query)
results = cursor.fetchall()
conn.close()
if len(results) != 0:
response = "This email is already registered. <br> Please sign-in."
else:
session["user"] = signup_email
session["choices"] = 0
response = "choices"
return response
# Signin page
signin_email = ""
@app.route('/signin', methods=['GET','POST'])
def signin():
global signin_email
response = render_template("signin.html")
if request.method == 'POST':
conn = db_connection("users")
cursor = conn.cursor()
signin_email = request.form["email"]
password = request.form["password"]
sql_query = "Select email, password from users where email= '"+signin_email+"'"
cursor.execute(sql_query)
results = cursor.fetchall()
conn.close()
if len(results) == 0:
response = "This email is not registered. <br> Please sign-up first."
elif password != results[0][1]:
response = "Incorrect Password."
elif password == results[0][1]:
session["user"] = signin_email
session["choices"] = 1
response = "recommendations"
return response
# Choices page
@app.route('/choices', methods=['GET','POST'])
def choices():
global signup_email
global movies_result
if "user" in session:
if "choices" in session and session["choices"] == 1:
return redirect(url_for("recommendations"))
else:
genre_names = []
cast_dict = {}
for row in movies_result:
mov_gen = list(row[5].split("$"))
for gen in mov_gen:
if gen not in genre_names and gen != '':
genre_names.append(gen)
mov_cast = list(row[7].split("$"))
for cast in mov_cast:
if cast in cast_dict and cast != '':
cast_dict[cast] = (cast_dict[cast] + 1)
else:
cast_dict[cast] = 0
cast_dict = dict(sorted(cast_dict.items(), key=operator.itemgetter(1), reverse=True))
cast_names = []
counter = 0
for key in cast_dict:
if counter < 25:
cast_names.append(key)
counter += 1
else:
break
return render_template("choices.html", genre_names=genre_names, cast_names=cast_names)
else:
print("Session not found")
return redirect(url_for("signup"))
movie_names = []
for row in movies_result:
movie_names.append(row[2])
@app.route('/getByGenre', methods=['GET','POST'])
def getByGenre():
genre = request.form["genre"]
genre_movies, genre_posters = byGenre(genre, movies_result)
response = jsonify(
{"genre_movies": genre_movies}, {"genre_posters": genre_posters}
)
return response
@app.route('/getByYear', methods=['GET','POST'])
def getByYear():
year = request.form["year"]
year_movies, year_posters = byYear(year, movies_result)
response = jsonify(
{"year_movies": year_movies}, {"year_posters": year_posters}
)
return response
# Recommendations page
@app.route('/recommendations', methods=['GET', 'POST'])
def recommendations():
global signin_email
global signup_email
if "user" in session:
conn = db_connection("users")
cursor = conn.cursor()
selected_genres = []
selected_cast = []
sql_query = " Select selected_genres, selected_cast from users where email= '" + session["user"] + "'"
cursor.execute(sql_query)
results = cursor.fetchall()
# sign-in
if len(results) != 0:
selected_genres = list(results[0][0].split("$"))
selected_cast = list(results[0][1].split("$"))
# sign-up
else:
genreList = request.form.getlist('genre-checkbox')
castList = request.form.getlist('cast-checkbox')
for i in genreList:
selected_genres.append(i.replace("-", " "))
for i in castList:
selected_cast.append(i.replace("-", " "))
# insert into database on signup
sg = ("$".join(selected_genres))
sc = ("$".join(selected_cast))
global signup_password
global signup_mobile
cursor.execute(INSERT_USER, (signup_email, signup_password, signup_mobile, sg, sc))
conn.commit()
print(cursor.lastrowid)
conn.close()
# session["choices"] = 1
conn.close()
choice_movies = byChoice(selected_genres, selected_cast)
choice_idx = []
choice_posters = []
for mov in choice_movies:
for row in movies_result:
if row[2] == mov:
movie_idx = row[0]
movie_id = row[1]
break;
choice_idx.append(movie_idx)
choice_posters.append(fetchBackdrop(movie_id))
genre_movies, genre_posters = byGenre("Action", movies_result)
year_movies, year_posters = byYear("2016", movies_result)
return render_template("recommendations.html", movie_names=movie_names, genre_movies=genre_movies, year_movies=year_movies, genre_posters=genre_posters, year_posters=year_posters, choice_idx=choice_idx, movies_result=movies_result, choice_posters=choice_posters)
else:
print("Session not found")
return redirect(url_for("signin"))
# Movie page
@app.route('/movie/<movie_name>')
def movie(movie_name):
if "user" in session:
def recommend(movie):
for row in movies_result:
if row[2] == movie:
movie_index = row[0]
break;
u = AnnoyIndex(5000, 'angular')
u.load('./model/data/vectors.ann')
movies_list = (u.get_nns_by_item(movie_index, 7))[1:7] # returns index of 6 similar movies
recommended_movies = []
movie_posters = []
for i in movies_list:
rec_movie_id = movies_result[i][1]
recommended_movies.append(movies_result[i][2])
movie_posters.append(fetchPoster(rec_movie_id, movies_result))
return recommended_movies, movie_posters
selected_movie = movie_name
for row in movies_result:
if row[2] == selected_movie:
movie_idx = row[0]
movie_id = row[1]
break;
recommendations, posters = recommend(selected_movie)
trailer_key = fetchTrailer(movie_id, movies_result)
movie_poster = fetchPoster(movie_id, movies_result)
mov_genre = []
mov_cast = []
for row in movies_result:
mg = list(row[5].split("$"))
mov_genre.append(mg)
mc = list(row[7].split("$"))
mov_cast.append(mc)
return render_template("movie.html", movie_names=movie_names, movies_result=movies_result, movie_idx=movie_idx, recommendations=recommendations, posters=posters, trailer_key=trailer_key, movie_poster=movie_poster, mov_genre=mov_genre, mov_cast=mov_cast)
else:
print("Session not found")
return redirect(url_for("signin"))
# Watch page
@app.route('/watch/<movie_name>')
def watch(movie_name):
if "user" in session:
return render_template("watch.html", movie_name=movie_name, movie_names=movie_names)
else:
print("Session not found")
return redirect(url_for("signin"))
otp = ""
# Forgot page
@app.route('/forgot', methods=['POST', 'GET'])
def forgot():
global signin_email
global otp
response = render_template("forgotPass.html")
if request.method == 'POST':
conn = db_connection("users")
cursor = conn.cursor()
signin_email = request.form["email"]
sql_query = "Select email, password from users where email= '"+signin_email+"'"
cursor.execute(sql_query)
results = cursor.fetchall()
conn.close()
if len(results) == 0:
response = "This email is not registered. <br> Please sign-up first."
else:
response = "forgot"
if response == "forgot":
range_start = 10**(6-1)
range_end = (10**6)-1
otp = randint(range_start, range_end)
message = Message("Next Up | OTP for password reset", sender="[email protected]", recipients=[signin_email])
message.body = "OTP: "+str(otp)
mail.send(message)
return response
# reset page
@app.route('/reset', methods=['GET','POST'])
def reset():
global otp
response = render_template("reset.html")
if request.method == 'POST':
num = request.form["otp"]
if str(num) == str(otp):
response = "valid"
else:
response = "OTP entered is incorrect"
return response
# change password in database
@app.route('/change', methods=['GET','POST'])
def change():
global signin_email
if request.method == 'POST':
newPass = request.form["newPass"]
conn = db_connection("users")
cursor = conn.cursor()
sql_query = "Update users set password = '"+newPass+"' where email = '"+signin_email+"'"
cursor.execute(sql_query)
conn.commit()
conn.close()
session["user"] = signin_email
session["choices"] = 1
return "recommendations"
# Logout
@app.route('/logout')
def logout():
session.pop("user", None)
session.pop("choices", None)
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(debug=True)