mirror of
https://github.com/Fare-spec/get_ovh_bills.git
synced 2025-12-07 10:20:36 +00:00
started the project
This commit is contained in:
27
fetcher.py
Normal file
27
fetcher.py
Normal file
@@ -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
|
||||
49
main.py
Normal file
49
main.py
Normal file
@@ -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)
|
||||
Reference in New Issue
Block a user