fetch b2 & groups and fetch all years

This commit is contained in:
Jan Bader
2026-04-07 23:35:38 +02:00
parent 03bb94db2d
commit 1cbe80ac00

View File

@@ -92,8 +92,8 @@ def login(context: BrowserContext, page: Page) -> Page:
return page return page
def get_group_options(context: BrowserContext, page: Page) -> tuple[Page, list[dict]]: def get_group_options(context: BrowserContext, page: Page, billing_url: str = BILLING_URL) -> tuple[Page, list[dict]]:
page = _goto_with_retry(context, page, BILLING_URL) page = _goto_with_retry(context, page, billing_url)
group_select = page.locator("select#groupSelection") group_select = page.locator("select#groupSelection")
if group_select.count() == 0: if group_select.count() == 0:
return page, [{"value": "", "label": "default"}] return page, [{"value": "", "label": "default"}]
@@ -109,7 +109,7 @@ def get_group_options(context: BrowserContext, page: Page) -> tuple[Page, list[d
def get_year_options(page: Page) -> list[str]: def get_year_options(page: Page) -> list[str]:
page.wait_for_load_state("domcontentloaded") page.wait_for_load_state("domcontentloaded")
year_select = page.locator("select#yearSelection") year_select = page.locator("select#yearSelection, select#receiptDateId").first
if year_select.count() == 0: if year_select.count() == 0:
return [] return []
options = year_select.locator("option").all() options = year_select.locator("option").all()
@@ -252,10 +252,16 @@ def sanitize_filename(name: str) -> str:
return name.strip('_') return name.strip('_')
def export_invoice_pdf(context: BrowserContext, page: Page, invoice: dict, output_path: Path) -> tuple[Page, Path]: def export_invoice_pdf(
context: BrowserContext,
page: Page,
invoice: dict,
output_path: Path,
billing_url: str = BILLING_URL,
) -> tuple[Page, Path]:
invoice_page = None invoice_page = None
if invoice.get("open_via_popup") and invoice.get("reference_id"): if invoice.get("open_via_popup") and invoice.get("reference_id"):
page = _goto_with_retry(context, page, BILLING_URL) page = _goto_with_retry(context, page, billing_url)
selector = f"a[data-reference-object-id='{invoice['reference_id']}']" selector = f"a[data-reference-object-id='{invoice['reference_id']}']"
anchor = page.locator(selector).first anchor = page.locator(selector).first
if anchor.count() == 0: if anchor.count() == 0:
@@ -310,17 +316,26 @@ def download_all_invoices() -> list[Path]:
input("Login failed. Inspect the browser, then press Enter to close it...") input("Login failed. Inspect the browser, then press Enter to close it...")
raise raise
page = _goto_with_retry(context, page, BILLING_URL) billing_pages = ["b2", "groups"]
page, groups = get_group_options(context, page) for billing_page in billing_pages:
logger.info("Found %d group(s): %s", len(groups), [g["label"] for g in groups]) billing_url = f"{BILLING_URL}?billing_page={billing_page}"
page = _goto_with_retry(context, page, billing_url)
page, groups = get_group_options(context, page, billing_url=billing_url)
logger.info(
"Found %d group(s) on billing page '%s': %s",
len(groups),
billing_page,
[g["label"] for g in groups],
)
for group in groups: for group in groups:
group_label = sanitize_filename(group["label"]) group_label = sanitize_filename(group["label"])
group_dir = output_dir / group_label if len(groups) > 1 else output_dir group_dir = output_dir / group_label if len(groups) > 1 else output_dir
if group["value"]: if group["value"]:
page = _goto_with_retry(context, page, BILLING_URL) page = _goto_with_retry(context, page, billing_url)
group_select = page.locator("select#groupSelection") group_select = page.locator("select#groupSelection")
if group_select.count() > 0: if group_select.count() > 0:
group_select.select_option(value=group["value"]) group_select.select_option(value=group["value"])
@@ -336,7 +351,7 @@ def download_all_invoices() -> list[Path]:
year_dir = group_dir / year if year != "all" else group_dir year_dir = group_dir / year if year != "all" else group_dir
if year != "all": if year != "all":
year_select = page.locator("select#yearSelection") year_select = page.locator("select#yearSelection, select#receiptDateId").first
if year_select.count() > 0: if year_select.count() > 0:
year_select.select_option(value=year) year_select.select_option(value=year)
page.wait_for_load_state("domcontentloaded") page.wait_for_load_state("domcontentloaded")
@@ -369,13 +384,19 @@ def download_all_invoices() -> list[Path]:
continue continue
try: try:
page, path = export_invoice_pdf(context, page, invoice, pdf_path) page, path = export_invoice_pdf(
context,
page,
invoice,
pdf_path,
billing_url=billing_url,
)
saved.append(path) saved.append(path)
except Exception: except Exception:
logger.exception("Failed to export: %s", invoice["url"]) logger.exception("Failed to export: %s", invoice["url"])
if year != "all": if year != "all":
page = _goto_with_retry(context, page, BILLING_URL) page = _goto_with_retry(context, page, billing_url)
if group["value"]: if group["value"]:
group_select = page.locator("select#groupSelection") group_select = page.locator("select#groupSelection")
if group_select.count() > 0: if group_select.count() > 0: