"""Download published ANAC flight records; no credentials or scraping bypasses.""" from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timezone import hashlib import json from pathlib import Path from urllib.parse import quote import requests ROOT = Path(__file__).resolve().parent BASE = 'https://sistemas.anac.gov.br/dadosabertos/Voos e operações aéreas/Voo Regular Ativo (VRA)/2026/' MONTHS = {1: 'Janeiro', 2: 'Fevereiro', 3: 'Março', 4: 'Abril', 5: 'Maio', 6: 'Junho', 7: 'Julho', 8: 'Agosto', 9: 'Setembro'} def acquire(item): month, name = item path = ROOT / 'raw' / f'VRA_2026{month}.csv' if path.exists(): return f'cached {path.name}: {path.stat().st_size} bytes' url = quote(BASE + f'{month:02d} - {name}/{path.name}', safe=':/()') with requests.get(url, timeout=(15, 35), stream=True) as response: response.raise_for_status() temp = path.with_suffix('.part') digest = hashlib.sha256() with temp.open('wb') as out: for chunk in response.iter_content(1024 * 256): digest.update(chunk) out.write(chunk) temp.replace(path) path.with_suffix('.source.json').write_text(json.dumps(dict(url=url, retrieved_at_utc=datetime.now(timezone.utc).isoformat(), sha256=digest.hexdigest(), bytes=path.stat().st_size, last_modified=response.headers.get('Last-Modified'), publisher='ANAC', metadata_url='https://www.anac.gov.br/acesso-a-informacao/dados-abertos/areas-de-atuacao/voos-e-operacoes-aereas/voo-regular-ativo-vra/62-voo-regular-ativo-vra'), indent=2), encoding='utf-8') return f'downloaded {path.name}: {path.stat().st_size} bytes' if __name__ == '__main__': (ROOT / 'raw').mkdir(exist_ok=True) with ThreadPoolExecutor(max_workers=2) as pool: for result in pool.map(acquire, MONTHS.items()): print(result, flush=True)