Transaction loading spinner
This commit is contained in:
@@ -45,65 +45,34 @@ def fetch_page(offset):
|
||||
r.raise_for_status()
|
||||
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():
|
||||
"""Haalt alle transactiepagina's op via paginering.
|
||||
Stopt zodra de record-nummers terugvallen (circulaire buffer bereikt).
|
||||
"""
|
||||
import sys
|
||||
|
||||
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")
|
||||
import sys, itertools
|
||||
spin = itertools.cycle('|/-\\')
|
||||
|
||||
all_raw = ""
|
||||
offset = 0
|
||||
|
||||
while True:
|
||||
sys.stdout.write(next(spin) + '\b')
|
||||
sys.stdout.flush()
|
||||
raw = fetch_page(offset)
|
||||
|
||||
stripped = raw.strip().rstrip('}').strip()
|
||||
if not stripped or stripped in ('{"version":2,', '{"version":2'):
|
||||
if max_record:
|
||||
_print_progress(max_record, max_record)
|
||||
print("\n Klaar.")
|
||||
break
|
||||
|
||||
all_raw += raw
|
||||
|
||||
offsets = re.findall(r'^(\d+)_\w+:', raw, re.MULTILINE)
|
||||
if not offsets:
|
||||
print("\n Klaar.")
|
||||
break
|
||||
|
||||
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:
|
||||
print("\n Klaar (circulaire buffer).")
|
||||
break
|
||||
|
||||
offset = next_offset
|
||||
@@ -293,10 +262,13 @@ if __name__ == "__main__":
|
||||
|
||||
login()
|
||||
try:
|
||||
print("\nTransacties ophalen...")
|
||||
print("\nTransacties ophalen...", end=' ', flush=True)
|
||||
raw = get_all_raw()
|
||||
print("Verwerken...", end=' ', flush=True)
|
||||
transactions = parse_transactions(raw)
|
||||
print("Klaar.\n")
|
||||
print_summary(transactions)
|
||||
print("Opslaan als CSV...", end=' ', flush=True)
|
||||
save_as_csv(raw, device_id)
|
||||
finally:
|
||||
logout()
|
||||
Reference in New Issue
Block a user