-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
executable file
·435 lines (350 loc) · 15.4 KB
/
__init__.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
from flask import Flask, session, render_template, request, redirect, Response, url_for
from flaskext.mysql import MySQL
from flask_restful import Resource, Api
import logging
import html
from PIL import Image
import requests
from bs4 import BeautifulSoup
import json
from io import BytesIO
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import datetime, timedelta
#from threading import Timer
import operator
import locale
from flask_paginate import Pagination, get_page_parameter
from flask_bootstrap import Bootstrap
import mysql.connector
import sys
sys.path.append("..")
from Frugally import DBqueries
app = Flask(__name__)
api = Api(app)
'''
All code is original and written by Casey Zduniak, 2020
Use app.logger.info() to log information to the flask.log file in the same directory. use like a normal print statement
This file is organized into APP ROUTES, FILTERS, and MAIL SENDER subheadings
'''
'''
:::'###::::'########::'########:::::'########:::'#######::'##::::'##:'########:'########::'######::
::'## ##::: ##.... ##: ##.... ##:::: ##.... ##:'##.... ##: ##:::: ##:... ##..:: ##.....::'##... ##:
:'##:. ##:: ##:::: ##: ##:::: ##:::: ##:::: ##: ##:::: ##: ##:::: ##:::: ##:::: ##::::::: ##:::..::
'##:::. ##: ########:: ########::::: ########:: ##:::: ##: ##:::: ##:::: ##:::: ######:::. ######::
#########: ##.....::: ##.....:::::: ##.. ##::: ##:::: ##: ##:::: ##:::: ##:::: ##...:::::..... ##:
##.... ##: ##:::::::: ##::::::::::: ##::. ##:: ##:::: ##: ##:::: ##:::: ##:::: ##:::::::'##::: ##:
##:::: ##: ##:::::::: ##::::::::::: ##:::. ##:. #######::. #######::::: ##:::: ########:. ######::
..:::::..::..:::::::::..::::::::::::..:::::..:::.......::::.......::::::..:::::........:::......:::
'''
#logging.basicConfig(filename='Flask.log', level=logging.INFO)
# This section upgrades http requests to https
@app.before_request
def before_request():
if(request.url.startswith('http://')):
url = request.url.replace('http://', 'https://', 1)
#app.logger.info("HTTPS redirect")
return redirect(url,code=301)
# Custom Internal Server Error Page
@app.errorhandler(500)
def InternalError(e):
return render_template('500.html'), 500
# Lets us know which product the user clicked on, then redirects them to the site
@app.route('/redirect/<pid>')
def perm_redirect(pid):
item = DBqueries.findByPID(pid)
ipaddr = request.remote_addr
# use product link as primary key (should we switch to product IDs?)
# associate the IP address with the product link
# check if item is sold out
if(item[0][1] == "Nordstrom Rack"):
page = requests.get("https://"+item[0][9])
soup = BeautifulSoup(page.content, 'html.parser')
if(len(soup.find_all("div", class_="status-badge--sold-out"))>=1):
#this item is sold out
#delete it from the Frugally DB
errorval = DBqueries.deleteSoldOut(item)
app.logger.info("Sold Out Status: %s" % errorval)
return redirect(session['prevLink'], code=301)
elif(item[0][1] == "Nike"):
page = requests.get("https://"+item[0][9])
soup = BeautifulSoup(page.content, 'html.parser')
if(len(soup.find_all("div", class_="sold-out"))>=1):
#this item is sold out
errorval = DBqueries.deleteSoldOut(item)
app.logger.info("Sold Out Status: %s" % errorval)
return redirect(session['prevLink'], code=301)
app.logger.info("%s: Link Clicked: %s | %s" % (datetime.now(),ipaddr,item))
errorval = DBqueries.Collect(item, ipaddr)
app.logger.info(errorval)
return redirect("https://"+item[0][9], code=301)
# landing page
@app.route('/home', methods=['GET'])
def index():
#app.logger.info("Index")
nordstrom = DBqueries.getSQLNordstrom()
nike = DBqueries.getSQLNike()
fullList = nordstrom + nike
items = len(fullList)
itemsinrow = 3
itemsperpage = 16
page = 0
brands = getBrands(fullList)
vendors = ["Nordstrom", "Nike"]
page = request.args.get(get_page_parameter(), type=int, default=1)
pagination = Pagination(page=page, per_page=itemsperpage, total=items//itemsinrow+1, css_framework='bootstrap3')
session['prevLink'] = "https://frugally.io/home"
return render_template('index.html', objects=fullList, itemsinrow=itemsinrow, items=items, pagination=pagination, brands=brands, vendors=vendors)
# post methods for homepage
@app.route('/home', methods=['POST'])
def feedback():
formid = request.form.get("homepage","")
app.logger.info("Post received")
# Send email
if(formid == "2"):
name = request.form['name']
email = request.form['email']
message = request.form['message']
# sends to gmail
sendMail(email, name, message)
return redirect('https://frugally.io/home', code=302)
else:
return redirect("https://frugally.io/home", code=302)
@app.route('/', methods=["GET", "POST"])
def about():
#POST
if(request.method == 'POST'):
formid = request.form.get("aboutpage","")
# Send email (gmail disabled our account for some reason)
if(formid == "2"):
name = request.form['name']
email = request.form['email']
message = request.form['message']
# sends to gmail
sendMail(email, name, message)
session['prevLink'] = "https://frugally.io/"
return render_template('about.html')
'''
The following contains two url paths, one for men, and one for women
<filters> is a dynamic route that acts as a url variable, containing various info
'''
@app.route('/men/<filters>', methods=["GET", "POST"])
def men(filters):
#POST
if(request.method == 'POST'):
formid = request.form.get("menspage","")
# Send email
if(formid == "2"):
name = request.form['name']
email = request.form['email']
message = request.form['message']
# sends to gmail
sendMail(email, name, message)
elif(formid == "1"):
radio = request.form.get('radio')
vendorfilter = request.form.getlist('vendorsBox')
brands = request.form.getlist('brandsBox')
prange = request.form.getlist('rangeBox')
return returnFilter(radio, vendorfilter, brands, prange, "men")
#GET
else:
session['prevLink'] = "https://frugally.io/men/"+filters
options = parseFilter(filters)
#app.logger.info(options)
objects, errorlogger = DBqueries.getSQLsort(options, gender='men')
#app.logger.info(errorlogger)
maxprice = round(DBqueries.getMaxPriceMen())
itemsinrow = 3
items = len(objects)
itemsperpage = 16
page = 0
brands = getBrands(objects)
prange = getPrices(maxprice)
vendors = ["Nordstrom Rack", "Nike"]
page = request.args.get(get_page_parameter(), type=int, default=1)
pagination = Pagination(page=page, per_page=itemsperpage, total=items//itemsinrow+1, css_framework='bootstrap3')
return render_template('mens.html', objects=objects, itemsinrow=itemsinrow, items=items, pagination=pagination, brands=brands, vendors=vendors, prange=prange)
return redirect('https://frugally.io/home', code=302)
@app.route('/women/<filters>', methods=["GET", "POST"])
def women(filters):
#POST
if(request.method == 'POST'):
formid = request.form.get("womenspage","")
# Send email
if(formid == "2"):
name = request.form['name']
email = request.form['email']
message = request.form['message']
# sends to gmail
sendMail(email, name, message)
elif(formid == "1"):
radio = request.form.get('radio')
#gender = request.form.get('radio2')
vendorfilter = request.form.getlist('vendorsBox')
brands = request.form.getlist('brandsBox')
prange = request.form.getlist('rangeBox')
return returnFilter(radio, vendorfilter, brands, prange, "women")
else:
session['prevLink'] = "https://frugally.io/women/"+filters
options = parseFilter(filters)
objects, errorlogger = DBqueries.getSQLsort(options, gender='women')
#app.logger.info(errorlogger)
maxprice = round(DBqueries.getMaxPriceWomen())
items = len(objects)
itemsinrow = 3
itemsperpage = 16
page = 0
brands = getBrands(objects)
prange = getPrices(maxprice)
vendors = ["Nordstrom Rack", "Nike"]
page = request.args.get(get_page_parameter(), type=int, default=1)
pagination = Pagination(page=page, per_page=itemsperpage, total=items//itemsinrow+1, css_framework='bootstrap3')
return render_template('womens.html', objects=objects, itemsinrow=itemsinrow, items=items, pagination=pagination, brands=brands, vendors=vendors, prange=prange)
return redirect('https://frugally.io/home', code=302)
#login page (unused at the moment)
@app.route('/login', methods=['GET', 'POST'])
def login():
return render_template('login.html')
#this is for google only
@app.route('/google5e9dcfe4850ad995.html')
def google():
return render_template('google5e9dcfe4850ad995.html')
#robots.txt
@app.route('/robots.txt', methods=['GET'])
def robots():
with open('/var/www/Frugally/Frugally/templates/robots.txt', 'r') as f:
content = f.read()
return Response(content, mimetype='text')
#sitemap
@app.route('/sitemap.xml', methods=['GET'])
def sitemap():
pages = []
deltadays = datetime.now() - timedelta(days=7)
deltadays = deltadays.strftime("%Y-%m-%d")
for rule in app.url_map.iter_rules():
if 'GET' in rule.methods and len(rule.arguments) == 0 and not rule.rule.startswith('/admin'):
pages.append([ url_for('index', _external=True) + rule.rule, deltadays])
sitemap_template = render_template('sitemap.xml', pages=pages)
response = Response(sitemap_template)
response.headers["Content-Type"] = "application/xml"
return response
'''
'########:'####:'##:::::::'########:'########:'########:::'######::
##.....::. ##:: ##:::::::... ##..:: ##.....:: ##.... ##:'##... ##:
##:::::::: ##:: ##:::::::::: ##:::: ##::::::: ##:::: ##: ##:::..::
######:::: ##:: ##:::::::::: ##:::: ######::: ########::. ######::
##...::::: ##:: ##:::::::::: ##:::: ##...:::: ##.. ##::::..... ##:
##:::::::: ##:: ##:::::::::: ##:::: ##::::::: ##::. ##::'##::: ##:
##:::::::'####: ########:::: ##:::: ########: ##:::. ##:. ######::
..::::::::....::........:::::..:::::........::..:::::..:::......:::
'''
# https://frugally.io/men/sort=discout+vendors=nordstromrack+brands=asics_guess+pr=
# The packing and unpacking URL filters
def returnFilter(radio, vendors, brands, prange, gender):
try:
if(radio == None):
radio = 'discount'
else:
radio = radio.lower()
filterstring = "sort="+radio+"+vendors="
for i in vendors:
filterstring = filterstring+str(i)+"_"
if(filterstring[-1] != '='):
filterstring = filterstring[:-1]
filterstring=filterstring+"+brands="
for i in brands:
filterstring = filterstring+str(i)+"_"
if(filterstring[-1] != "="):
filterstring = filterstring[:-1]
filterstring = filterstring+"+range="
for i in prange:
filterstring = filterstring+str(i)+"_"
if(filterstring[-1] != "="):
filterstring = filterstring[:-1]
return redirect(("https://frugally.io/"+gender+"/"+filterstring), code=302)
except:
return redirect("https://frugally.io", code=302)
def parseFilter(filter):
options = filter.split('+')
if((len(options) <= 1) or (filter == 'home')):
return [['sort', 'discount'], ['vendors', 'all'], ['brands', 'all'], ['range', 'all']]
values = []
#app.logger.info(options)
for i in options:
values.append(i.split('='))
if(values[-1][0] == 'vendors'):
if(len(values[-1][1]) <= 1):
values[-1][1] = "all"
else:
vends = values[-1].pop(-1)
values[-1].append(vends.split('_'))
if(values[-1][0] == 'brands'):
if(len(values[-1][1]) <= 1):
values[-1][1] = "all"
else:
brands = values[-1].pop(-1)
values[-1].append(brands.split('_'))
if(values[-1][0] == 'range'):
if(len(values[-1][1]) <= 1):
values[-1][1] = "all"
else:
brands = values[-1].pop(-1)
values[-1].append(brands.split('_'))
#app.logger.info(values)
return values
# This just gathers the list of brands to be displayed in the filter menu
def getBrands(objects):
brands = []
for i in objects:
if(i[4] not in brands):
if(i[4] != None):
brands.append(i[4])
brands.sort()
return brands
# This gathers price ranges to be output in the filters menu
def getPrices(maxprice):
mprices = ["$0-$100"]
for i in range((maxprice//100)):
mprices.append("$%s00-$%s00" % (str(i+1), str(i+2)))
return mprices
'''
'##::::'##::::'###::::'####:'##:::::::::::'######::'########:'##::: ##:'########::'########:'########::
###::'###:::'## ##:::. ##:: ##::::::::::'##... ##: ##.....:: ###:: ##: ##.... ##: ##.....:: ##.... ##:
####'####::'##:. ##::: ##:: ##:::::::::: ##:::..:: ##::::::: ####: ##: ##:::: ##: ##::::::: ##:::: ##:
## ### ##:'##:::. ##:: ##:: ##::::::::::. ######:: ######::: ## ## ##: ##:::: ##: ######::: ########::
##. #: ##: #########:: ##:: ##:::::::::::..... ##: ##...:::: ##. ####: ##:::: ##: ##...:::: ##.. ##:::
##:.:: ##: ##.... ##:: ##:: ##::::::::::'##::: ##: ##::::::: ##:. ###: ##:::: ##: ##::::::: ##::. ##::
##:::: ##: ##:::: ##:'####: ########::::. ######:: ########: ##::. ##: ########:: ########: ##:::. ##:
..:::::..::..:::::..::....::........::::::......:::........::..::::..::........:::........::..:::::..::
'''
# Sends email from burner gmail to frugally gmail
def sendMail(customer, name, message):
user = '[email protected]'
pw = 'z4M<c2=3W:n;Fg@^'
recv = '[email protected]'
msg = MIMEMultipart()
msg['From'] = user
msg['To'] = recv
msg['Subject'] = 'Website Feedback'
body = '%s at %s\n\n%s\n\nSent from Frugally Application Server' % (name, customer, message)
msg.attach(MIMEText(body))
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
#server.ehlo()
server.login(user, pw)
server.sendmail(user, recv, msg.as_string())
server.quit()
return 'success'
# Setter and Getter class object used as a pseudo global variable
class previousPage:
def __init__(self, link=None):
self._link = link
def get_Link(self):
return self._link
def set_Link(self, x):
self._link = x
if __name__ == '__main__':
prevLink = previousPage()
app.run(ssl_context=('/var/www/Frugally/frugally.io-ssl-bundle/domain.cert.pem', '/var/www/Frugally/frugally.io-ssl-bundle/private.key.pem'))