-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
149 lines (118 loc) · 4.03 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
from flask import Flask, flash, render_template, request, redirect
from werkzeug import generate_password_hash, check_password_hash
import MySQLdb as my
import yaml
from get_movie import *
"""This File uses MySQL ONLY"""
app = Flask(__name__)
db = my.connect(ADDRESS, USERNAME, PASSWORD, "login")
app.secret_key = KEY
home_address = "http://%s:%d" % (ADDRESS, PORT)
USN = ''
EMAIL = ''
# ___________________________________________________ #
"""Home Page """
@app.route('/')
def home():
return render_template('index.html', addr=home_address)
# ___________________________________________________ #
"""Sign up and Sign in functions"""
@app.route('/signup')
def signup_help():
global USN, EMAIL
email = EMAIL
USN = EMAIL = ''
return render_template('sign.html', type=1, email=email, username='')
@app.route('/signin')
def signin_help():
global USN, EMAIL
username = USN
USN = EMAIL = ''
return render_template('sign.html', type=2, email='', username=username)
@app.route('/main', methods=['POST', 'GET'])
def sign_up_in():
global EMAIL, USN
if request.method == 'POST':
result = request.form
if 'email' in result: # If email present : it means it is sign up, else sign in!
dump_to_json('signup.json', result) # Temporarily write to json, the db funcs will use this to insert or validate.
if not update_db():
EMAIL = result['email']
return redirect('/signup')
return redirect('/movie')
else:
dump_to_json('signin.json', result)
if check_with_db():
return redirect('/movie')
else:
flash("Please Try Again")
USN = result['username']
return redirect('/signin')
@app.route('/movie')
def movie():
return render_template('movie.html', found=True)
@app.route('/movieilla')
def movie_illa():
return render_template('movie.html', found=False)
# ___________________________________________________________ #
"""Results from api: Use get_movie.py"""
@app.route('/results', methods=['POST', 'GET'])
def results():
if request.method == 'POST':
result = request.form
try:
movie_info = get_movie(result['Title'].encode('utf-8'))
if movie_info['Response'] == False:
return redirect('/movieilla')
return render_template('results.html', dict=movie_info)
except:
flash("Invalid, try again!")
return redirect('/movieilla')
@app.route('/senti')
def senti():
f = open('movie.json', 'r').read()
parsed_json = yaml.safe_load(f)
senti_dict = senti_analysis(parsed_json['Title'])
return render_template('senti.html', list=senti_dict['document_tone']['tone_categories'])
# ______________________________________________________ #
@app.route('/logout')
def logout():
"""
# Try Later if time permits
query = "delete UserName from logged_users where UserName ..."
"""
dump_to_json("signin.json", {})
dump_to_json("signup.json", {})
return redirect('/')
# _______________________________________________________ #
"""Database stuff"""
def check_with_db():
f = open('signin.json').read()
f = json.loads(f)
password = f['password']
sql = "Select password from user where userName = '" + f['username'] + "';"
try:
cursor = db.cursor()
cursor.execute(sql)
db.commit()
data = list(cursor.fetchone()) + [password]
return check_password_hash(data[0], data[1])
except:
db.rollback()
return False
def update_db():
f = open('signup.json').read()
f = json.loads(f)
password = generate_password_hash(f['password'])
try:
sql = "insert into user values('" + f['username'] + "','" + f['email'] + "' ,'"+ password +"');"
cursor = db.cursor()
cursor.execute(sql)
db.commit()
except:
db.rollback()
return False
return True
# _______________________________________________________ #
if __name__ == "__main__":
app.run(debug=True, port=PORT)