forked from harshilp24/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
71 lines (53 loc) · 2.61 KB
/
test.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
# import required files and modules
import requests
from bs4 import BeautifulSoup
import smtplib
import time
# set the headers and user string
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
}
# send a request to fetch HTML of the page
response = requests.get('https://www.amazon.in/Lenovo-Legion-Windows-Graphics-81YU002AIN/dp/B08FJCRGPR/ref=sr_1_1_sspa?dchild=1&keywords=lenovo+legion&qid=1628964644&s=computers&sr=1-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExT0FFWlRGWU5HQUZBJmVuY3J5cHRlZElkPUEwNTY4NTgwMUtFUDNZQkhaM0NOVCZlbmNyeXB0ZWRBZElkPUEwMjA5NTE3MVlSM0xDRFpXWDZDWiZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=', headers=headers)
# create the soup object
soup = BeautifulSoup(response.content, 'html.parser')
# change the encoding to utf-8
soup.encode('utf-8')
#print(soup.prettify())
# function to check if the price has dropped below 20,000
def check_price():
title = soup.find(id= "productTitle").get_text()
price = soup.find(id = "priceblock_ourprice").get_text().replace(',', '').replace('₹', '').replace(' ', '').strip()
#print(price)
#converting the string amount to float
converted_price = float(price[0:6])
print(converted_price)
if(converted_price < 181000):
send_mail()
else :
print("You have to wait,The prices are still high")
#using strip to remove extra spaces in the title
print(title.strip())
# function that sends an email if the prices fell down
def send_mail():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('[email protected]', 'rlxnwitdhtsxgsot')
subject = 'Hey Tushar,Hurry Up ! Prices Have Fallen Down'
body = "Check out the amazon link here -> https://www.amazon.in/Lenovo-Legion-Windows-Graphics-81YU002AIN/dp/B08FJCRGPR/ref=sr_1_1_sspa?dchild=1&keywords=lenovo+legion&qid=1628964644&s=computers&sr=1-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExT0FFWlRGWU5HQUZBJmVuY3J5cHRlZElkPUEwNTY4NTgwMUtFUDNZQkhaM0NOVCZlbmNyeXB0ZWRBZElkPUEwMjA5NTE3MVlSM0xDRFpXWDZDWiZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU="
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
msg
)
#print a message to check if the email has been sent
print('Hey,Email has been sent!')
# quit the server
server.quit()
#loop that allows the program to regularly check for prices
while(True):
check_price()
time.sleep(60 * 60)