-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload_price.py
62 lines (50 loc) · 1.94 KB
/
upload_price.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from bovespa_extractor.connection.database import Postgres
from bovespa_extractor.common.utils import load_file_json
import glob
import csv
configs_db = load_file_json("./configuration/database.json")
configs_main = load_file_json("./configuration/extraction_main.json")
data_stock_dir = configs_main.get("DATA_STOCK_DIR")
data_index_dir = configs_main.get("DATA_INDEX_DIR")
def upload_files_data_to_db(data_dir, schema):
cursor = psql.get_cursor
files_to_upload = [file for file in glob.glob("{dir}*.csv".format(dir=data_dir))]
for file in files_to_upload:
table_name = file.split("/")[-1].split(".")[0]
cursor.execute(
"""
CREATE TABLE {schema}.{table_name} (
date date PRIMARY KEY,
open float,
high float,
low float,
close float,
adj_close float,
volume float
)
""".format(schema=schema, table_name=table_name)
)
with open(file, "r") as f:
reader = csv.reader(f)
next(reader)
for row in reader:
row = [None if value == "" else value for value in row]
cursor.execute(
"""
INSERT INTO {schema}.{table_name} (date, open, high, low, close, adj_close, volume)
VALUES (%s, %s, %s, %s, %s, %s, %s)
""".format(schema=schema, table_name=table_name),
row
)
psql.commit()
if __name__ == "__main__":
psql = Postgres(
host=configs_db.get("HOST"),
user=configs_db.get("USER"),
password=configs_db.get("PASSWORD"),
port=configs_db.get("PORT"),
database=configs_db.get("DATABASE")
)
upload_files_data_to_db(data_stock_dir, "stocks")
upload_files_data_to_db(data_index_dir, "indexes")
psql.close()