feat: now loops over all the facts jsons
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
|
import os
|
||||||
import mariadb
|
import mariadb
|
||||||
import json
|
import json
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
import os
|
|
||||||
|
|
||||||
# Load environment variables from .env file
|
# Load environment variables from .env file
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
def connect_to_db():
|
def connect_to_db():
|
||||||
try:
|
try:
|
||||||
# Read the needed variables from the environment
|
# Read the connection parameters from the environment
|
||||||
conn = mariadb.connect(
|
conn = mariadb.connect(
|
||||||
user=os.getenv("DB_USER"),
|
user=os.getenv("DB_USER"),
|
||||||
password=os.getenv("DB_PASSWORD"),
|
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)
|
(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:
|
with open(file_path, 'r') as file:
|
||||||
data = json.load(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
|
# Insert the entity
|
||||||
cik = data['cik']
|
cik = data.get('cik')
|
||||||
entity_name = data['entityName']
|
entity_name = data.get('entityName')
|
||||||
insert_entity(cursor, cik, entity_name)
|
insert_entity(cursor, cik, entity_name)
|
||||||
|
|
||||||
# Iterate over facts
|
# 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)
|
insert_data(cursor, cik, fact_id, start, end, val, accn, fy, fp, form, filed, frame)
|
||||||
|
|
||||||
def main():
|
# Commit the transaction for the whole file
|
||||||
# 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
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error occurred: {e}")
|
print(f"Error occurred while processing {file_path}: {e}")
|
||||||
conn.rollback()
|
conn.rollback()
|
||||||
|
return False
|
||||||
finally:
|
finally:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.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__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user