From 313265b1856b21a74821a351d3d9703c777b01ee Mon Sep 17 00:00:00 2001 From: padreug Date: Mon, 10 Nov 2025 03:46:17 +0100 Subject: [PATCH] Supports new EUR/USD amount string format Adds support for parsing direct EUR/USD amount strings in the format "37.22 EUR" or "12.34 USD". It also retrieves the SATS equivalent from the metadata if present, for the new amount format. This ensures compatibility with both the old "SATS {EUR}" format and the newer, direct fiat formats. --- views_api.py | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/views_api.py b/views_api.py index 82f85e6..590aa9f 100644 --- a/views_api.py +++ b/views_api.py @@ -390,19 +390,32 @@ async def api_get_user_entries( if isinstance(first_posting, dict): amount_str = first_posting.get("amount", "") - # Parse amount string format: "36791 SATS {33.33 EUR, 2025-11-09}" or "36791 SATS" + # Parse amount string: can be EUR/USD directly (new format) or "SATS {EUR}" (old format) 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))) + # Try EUR/USD format first (new format: "37.22 EUR") + fiat_match = re.match(r'^(-?[\d.]+)\s+([A-Z]{3})$', amount_str) + if fiat_match and fiat_match.group(2) in ('EUR', 'USD', 'GBP'): + # Direct fiat amount (new approach) + fiat_amount = abs(float(fiat_match.group(1))) + fiat_currency = fiat_match.group(2) - # 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) + # Get SATS from metadata + posting_meta = first_posting.get("meta", {}) + sats_equiv = posting_meta.get("sats-equivalent") + if sats_equiv: + amount_sats = abs(int(sats_equiv)) + else: + # Old format: "36791 SATS {33.33 EUR, 2025-11-09}" or "36791 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