-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
155 lines (133 loc) · 5.04 KB
/
app.py
File metadata and controls
155 lines (133 loc) · 5.04 KB
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
from flask import Flask, request, redirect, flash, session
from flask.templating import render_template
import uuid
app = Flask(__name__, template_folder="static")
app.config['SECRET_KEY'] = str(uuid.uuid4())
@app.get("/")
def home():
return render_template('home.html')
@app.get("/login")
def login():
if session.get('user', None):
return redirect(f"/users/{session['user']}")
else:
return render_template('login.html')
@app.get("/signup")
def signup():
return render_template('signup.html')
@app.post("/signup")
def signup_post():
import mysql.connector as mc
import os
passwd = os.getenv("DB_PASSWD_MYSQL")
uname = request.form.get("uname")
upassNew = request.form.get("passNew")
upassCon = request.form.get("passCon")
if upassNew != upassCon:
flash("Passwords entered should match!")
return redirect('/signup')
if 8 > len(upassNew) or len(upassNew) > 16:
flash("Passwords should be between 8 and 16 characters")
return redirect('/signup')
digit = alpha = False
for val in upassNew:
if val.isdigit():
digit = True
if val.isalpha():
alpha = True
if not (alpha and digit):
flash("Passwords should be alphanumeric!")
return redirect('/signup')
try:
with mc.connect(user="mydb_framebook", host="6ot.h.filess.io", passwd=passwd, port=3306) as obj:
cursor = obj.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS mydb_framebook")
cursor.execute("USE mydb_framebook")
cursor.execute(
"CREATE TABLE IF NOT EXISTS Data (UNo INT NOT NULL AUTO_INCREMENT PRIMARY KEY, UName VARCHAR(20) UNIQUE, UPass BINARY(128))")
cursor.execute(f"SELECT * FROM Data WHERE UName='{uname}'")
if cursor.fetchall():
flash("This user already exists! Please try logging in!")
return redirect('/login')
else:
cursor.execute(
f"INSERT INTO Data (UName, UPass) VALUES ('{uname}', AES_ENCRYPT('{upassNew}', 'key'))")
obj.commit()
return redirect('/login')
except Exception as e:
flash(f'{e}')
return redirect('/error')
@app.post('/login')
def login_post():
import mysql.connector as mc
import os
passwd = os.getenv("DB_PASSWD_MYSQL")
uname = request.form.get("uname")
upass = request.form.get("pass")
try:
with mc.connect(user="mydb_framebook", host="6ot.h.filess.io", passwd=passwd, database="mydb_framebook") as obj:
cursor = obj.cursor()
cursor.execute(f"SELECT IF(CAST(AES_ENCRYPT('{upass}', 'key') AS BINARY(128)) = UPass, '1', '0') FROM Data WHERE UName = '{uname}';")
val = cursor.fetchone()
if val is not None:
if val[0] == '1':
session['user'] = uname
return redirect(f'/users/{uname}')
else:
flash('Invalid Credentials !')
return redirect('/login')
else:
flash('Invalid Credentials !')
return redirect('/login')
except Exception as e:
flash(f'{e}')
return redirect('/error')
@app.get('/users/<uname>')
def users(uname):
try:
if session['user'] == uname:
return render_template('dash.html', first_time=True)
else:
return redirect('/login')
except:
return redirect('/login')
@app.get('/logout')
def logout():
if session.get('user', None):
session.pop('user')
return redirect('/login')
else:
return redirect('/login')
@app.post('/users/recommend')
def recommend():
if session.get('user', None):
import csv, random
genre = request.form.get('inputGen')
year = request.form.get('inputYr')
with open("./data/data.csv", 'r', encoding="utf-8") as fin:
csvr = csv.reader(fin)
rec = []
next(csvr)
for row in csvr:
if genre in row[3]:
if year == "new":
if int(row[4]) >= 2000:
rec.append(row)
elif year == "old":
if int(row[4]) < 2000:
rec.append(row)
recommended = rec[int(random.random()*(len(rec)-1))]
title = recommended[1]
link = f"https://open.spotify.com/search/{'%20'.join(title.split())}"
return render_template('dash.html', recommended=recommended, link=link)
else:
return redirect('/login')
@app.get('/users/recommend')
def getRec():
if session.get('user', None):
return redirect(f"/users/{session.get('user')}")
@app.get('/error')
def error():
return render_template('error.html')
if __name__ == "__main__":
app.run(debug=True)