Files
set50-system/backend/scripts/collect_auto_credit.py
Kunthawat Greethong f6dd6304cc [verified] Add Auto Credit (Trading Economics) + Refining/Energy (EIA) factor collectors
- auto_credit.py: scrape Trading Economics Thailand total-vehicle-sales HTML -> total sales + new car sales YoY + vehicle production/passenger/exports
- refining_energy.py: scrape EIA prices.php 3:2:1 crack spread (Gulf LLS) + WTI/Brent/gasoline
- Both server-rendered HTML scrapers (har-derived-api-client pattern); EIA used instead of RBN (RBN is Cloudflare-challenged, 403 via urllib; EIA is open US gov data, HTTP 200 direct)
- collect_auto_credit.py / collect_refining.py CLI -> JSON snapshot
- 8 tests; full backend suite 148 OK; live verified (auto 59198/20% YoY; crack 71.10 $/bbl); static scan clean
2026-08-25 11:05:13 +07:00

39 lines
1.2 KiB
Python

"""Collect the Auto Credit factor (Trading Economics vehicle sales) to a JSON snapshot."""
from __future__ import annotations
import argparse
import json
from datetime import datetime, timezone
from pathlib import Path
from app import auto_credit
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--out", type=Path,
help="write JSON snapshot to this file (default: stdout)")
parser.add_argument("--timeout", type=float, default=30.0)
args = parser.parse_args()
snap = auto_credit.fetch_auto_credit(timeout=args.timeout)
payload = {
"source": "tradingeconomics",
"retrieved_at": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"factor": "auto_credit",
"data": snap.to_dict(),
}
text = json.dumps(payload, ensure_ascii=False, sort_keys=True, indent=2)
if args.out:
args.out.parent.mkdir(parents=True, exist_ok=True)
args.out.write_text(text, encoding="utf-8")
print(f"wrote auto_credit snapshot -> {args.out}")
else:
print(text)
return 0
if __name__ == "__main__":
raise SystemExit(main())