Skip to content

Commit d8e0036

Browse files
authored
Merge pull request #195 from Namyalg/Product-price-tracker
Scripts for Amazon product price tracker added
2 parents 07e78b4 + 0a6a205 commit d8e0036

12 files changed

+204
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## Amazon Price Tracker ##
2+
- This script built in Python is an Amazon Price Tracker.
3+
- The user enters :
4+
- The URL of the product of which he would like the track the price of.
5+
- His/Her budget for the product.
6+
- His/Her Email credentials.
7+
- The script runs continuously and checks on the price of the product every 12 hours.
8+
- If the price of the product is equal to or below the user's budget, the user receives an email confirmation.
9+
- The price of the product is logged into a file named price_logger.txt every 12 hours.
10+
11+
## Working and Usage ##
12+
- The BeautifulSoup library is used to scrape the price of the product from the Amazon site.
13+
- On Amazon, the prices of products are either expressed as a range or as a single number.
14+
15+
![Image](single.png)
16+
17+
18+
![Image](range.png)
19+
20+
- If the budget is within the range, an email will be sent.
21+
- In the script, headers need to be used to make the get request to the Amazon site.
22+
- In place of headers, the user must replace it with the result of **my user agent** must be used instead.
23+
24+
![Image](myagent.png)
25+
26+
- The Email settings of the user must be configured to operate on less secure mode to facilitate the sending of emails.
27+
28+
![Image](lesssecure.png)
29+
30+
- After this, the script can be run.
31+
32+
![Image](mail.png)
33+
34+
![Image](email.png)
35+
36+
- Using this as an example
37+
38+
![Image](single.png)
39+
40+
- The user enters 700 rupees as the budget, as the price is lesser than the budget the following email is sent, else the program continues to run till the condition is satisfied.
41+
42+
![Image](confirm.png)
43+
44+
- The prices are also logged into the file price_logger.txt as shown, so the user will have an account of the changes the price underwent.
45+
46+
![Image](change.png)
1.93 KB
Loading
4.41 KB
Loading
2.27 KB
Loading
14.1 KB
Loading

Amazon-product-price-tracker/mail.png

25.8 KB
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/python
2+
3+
import smtplib
4+
5+
#The smtp module (Simple Mail Transfer Protocol) enables sending emails in python
6+
#The sender's email must be configured to less secure apps.
7+
#This configuration can be made on visiting account information.
8+
#Under the category security, less secure apps must turned on
9+
10+
def send_confirmation(sender_email, receiver_email, password, price_range):
11+
12+
#Subject of the Email
13+
subject = "Amazon product price "
14+
15+
if len(price_range) == 1:
16+
cost = "The cost of the product is" + str(price_range[0])
17+
else:
18+
cost = "The cost of the product is within the range " + str(price_range[0]) + " and " + str(price_range[1])
19+
20+
#Content of the email
21+
body_of_the_email = "Hello, This is to inform you that the price of the product you were looking for on Amazon is well-within your budget." + cost + " You can buy it right away."
22+
23+
content = "Subject: {}\n\n{}".format(subject, body_of_the_email)
24+
25+
#Specifications of the Email
26+
27+
server = smtplib.SMTP("smtp.gmail.com" , 587)
28+
29+
#Here the Gmail service is used, a different Email service can also be used
30+
#The port 587, across which the email is sent
31+
32+
server.starttls()
33+
server.login(sender_email, password)
34+
35+
#Login is authorised
36+
server.sendmail(sender_email, receiver_email, content)
37+
38+
#Email is sent, prints success on sending the email
39+
8.94 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The cost of the product is 649.0 at 2020-08-11 19:23:36.359383
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#Imports and dependencies
2+
3+
import requests
4+
import time
5+
from bs4 import BeautifulSoup
6+
from mail_in_python import send_confirmation
7+
import datetime
8+
9+
#This variable is to test the feasibility of the product
10+
feasible = False
11+
12+
#This function is to indicate to the user the currency the product is mentioned in, so the same currency is used for budget.
13+
14+
def currency_used(URL):
15+
16+
#These headers are user specific, look for "my user agent" in the google search bar and replace your user agent here"
17+
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
18+
19+
#Response got from the request sent to the desired product URL
20+
response = requests.get(URL, headers=headers)
21+
soup = BeautifulSoup(response.content, "html.parser")
22+
23+
currency_symbols = {'€' : "Euro", '£' : "Pound", '$' : "Dollars", "¥" : "Renminbi", "HK$" : "Hong Kong Dollars", "₹" : "Rupeees"}
24+
25+
#Using web-scraping the price of the product is found
26+
price = soup.find(id="priceblock_ourprice").get_text()
27+
currency = ""
28+
29+
for symbol in currency_symbols:
30+
if symbol in price:
31+
currency = currency_symbols[symbol]
32+
price = price.replace(symbol, "")
33+
34+
#The currency of the product is stored here"
35+
return(currency)
36+
37+
38+
def price_check(URL, budget):
39+
40+
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
41+
feasible = False
42+
response = requests.get(URL, headers=headers)
43+
soup = BeautifulSoup(response.content, "html.parser")
44+
currency_symbols = {'€' : "Euro", '£' : "Pound", '$' : "Dollars", "¥" : "Renminbi", "HK$" : "Hong Kong Dollars", "₹" : "Rupeees"}
45+
price = soup.find(id="priceblock_ourprice").get_text()
46+
currency = ""
47+
48+
for symbol in currency_symbols:
49+
if symbol in price:
50+
currency = currency_symbols[symbol]
51+
price = price.replace(symbol, "")
52+
53+
#string is converted to a number and extra characters are removed
54+
55+
if "," in price:
56+
price = price.replace(",", "")
57+
58+
#The price for some products is expressed as a range, thus the following is performed
59+
price = price = price.split("-")
60+
price_now = []
61+
if len(price) == 2:
62+
lower, upper = float(price[0].strip('\xa0')) , float(price[1].strip('\xa0'))
63+
price_now.append(lower)
64+
price_now.append(upper)
65+
price_range = [*(range(int(lower), int(upper+1)))]
66+
if budget in price_range:
67+
feasible = True
68+
else:
69+
price = float(price[0].strip("\xa0"))
70+
if budget >= int(price):
71+
feasible = True
72+
price_now.append(price)
73+
return(feasible, price_now)
74+
75+
#Enter the URL of the amazon product
76+
URL = input("Enter the URL of the Amazon product : ")
77+
78+
print("The price of the product is expressed in " + (currency_used(URL)))
79+
80+
#Enter your budget in the same currency as mentioned above
81+
budget = int(input("Enter your budget for the product in same unit of currency as mentioned above: "))
82+
83+
print("Please enter your email details as asked, when the price of the product is below or equal to your budget, you will receive an email conformation")
84+
85+
#Since a mail is sent from this script, security mode has to be disabled
86+
print("Also ensure that security mode is switched off in your email settings")
87+
88+
sender_email = input("Enter the sender's Email-ID : ")
89+
receiver_email = input("Enter the receiver's Email-ID : ")
90+
password = input("Enter the sender's password : ")
91+
92+
feasible, price_range = price_check(URL, budget)
93+
94+
#The price_check function will execute every 12 hours to check, the prices will also be logged so that a comparison can be made
95+
while not feasible:
96+
feasible, price_range = price_check(URL, budget)
97+
98+
if len(price_range) == 1:
99+
cost = "The cost of the product is " + str(price_range[0])
100+
else:
101+
cost = "The cost of the product is within the range " + str(price_range[0]) + " and " + str(price_range[1])
102+
103+
current_time = datetime.datetime.now()
104+
105+
#Logging records into the text file.
106+
with open("price_logger.txt" , "w") as file:
107+
file.write(cost + " at " + str(current_time))
108+
file.write("\n")
109+
110+
if feasible:
111+
break
112+
else:
113+
#Sleeps for 12 hours and then checks for the same
114+
time.sleep(43200)
115+
116+
#The user will be notified through an email
117+
send_confirmation(sender_email, receiver_email, password, price_range)
118+
14.3 KB
Loading
10.2 KB
Loading

0 commit comments

Comments
 (0)