|
| 1 | +import requests |
| 2 | +from bs4 import BeautifulSoup as bs |
| 3 | +import smtplib |
| 4 | +import time |
| 5 | +headers = {'User-Agent': 'Mozilla/5.0 Chrome/86.0.4240.75'} |
| 6 | + |
| 7 | +def sendMail(title): |
| 8 | + '''Send Email''' |
| 9 | + server = smtplib.SMTP('smtp.gmail.com', 587) |
| 10 | + server.ehlo() |
| 11 | + server.starttls() |
| 12 | + server.ehlo() |
| 13 | + server.login(MY_EMAIL, MY_APP_PASSWORD) |
| 14 | + subject = 'Change in price detected for ' + title |
| 15 | + print(subject) |
| 16 | + body = 'Click the link to go to the product page \n' + PRODUCT_URL |
| 17 | + msg = f"Subject: {subject}\n\n{body}" |
| 18 | + print(msg) |
| 19 | + server.sendmail(MY_EMAIL, RECEIVER_EMAIL, msg) |
| 20 | + print('Email Sent') |
| 21 | + server.quit() |
| 22 | + |
| 23 | + |
| 24 | +def priceCheck(): |
| 25 | + '''Price checking function''' |
| 26 | + page = requests.get(PRODUCT_URL, headers=headers) |
| 27 | + soup = bs(page.content, 'html.parser') |
| 28 | + # title from 'B_NuCI' class |
| 29 | + title = soup.find("span", {"class": "B_NuCI"}).get_text()[0:8] + '..' |
| 30 | + print(title) |
| 31 | + # price from '_30jeq3 _16Jk6d' class, |
| 32 | + raw_price = soup.find("div", {"class": "_30jeq3 _16Jk6d"}) |
| 33 | + # removing unnecessary characters from the price. |
| 34 | + price = float(raw_price.get_text()[1:].replace(',', '')) |
| 35 | + print(price) |
| 36 | + print(THRESHHOLD) |
| 37 | + # If the price falls below threshold, send an email |
| 38 | + if(price < THRESHHOLD): |
| 39 | + sendMail(title) |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +PRODUCT_URL = input('Enter the product url:') |
| 44 | +THRESHHOLD = float(input('Enter the desired price:')) |
| 45 | +MY_EMAIL = input('Enter the email address that will be used to send alerts:') |
| 46 | +MY_APP_PASSWORD = input('Enter password:') |
| 47 | +RECEIVER_EMAIL = input('Enter the receiver email:') |
| 48 | +CHECK_AGAIN = int(input('Enter the time between checks in minutes:')) |
| 49 | + |
| 50 | +if (PRODUCT_URL == '' or MY_APP_PASSWORD == '' or MY_EMAIL == '' or RECEIVER_EMAIL == ''): |
| 51 | + print('VALUES MISSING! TRY AGAIN') |
| 52 | + exit() |
| 53 | +while(True): |
| 54 | + priceCheck() |
| 55 | + time.sleep(CHECK_AGAIN*60) |
0 commit comments