-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (115 loc) · 4.88 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
from flask import Flask, render_template, redirect, url_for, request
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
import requests
import os
API_KEY = os.environ.get("API_KEY_MOVIE")
movie_endpoint = "https://api.themoviedb.org/3/search/movie"
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get("APP_CONFIG_SECRET_KEY")
Bootstrap(app)
# Create SQLite DATABASE:
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get("DATABASE_URL", "sqlite:///blog.db")
# Optional: But it will silence the deprecation warning in the console.
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Create Table
class Movie(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), unique=True, nullable=False)
year = db.Column(db.Integer, nullable=False)
description = db.Column(db.String(500), nullable=False)
rating = db.Column(db.Float, nullable=True)
ranking = db.Column(db.Integer, nullable=True)
review = db.Column(db.String(250), nullable=True)
img_url = db.Column(db.String(250), nullable=False)
db.create_all()
## After adding the new_movie the code needs to be commented out/deleted.
## So you are not trying to add the same movie twice.
# Create Record
# new_movie = Movie(
# title="Phone Booth",
# year=2002,
# description="Publicist Stuart Shepard finds himself trapped in a phone booth, pinned down by an extortionist's sniper rifle. Unable to leave or receive outside help, Stuart's negotiation with the caller leads to a jaw-dropping climax.",
# rating=7.3,
# ranking=10,
# review="My favourite character was the caller.",
# img_url="https://image.tmdb.org/t/p/w500/tjrX2oWRCM3Tvarz38zlZM7Uc10.jpg"
# )
# db.session.add(new_movie)
# db.session.commit()
@app.route("/")
def home():
# READ ALL RECORDS:
all_movies = Movie.query.order_by(Movie.rating).all()
#This line loops through all the movies
for i in range(len(all_movies)):
#This line gives each movie a new ranking reversed from their order in all_movies
all_movies[i].ranking = len(all_movies) - i
# db.session.commit()
return render_template("index.html", movies_data=all_movies)
# UPDATE RATING AND REVIEW
class RateMovieForm(FlaskForm):
rating = StringField("Your Rating Out of 10 e.g. 7.5")
review = StringField("Your Review")
submit = SubmitField("Done")
@app.route("/edit", methods=["GET", "POST"])
def rate_movie():
form = RateMovieForm()
movie_id = request.args.get("id")
movie = Movie.query.get(movie_id)
if form.validate_on_submit():
movie.rating = float(form.rating.data)
movie.review = form.review.data
db.session.commit()
return redirect(url_for("home"))
return render_template("edit.html", movie=movie, form=form)
@app.route("/delete")
def delete_movie():
movie_id = request.args.get("id")
# DELETE RECORD
movie_to_delete = Movie.query.get(movie_id)
db.session.delete(movie_to_delete)
db.session.commit()
return redirect(url_for("home"))
# UPDATE RATING AND REVIEW
class FindMovieForm(FlaskForm):
title_search = StringField("Movie Title", validators=[DataRequired()])
submit = SubmitField("Add Movie")
@app.route("/add", methods=["GET", "POST"])
def add_movie():
form = FindMovieForm()
if form.validate_on_submit():
movie_title = form.title_search.data
response = requests.get(url=movie_endpoint,
params={"api_key": API_KEY, "query": movie_title, "include_adult": True})
print(response.json())
data = response.json()["results"]
return render_template("select.html", options=data)
return render_template("add.html", form=form)
@app.route("/find")
def find_movie():
movie_api_id = request.args.get("id")
if movie_api_id:
MOVIE_DB_INFO_URL = "https://api.themoviedb.org/3/movie"
MOVIE_DB_IMAGE_URL = "https://image.tmdb.org/t/p/w500"
movie_api_url = f"{MOVIE_DB_INFO_URL}/{movie_api_id}"
#The language parameter is optional, if you were making the website for a different audience
#e.g. Hindi speakers then you might choose "hi-IN"
response = requests.get(movie_api_url, params={"api_key": API_KEY, "language": "en-US"})
data = response.json()
new_movie = Movie(
title=data["title"],
# The data in release_date includes month and day, we will want to get rid of.
year=data["release_date"].split("-")[0],
img_url=f"{MOVIE_DB_IMAGE_URL}{data['poster_path']}",
description=data["overview"]
)
db.session.add(new_movie)
db.session.commit()
return redirect(url_for("rate_movie", id=new_movie.id))
if __name__ == '__main__':
app.run(debug=True)