From 363e5cd8c05a020f77914d0e7e06cf6499de0cd8 Mon Sep 17 00:00:00 2001 From: Spectre Date: Thu, 4 Sep 2025 18:57:48 +0200 Subject: [PATCH] started the project --- fetcher.py | 27 +++++++++++++++++++++++++++ main.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 fetcher.py create mode 100644 main.py diff --git a/fetcher.py b/fetcher.py new file mode 100644 index 0000000..abaeb88 --- /dev/null +++ b/fetcher.py @@ -0,0 +1,27 @@ +import json +from re import error +import ovh + + +def fetch_api(app_key: str, app_secret: str, consumer_key: str) -> list[str]: + client = ovh.Client( + endpoint="ovh-eu", + application_key=app_key, + application_secret=app_secret, + consumer_key=consumer_key, + ) + bills = client.get("/me/bill/") + return bills + + +def fetch_invoice_content( + id: str, app_key: str, app_secret: str, consumer_key: str +) -> dict: + client = ovh.Client( + endpoint="ovh-eu", + application_key=app_key, + application_secret=app_secret, + consumer_key=consumer_key, + ) + bill = client.get(f"/me/bill/{id}") + return bill diff --git a/main.py b/main.py new file mode 100644 index 0000000..d258dc8 --- /dev/null +++ b/main.py @@ -0,0 +1,49 @@ +import os +import dotenv +import ovh +import fetcher as ft +import datetime +from urllib.request import urlretrieve + +dotenv.load_dotenv() +APP_KEY = os.getenv("APP_KEY") +APP_SECRET = os.getenv("APP_SECRET") +CONSUMER_KEY = os.getenv("CONSUMER_KEY") + + +def get_ids() -> list[str]: + try: + ids = ft.fetch_api( + app_key=APP_KEY, + app_secret=APP_SECRET, + consumer_key=CONSUMER_KEY, + ) + return ids + except ovh.exceptions.APIError as e: + raise RuntimeError(f"Échec récupération IDs factures: {e}") from e + + +def get_bill(bill_id: str) -> dict: + try: + return ft.fetch_invoice_content( + bill_id, + app_key=APP_KEY, + app_secret=APP_SECRET, + consumer_key=CONSUMER_KEY, + ) + except ovh.exceptions.APIError as e: + raise RuntimeError(f"Échec récupération facture {bill_id}: {e}") from e + + +def get_pdf(bill: dict): + url = bill["pdfUrl"] + date = f"{datetime.datetime.fromisoformat(bill['date']).date()}.pdf" + urlretrieve(url, date) + + +if __name__ == "__main__": + ids = get_ids() + print(ids) + if ids: + bill = get_bill(ids[0]) + get_pdf(bill)