-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceTracker.py
More file actions
63 lines (49 loc) · 2.36 KB
/
PriceTracker.py
File metadata and controls
63 lines (49 loc) · 2.36 KB
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
import requests
import datetime
from bs4 import BeautifulSoup
import smtplib
class tracker:
def __init__(self,args=None):
self.URL = args['URL']
self.WEBSITE = self.URL.split("/")[2]
self.header = args['Header']
self.priceThreshold = args['PriceThreshold']
self.EmailAccount = args['Email']
self.two_step_password = args['Password']
self.RecieverEmail = args['Reciever'] if(args['Reciever'] is not "") else self.EmailAccount
def getData(self):
return self.priceThreshold,self.EmailAccount,self.two_step_password,self.RecieverEmail
def checkPrice(self):
req = requests.get(self.URL,headers=self.header).text
soup = BeautifulSoup(req,'html.parser')
# You can Inspect element the product title and price
# if you want to run this script fro any other E-Commerce Website
if(self.WEBSITE == "www.amazon.in"):
productTitle = soup.find(id = "productTitle").get_text().strip()
productPrice = float(soup.find(id = "priceblock_ourprice").get_text().strip()[2:7].replace(',',''))
elif(self.WEBSITE == "www.flipkart.com"):
productTitle = self.URL.split("/")[3].replace("-"," ")
productPrice = float(soup.findAll("div", {"class": "_1vC4OE _3qQ9m1"})[0].text[1:].replace(",",""))
else:
return "WRONG URL"
if(productPrice <= self.priceThreshold):
self.sendMail(productTitle,productPrice)
return "Process Completed at "+str(datetime.datetime.now())
def sendMail(self,title,price):
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(self.EmailAccount,self.two_step_password)
subject = 'Price Fall for '+ title
body = title + " \nis now currently available for INR " + str(price)\
+ "\n\nCheck the "+self.WEBSITE.split(".")[1]+" Link \n" + self.URL \
+"\n\nThis action was generated by a script.\n\n\nFor more info visit \
\nhttps://github.com/ayushbasak/AmazonPriceTracker"
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
self.EmailAccount,
self.RecieverEmail,
msg
)
server.quit()