From 721a565e2211315df50a3b3fcd4185a6a64c9f1c9109c10518d948ba6db6441c Mon Sep 17 00:00:00 2001 From: Moses Rolston Date: Sat, 1 Mar 2025 22:11:51 -0800 Subject: [PATCH] enable add node from csv --- add_csv.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ executive_orders.csv | 10 ++++++++ requirments.txt | 3 +++ 3 files changed, 69 insertions(+) create mode 100644 add_csv.py create mode 100644 executive_orders.csv create mode 100644 requirments.txt diff --git a/add_csv.py b/add_csv.py new file mode 100644 index 0000000..0ecbb1f --- /dev/null +++ b/add_csv.py @@ -0,0 +1,56 @@ +import pandas as pd +from neo4j import GraphDatabase +import argparse +import os +from dotenv import load_dotenv + +# Load environment variables from .env file +load_dotenv() + +def create_neo4j_session(uri, user, password): + return GraphDatabase.driver(uri, auth=(user, password)) + +def create_node(tx, node_type, properties): + # Create a Cypher query to create a node with given type and properties + properties_string = ', '.join([f"{key}: '{value}'" for key, value in properties.items()]) + query = f"CREATE (n:{node_type} {{{properties_string}}})" + tx.run(query) + +def main(csv_file_path): + # Read environment variables + NEO4J_URI = os.getenv("NEO4J_URI") + NEO4J_USER = os.getenv("NEO4J_USER") + NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD") + + if not all([NEO4J_URI, NEO4J_USER, NEO4J_PASSWORD]): + raise ValueError("Neo4j URI, user, and password must be set in the environment variables.") + + # Read the CSV file into a DataFrame + df = pd.read_csv(csv_file_path, delimiter='^', usecols=['type', 'id', 'title', 'description', 'url', 'date_enacted', 'date_repealed']) + + # Connect to Neo4j + driver = create_neo4j_session(NEO4J_URI, NEO4J_USER, NEO4J_PASSWORD) + + with driver.session() as session: + for _, row in df.iterrows(): + node_type = row['type'] + properties = { + 'id': str(row['id']), + 'title': row['title'], + 'description': row['description'], + 'url': row['url'], + 'date_enacted': pd.to_datetime(row['date_enacted']).strftime('%Y-%m-%d') if pd.notnull(row['date_enacted']) else None, + 'date_repealed': pd.to_datetime(row['date_repealed']).strftime('%Y-%m-%d') if pd.notnull(row['date_repealed']) else None + } + + # Create the node in Neo4j + session.write_transaction(create_node, node_type, properties) + + driver.close() + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Read a CSV file and create nodes in Neo4j.") + parser.add_argument("csv_file_path", type=str, help="Path to the CSV file") + + args = parser.parse_args() + main(args.csv_file_path) diff --git a/executive_orders.csv b/executive_orders.csv new file mode 100644 index 0000000..a786cb2 --- /dev/null +++ b/executive_orders.csv @@ -0,0 +1,10 @@ +type^id^title^description^url^date_enacted^date_repealed +order^EO 14222^"Implementing the Presidents ""Department of Government Efficiency"" Cost Efficiency Initiative"^^https://www.federalregister.gov/d/2025-03527^February 26, 2025^ +order^EO 14221^Making America Healthy Again by Empowering Patients With Clear, Accurate, and Actionable Healthcare Pricing Information^^https://www.federalregister.gov/d/2025-03440^February 25, 2025^ +order^EO 14220^Addressing the Threat to National Security From Imports of Copper^^https://www.federalregister.gov/d/2025-03439^February 25, 2025^ +order^EO 14219^"Ensuring Lawful Governance and Implementing the Presidents ""Department of Government Efficiency"" Deregulatory Initiative"^^https://www.federalregister.gov/d/2025-03138^February 19, 2025^ +order^EO 14218^Ending Taxpayer Subsidization of Open Borders^^https://www.federalregister.gov/d/2025-03137^February 19, 2025^ +order^EO 14216^Expanding Access to In Vitro Fertilization^^https://www.federalregister.gov/d/2025-03064^February 18, 2025^ +order^EO 14215^Ensuring Accountability for All Agencies^^https://www.federalregister.gov/d/2025-03063^February 18, 2025^ +order^EO 14214^Keeping Education Accessible and Ending COVID-19 Vaccine Mandates in Schools^^https://www.federalregister.gov/d/2025-02931^February 14, 2025^ +order^EO 14213^Establishing the National Energy Dominance Council^^https://www.federalregister.gov/d/2025-02928^February 14, 2025^ diff --git a/requirments.txt b/requirments.txt new file mode 100644 index 0000000..245df07 --- /dev/null +++ b/requirments.txt @@ -0,0 +1,3 @@ +pandas +neo4j +python-dotenv