From be101beac8b028deec93938bba420ed43c13af7ee7a82a2d043bf76af5340063 Mon Sep 17 00:00:00 2001 From: Moses Rolston Date: Sat, 8 Mar 2025 14:16:40 -0800 Subject: [PATCH] cache working --- api/endpoints/get_sponsored.py | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/api/endpoints/get_sponsored.py b/api/endpoints/get_sponsored.py index 78aecc2..12b1b90 100644 --- a/api/endpoints/get_sponsored.py +++ b/api/endpoints/get_sponsored.py @@ -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