mirror of
https://github.com/Fare-spec/get_ovh_bills.git
synced 2025-12-07 10:20:36 +00:00
add some logging errors that will be send by email
This commit is contained in:
62
main.py
62
main.py
@@ -1,12 +1,13 @@
|
||||
import os
|
||||
import mail as ml
|
||||
from datetime import date, datetime, time
|
||||
from datetime import date, datetime
|
||||
import dotenv
|
||||
import ovh
|
||||
import fetcher as ft
|
||||
from urllib.request import urlretrieve
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
import traceback
|
||||
import sqlite3
|
||||
|
||||
# --- Configuration du logging ---
|
||||
@@ -69,6 +70,21 @@ def get_conn():
|
||||
raise
|
||||
|
||||
|
||||
def send_error_mail(error_msg):
|
||||
try:
|
||||
ml.send_email(
|
||||
subject="[OVH_FACTURES] ERREUR",
|
||||
content=f"<pre>{error_msg}</pre>",
|
||||
email_from=EMAIL,
|
||||
email_password=EMAIL_PASSWORD,
|
||||
smpt_port=SMTP_PORT,
|
||||
smtp_mail_address=SMTP_MAIL_ADDRESS,
|
||||
email_to=EMAIL_TO,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def add_entries_to_db(entries: list[tuple[str, int]], conn):
|
||||
"""
|
||||
Insère en lot des paires (bill_id, bill_year) dans la table 'bills' avec gestion de conflit sur bill_id.
|
||||
@@ -87,6 +103,7 @@ def add_entries_to_db(entries: list[tuple[str, int]], conn):
|
||||
logger.info("Insertion batch dans 'bills' validée")
|
||||
except Exception as e:
|
||||
logger.exception("Échec d'insertion batch dans 'bills': %s", e)
|
||||
send_error_mail(traceback.format_exc())
|
||||
raise
|
||||
|
||||
|
||||
@@ -102,6 +119,7 @@ def get_entries_from_db(conn) -> set[str]:
|
||||
return {row[0] for row in rows}
|
||||
except Exception as e:
|
||||
logger.exception("Échec de lecture des bill_id depuis 'bills': %s", e)
|
||||
send_error_mail(traceback.format_exc())
|
||||
raise
|
||||
|
||||
|
||||
@@ -146,6 +164,8 @@ def indexer(ids: list[str]) -> list[str]:
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Impossible de récupérer le json pour %s : %s", bill_id, e)
|
||||
|
||||
send_error_mail(traceback.format_exc())
|
||||
continue
|
||||
bill_year = datetime.fromisoformat(meta["date"]).year
|
||||
if bill_year == YEAR:
|
||||
@@ -172,6 +192,8 @@ def get_ids() -> list[str]:
|
||||
)
|
||||
except ovh.exceptions.APIError as e:
|
||||
logger.error("Échec récupération des IDs de factures : %s", e)
|
||||
|
||||
send_error_mail(traceback.format_exc())
|
||||
raise RuntimeError(f"Échec de la récupération des IDs de factures : {e}") from e
|
||||
|
||||
|
||||
@@ -189,6 +211,8 @@ def get_bill(bill_id: str) -> dict:
|
||||
)
|
||||
except ovh.exceptions.APIError as e:
|
||||
logger.error("Échec récupération de la facture %s : %s", bill_id, e)
|
||||
|
||||
send_error_mail(traceback.format_exc())
|
||||
raise RuntimeError(
|
||||
f"Échec de la récupération de la facture {bill_id} : {e}"
|
||||
) from e
|
||||
@@ -211,6 +235,7 @@ def save_pdf(bill: dict) -> None:
|
||||
logger.info("Facture %s sauvegardée dans %s", bill["billId"], dest)
|
||||
except Exception as e:
|
||||
logger.error("Impossible de télécharger la facture %s : %s", bill["billId"], e)
|
||||
send_error_mail(traceback.format_exc())
|
||||
raise
|
||||
|
||||
|
||||
@@ -224,24 +249,25 @@ if __name__ == "__main__":
|
||||
for bill_id in ids_candidats:
|
||||
bills_json.append((bill_id, get_bill(bill_id)))
|
||||
# pdf enregistrement.
|
||||
for bill_json in bills_json:
|
||||
save_pdf(bill_json[1])
|
||||
date = datetime.fromisoformat(bill_json[1]["date"]).date()
|
||||
if len(bills_json) > 0:
|
||||
for bill_json in bills_json:
|
||||
save_pdf(bill_json[1])
|
||||
date = datetime.fromisoformat(bill_json[1]["date"]).date()
|
||||
|
||||
bills_str.append(
|
||||
(
|
||||
bill_json[0],
|
||||
f"{date}",
|
||||
bills_str.append(
|
||||
(
|
||||
bill_json[0],
|
||||
f"{date}",
|
||||
)
|
||||
)
|
||||
content = ml.construct_html(bills_str)
|
||||
ml.send_email(
|
||||
"Reçu de facture(s)",
|
||||
content,
|
||||
email_from=EMAIL,
|
||||
email_password=EMAIL_PASSWORD,
|
||||
smpt_port=SMTP_PORT,
|
||||
smtp_mail_address=SMTP_MAIL_ADDRESS,
|
||||
email_to=EMAIL_TO,
|
||||
)
|
||||
content = ml.construct_html(bills_str)
|
||||
ml.send_email(
|
||||
"Reçu de facture(s)",
|
||||
content,
|
||||
email_from=EMAIL,
|
||||
email_password=EMAIL_PASSWORD,
|
||||
smpt_port=SMTP_PORT,
|
||||
smtp_mail_address=SMTP_MAIL_ADDRESS,
|
||||
email_to=EMAIL_TO,
|
||||
)
|
||||
logger.info("Traitement terminé : %d factures téléchargées", len(ids_candidats))
|
||||
|
||||
Reference in New Issue
Block a user