From fdf51e79662f2a611737daf04108d1545411ea9e Mon Sep 17 00:00:00 2001 From: Adrien Date: Sat, 13 Feb 2021 14:13:04 +0100 Subject: [PATCH] fixing mail bug and adding sorting feature --- src/fuel_price_tracker/parsing.py | 72 +++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/src/fuel_price_tracker/parsing.py b/src/fuel_price_tracker/parsing.py index 9b19661..2f76e81 100644 --- a/src/fuel_price_tracker/parsing.py +++ b/src/fuel_price_tracker/parsing.py @@ -3,13 +3,17 @@ from types import prepare_class import xml.etree.ElementTree as ET import argparse import manage_data -import datetime + + +sorting_by_price = True parser = argparse.ArgumentParser() parser.add_argument("--dept", help="french departement number to process") -parser.add_argument("--gas-type", help="king of fuel to process") +parser.add_argument("--gas", help="king of fuel to process") args = parser.parse_args() +import dotenv +import smtplib class Pomp(object): @@ -29,48 +33,78 @@ class Pomp(object): def info(self): string = "---------------------------\n" + d = {} for key, value, data, index in zip( self.tag_list, self.text_list, self.attrib_list, range(len(self.tag_list)) ): + if key == "services": - string += f"""{key} : {', '.join([self.node[index][i].text for i in range(len(self.node[index]))])}\n""" + string += f"""{key} : {', '.join([self.node[index][i].text for i + in range(len(self.node[index]))])}\n""" + d.update( + { + key: { + ", ".join( + [ + self.node[index][i].text + for i in range(len(self.node[index])) + ] + ) + } + } + ) elif key == "prix": string += f"""{key} : {data['nom']} : {data['valeur']} € (dernière mise à jour {data['maj']})\n""" + d.update({key + "_" + data["nom"]: (data["valeur"], data["maj"])}) elif data == {}: + d.update({key: value}) string += f"""{key} : {value}\n""" else: + d.update({key: (value, data)}) string += f"""{key} : {value} {data}\n""" - return string + + return string, d tree = ET.parse("../../data/PrixCarburants_instantane.xml") root = tree.getroot() -interesting_pomp = " ".join( - [Pomp(item).info() for item in root if item.attrib["cp"][:2] == args.dept] -) +# = sorted(interesting_pomp, key=lambda student: student[2]) +interesting_pomp = [ + Pomp(item).info()[1] for item in root if item.attrib["cp"][:2] == args.dept +] + +if sorting_by_price: + interesting_pomp = [ + item for item in interesting_pomp if "prix_" + str(args.gas) in item.keys() + ] + interesting_pomp = sorted( + interesting_pomp, key=lambda dict: dict["prix_" + str(args.gas)] + ) -import dotenv -import smtplib -from email.mime.text import MIMEText dotenv.load_dotenv() sender = os.getenv("EMAIL") password = os.getenv("PASSWORD") + receivers = [os.getenv("ADRESS_1"), os.getenv("ADRESS_2"), os.getenv("ADRESS_3")] smtp_serv = os.getenv("SERVER") -port = 465 -msg = MIMEText(interesting_pomp) +port = 587 +from email.message import EmailMessage -msg["Subject"] = "Prix carburants de la vienne aujourd'hui" -msg["From"] = "services@adriorsn.eu" -msg["To"] = receivers +msg = EmailMessage() +msg.set_content(str(interesting_pomp)) -with smtplib.SMTP_SSL("localhost", port) as server: +msg[ + "Subject" +] = f"Prix carburants ({args.gas}) dans le {args.dept} aujourd'hui (du - chère au + chère)" +msg["From"] = os.getenv("EMAIL") +msg["To"] = ", ".join(receivers) + +with smtplib.SMTP(smtp_serv, port) as server: server.login(sender, password) - for person in receivers: - server.sendmail(sender, person, msg.as_string()) - print("Successfully sent email") \ No newline at end of file + server.set_debuglevel(1) + server.send_message(msg) \ No newline at end of file