From b81f26daa8faffda46f8954cd587d404407f3ace Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Wed, 11 Feb 2026 20:14:56 -0800 Subject: [PATCH] Finalize the finance cache puller --- database.py | 27 +++++++++++++++++++++++++++ main.py | 41 ++++++++--------------------------------- 2 files changed, 35 insertions(+), 33 deletions(-) create mode 100644 database.py diff --git a/database.py b/database.py new file mode 100644 index 00000000..ce227095 --- /dev/null +++ b/database.py @@ -0,0 +1,27 @@ +import sqlite3 + +class DataBase: + + def __init__(self): + # Initilize SqLite + self.conn = sqlite3.connect("./data/appdata.db") + self.cursor = self.conn.cursor() + + # Create keystore table if it doesnt exist already + self.cursor.execute(''' + CREATE TABLE IF NOT EXISTS keystore ( + key TEXT PRIMARY KEY, + value TEXT + ) + ''') + + # Set a key in the keystore + def SetKey(self, Key, Value): + self.cursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value)) + self.conn.commit() + + # Get a key from the keystore + def GetKey(self, Key): + self.cursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, )) + result = self.cursor.fetchone() + return result[0] if result else None \ No newline at end of file diff --git a/main.py b/main.py index 40cb69aa..a911c11d 100644 --- a/main.py +++ b/main.py @@ -1,45 +1,20 @@ -import sqlite3 from datetime import date, datetime from datapuller import DataPuller +from database import DataBase def main(): - # Initilize SqLite - dbconn = sqlite3.connect("./data/appdata.db") - dbcursor = dbconn.cursor() - # Create keystore table if it doesnt exist already - dbcursor.execute(''' - CREATE TABLE IF NOT EXISTS keystore ( - key TEXT PRIMARY KEY, - value TEXT - ) - ''') + # Load the database object + db = DataBase() - def SetKey(Key, Value): - dbcursor.execute('INSERT OR REPLACE INTO keystore (key, value) VALUES (?, ?)', (Key, Value)) - dbconn.commit() - - def GetKey(Key): - dbcursor.execute('SELECT value FROM keystore WHERE key = ?', (Key, )) - result = dbcursor.fetchone() - return result[0] if result else None - - # Pull the last run data - lastrundata = GetKey("LastRun") - lastrun = None - if lastrundata != None: - lastrun = datetime.strptime( lastrundata, "%Y-%m-%d" ) - else: - lastrun = datetime.strptime( "2000-01-01", "%Y-%m-%d" ) - - # If the strings dont match becuase were only checking day it has to be a new day so update the data - if lastrun != str(date.today()): - # This will update the data store in the data folder + # If we havent already pulled the stock data today + if db.GetKey("LastRun") != str(date.today()): + # Update the data store in the data folder DataPuller.pull() # Update the last run to today - SetKey("LastRun", str(date.today())) + db.SetKey("LastRun", str(date.today())) - # Load in the AI algorithm late so yfinance works properly + # Lazy Load in the AI algorithms so yfinance works properly import tensorflow as tf import keras from keras.layers import Dense, Flatten, Conv2D