Allow sending emails

This commit is contained in:
2024-01-30 20:23:01 +01:00
parent b7f71ab9f2
commit d6da261c17
3 changed files with 44 additions and 8 deletions

26
mail_sender.py Normal file
View File

@@ -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()

View File

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

6
utils.py Normal file
View File

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