Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit e8260fe

Browse files
authored
Merge pull request #142 from Namyalg/Amazon-product-price-tracker
Amazon product price tracker
2 parents 6afb0eb + 985ebe0 commit e8260fe

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
- If the budget is within the range, an email will be sent.
15+
- In the script, headers need to be used to make the get request to the Amazon site.
16+
- In place of headers, the user must replace it with the result of **my user agent** must be used instead (this can be looked up in Google).
17+
- The Email settings of the user must be configured to operate on less secure mode to facilitate the sending of emails.
18+
19+
![Image](../images/lesssecure.png)
20+
21+
- After this, the script can be run.
22+
23+
- Using this as an example
24+
25+
![Image](../images/single.png)
26+
27+
- 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.
28+
29+
![Image](../images/confirm.png)
30+
31+
- 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.
32+
Loading
Loading
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+
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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
76+
if __name__ == "__main__":
77+
#Enter the URL of the amazon product
78+
URL = input("Enter the URL of the Amazon product : ")
79+
80+
print("The price of the product is expressed in " + (currency_used(URL)))
81+
82+
#Enter your budget in the same currency as mentioned above
83+
budget = int(input("Enter your budget for the product in same unit of currency as mentioned above: "))
84+
85+
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")
86+
87+
#Since a mail is sent from this script, security mode has to be disabled
88+
print("Also ensure that security mode is switched off in your email settings")
89+
90+
sender_email = input("Enter the sender's Email-ID : ")
91+
receiver_email = input("Enter the receiver's Email-ID : ")
92+
password = input("Enter the sender's password : ")
93+
94+
feasible, price_range = price_check(URL, budget)
95+
96+
#The price_check function will execute every 12 hours to check, the prices will also be logged so that a comparison can be made
97+
while not feasible:
98+
feasible, price_range = price_check(URL, budget)
99+
100+
if len(price_range) == 1:
101+
cost = "The cost of the product is " + str(price_range[0])
102+
else:
103+
cost = "The cost of the product is within the range " + str(price_range[0]) + " and " + str(price_range[1])
104+
105+
current_time = datetime.datetime.now()
106+
107+
#Logging records into the text file.
108+
with open("price_logger.txt" , "w") as file:
109+
file.write(cost + " at " + str(current_time))
110+
file.write("\n")
111+
112+
if feasible:
113+
break
114+
else:
115+
#Sleeps for 12 hours and then checks for the same
116+
time.sleep(43200)
117+
118+
#The user will be notified through an email
119+
send_confirmation(sender_email, receiver_email, password, price_range)
120+

0 commit comments

Comments
 (0)