feat: now loops over all the facts jsons
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import os
|
||||
import mariadb
|
||||
import json
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
def connect_to_db():
|
||||
try:
|
||||
# Read the needed variables from the environment
|
||||
# Read the connection parameters from the environment
|
||||
conn = mariadb.connect(
|
||||
user=os.getenv("DB_USER"),
|
||||
password=os.getenv("DB_PASSWORD"),
|
||||
@@ -38,13 +38,21 @@ def insert_data(cursor, cik, fact_id, start, end, val, accn, fy, fp, form, filed
|
||||
(cik, fact_id, start, end, val, accn, fy, fp, form, filed, frame)
|
||||
)
|
||||
|
||||
def parse_json_and_insert_data(file_path, cursor):
|
||||
def parse_json_and_insert_data(file_path):
|
||||
with open(file_path, 'r') as file:
|
||||
data = json.load(file)
|
||||
|
||||
# Get the connection and cursor
|
||||
conn = connect_to_db()
|
||||
if conn is None:
|
||||
return False
|
||||
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Insert the entity
|
||||
cik = data['cik']
|
||||
entity_name = data['entityName']
|
||||
cik = data.get('cik')
|
||||
entity_name = data.get('entityName')
|
||||
insert_entity(cursor, cik, entity_name)
|
||||
|
||||
# Iterate over facts
|
||||
@@ -72,29 +80,39 @@ def parse_json_and_insert_data(file_path, cursor):
|
||||
|
||||
insert_data(cursor, cik, fact_id, start, end, val, accn, fy, fp, form, filed, frame)
|
||||
|
||||
def main():
|
||||
# Connect to the database
|
||||
conn = connect_to_db()
|
||||
if conn is None:
|
||||
return
|
||||
|
||||
try:
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Load JSON and insert data
|
||||
json_file_path = 'CIK0000320193.json'
|
||||
parse_json_and_insert_data(json_file_path, cursor)
|
||||
|
||||
# Commit the transaction
|
||||
# Commit the transaction for the whole file
|
||||
conn.commit()
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error occurred: {e}")
|
||||
print(f"Error occurred while processing {file_path}: {e}")
|
||||
conn.rollback()
|
||||
return False
|
||||
finally:
|
||||
cursor.close()
|
||||
conn.close()
|
||||
print("Connection closed")
|
||||
|
||||
def process_all_files_in_directory(directory_path):
|
||||
files = [f for f in os.listdir(directory_path) if f.endswith('.json')]
|
||||
total_files = len(files)
|
||||
processed_files = 0
|
||||
|
||||
for idx, file_name in enumerate(files, start=1):
|
||||
file_path = os.path.join(directory_path, file_name)
|
||||
print(f"Processing file {idx} of {total_files}: {file_name}")
|
||||
|
||||
if parse_json_and_insert_data(file_path):
|
||||
processed_files += 1
|
||||
print(f"Successfully processed {file_name}")
|
||||
else:
|
||||
print(f"Failed to process {file_name}")
|
||||
|
||||
print(f"Finished processing {processed_files} out of {total_files} files.")
|
||||
|
||||
def main():
|
||||
# Process all JSON files in the directory
|
||||
directory_path = './sec_data/companyfacts/'
|
||||
process_all_files_in_directory(directory_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user