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