fixing mail bug and adding sorting feature

This commit is contained in:
Adrien 2021-02-13 14:13:04 +01:00
parent cdb73e2666
commit fdf51e7966

View File

@ -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")
server.set_debuglevel(1)
server.send_message(msg)