-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
245 lines (199 loc) · 7.19 KB
/
app.py
File metadata and controls
245 lines (199 loc) · 7.19 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
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
import sqlite3
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
from functools import wraps
from scrapper import get_product_details
from hashlib import md5
from mail import send_mail
from bs4 import BeautifulSoup
import schedule
import time
import threading
def get_db_connection():
conn = sqlite3.connect('database.db')
conn.row_factory = sqlite3.Row
return conn
app = Flask(__name__)
@app.route('/')
def index():
if "logged_in" in session:
redirect(url_for('dashboard'))
return render_template("home.html")
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords do not match!!')
])
confirm = PasswordField('Confirm Password')
# registration route
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = sha256_crypt.hash(str(form.password.data))
print(str(form.password.data)+': '+password)
conn = get_db_connection()
conn.execute("INSERT INTO users (name, email, username, password) VALUES (?, ?, ?, ?)",
(name, email, username, password))
conn.commit()
conn.close()
flash("You are now registered and can Login", "success")
return redirect(url_for('login'))
return render_template('register.html', form=form)
# User-Login route
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Get Form Fields
email = request.form['email']
password = request.form['password']
# Create cursor
conn = get_db_connection()
cur = conn.cursor()
# Get user by username
find_user = ("SELECT * FROM users WHERE email = ?")
cur.execute(find_user, [email])
results = cur.fetchone()
if results:
if sha256_crypt.verify(password, results['password']):
# Passed
session["logged_in"] = True
session["email"] = email
session["name"] = results['name']
session["userid"] = results['id']
flash("You are now logged in.", "success")
return redirect(url_for("dashboard"))
else:
error = "Invalid login"
return render_template("login.html", error=error)
# Close connection
cur.close()
else:
error = "Email not found!!"
return render_template("login.html", error=error)
return render_template('login.html')
# check if user is logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if "logged_in" in session:
return f(*args, **kwargs)
else:
flash("Unauthorised, Please login to continue!!", 'danger')
return redirect(url_for("login"))
return wrap
# logout route
@app.route("/logout")
def logout():
session.clear()
flash("You are now LoggedOut.", "warning")
return redirect(url_for("login"))
# Add new url //links form
class LinkForm(Form):
url = StringField('Add New Product')
def gravatar(email, size):
digest = md5(email.lower().encode('utf-8')).hexdigest()
return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(digest, size)
# dashboard
@app.route('/dashboard')
@is_logged_in
def dashboard():
conn = get_db_connection()
form = LinkForm(request.form)
glink = gravatar(session['email'], 128)
links = conn.execute('SELECT * FROM links WHERE userid = ? ORDER BY link_date DESC',
[session['userid']]).fetchall()
conn.close()
return render_template("dashboard.html", links=links, form=form, glink=glink)
# add delete
@app.route('/delete/<string:id>', methods=['POST'])
@is_logged_in
def delete_url(id):
# Create cursor
cur = get_db_connection()
# Execute
cur.execute("DELETE FROM links WHERE id = ?", [id])
# Commit to DB
cur.commit()
# Close connection
cur.close()
flash('URL Deleted', 'danger')
return redirect(url_for('dashboard'))
# add url route
@app.route('/add', methods=['GET', 'POST'])
@is_logged_in
def add_url():
form = LinkForm(request.form)
if request.method == 'POST':
url = form.url.data
userid = session["userid"]
detail = get_product_details(url)
print(detail)
# Create cursor
conn = get_db_connection()
conn.execute("INSERT INTO links(url ,product, price, availability, image_url, userid) VALUES(?,?,?,?,?,?)",
(detail['url'], detail['name'], detail['price'], detail['availability'], detail['image_url'], userid))
# connection commit
conn.commit()
conn.close()
redirect(url_for("dashboard"))
flash("URL Added", 'success')
return redirect(url_for('dashboard'))
def checkFunction():
# get db conn
print("checkFunction Called")
conn = get_db_connection()
products = conn.execute(
"SELECT * FROM links INNER JOIN users ON links.userid=users.id").fetchall()
conn.close()
for product in products:
newDetails = get_product_details(product['url'])
print("Product: "+product['product'])
print(product['price'])
print(newDetails['price'])
if newDetails['price'] < product['price']:
send_mail(product, newDetails)
print('Email has been sent to '+product['name'])
# Create cursor
conn = get_db_connection()
conn.execute("UPDATE links SET price=? WHERE id=?",
(newDetails['price'], product['id']))
# connection commit
conn.commit()
conn.close()
# Auto Scheduler doesn't run concurrently with Flask Server
# Threading needs to be implemented to automate both tasks
# To check the updation and email sending hit below endpoint
def updateThread():
print("Update Thread Called")
print(threading.current_thread().name)
# schedule.every(1).minutes.do(checkFunction)
schedule.every(1).hours.do(checkFunction)
while True:
# print("While loop of updateThread")
schedule.run_pending()
time.sleep(5)
# Updation Check Endpoint
# Hit this endpoint to manually update the Prices
# Can be given as an option to the user to refresh prices manually
@app.route('/update')
@is_logged_in
def update():
checkFunction()
return redirect(url_for("dashboard"))
@app.before_first_request
def thread_start():
threading.Thread(target=updateThread).start()
app.secret_key = 'secret123'
# For Production/Deployment
if __name__ == '__main__':
app.run(debug=True, threaded=True, use_reloader=False)
# For Development
# app.run(debug=True)