diff --git a/System-Automation-Scripts/Amazon-price-tracker/README.md b/System-Automation-Scripts/Amazon-price-tracker/README.md new file mode 100644 index 00000000..ff28392b --- /dev/null +++ b/System-Automation-Scripts/Amazon-price-tracker/README.md @@ -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](../images/lesssecure.png) + +- After this, the script can be run. + +- Using this as an example + +![Image](../images/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](../images/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. + diff --git a/System-Automation-Scripts/Amazon-price-tracker/images/confirm.png b/System-Automation-Scripts/Amazon-price-tracker/images/confirm.png new file mode 100755 index 00000000..7f2bc3b7 Binary files /dev/null and b/System-Automation-Scripts/Amazon-price-tracker/images/confirm.png differ diff --git a/System-Automation-Scripts/Amazon-price-tracker/images/lesssecure.png b/System-Automation-Scripts/Amazon-price-tracker/images/lesssecure.png new file mode 100755 index 00000000..2501a081 Binary files /dev/null and b/System-Automation-Scripts/Amazon-price-tracker/images/lesssecure.png differ diff --git a/System-Automation-Scripts/Amazon-price-tracker/images/single.png b/System-Automation-Scripts/Amazon-price-tracker/images/single.png new file mode 100755 index 00000000..40296991 Binary files /dev/null and b/System-Automation-Scripts/Amazon-price-tracker/images/single.png differ diff --git a/System-Automation-Scripts/Amazon-price-tracker/mail_in_python.py b/System-Automation-Scripts/Amazon-price-tracker/mail_in_python.py new file mode 100644 index 00000000..953d99d9 --- /dev/null +++ b/System-Automation-Scripts/Amazon-price-tracker/mail_in_python.py @@ -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 + diff --git a/System-Automation-Scripts/Amazon-price-tracker/price_logger.txt b/System-Automation-Scripts/Amazon-price-tracker/price_logger.txt new file mode 100644 index 00000000..36a65739 --- /dev/null +++ b/System-Automation-Scripts/Amazon-price-tracker/price_logger.txt @@ -0,0 +1 @@ +The cost of the product is 649.0 at 2020-08-11 19:23:36.359383 diff --git a/System-Automation-Scripts/Amazon-price-tracker/price_tracker.py b/System-Automation-Scripts/Amazon-price-tracker/price_tracker.py new file mode 100644 index 00000000..4e0d5539 --- /dev/null +++ b/System-Automation-Scripts/Amazon-price-tracker/price_tracker.py @@ -0,0 +1,120 @@ +#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) + + +if __name__ == "__main__": + #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) +