From 0c7356e2283993a39ab890ba886e7e80d5ff2c14 Mon Sep 17 00:00:00 2001 From: padreug Date: Mon, 10 Nov 2025 01:00:43 +0100 Subject: [PATCH] Parses amount string for SATS and fiat Improves handling of the amount field in user entries by parsing string formats that include both SATS and fiat currency information. This change allows extracting the SATS amount and fiat amount/currency directly from the string, accommodating different display formats. --- views_api.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/views_api.py b/views_api.py index 4611b60..b5332a1 100644 --- a/views_api.py +++ b/views_api.py @@ -387,14 +387,21 @@ async def api_get_user_entries( if postings: first_posting = postings[0] if isinstance(first_posting, dict): - amount_field = first_posting.get("amount") - if isinstance(amount_field, dict): - amount_sats = abs(int(float(amount_field.get("number", 0)))) + amount_str = first_posting.get("amount", "") - cost = first_posting.get("cost") - if isinstance(cost, dict): - fiat_amount = float(cost.get("number", 0)) - fiat_currency = cost.get("currency") + # Parse amount string format: "36791 SATS {33.33 EUR, 2025-11-09}" or "36791 SATS" + if isinstance(amount_str, str) and amount_str: + import re + # Extract SATS amount (before " SATS") + sats_match = re.match(r'^(-?\d+)\s+SATS', amount_str) + if sats_match: + amount_sats = abs(int(sats_match.group(1))) + + # Extract fiat from cost syntax: {33.33 EUR, ...} + cost_match = re.search(r'\{([\d.]+)\s+([A-Z]+)', amount_str) + if cost_match: + fiat_amount = float(cost_match.group(1)) + fiat_currency = cost_match.group(2) # Extract reference from links (first non-castle link) reference = None