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

Amazon product price tracker #142

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions System-Automation-Scripts/Amazon-price-tracker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Amazon Price Tracker ##
- This script built in Python is an Amazon Price Tracker.
- The user enters :
- The URL of the product of which he would like the track the price of.
- His/Her budget for the product.
- His/Her Email credentials.
- The script runs continuously and checks on the price of the product every 12 hours.
- If the price of the product is equal to or below the user's budget, the user receives an email confirmation.
- The price of the product is logged into a file named price_logger.txt every 12 hours.

## Working and Usage ##
- The BeautifulSoup library is used to scrape the price of the product from the Amazon site.
- On Amazon, the prices of products are either expressed as a range or as a single number.
- If the budget is within the range, an email will be sent.
- In the script, headers need to be used to make the get request to the Amazon site.
- 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).
- The Email settings of the user must be configured to operate on less secure mode to facilitate the sending of emails.

![Image](lesssecure.png)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the image routes after shifting these three corresponding images into a separate folder of your directory.


- After this, the script can be run.

- Using this as an example

![Image](single.png)

- 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.

![Image](confirm.png)

- 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.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions System-Automation-Scripts/Amazon-price-tracker/mail_in_python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/python

import smtplib

#The smtp module (Simple Mail Transfer Protocol) enables sending emails in python
#The sender's email must be configured to less secure apps.
#This configuration can be made on visiting account information.
#Under the category security, less secure apps must turned on

def send_confirmation(sender_email, receiver_email, password, price_range):

#Subject of the Email
subject = "Amazon product price "

if len(price_range) == 1:
cost = "The cost of the product is" + str(price_range[0])
else:
cost = "The cost of the product is within the range " + str(price_range[0]) + " and " + str(price_range[1])

#Content of the email
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."

content = "Subject: {}\n\n{}".format(subject, body_of_the_email)

#Specifications of the Email

server = smtplib.SMTP("smtp.gmail.com" , 587)

#Here the Gmail service is used, a different Email service can also be used
#The port 587, across which the email is sent

server.starttls()
server.login(sender_email, password)

#Login is authorised
server.sendmail(sender_email, receiver_email, content)

#Email is sent, prints success on sending the email

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The cost of the product is 649.0 at 2020-08-11 19:23:36.359383
118 changes: 118 additions & 0 deletions System-Automation-Scripts/Amazon-price-tracker/price_tracker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#Imports and dependencies

import requests
import time
from bs4 import BeautifulSoup
from mail_in_python import send_confirmation
import datetime

#This variable is to test the feasibility of the product
feasible = False

#This function is to indicate to the user the currency the product is mentioned in, so the same currency is used for budget.

def currency_used(URL):

#These headers are user specific, look for "my user agent" in the google search bar and replace your user agent here"
headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}

#Response got from the request sent to the desired product URL
response = requests.get(URL, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")

currency_symbols = {'€' : "Euro", '£' : "Pound", '$' : "Dollars", "¥" : "Renminbi", "HK$" : "Hong Kong Dollars", "₹" : "Rupeees"}

#Using web-scraping the price of the product is found
price = soup.find(id="priceblock_ourprice").get_text()
currency = ""

for symbol in currency_symbols:
if symbol in price:
currency = currency_symbols[symbol]
price = price.replace(symbol, "")

#The currency of the product is stored here"
return(currency)


def price_check(URL, budget):

headers = {"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0'}
feasible = False
response = requests.get(URL, headers=headers)
soup = BeautifulSoup(response.content, "html.parser")
currency_symbols = {'€' : "Euro", '£' : "Pound", '$' : "Dollars", "¥" : "Renminbi", "HK$" : "Hong Kong Dollars", "₹" : "Rupeees"}
price = soup.find(id="priceblock_ourprice").get_text()
currency = ""

for symbol in currency_symbols:
if symbol in price:
currency = currency_symbols[symbol]
price = price.replace(symbol, "")

#string is converted to a number and extra characters are removed

if "," in price:
price = price.replace(",", "")

#The price for some products is expressed as a range, thus the following is performed
price = price = price.split("-")
price_now = []
if len(price) == 2:
lower, upper = float(price[0].strip('\xa0')) , float(price[1].strip('\xa0'))
price_now.append(lower)
price_now.append(upper)
price_range = [*(range(int(lower), int(upper+1)))]
if budget in price_range:
feasible = True
else:
price = float(price[0].strip("\xa0"))
if budget >= int(price):
feasible = True
price_now.append(price)
return(feasible, price_now)

#Enter the URL of the amazon product
URL = input("Enter the URL of the Amazon product : ")

print("The price of the product is expressed in " + (currency_used(URL)))

#Enter your budget in the same currency as mentioned above
budget = int(input("Enter your budget for the product in same unit of currency as mentioned above: "))

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")

#Since a mail is sent from this script, security mode has to be disabled
print("Also ensure that security mode is switched off in your email settings")

sender_email = input("Enter the sender's Email-ID : ")
receiver_email = input("Enter the receiver's Email-ID : ")
password = input("Enter the sender's password : ")

feasible, price_range = price_check(URL, budget)

#The price_check function will execute every 12 hours to check, the prices will also be logged so that a comparison can be made
while not feasible:
feasible, price_range = price_check(URL, budget)

if len(price_range) == 1:
cost = "The cost of the product is " + str(price_range[0])
else:
cost = "The cost of the product is within the range " + str(price_range[0]) + " and " + str(price_range[1])

current_time = datetime.datetime.now()

#Logging records into the text file.
with open("price_logger.txt" , "w") as file:
file.write(cost + " at " + str(current_time))
file.write("\n")

if feasible:
break
else:
#Sleeps for 12 hours and then checks for the same
time.sleep(43200)

#The user will be notified through an email
send_confirmation(sender_email, receiver_email, password, price_range)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.