From 5a3d60e45e21a34799c97636d1f4781dde24a0c6 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Wed, 8 Apr 2026 09:57:54 +0200 Subject: [PATCH] use date for invoice name --- downloader.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/downloader.py b/downloader.py index f7f098c..f78427d 100644 --- a/downloader.py +++ b/downloader.py @@ -234,7 +234,7 @@ def export_invoice_pdf( context: BrowserContext, page: Page, invoice: dict, - output_path: Path, + output_dir: Path, billing_url: str = BILLING_URL, ) -> tuple[Page, Path]: invoice_page = None @@ -259,6 +259,25 @@ def export_invoice_pdf( fill_invoice_fields(invoice_page) time.sleep(0.5) + date_text = "" + date_container = invoice_page.locator(".b2-invoice-customer-right").first + if date_container.count() > 0: + lines = [line.strip() for line in date_container.inner_text().splitlines() if line.strip()] + if lines: + date_text = lines[0].replace(" UTC", "") + if not date_text: + date_text = "unknown-date" + + invoice_id = str(invoice.get("reference_id", "unknown")) + filename = sanitize_filename(f"{date_text} Invoice {invoice_id}") + output_path = output_dir / f"{filename}.pdf" + + if output_path.exists(): + logger.info("Skipping (exists): %s", output_path) + if invoice_page is not page: + invoice_page.close() + return page, output_path + invoice_page.pdf(path=str(output_path), format="A4", print_background=True) logger.info("Saved: %s", output_path) @@ -356,21 +375,13 @@ def download_all_invoices() -> list[Path]: year_dir.mkdir(parents=True, exist_ok=True) - for idx, invoice in enumerate(invoices): - label = sanitize_filename(invoice["label"]) or f"invoice_{idx + 1}" - pdf_path = year_dir / f"{label}.pdf" - - if pdf_path.exists(): - logger.info("Skipping (exists): %s", pdf_path) - saved.append(pdf_path) - continue - + for invoice in invoices: try: page, path = export_invoice_pdf( context, page, invoice, - pdf_path, + year_dir, billing_url=billing_url, ) saved.append(path)