|
| 1 | +# Developed and maintained by https://github.com/sarthak1905 |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +from requests_html import HTMLSession |
| 4 | +import os |
| 5 | +import time |
| 6 | +import smtplib |
| 7 | +import ssl |
| 8 | + |
| 9 | + |
| 10 | +class Scraper: |
| 11 | + |
| 12 | + # Initializes the scraper C3PO |
| 13 | + def __init__(self, url, budget, u_email): |
| 14 | + |
| 15 | + # Attributes about product |
| 16 | + self.url = url |
| 17 | + self.budget = budget |
| 18 | + |
| 19 | + # Setting user email |
| 20 | + self.u_email = u_email |
| 21 | + |
| 22 | + # Attributes about scraping |
| 23 | + self.session = HTMLSession() |
| 24 | + self.webpage = self.session.get(self.url).content |
| 25 | + self.parser = 'lxml' |
| 26 | + self.soup = BeautifulSoup(self.webpage, self.parser) |
| 27 | + |
| 28 | + # Prints the object |
| 29 | + def __str__(self): |
| 30 | + return self.soup.prettify() |
| 31 | + |
| 32 | + # Stores the title of the product |
| 33 | + def get_title(self): |
| 34 | + temp_title = self.soup.find('span', id='productTitle').text.strip() |
| 35 | + temp_list_title = [] |
| 36 | + for x in temp_title: |
| 37 | + if x == '(': |
| 38 | + break |
| 39 | + temp_list_title.append(x) |
| 40 | + self.product_title = ''.join(temp_list_title) |
| 41 | + return self.product_title |
| 42 | + |
| 43 | + # Stores the price of the product after filtering the string and |
| 44 | + # converting it to an integer |
| 45 | + def get_price(self): |
| 46 | + price_raw = self.soup.find( |
| 47 | + 'span', id='priceblock_ourprice').text.strip() |
| 48 | + price_filtered = price_raw[2:len(price_raw) - 3] |
| 49 | + self.product_price = int( |
| 50 | + ''.join([x for x in price_filtered if x != ','])) |
| 51 | + return |
| 52 | + |
| 53 | + # Prints product title |
| 54 | + def print_title(self): |
| 55 | + print(self.product_title) |
| 56 | + return |
| 57 | + |
| 58 | + # Prints product price |
| 59 | + def print_price(self): |
| 60 | + print(self.product_price) |
| 61 | + return |
| 62 | + |
| 63 | + # Checks if the price of the product is below the budget |
| 64 | + def is_below_budget(self): |
| 65 | + if self.product_price <= self.budget: |
| 66 | + return True |
| 67 | + else: |
| 68 | + return False |
| 69 | + |
| 70 | + # Runs the scraper |
| 71 | + def run(self): |
| 72 | + |
| 73 | + self.get_title() |
| 74 | + self.get_price() |
| 75 | + self.alert = self.is_below_budget() |
| 76 | + self.status = False |
| 77 | + if self.alert: |
| 78 | + self.status = self.send_email() |
| 79 | + return self.status |
| 80 | + |
| 81 | + # Sends an email when the condition is satisfied. Under testing! |
| 82 | + def send_email(self): |
| 83 | + |
| 84 | + # Attributes for email sending |
| 85 | + port = 587 |
| 86 | + smtp_server = 'smtp.gmail.com' |
| 87 | + self.email = str(os.environ.get('DEVELOPER_MAIL')) |
| 88 | + self.app_pw = str(os.environ.get('DEVELOPER_PASS')) |
| 89 | + |
| 90 | + # Message details |
| 91 | + subject = f'The price of {self.get_title()} is within your budget!' |
| 92 | + |
| 93 | + body_start = """Hey there!\n |
| 94 | + The price is now within your budget. Here is the link, buy it now!\n""" |
| 95 | + body_mid = self.url |
| 96 | + body_end = '\n\nRegards\nYour friendly neighbourhood programmer' |
| 97 | + body = str(body_start) + str(body_mid) + str(body_end) |
| 98 | + |
| 99 | + message = f"Subject: {subject}\n\n{body}" |
| 100 | + |
| 101 | + # Establishing server |
| 102 | + context = ssl.create_default_context() |
| 103 | + self.server = smtplib.SMTP(smtp_server, port) |
| 104 | + |
| 105 | + # Mail sending |
| 106 | + self.server.ehlo() |
| 107 | + self.server.starttls(context=context) |
| 108 | + self.server.ehlo() |
| 109 | + self.server.login(self.email, self.app_pw) |
| 110 | + |
| 111 | + self.server.sendmail(self.email, self.u_email, message) |
| 112 | + |
| 113 | + print("Email sent successfully!") |
| 114 | + self.server.quit() |
| 115 | + return True |
| 116 | + |
| 117 | + |
| 118 | +def main(): |
| 119 | + url = input( |
| 120 | + "Paste the link of the Amazon product:") |
| 121 | + budget = int(input("Enter you budget price:")) |
| 122 | + u_email = input("Enter your email:") |
| 123 | + inp_str = ("How frequuently would you like to check the price?" |
| 124 | + "\n1.Every hour\n2.Every 3 hours\n3.Every 6 hours" |
| 125 | + "\nEnter your choice(default is 6 hours):") |
| 126 | + time_choice = int(input(inp_str)) |
| 127 | + if time_choice == 1: |
| 128 | + time_delay = 60 * 60 |
| 129 | + elif time_choice == 2: |
| 130 | + time_delay = 3 * 60 * 60 |
| 131 | + else: |
| 132 | + time_delay = 6 * 60 * 60 |
| 133 | + msg = ( |
| 134 | + "Great! Now just sit back and relax." |
| 135 | + "Minimize this program and be sure " |
| 136 | + "that it is running.\nAdditionally, ensure that there" |
| 137 | + "is stable internet connection " |
| 138 | + "during the time this program runs.\nIf the price of the " |
| 139 | + "product falls within your budget, " |
| 140 | + "you will recieve an email regarding the same and this" |
| 141 | + "program will auto-close.\nThank you for using " |
| 142 | + "C3PO scraper! Beep-bop bop-beep.") |
| 143 | + print(msg) |
| 144 | + c3po = Scraper(url, budget, u_email) |
| 145 | + while True: |
| 146 | + if c3po.run(): |
| 147 | + break |
| 148 | + time.sleep(time_delay) |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == '__main__': |
| 152 | + main() |
0 commit comments