Introduce context manager for DB connections. Add DB constants. Use contextmanager to fetch data.
This commit is contained in:
parent
af8db5cfee
commit
1c46709962
100
app/main.py
100
app/main.py
@ -19,6 +19,7 @@ from benzinga import financial_data
|
|||||||
|
|
||||||
# Database related imports
|
# Database related imports
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
from contextlib import contextmanager
|
||||||
from pocketbase import PocketBase
|
from pocketbase import PocketBase
|
||||||
|
|
||||||
# FastAPI and related imports
|
# FastAPI and related imports
|
||||||
@ -29,79 +30,82 @@ from fastapi.openapi.utils import get_openapi
|
|||||||
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
|
|
||||||
|
# DB constants & context manager
|
||||||
|
|
||||||
berlin_tz = pytz.timezone('Europe/Berlin')
|
STOCK_DB = 'stocks'
|
||||||
|
ETF_DB = 'etf'
|
||||||
|
CRYPTO_DB = 'crypto'
|
||||||
|
INSTITUTE_DB = 'institute'
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def db_connection(db_name):
|
||||||
|
conn = sqlite3.connect(f'{db_name}.db')
|
||||||
|
cursor = conn.cursor()
|
||||||
|
cursor.execute("PRAGMA journal_mode = wal")
|
||||||
|
try:
|
||||||
|
yield cursor
|
||||||
|
finally:
|
||||||
|
conn.commit()
|
||||||
|
cursor.close()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
################# Redis #################
|
||||||
redis_client = redis.Redis(host='localhost', port=6380, db=0)
|
redis_client = redis.Redis(host='localhost', port=6380, db=0)
|
||||||
redis_client.flushdb()
|
redis_client.flushdb() # TECH DEBT
|
||||||
|
|
||||||
|
|
||||||
caching_time = 3600*12 #Cache data for 12 hours
|
caching_time = 3600*12 #Cache data for 12 hours
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
|
||||||
#------Start Stocks DB------------#
|
#------Start Stocks DB------------#
|
||||||
con = sqlite3.connect('stocks.db')
|
with db_connection(STOCK_DB) as cursor:
|
||||||
cursor = con.cursor()
|
cursor.execute("SELECT DISTINCT symbol FROM stocks")
|
||||||
cursor.execute("PRAGMA journal_mode = wal")
|
symbols = [row[0] for row in cursor.fetchall()]
|
||||||
cursor.execute("SELECT DISTINCT symbol FROM stocks")
|
|
||||||
symbols = [row[0] for row in cursor.fetchall()]
|
|
||||||
|
|
||||||
con.commit()
|
cursor.execute("SELECT symbol, name, type FROM stocks")
|
||||||
|
raw_data = cursor.fetchall()
|
||||||
|
stock_list_data = [{
|
||||||
cursor.execute("SELECT symbol, name, type FROM stocks")
|
|
||||||
raw_data = cursor.fetchall()
|
|
||||||
stock_list_data = [{
|
|
||||||
'symbol': row[0],
|
'symbol': row[0],
|
||||||
'name': row[1],
|
'name': row[1],
|
||||||
'type': row[2].capitalize(),
|
'type': row[2].capitalize(),
|
||||||
} for row in raw_data]
|
} for row in raw_data]
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
#------End Stocks DB------------#
|
#------End Stocks DB------------#
|
||||||
|
|
||||||
#------Start ETF DB------------#
|
#------Start ETF DB------------#
|
||||||
etf_con = sqlite3.connect('etf.db')
|
with db_connection(ETF_DB) as cursor:
|
||||||
etf_cursor = etf_con.cursor()
|
cursor.execute("SELECT DISTINCT symbol FROM etfs")
|
||||||
etf_cursor.execute("PRAGMA journal_mode = wal")
|
etf_symbols = [row[0] for row in cursor.fetchall()]
|
||||||
etf_cursor.execute("SELECT DISTINCT symbol FROM etfs")
|
|
||||||
etf_symbols = [row[0] for row in etf_cursor.fetchall()]
|
cursor.execute("SELECT symbol, name, type FROM etfs")
|
||||||
etf_con.commit()
|
raw_data = cursor.fetchall()
|
||||||
etf_cursor.execute("SELECT symbol, name, type FROM etfs")
|
etf_list_data = [{
|
||||||
etf_raw_data = etf_cursor.fetchall()
|
|
||||||
etf_list_data = [{
|
|
||||||
'symbol': row[0],
|
'symbol': row[0],
|
||||||
'name': row[1],
|
'name': row[1],
|
||||||
'type': row[2].upper(),
|
'type': row[2].upper(),
|
||||||
} for row in etf_raw_data]
|
} for row in raw_data]
|
||||||
etf_cursor.close()
|
|
||||||
#------End ETF DB------------#
|
#------End ETF DB------------#
|
||||||
|
|
||||||
#------Start Crypto DB------------#
|
#------Start Crypto DB------------#
|
||||||
crypto_con = sqlite3.connect('crypto.db')
|
with db_connection(CRYPTO_DB) as cursor:
|
||||||
crypto_cursor = crypto_con.cursor()
|
cursor.execute("SELECT DISTINCT symbol FROM cryptos")
|
||||||
crypto_cursor.execute("PRAGMA journal_mode = wal")
|
crypto_symbols = [row[0] for row in cursor.fetchall()]
|
||||||
crypto_cursor.execute("SELECT DISTINCT symbol FROM cryptos")
|
|
||||||
crypto_symbols = [row[0] for row in crypto_cursor.fetchall()]
|
cursor.execute("SELECT symbol, name, type FROM cryptos")
|
||||||
crypto_con.commit()
|
raw_data = cursor.fetchall()
|
||||||
crypto_cursor.execute("SELECT symbol, name, type FROM cryptos")
|
crypto_list_data = [{
|
||||||
crypto_raw_data = crypto_cursor.fetchall()
|
|
||||||
crypto_list_data = [{
|
|
||||||
'symbol': row[0],
|
'symbol': row[0],
|
||||||
'name': row[1],
|
'name': row[1],
|
||||||
'type': row[2].capitalize(),
|
'type': row[2].capitalize(),
|
||||||
} for row in crypto_raw_data]
|
} for row in raw_data]
|
||||||
crypto_cursor.close()
|
|
||||||
#------End Crypto DB------------#
|
#------End Crypto DB------------#
|
||||||
|
|
||||||
|
#------Init Searchbar Data------------#
|
||||||
searchbar_data = stock_list_data + etf_list_data + crypto_list_data
|
searchbar_data = stock_list_data + etf_list_data + crypto_list_data
|
||||||
|
|
||||||
|
#------Start Institute DB------------#
|
||||||
con_inst = sqlite3.connect('institute.db')
|
with db_connection(INSTITUTE_DB) as cursor:
|
||||||
cursor_inst = con_inst.cursor()
|
cursor.execute("SELECT cik FROM institutes")
|
||||||
cursor_inst.execute("PRAGMA journal_mode = wal")
|
cik_list = [row[0] for row in cursor.fetchall()]
|
||||||
cursor_inst.execute("SELECT cik FROM institutes")
|
#------End Institute DB------------#
|
||||||
cik_list = [row[0] for row in cursor_inst.fetchall()]
|
|
||||||
con_inst.commit()
|
|
||||||
cursor_inst.close()
|
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user