policymap/api/endpoints/get_sponsored.py

70 lines
2.5 KiB
Python
Raw Normal View History

2025-03-06 20:00:45 -08:00
# endpoints/get_sponsored.py
from flask import Blueprint, jsonify
from app import get_driver, neo4j_logger
import requests
import json
import os
bp = Blueprint('get_sponsored', __name__)
CACHE_FILE = 'cache.json'
def load_cache():
if os.path.exists(CACHE_FILE):
with open(CACHE_FILE, 'r') as f:
return json.load(f)
return {}
def save_cache(cache_data):
with open(CACHE_FILE, 'w') as f:
json.dump(cache_data, f)
@bp.route('/get_sponsored')
def get_sponsored():
cache = load_cache()
if 'bioguideids' not in cache or len(cache['bioguideids']) == 0:
return jsonify({"message": "No bioguideids found in cache"}), 404
# Print the number of items found in the cache initially
initial_bioguideids_count = len(cache['bioguideids'])
print(f"Initial bioguideids count: {initial_bioguideids_count}")
processed_legislation_count = 0
while 'bioguideids' in cache and len(cache['bioguideids']) > 0:
# Print the current bioguideid being processed
current_bioguideid = cache['bioguideids'].pop(0)
print(f"Processing bioguideid: {current_bioguideid}")
congress_api_url = f"https://api.congress.gov/v3/member/{current_bioguideid}/sponsored-legislation"
# Step 2: Fetch sponsored legislation for the member
response = requests.get(congress_api_url)
if response.status_code != 200:
neo4j_logger.error(f"Failed to fetch sponsored legislation for bioguideid {current_bioguideid}")
continue
legislations = response.json().get('results', [])
# Step 3: Store each piece of legislation in the cache along with the sponsor bioguideid
for legislation in legislations:
key = f"legislation_{legislation['id']}"
if key not in cache:
cache[key] = {
'bioguideid': current_bioguideid,
**legislation
}
processed_legislation_count += 1
# Step 4: Delete the sponsor from the cache (already done by popping)
neo4j_logger.info(f"Processed sponsored legislation for bioguideid {current_bioguideid}")
save_cache(cache)
# Print the total number of legislation items stored and overall items added to the cache
print(f"Total processed legislation count: {processed_legislation_count}")
print(f"Overall items added to cache: {len(cache)}")
return jsonify({"message": "Sponsored legislation processed successfully", "processed_legislation_count": processed_legislation_count}), 200