Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Add LegiScan API call counter to track usage per pipeline run
Browse files
data_updating_scripts/fix_pdf_bills.py
CHANGED
|
@@ -48,7 +48,7 @@ def _log_pipeline_error(error, bill_id="", bill_key=""):
|
|
| 48 |
pass
|
| 49 |
|
| 50 |
# Import shared LegiScan API helper
|
| 51 |
-
from legiscan_api import legi_request, RATE_LIMIT
|
| 52 |
|
| 53 |
# Files
|
| 54 |
INPUT_FILE = "data/known_bills.json"
|
|
@@ -296,11 +296,13 @@ def main(overwrite: bool | None = None):
|
|
| 296 |
with open(OUTPUT_FILE, 'w') as f:
|
| 297 |
json.dump(bills, f, indent=2)
|
| 298 |
|
|
|
|
| 299 |
logger.info(f"Processing complete!")
|
| 300 |
logger.info(f"Fixed locally (no API calls): {fixed_local} bills")
|
| 301 |
logger.info(f"Fixed via API: {fixed_api} bills")
|
| 302 |
logger.info(f"Failed to fix: {failed_count} bills")
|
| 303 |
logger.info(f"Already fixed (skipped): {already_fixed} bills")
|
|
|
|
| 304 |
if fixed_local > 0:
|
| 305 |
logger.info(f"Saved ~{fixed_local * 2} API calls by using local PDF extraction")
|
| 306 |
logger.info(f"Output saved to: {OUTPUT_FILE}")
|
|
|
|
| 48 |
pass
|
| 49 |
|
| 50 |
# Import shared LegiScan API helper
|
| 51 |
+
from legiscan_api import legi_request, RATE_LIMIT, get_api_call_count
|
| 52 |
|
| 53 |
# Files
|
| 54 |
INPUT_FILE = "data/known_bills.json"
|
|
|
|
| 296 |
with open(OUTPUT_FILE, 'w') as f:
|
| 297 |
json.dump(bills, f, indent=2)
|
| 298 |
|
| 299 |
+
api_calls = get_api_call_count()
|
| 300 |
logger.info(f"Processing complete!")
|
| 301 |
logger.info(f"Fixed locally (no API calls): {fixed_local} bills")
|
| 302 |
logger.info(f"Fixed via API: {fixed_api} bills")
|
| 303 |
logger.info(f"Failed to fix: {failed_count} bills")
|
| 304 |
logger.info(f"Already fixed (skipped): {already_fixed} bills")
|
| 305 |
+
logger.info(f"Total LegiScan API calls this run: {api_calls}")
|
| 306 |
if fixed_local > 0:
|
| 307 |
logger.info(f"Saved ~{fixed_local * 2} API calls by using local PDF extraction")
|
| 308 |
logger.info(f"Output saved to: {OUTPUT_FILE}")
|
data_updating_scripts/get_data.py
CHANGED
|
@@ -38,7 +38,7 @@ def _update_pipeline_progress(current, total, unit="bills", message=""):
|
|
| 38 |
pass
|
| 39 |
|
| 40 |
# Import shared LegiScan API helper
|
| 41 |
-
from legiscan_api import legi_request, API_KEY, RATE_LIMIT as _DEFAULT_RATE_LIMIT
|
| 42 |
|
| 43 |
if not API_KEY:
|
| 44 |
print("Error: Please set LEGISCAN_API_KEY in your .env file.")
|
|
@@ -286,9 +286,11 @@ def main():
|
|
| 286 |
save_json(CACHE_FILE, cache)
|
| 287 |
_update_pipeline_progress(len(all_bills), len(all_bills), "bills",
|
| 288 |
f"Done: {len(all_bills)} bills ({new_or_updated} new, {skipped_unchanged} unchanged)")
|
|
|
|
| 289 |
logger.info(f"Completed run, saved {len(all_bills)} bills to {OUTPUT_FILE}")
|
| 290 |
logger.info(f" Unchanged (skipped API calls): {skipped_unchanged}")
|
| 291 |
logger.info(f" New or updated (fetched from API): {new_or_updated}")
|
|
|
|
| 292 |
if skipped_unchanged > 0:
|
| 293 |
logger.info(f" Saved ~{skipped_unchanged * 2} API calls by skipping unchanged bills")
|
| 294 |
|
|
|
|
| 38 |
pass
|
| 39 |
|
| 40 |
# Import shared LegiScan API helper
|
| 41 |
+
from legiscan_api import legi_request, API_KEY, RATE_LIMIT as _DEFAULT_RATE_LIMIT, get_api_call_count
|
| 42 |
|
| 43 |
if not API_KEY:
|
| 44 |
print("Error: Please set LEGISCAN_API_KEY in your .env file.")
|
|
|
|
| 286 |
save_json(CACHE_FILE, cache)
|
| 287 |
_update_pipeline_progress(len(all_bills), len(all_bills), "bills",
|
| 288 |
f"Done: {len(all_bills)} bills ({new_or_updated} new, {skipped_unchanged} unchanged)")
|
| 289 |
+
api_calls = get_api_call_count()
|
| 290 |
logger.info(f"Completed run, saved {len(all_bills)} bills to {OUTPUT_FILE}")
|
| 291 |
logger.info(f" Unchanged (skipped API calls): {skipped_unchanged}")
|
| 292 |
logger.info(f" New or updated (fetched from API): {new_or_updated}")
|
| 293 |
+
logger.info(f" Total LegiScan API calls this run: {api_calls}")
|
| 294 |
if skipped_unchanged > 0:
|
| 295 |
logger.info(f" Saved ~{skipped_unchanged * 2} API calls by skipping unchanged bills")
|
| 296 |
|
data_updating_scripts/legiscan_api.py
CHANGED
|
@@ -13,6 +13,14 @@ API_KEY = os.getenv("LEGISCAN_API_KEY")
|
|
| 13 |
|
| 14 |
RATE_LIMIT = 0.2 # seconds between requests
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def legi_request(op, params):
|
| 18 |
"""Make a request to the LegiScan API.
|
|
@@ -20,6 +28,8 @@ def legi_request(op, params):
|
|
| 20 |
Returns the parsed JSON response on success, or None on failure.
|
| 21 |
"""
|
| 22 |
base = "https://api.legiscan.com/"
|
|
|
|
|
|
|
| 23 |
params.update({"key": API_KEY, "op": op})
|
| 24 |
try:
|
| 25 |
resp = requests.get(base, params=params, timeout=10)
|
|
|
|
| 13 |
|
| 14 |
RATE_LIMIT = 0.2 # seconds between requests
|
| 15 |
|
| 16 |
+
# API call counter — tracks total calls made in this process
|
| 17 |
+
_api_call_count = 0
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_api_call_count():
|
| 21 |
+
"""Return the number of LegiScan API calls made so far."""
|
| 22 |
+
return _api_call_count
|
| 23 |
+
|
| 24 |
|
| 25 |
def legi_request(op, params):
|
| 26 |
"""Make a request to the LegiScan API.
|
|
|
|
| 28 |
Returns the parsed JSON response on success, or None on failure.
|
| 29 |
"""
|
| 30 |
base = "https://api.legiscan.com/"
|
| 31 |
+
global _api_call_count
|
| 32 |
+
_api_call_count += 1
|
| 33 |
params.update({"key": API_KEY, "op": op})
|
| 34 |
try:
|
| 35 |
resp = requests.get(base, params=params, timeout=10)
|