From d6da261c17741940748881bab547d818b3d700d4 Mon Sep 17 00:00:00 2001 From: Jakub Knetl Date: Tue, 30 Jan 2024 20:23:01 +0100 Subject: [PATCH] Allow sending emails --- mail_sender.py | 26 ++++++++++++++++++++++++++ scrape.py | 20 ++++++++++++-------- utils.py | 6 ++++++ 3 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 mail_sender.py create mode 100644 utils.py diff --git a/mail_sender.py b/mail_sender.py new file mode 100644 index 0000000..a17b3e3 --- /dev/null +++ b/mail_sender.py @@ -0,0 +1,26 @@ +import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +from utils import format_product_table + +class EmailSender: + def __init__(self, from_email, password): + self.from_email = from_email + self.password = password + def send_email(self, to_email, product_info): + + msg = MIMEMultipart() + msg['From'] = self.from_email + msg['To'] = to_email + msg['Subject'] = 'Items in action' + + body = format_product_table(product_info) + msg.attach(MIMEText(body, 'plain')) + + server = smtplib.SMTP('smtp.gmail.com', 587) + server.starttls() + server.login(self.from_email, self.password) + text = msg.as_string() + server.sendmail(self.from_email, to_email, text) + server.quit() diff --git a/scrape.py b/scrape.py index e02ed6f..ac92095 100644 --- a/scrape.py +++ b/scrape.py @@ -1,7 +1,10 @@ +import os +import re import json import requests from bs4 import BeautifulSoup -from tabulate import tabulate +from mail_sender import EmailSender +from utils import format_product_table def fetch_product_info(urls): product_info = [] @@ -15,7 +18,7 @@ def fetch_product_info(urls): original_price_element = soup.find('span', class_='m-price__rrp') original_price = original_price_element.text.strip() if original_price_element else "-" discount_element = soup.find('div', class_='m-price__label') - discount = discount_element.text.strip() if discount_element else "-" + discount = int(re.findall(r'\d+', discount_element.text.strip())[0]) if discount_element else 0 product_info.append({ "name": product_name, @@ -29,10 +32,6 @@ def fetch_product_info(urls): return product_info -def print_product_table(product_info): - headers = ["Product Name", "Price", "Discount", "original price", "URL"] - table_data = [[info["name"], info["price"], info["discount"], info["originalPrice"], info["url"]] for info in product_info] - print(tabulate(table_data, headers=headers, tablefmt="grid")) def save_to_json(product_info, output_file): with open(output_file, 'w') as f: @@ -46,5 +45,10 @@ if __name__ == "__main__": urls = [line.strip() for line in f.readlines()] product_info = fetch_product_info(urls) - print_product_table(product_info) - save_to_json(product_info, output_file) \ No newline at end of file + print(format_product_table(product_info)) + save_to_json(product_info, output_file) + + products_on_sale = [product for product in product_info if product["discount"] > 0] + + sender = EmailSender(os.environ["SCRAPER_SMTP_USER"], os.environ["SCRAPER_SMTP_PASSWORD"]) + sender.send_email(os.environ["SCRAPER_SMTP_USER"], products_on_sale) diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..2f99d2c --- /dev/null +++ b/utils.py @@ -0,0 +1,6 @@ +from tabulate import tabulate + +def format_product_table(product_info): + headers = ["Product Name", "Price", "Discount", "original price", "URL"] + table_data = [[info["name"], info["price"], info["discount"], info["originalPrice"], info["url"]] for info in product_info] + return tabulate(table_data, headers=headers, tablefmt="grid")