Improves amount parsing for new architecture
Prioritizes parsing amount strings in the new EUR/USD format and introduces support for metadata containing sats equivalent. Fallbacks to legacy parsing when the new format is not detected. This ensures correct interpretation of amount data from different sources.
This commit is contained in:
parent
b6886793ee
commit
700beb6f7f
1 changed files with 24 additions and 10 deletions
34
views_api.py
34
views_api.py
|
|
@ -522,19 +522,33 @@ async def api_get_pending_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 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)))
|
||||
|
||||
# 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)
|
||||
# Try EUR/USD format first (new architecture): "50.00 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'):
|
||||
fiat_amount = abs(float(fiat_match.group(1)))
|
||||
fiat_currency = fiat_match.group(2)
|
||||
|
||||
# Extract sats equivalent 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:
|
||||
# Legacy SATS 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)
|
||||
|
||||
entry_data = {
|
||||
"id": entry_id or "unknown",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue