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

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)