-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
249 lines (162 loc) · 7.28 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
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
import os
from sqlite3.dbapi2 import Cursor
import requests
import validators
import sqlite3
import smtplib
from bs4 import BeautifulSoup
from lxml import etree as et
from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
from tempfile import mkdtemp
from validators.url import url
from werkzeug.useragents import UserAgent
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import and_
import atexit
from helpers import check_email
def compare():
d = db.session.query(Amazon).all()
for entry in d:
email = entry.email
url = entry.url
price = entry.priceAlert
useragent = entry.useragent
currentPrice = check_price(url, useragent)
if currentPrice <= price:
send_email(email, url, price)
app = Flask(__name__)
ENV = 'prod'
if ENV == 'dev':
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:123@localhost/amazon1'
else:
app.debug = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://znapmrfqzymuea:a89600dcdd223aca081da828e9644576e9d6443b9486485a5bf2864943fdaee8@ec2-3-211-6-217.compute-1.amazonaws.com:5432/d4j7hvj2h2pb6f'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Amazon(db.Model):
__tablename__ = 'amazon1'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String)
url = db.Column(db.String)
priceAlert = db.Column(db.Float)
useragent = db.Column(db.String)
def __init__(self, email, url, priceAlert, useragent):
self.email = email
self.url = url
self.priceAlert = priceAlert
self.useragent = useragent
if not app.debug or os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
scheduler = BackgroundScheduler()
scheduler.add_job(func=compare, trigger="interval", hours=24)
scheduler.start()
# Ensure templates are auto-reloaded
app.config["TEMPLATES_AUTO_RELOAD"] = True
# Ensure responses aren't cached
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# Configure session to use filesystem (instead of signed cookies)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
link = request.form.get("url")
if not validators.url(link):
return render_template("index.html", message="Enter a valid URL (ex: https://...)")
# user_agent = UserAgent(request.headers.get('User-Agent')).string
# user_agent kept static to avoid Amazon blocking scraping access.
user_agent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'
headers = {"User-Agent": user_agent, "Referer": link}
page = requests.get(link, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
title = soup.find(id="productTitle").get_text().strip()
price = soup.find("span", {"class": "a-offscreen"})
print(price)
if price == None:
price = soup.find(id="a-price-whole")
if price == None:
price = soup.find(id="priceblock_saleprice")
if price == None:
price = soup.find(id="priceblock_dealprice")
if price is not None:
price = price.get_text().strip()
else:
price = "Price not available"
print(price)
convertedPrice = (price[1:])
convertedPrice = float(convertedPrice)
return render_template("quoted.html", price=price, title=title, convertedPrice=convertedPrice, link=link)
return render_template("index.html")
@app.route("/email", methods=["GET", "POST"])
def email():
if request.method == "POST":
#useragent = UserAgent(request.headers.get('User-Agent')).string
useragent = 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'
email = request.form.get("email")
url = request.form.get("link")
if bool(Amazon.query.filter_by(email=email, url=url).first()) == True:
return render_template("index.html", error1='You have already inserted this email and URL into the database! Try again with a different product!')
if url == '' or email == '':
return "Please enter the required fields"
priceAlert = float(request.form.get("priceAlert"))
if priceAlert <= 0:
return "Enter a positive number"
if not check_email(email):
return "Invalid Email"
data = (Amazon(email, url, priceAlert, useragent))
db.session.add(data)
db.session.commit()
return render_template("index.html", message1="Success! An email will be sent once the price is at or below your target price! We will send an email every 24 hrs to remind you. To remove your entry from the Price Tracker, please visit the 'Remove Alert' tab!")
return redirect("/")
@app.route("/remove", methods=["GET", "POST"])
def remove():
if request.method == "POST":
url_1 = request.form.get("url")
email_1 = request.form.get("email")
if url == '' or email == '':
return render_template('remove.html', message='Please enter the required fields')
if bool(Amazon.query.filter_by(email=email_1, url=url_1).first()) == True:
Amazon.query.filter_by(email=email_1, url=url_1).delete()
db.session.commit()
return render_template("index.html", message2='The Price Alert has been removed!')
else:
return render_template("remove.html", error='Data not found in Database. Are you sure you had entered the information correctly?')
return render_template("remove.html")
def check_price(url, useragent):
headers = {"User-Agent": useragent, "Referer": url}
page = requests.get(url, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
price = soup.find("span", {"class": "a-offscreen"})
if price == None:
price = soup.find(id="priceblock_saleprice")
if price == None:
price = soup.find(id="priceblock_dealprice")
price = price.get_text().strip()
convertedPrice = (price[1:])
convertedPrice = float(convertedPrice)
return convertedPrice
def send_email(email, link, price):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('[email protected]', 'oscnyfjzyflsdlzb')
remove_link = 'https://ama-zon-price-tracker.herokuapp.com/remove'
subject = 'Amazon Price Tracker: The Price Fell Below Your Target!'
body = f'Your tracked item fell below your target price of {price}. \r\r\nCheck the Amazon link: {link}. \r\r\nTo cancel alerts, visit {remove_link}.'
msg = f'Subject: {subject}\n\n{body}'
server.sendmail('[email protected]', f'{email}', msg)
print('Email has been sent!')
server.quit()
if __name__ == '__main__':
app.run()