fixing mail bug and adding sorting feature
This commit is contained in:
parent
cdb73e2666
commit
fdf51e7966
|
@ -3,13 +3,17 @@ from types import prepare_class
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
import argparse
|
import argparse
|
||||||
import manage_data
|
import manage_data
|
||||||
import datetime
|
|
||||||
|
|
||||||
|
sorting_by_price = True
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--dept", help="french departement number to process")
|
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()
|
args = parser.parse_args()
|
||||||
|
import dotenv
|
||||||
|
import smtplib
|
||||||
|
|
||||||
|
|
||||||
class Pomp(object):
|
class Pomp(object):
|
||||||
|
@ -29,48 +33,78 @@ class Pomp(object):
|
||||||
|
|
||||||
def info(self):
|
def info(self):
|
||||||
string = "---------------------------\n"
|
string = "---------------------------\n"
|
||||||
|
d = {}
|
||||||
for key, value, data, index in zip(
|
for key, value, data, index in zip(
|
||||||
self.tag_list, self.text_list, self.attrib_list, range(len(self.tag_list))
|
self.tag_list, self.text_list, self.attrib_list, range(len(self.tag_list))
|
||||||
):
|
):
|
||||||
|
|
||||||
if key == "services":
|
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":
|
elif key == "prix":
|
||||||
string += f"""{key} : {data['nom']} : {data['valeur']} € (dernière mise à jour {data['maj']})\n"""
|
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 == {}:
|
elif data == {}:
|
||||||
|
d.update({key: value})
|
||||||
string += f"""{key} : {value}\n"""
|
string += f"""{key} : {value}\n"""
|
||||||
else:
|
else:
|
||||||
|
d.update({key: (value, data)})
|
||||||
string += f"""{key} : {value}
|
string += f"""{key} : {value}
|
||||||
{data}\n"""
|
{data}\n"""
|
||||||
return string
|
|
||||||
|
return string, d
|
||||||
|
|
||||||
|
|
||||||
tree = ET.parse("../../data/PrixCarburants_instantane.xml")
|
tree = ET.parse("../../data/PrixCarburants_instantane.xml")
|
||||||
root = tree.getroot()
|
root = tree.getroot()
|
||||||
interesting_pomp = " ".join(
|
# = sorted(interesting_pomp, key=lambda student: student[2])
|
||||||
[Pomp(item).info() for item in root if item.attrib["cp"][:2] == args.dept]
|
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()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
sender = os.getenv("EMAIL")
|
sender = os.getenv("EMAIL")
|
||||||
password = os.getenv("PASSWORD")
|
password = os.getenv("PASSWORD")
|
||||||
|
|
||||||
receivers = [os.getenv("ADRESS_1"), os.getenv("ADRESS_2"), os.getenv("ADRESS_3")]
|
receivers = [os.getenv("ADRESS_1"), os.getenv("ADRESS_2"), os.getenv("ADRESS_3")]
|
||||||
|
|
||||||
smtp_serv = os.getenv("SERVER")
|
smtp_serv = os.getenv("SERVER")
|
||||||
port = 465
|
port = 587
|
||||||
msg = MIMEText(interesting_pomp)
|
from email.message import EmailMessage
|
||||||
|
|
||||||
msg["Subject"] = "Prix carburants de la vienne aujourd'hui"
|
msg = EmailMessage()
|
||||||
msg["From"] = "services@adriorsn.eu"
|
msg.set_content(str(interesting_pomp))
|
||||||
msg["To"] = receivers
|
|
||||||
|
|
||||||
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)
|
server.login(sender, password)
|
||||||
for person in receivers:
|
server.set_debuglevel(1)
|
||||||
server.sendmail(sender, person, msg.as_string())
|
server.send_message(msg)
|
||||||
print("Successfully sent email")
|
|
Loading…
Reference in New Issue