-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
261 lines (202 loc) · 8.02 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
from flask import Flask, render_template, url_for, redirect, request
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import Form, BooleanField, StringField, PasswordField, validators
from wtforms.validators import Required, InputRequired, Email, Length, ValidationError
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
import sqlite3
from justwatch import JustWatch
import json
import query
app = Flask(__name__)
app.config['SECRET_KEY'] = "CS330FinalProject!"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///movies.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
bootstrap = Bootstrap(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
class Users(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), unique=True)
email = db.Column(db.String(50), unique=True)
password = db.Column(db.String(80))
@login_manager.user_loader
def load_user(user_id):
return Users.query.get(int(user_id))
class LoginForm(FlaskForm):
username = StringField('Username', validators=[
InputRequired(), Length(min=4, max=15)])
password = PasswordField('Password', validators=[
InputRequired(), Length(min=8, max=80)])
remember = BooleanField('Remember Me')
class RegisterForm(FlaskForm):
email = StringField('Email', validators=[InputRequired(), Email(
message='Invalid email'), Length(max=50)])
username = StringField('Username', validators=[
InputRequired(), Length(min=4, max=15)])
password = PasswordField('Password', validators=[
InputRequired(), Length(min=8, max=80)])
def validate_username(self, username):
user = Users.query.filter_by(username=username.data).first()
if user is not None:
raise ValidationError('Please use a different username.')
def validate_email(self, email):
user = Users.query.filter_by(email=email.data).first()
if user is not None:
raise ValidationError('Please use a different email address.')
class SearchCriteria(FlaskForm):
search = StringField("", validators=[
InputRequired(), Length(max=30)])
""" class Liked(FlaskForm):
liked = BooleanField("Like")
"""
def streaming(title):
just_watch = JustWatch(country='US')
results = just_watch.search_for_item(query=title)
providers = {2: "iTunes", 10: "Youtube", 68: "Microsoft",
15: "Hulu", 8: "Netflix", 7: "Vudu", 3: "Google Play"}
dct = {"rent": [], "buy": []}
for item in results["items"][0]["offers"]:
try:
dct2 = {}
dct2["provider"] = providers[item["provider_id"]]
dct2["price"] = item["retail_price"]
dct2["url"] = item["urls"]["standard_web"]
dct[item["monetization_type"]].append(dct2)
except:
continue
return dct
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = Users.query.filter_by(username=form.username.data).first()
if user:
if check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember.data)
return redirect(url_for('main'))
else:
return render_template('failed.html')
return render_template('login.html', form=form)
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
hashed_password = generate_password_hash(
form.password.data, method='sha256')
new_user = Users(username=form.username.data,
email=form.email.data, password=hashed_password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('index'))
@app.route("/main", methods=['GET', 'POST'])
@login_required
def main():
films = query.randomMovies()
if len(films) >= 20:
print(len(films))
for item in films:
if len(item["title"]) > 15:
item["title"] = item["title"][:14] + "..."
form = SearchCriteria()
if form.validate_on_submit():
search = str(form.search.data)
films = query.returnFilm(search)
if len(films) == 0:
msg = "No results found for %s" % (search)
return render_template("main.html", form=form)
return render_template("main.html", films=films, form=form)
return render_template("main.html", films=films, form=form)
@app.route("/movie", methods=["GET", "POST"])
@login_required
def movie():
form = SearchCriteria()
if form.validate_on_submit():
search = str(form.search.data)
films = query.returnFilm(search)
if len(films) == 0:
msg = "No results found for %s" % (search)
return render_template("main.html", form=form)
return render_template("main.html", films=films, form=form)
movieid = int(request.args["id"])
film = query.returnOneFilm(movieid)
cast = query.returnCast(movieid)
crew = query.returnCrew(movieid)
ratings = query.returnRatings(movieid)
try:
rating = round(ratings[0]["rating"])
except:
rating = 0
stream = streaming(film[0]["title"])
rent = stream["rent"]
buy = stream["buy"]
'''
like = Liked()
if like.validate_on_submit():
query.insert(userid, movieid, "liked")
return render_template("movie.html", like = like, rent = rent, buy = buy, form=form, film=film, cast=cast, crew=crew, rating=rating)
'''
return render_template("movie.html", rent=rent, buy=buy, form=form, film=film, cast=cast, crew=crew, rating=rating)
@app.route("/liked", methods=["GET", "POST"])
@login_required
def liked():
form = SearchCriteria()
if form.validate_on_submit():
search = str(form.search.data)
films = query.returnFilm(search)
if len(films) == 0:
msg = "No results found for %s" % (search)
return render_template("main.html", form=form)
return render_template("main.html", films=films, form=form)
return render_template("liked.html", form=form)
@app.route("/viewed", methods=["GET", "POST"])
@login_required
def viewed():
form = SearchCriteria()
if form.validate_on_submit():
search = str(form.search.data)
films = query.returnFilm(search)
if len(films) == 0:
msg = "No results found for %s" % (search)
return render_template("main.html", form=form)
return render_template("main.html", films=films, form=form)
return render_template("viewed.html")
@app.route("/searched", methods=["GET", "POST"])
@login_required
def searched():
form = SearchCriteria()
if form.validate_on_submit():
search = str(form.search.data)
films = query.returnFilm(search)
if len(films) == 0:
msg = "No results found for %s" % (search)
return render_template("main.html", form=form)
return render_template("main.html", films=films, form=form)
return render_template("searched.html")
@app.route("/about", methods=["GET", "POST"])
@login_required
def about():
form = SearchCriteria()
if form.validate_on_submit():
search = str(form.search.data)
films = query.returnFilm(search)
if len(films) == 0:
msg = "No results found for %s" % (search)
return render_template("main.html", form=form)
return render_template("main.html", films=films, form=form)
return render_template("about.html", form=form)
if __name__ == '__main__':
app.run(debug=True)