cache working

This commit is contained in:
Moses Rolston 2025-03-08 14:16:40 -08:00
parent 574c889258
commit be101beac8

View File

@ -1,4 +1,3 @@
# endpoints/get_sponsored.py
from flask import Blueprint, jsonify
from app import get_driver, neo4j_logger
import requests
@ -26,23 +25,19 @@ def save_cache(cache_data):
def get_sponsored():
# Load bioguideIds and legislation from the same cache
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:
# Step 1: Retrieve a sponsor from the cache
current_bioguideId = cache['bioguideIds'].pop(0)
if current_bioguideId is None:
continue
print(f"Processing bioguideId: {current_bioguideId}")
congress_api_url = f"https://api.congress.gov/v3/member/{current_bioguideId}/sponsored-legislation"
@ -59,7 +54,6 @@ def get_sponsored():
# Step 2: Fetch sponsored legislation for the member
response = requests.get(congress_api_url, headers=headers)
print(f"Response Status Code: {response.status_code}")
print(f"Response Text: {response.text}")
@ -67,19 +61,32 @@ def get_sponsored():
neo4j_logger.error(f"Failed to fetch sponsored legislation for bioguideId {current_bioguideId}: Status Code {response.status_code}, Response: {response.text}")
continue
legislations = response.json().get('results', [])
sponsored_legislation = response.json().get('sponsoredLegislation', [])
# Step 3: Store each piece of legislation in the cache along with the sponsor bioguideId
for legislation in legislations:
key = f"legislation_{legislation['id']}"
for legislation in sponsored_legislation:
if 'number' not in legislation:
neo4j_logger.error(f"Missing 'number' field in legislation data for bioguideId {current_bioguideId}: {legislation}")
continue
key = f"legislation_{legislation['number']}"
print(f"About to write to cache: {key} - {legislation}")
if key not in cache:
cache[key] = {
# Ensure all nested dictionaries are handled properly
legislation_info = {
'bioguideId': current_bioguideId,
**legislation
**legislation,
'latestAction': {
'actionDate': legislation['latestAction']['actionDate'],
'text': legislation['latestAction']['text']
},
'policyArea': {
'name': legislation['policyArea']['name']
}
}
cache[key] = legislation_info
processed_legislation_count += 1
# Save the cache immediately after writing each entry
@ -102,4 +109,7 @@ def get_sponsored():
print(f"Total processed legislation count: {processed_legislation_count}")
print(f"Overall items in cache: {len(cache)}")
return jsonify({"message": "Sponsored legislation processed successfully", "processed_legislation_count": processed_legislation_count}), 200
return jsonify({
"message": "Sponsored legislation processed successfully",
"processed_legislation_count": processed_legislation_count
}), 200