-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpriceupdate.py
88 lines (76 loc) · 2.48 KB
/
priceupdate.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
import requests
from bs4 import BeautifulSoup
import smtplib
import time
# Declarations
URL = ''
fromEmail = ''
# Generate App password (mail) for 2FA enabled accounts, for others disable secure login from Account Settings->Security
fromEmailPwd = ''
toEmail = ''
updateAfter = 0
price = 0.0
title = ''
headers = {
"User Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}
def main():
# Product URL
global URL
URL = input()
global fromEmail
fromEmail = input()
global fromEmailPwd
fromEmailPwd = input()
global toEmail
toEmail = input()
global updateAfter
updateAfter = int(input())
global price
page = requests.get(URL, headers=headers)
soup = BeautifulSoup(page.content, 'html.parser')
global title
title = soup.find(id="productTitle").get_text().strip()
global price
# Get the price when the program runs for the first time
price = float(soup.find(id="priceblock_ourprice").get_text().strip()[
2:7].replace(',', '.'))
time.sleep(10)
# Start monitoring
if(updateAfter!=0):
while(True):
priceVariationCheck()
time.sleep(60*60*updateAfter)
else:
priceVariationCheck()
def priceVariationCheck():
newPrice = updateprice()
global price
if(newPrice < price):
price = newPrice
mailer(newPrice)
def updateprice():
page = requests.get(URL, headers=headers)
# If response is 200 then:
# Parse HTML as page.content is still nascent
soup = BeautifulSoup(page.content, 'html.parser')
# Get necessary HTML items
priceNow = soup.find(id="priceblock_ourprice").get_text().strip()
# Since we need to compare prices, we need the price in integer or float
# I chose float since it was easiet to just replace ',' with '.' and make it float
priceNum = float(priceNow[2:7].replace(',', '.'))
return priceNum
# mailer() method uses Gmail's SMTP
def mailer(currentPrice):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromEmail, fromEmailPwd)
subject = f'Prices Down for {title}'
body = f'Check the Amazon link: {URL}\nPrice Reduced By: Rs.{price-currentPrice}\nCurrent Price: Rs.{currentPrice}'
message = f'Subject: {subject}\n\n{body}'
server.sendmail(fromEmail,
toEmail, message)
server.quit()
if __name__ == "__main__":
main()