Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| """Shared LegiScan API helper used by get_data.py and fix_pdf_bills.py.""" | |
| import os | |
| import logging | |
| import requests | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| logger = logging.getLogger(__name__) | |
| API_KEY = os.getenv("LEGISCAN_API_KEY") | |
| RATE_LIMIT = 0.2 # seconds between requests | |
| # API call counter — tracks total calls made in this process | |
| _api_call_count = 0 | |
| def get_api_call_count(): | |
| """Return the number of LegiScan API calls made so far.""" | |
| return _api_call_count | |
| def legi_request(op, params): | |
| """Make a request to the LegiScan API. | |
| Returns the parsed JSON response on success, or None on failure. | |
| """ | |
| base = "https://api.legiscan.com/" | |
| global _api_call_count | |
| _api_call_count += 1 | |
| params.update({"key": API_KEY, "op": op}) | |
| try: | |
| resp = requests.get(base, params=params, timeout=10) | |
| resp.raise_for_status() | |
| data = resp.json() | |
| if data.get("status") != "OK": | |
| logger.error(f"API error {op}: {data.get('message', data)}") | |
| return None | |
| return data | |
| except requests.RequestException as e: | |
| logger.error(f"Request failed ({op}): {e}") | |
| return None | |