2025-03-06 20:00:45 -08:00
|
|
|
# endpoints/store_sponsors.py
|
|
|
|
from flask import Blueprint, jsonify
|
|
|
|
from app import get_driver, neo4j_logger
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
|
|
|
|
bp = Blueprint('store_sponsors', __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('/store_sponsors')
|
|
|
|
def store_sponsors():
|
|
|
|
cache = load_cache()
|
|
|
|
|
|
|
|
if 'bioguideids' in cache and len(cache['bioguideids']) > 0:
|
|
|
|
return jsonify({"message": "Bioguideids already cached"}), 200
|
|
|
|
|
|
|
|
driver = get_driver()
|
|
|
|
with driver.session() as session:
|
|
|
|
query = "MATCH (n:Person) RETURN n.bioguideid"
|
|
|
|
neo4j_logger.info(f"Executing query: {query}")
|
|
|
|
|
2025-03-06 21:01:42 -08:00
|
|
|
# Fetch the records
|
|
|
|
records = session.run(query)
|
|
|
|
|
|
|
|
bioguideids = []
|
|
|
|
|
|
|
|
for record in records:
|
|
|
|
bioguideid = record['n.bioguideid']
|
|
|
|
if bioguideid is not None:
|
|
|
|
bioguideids.append(bioguideid)
|
|
|
|
print(f"Storing bioguideid: {bioguideid}") # Print each bioguideid as it's added
|
|
|
|
else:
|
|
|
|
print("Found a record with None bioguideid") # Log any None values
|
|
|
|
|
2025-03-06 20:00:45 -08:00
|
|
|
cache['bioguideids'] = bioguideids
|
|
|
|
|
|
|
|
neo4j_logger.info(f"Cached bioguideids: {len(bioguideids)}")
|
|
|
|
|
2025-03-06 21:01:42 -08:00
|
|
|
# Save the updated cache
|
2025-03-06 20:00:45 -08:00
|
|
|
save_cache(cache)
|
|
|
|
|
|
|
|
return jsonify({"message": "Bioguideids cached successfully", "cached_bioguideids_count": len(cache['bioguideids'])}), 200
|