Transaction loading spinner
This commit is contained in:
@@ -45,65 +45,34 @@ def fetch_page(offset):
|
|||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
return r.text
|
return r.text
|
||||||
|
|
||||||
def _probe_max_offset():
|
|
||||||
"""Doe één request met een enorm offset-nummer om het hoogste record-nummer te vinden."""
|
|
||||||
raw = fetch_page(999_999_999)
|
|
||||||
offsets = re.findall(r'^(\d+)_\w+:', raw, re.MULTILINE)
|
|
||||||
return max(int(x) for x in offsets) if offsets else None
|
|
||||||
|
|
||||||
def _print_progress(current, total, width=40):
|
|
||||||
import sys
|
|
||||||
pct = min(current / total, 1.0)
|
|
||||||
filled = int(width * pct)
|
|
||||||
bar = '#' * filled + '-' * (width - filled)
|
|
||||||
line = f" [{bar}] {pct*100:5.1f}% (record {current}/{total})"
|
|
||||||
sys.stdout.write(f"\r{line:<70}")
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
def get_all_raw():
|
def get_all_raw():
|
||||||
"""Haalt alle transactiepagina's op via paginering.
|
"""Haalt alle transactiepagina's op via paginering.
|
||||||
Stopt zodra de record-nummers terugvallen (circulaire buffer bereikt).
|
Stopt zodra de record-nummers terugvallen (circulaire buffer bereikt).
|
||||||
"""
|
"""
|
||||||
import sys
|
import sys, itertools
|
||||||
|
spin = itertools.cycle('|/-\\')
|
||||||
print(" Bepalen totaal aantal records...", end='', flush=True)
|
|
||||||
max_record = _probe_max_offset()
|
|
||||||
if max_record:
|
|
||||||
print(f" max record = {max_record}")
|
|
||||||
else:
|
|
||||||
print(" onbekend, toon teller")
|
|
||||||
|
|
||||||
all_raw = ""
|
all_raw = ""
|
||||||
offset = 0
|
offset = 0
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
sys.stdout.write(next(spin) + '\b')
|
||||||
|
sys.stdout.flush()
|
||||||
raw = fetch_page(offset)
|
raw = fetch_page(offset)
|
||||||
|
|
||||||
stripped = raw.strip().rstrip('}').strip()
|
stripped = raw.strip().rstrip('}').strip()
|
||||||
if not stripped or stripped in ('{"version":2,', '{"version":2'):
|
if not stripped or stripped in ('{"version":2,', '{"version":2'):
|
||||||
if max_record:
|
|
||||||
_print_progress(max_record, max_record)
|
|
||||||
print("\n Klaar.")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
all_raw += raw
|
all_raw += raw
|
||||||
|
|
||||||
offsets = re.findall(r'^(\d+)_\w+:', raw, re.MULTILINE)
|
offsets = re.findall(r'^(\d+)_\w+:', raw, re.MULTILINE)
|
||||||
if not offsets:
|
if not offsets:
|
||||||
print("\n Klaar.")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
next_offset = max(int(x) for x in offsets) + 1
|
next_offset = max(int(x) for x in offsets) + 1
|
||||||
|
|
||||||
if max_record:
|
|
||||||
_print_progress(next_offset, max_record)
|
|
||||||
else:
|
|
||||||
sys.stdout.write(f"\r Record {next_offset}...")
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
# Circulaire buffer: nummers vallen terug → we hebben alles gehad
|
|
||||||
if next_offset <= offset:
|
if next_offset <= offset:
|
||||||
print("\n Klaar (circulaire buffer).")
|
|
||||||
break
|
break
|
||||||
|
|
||||||
offset = next_offset
|
offset = next_offset
|
||||||
@@ -293,10 +262,13 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
login()
|
login()
|
||||||
try:
|
try:
|
||||||
print("\nTransacties ophalen...")
|
print("\nTransacties ophalen...", end=' ', flush=True)
|
||||||
raw = get_all_raw()
|
raw = get_all_raw()
|
||||||
|
print("Verwerken...", end=' ', flush=True)
|
||||||
transactions = parse_transactions(raw)
|
transactions = parse_transactions(raw)
|
||||||
|
print("Klaar.\n")
|
||||||
print_summary(transactions)
|
print_summary(transactions)
|
||||||
|
print("Opslaan als CSV...", end=' ', flush=True)
|
||||||
save_as_csv(raw, device_id)
|
save_as_csv(raw, device_id)
|
||||||
finally:
|
finally:
|
||||||
logout()
|
logout()
|
||||||
Reference in New Issue
Block a user