-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
418 lines (401 loc) · 17.8 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
from flask import Flask, render_template, redirect, url_for, flash
from flask_bootstrap import Bootstrap5
from flask_ckeditor import CKEditor
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField
from wtforms.validators import DataRequired
import requests
import random
import os
import smtplib
API_KEY = os.environ.get('MOVIE_API')
ORIGIN_COUNTRY = 'US'
categories_dictionary = {'Action & Adventure': 10759,
'Animation': 16,
'Comedy': 35,
'Crime': 80,
'Documentary': 99,
'Drama': 18,
'Family': 10751,
'Kids': 10762,
'Mystery': 9648,
'News': 10763,
'Reality': 10764,
'Sci-Fi & Fantasy': 10765,
'Soap': 10766,
'Talk': 10767,
'War & Politics': 10768,
'Western': 37,}
movies_categories ={
'Action': 28,
'Adventure': 12,
'Animation': 16,
'Comedy': 35,
'Crime': 80,
'Documentary': 99,
'Drama': 18,
'Family': 10751,
'Fantasy': 14,
'History':36,
'Horror':27,
'Music':10402,
'Mystery': 9648,
'Romance':10749,
'Science Fiction':878,
'TV Movie':10770,
'Thriller':53,
'War':10752,
'Western': 37,}
app = Flask(__name__)
ckeditor = CKEditor(app)
Bootstrap5(app)
app.config['SECRET_KEY'] = os.environ.get('FLASK_KEY')
class Feedback(FlaskForm):
feedback = StringField("Feedback", validators=[DataRequired()], render_kw={'class': 'form_class'})
submit = SubmitField("Provide Feedback")
class TV_Filters(FlaskForm):
category = SelectField(label="TV Show Category", choices=["Action & Adventure",
"Animation",
"Comedy",
"Crime",
"Documentary",
"Drama",
"Family",
"Kids",
"Mystery",
"News",
"Reality",
"Sci-Fi & Fantasy",
"Soap",
"Talk",
"War & Politics",
"Western"], render_kw={'class':'form_class'})
with_type = SelectField("TV Show Type", choices=["Scripted",
"Documentary",
"Miniseries",
"Reality",
"Talk Show",
"I Don't Care",], render_kw={'class':'form_class'})
quality_of_show = SelectField("Quality of TV Show", choices=["I Don't Care",
"Decent or Better",
"Highly Rated"], render_kw={'class':'form_class'})
popularity = SelectField("Popularity of TV Show", choices=["Popular",
"Any Popularity",], render_kw={'class':'form_class'})
submit = SubmitField("Find your show")
class Movie_Filters(FlaskForm):
category = SelectField(label="Movie Category", choices=['Action',
'Adventure',
'Animation',
'Comedy',
'Crime',
'Documentary',
'Drama',
'Family',
'Fantasy',
'History',
'Horror',
'Music',
'Mystery',
'Romance',
'Science Fiction',
'TV Movie',
'Thriller',
'War',
'Western',], render_kw={'class':'form_class'})
quality_of_movie = SelectField("Quality of Movie", choices=["I Don't Care",
"Decent or Better",
"Highly Rated"], render_kw={'class':'form_class'})
popularity = SelectField("Popularity of Movie", choices=["Popular",
"Any Popularity",], render_kw={'class':'form_class'})
submit = SubmitField("Find your Movie")
@app.route('/', methods=["GET", "POST"])
def find_show():
form=TV_Filters()
if form.validate_on_submit():
list = []
five_shows = []
full_details = []
seasons = []
episodes = []
show_id = []
homepage = []
#sets the page limit at 150 to purposefully fail to get actual page total
page_to_fail=150
category = form.category.data
with_type = form.with_type.data
quality_of_show = form.quality_of_show.data
popularity = form.popularity.data
#create blank type to fille the actual value into it
type = ""
if with_type == "Documentary":
type = 0
elif with_type == "Miniseries":
type = 2
elif with_type == "Reality":
type = 3
elif with_type == "Scripted":
type = 4
elif with_type == "Talk Show":
type = 5
elif with_type == "I Don't Care":
type = ''
# choices=["I Don't Care", "At least Decent", "Incredible"]
quality = 0
if quality_of_show == "I Don't Care":
quality = 0
elif quality_of_show == "Decent or Better":
quality = 5
elif quality_of_show == "Highly Rated":
quality = 7.5
popularity_value = 0
if popularity == "Popular":
popularity_value = 750
elif popularity == "Any Popularity":
popularity_value = 25
param = {
"include_adult":"true",
"with_origin_country": ORIGIN_COUNTRY,
"with_genres": categories_dictionary[category],
"vote_average.gte": quality,
"page": page_to_fail,
"with_type": type,
"vote_count.gte":popularity_value,
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"accept": "application/json",
}
response = requests.get("https://api.themoviedb.org/3/discover/tv", headers=headers, params=param)
data = response.json()
#if the results don't produce a value, rerun the API using new API values
if data['results'] == []:
try:
max_pages=data['total_pages']-1
param = {
"include_adult":"true",
"with_origin_country": ORIGIN_COUNTRY,
"with_genres": categories_dictionary[category],
"vote_average.gte": quality,
"page": random.randint(1, max_pages),
"with_type": type,
"vote_count.gte":popularity_value,
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"accept": "application/json",
}
response = requests.get("https://api.themoviedb.org/3/discover/tv", headers=headers, params=param)
data = response.json()
except ValueError:
x=data['total_pages']
param = {
"include_adult":"true",
"with_origin_country": ORIGIN_COUNTRY,
"with_genres": categories_dictionary[category],
"vote_average.gte": quality,
"page": 1,
"with_type": type,
"vote_count.gte":popularity_value,
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"accept": "application/json",
}
response = requests.get("https://api.themoviedb.org/3/discover/tv", headers=headers, params=param)
data = response.json()
for x in range (data['total_results']):
if data['total_results'] <= 19 and data['total_results'] >= 1:
show = data['results'][x]
list.append(show['name'])
full_details.append(show)
elif data['total_results'] == 0:
pass
else:
show = data['results'][random.randint(0,19)]
list.append(show['name'])
full_details.append(show)
for x in range(5):
try:
chosen_show = random.choice(full_details)
while chosen_show in five_shows:
chosen_show = random.choice(full_details)
five_shows.append(chosen_show)
full_details.remove(chosen_show)
except IndexError: #used because some may not have 5 data points
pass
for show in five_shows:
response = requests.get(f"https://api.themoviedb.org/3/tv/{show['id']}?language=en-US", headers=headers, params=param)
data = response.json()
show_season = data['number_of_seasons']
seasons.append(show_season)
show_episodes = data['number_of_episodes']
episodes.append(show_episodes)
homepage_link = data['homepage']
homepage.append(homepage_link)
return render_template('tv_results.html',
category=category,
five_shows=five_shows,
data=data,
details=full_details,
genres=form.category.data,
quality=quality_of_show,
type=with_type,
seasons=seasons,
episodes=episodes,
homepage=homepage)
return render_template("tv_index.html", form=form)
@app.route('/movie', methods=["GET", "POST"])
def find_movie():
form=Movie_Filters()
if form.validate_on_submit():
list = []
five_movies = []
full_details = []
seasons = []
episodes = []
show_id = []
homepage = []
budget_list = []
runtime_list = []
#sets the page limit at 501 to purposefully fail to get actual page total
page_to_fail=500
category = form.category.data
quality_of_movie = form.quality_of_movie.data
popularity = form.popularity.data
#create blank type to fille the actual value into it
# choices=["I Don't Care", "At least Decent", "Incredible"]
quality = 0
if quality_of_movie == "I Don't Care":
quality = 0
elif quality_of_movie == "Decent or Better":
quality = 5
elif quality_of_movie == "Highly Rated":
quality = 7.5
popularity_value = 0
if popularity == "Popular":
popularity_value = 1000
else:
popularity_value = 25
param = {
"include_adult":"true",
"with_origin_country": ORIGIN_COUNTRY,
"with_genres": movies_categories[category],
"vote_average.gte": quality,
"page": random.randint(1, page_to_fail),
"vote_count.gte":popularity_value,
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"accept": "application/json",
}
response = requests.get("https://api.themoviedb.org/3/discover/movie", headers=headers, params=param)
movie_data = response.json()
#if the results don't produce a value, rerun the API using new API values
if movie_data['results'] == []:
try:
max_pages=movie_data['total_pages']-1
param = {
"include_adult":"true",
"with_origin_country": ORIGIN_COUNTRY,
"with_genres": movies_categories[category],
"vote_average.gte": quality,
"page": random.randint(1, max_pages),
"with_type": type,
"vote_count.gte":popularity_value,
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"accept": "application/json",
}
response = requests.get("https://api.themoviedb.org/3/discover/movie", headers=headers, params=param)
movie_data = response.json()
except ValueError:
x=movie_data['total_pages']
param = {
"include_adult":"true",
"with_origin_country": ORIGIN_COUNTRY,
"with_genres": movies_categories[category],
"vote_average.gte": quality,
"page": 1,
"with_type": type,
"vote_count.gte":popularity_value,
}
headers = {
"Authorization": f"Bearer {API_KEY}",
"accept": "application/json",
}
response = requests.get("https://api.themoviedb.org/3/discover/movie", headers=headers, params=param)
movie_data = response.json()
for x in range (movie_data['total_results']):
if movie_data['total_results'] <= 19 and movie_data['total_results'] >= 1:
show = movie_data['results'][x]
list.append(show['title'])
full_details.append(show)
elif movie_data['total_results'] == 0:
pass
else:
show = movie_data['results'][random.randint(0,19)]
list.append(show['title'])
full_details.append(show)
for x in range(5):
try:
chosen_movie = random.choice(full_details)
while chosen_movie in five_movies:
chosen_movie = random.choice(full_details)
five_movies.append(chosen_movie)
full_details.remove(chosen_movie)
except IndexError: #used because some may not have 5 data points
pass
for show in five_movies:
response = requests.get(f"https://api.themoviedb.org/3/movie/{show['id']}?language=en-US", headers=headers, params=param)
movie_data = response.json()
homepage_link = movie_data['homepage']
homepage.append(homepage_link)
budget = movie_data['budget']
budget_list.append(budget)
runtime = movie_data['runtime']
runtime_list.append(runtime)
return render_template('movie_results.html',
category=category,
five_movies=five_movies,
data=movie_data,
details=full_details,
genres=form.category.data,
quality=quality_of_movie,
seasons=seasons,
episodes=episodes,
homepage=homepage,
budget=budget_list,
runtime=runtime_list)
return render_template("movie_index.html", form=form)
@app.route('/retry', methods=["GET", "POST"])
def retry():
return redirect(url_for('find_show'))
@app.route('/search-movie', methods=["GET", "POST"])
def movie_redirect():
return redirect(url_for('find_movie'))
@app.route('/privacy-policy', methods=['POST', 'GET'])
def privacy_policy():
return render_template("privacy_policy.html")
@app.route('/terms-and-conditions', methods=['POST', 'GET'])
def terms_and_conditions():
return render_template("terms_and_conditions.html")
@app.route('/feedback', methods=['POST', 'GET'])
def feedback():
form=Feedback()
if form.validate_on_submit():
feedback = form.feedback.data
my_email = os.environ.get('FROM_EMAIL')
password = os.environ.get('EMAIL_PASS')
connection = smtplib.SMTP("smtp.gmail.com", 587)
connection.starttls()
connection.login(user=my_email, password=password)
connection.sendmail(from_addr=my_email,
to_addrs=os.environ.get('TO_EMAIL'),
msg=f"Subject:Feedback from Binge Buddy!\n\nFeedback: {feedback}",
)
connection.close()
flash('Feedback received! Thank you for taking the time to help.')
return render_template("feedback.html", form=form)
if __name__ == "__main__":
app.run(debug=False, port=5002)