Added logger.
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
import sqlite3
|
||||
import time
|
||||
from .ticker_extractor import COMMON_WORDS_BLACKLIST
|
||||
from .logger_setup import get_logger
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
DB_FILE = "reddit_stocks.db"
|
||||
log = get_logger()
|
||||
|
||||
def get_db_connection():
|
||||
"""Establishes a connection to the SQLite database."""
|
||||
@@ -71,14 +73,14 @@ def initialize_db():
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Database initialized successfully.")
|
||||
log.info("Database initialized successfully.")
|
||||
|
||||
def clean_stale_tickers():
|
||||
"""
|
||||
Removes tickers and their associated mentions from the database
|
||||
if the ticker symbol exists in the COMMON_WORDS_BLACKLIST.
|
||||
"""
|
||||
print("\n--- Cleaning Stale Tickers from Database ---")
|
||||
log.info("\n--- Cleaning Stale Tickers from Database ---")
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
@@ -89,27 +91,27 @@ def clean_stale_tickers():
|
||||
stale_tickers = cursor.fetchall()
|
||||
|
||||
if not stale_tickers:
|
||||
print("No stale tickers to clean.")
|
||||
log.info("No stale tickers to clean.")
|
||||
conn.close()
|
||||
return
|
||||
|
||||
for ticker in stale_tickers:
|
||||
ticker_id = ticker['id']
|
||||
ticker_symbol = ticker['symbol']
|
||||
print(f"Removing stale ticker '{ticker_symbol}' (ID: {ticker_id})...")
|
||||
log.info(f"Removing stale ticker '{ticker_symbol}' (ID: {ticker_id})...")
|
||||
cursor.execute("DELETE FROM mentions WHERE ticker_id = ?", (ticker_id,))
|
||||
cursor.execute("DELETE FROM tickers WHERE id = ?", (ticker_id,))
|
||||
|
||||
deleted_count = conn.total_changes
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print(f"Cleanup complete. Removed {deleted_count} records.")
|
||||
log.info(f"Cleanup complete. Removed {deleted_count} records.")
|
||||
|
||||
def clean_stale_subreddits(active_subreddits):
|
||||
"""
|
||||
Removes all data associated with subreddits that are NOT in the active list.
|
||||
"""
|
||||
print("\n--- Cleaning Stale Subreddits from Database ---")
|
||||
log.info("\n--- Cleaning Stale Subreddits from Database ---")
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT id, name FROM subreddits")
|
||||
@@ -117,20 +119,20 @@ def clean_stale_subreddits(active_subreddits):
|
||||
stale_sub_ids = []
|
||||
for sub in db_subreddits:
|
||||
if sub['name'] not in active_subreddits:
|
||||
print(f"Found stale subreddit to remove: r/{sub['name']}")
|
||||
log.info(f"Found stale subreddit to remove: r/{sub['name']}")
|
||||
stale_sub_ids.append(sub['id'])
|
||||
if not stale_sub_ids:
|
||||
print("No stale subreddits to clean.")
|
||||
log.info("No stale subreddits to clean.")
|
||||
conn.close()
|
||||
return
|
||||
for sub_id in stale_sub_ids:
|
||||
print(f" -> Deleting associated data for subreddit ID: {sub_id}")
|
||||
log.info(f" -> Deleting associated data for subreddit ID: {sub_id}")
|
||||
cursor.execute("DELETE FROM mentions WHERE subreddit_id = ?", (sub_id,))
|
||||
cursor.execute("DELETE FROM posts WHERE subreddit_id = ?", (sub_id,))
|
||||
cursor.execute("DELETE FROM subreddits WHERE id = ?", (sub_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Stale subreddit cleanup complete.")
|
||||
log.info("Stale subreddit cleanup complete.")
|
||||
|
||||
def get_db_connection():
|
||||
conn = sqlite3.connect(DB_FILE)
|
||||
@@ -184,7 +186,7 @@ def initialize_db():
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Database initialized successfully.")
|
||||
log.info("Database initialized successfully.")
|
||||
|
||||
def add_mention(conn, ticker_id, subreddit_id, post_id, mention_type, timestamp, mention_sentiment, post_avg_sentiment=None):
|
||||
cursor = conn.cursor()
|
||||
@@ -230,7 +232,7 @@ def get_ticker_info(conn, ticker_id):
|
||||
|
||||
def generate_summary_report(limit=20):
|
||||
"""Queries the DB to generate a summary for the command-line tool."""
|
||||
print(f"\n--- Top {limit} Tickers by Mention Count ---")
|
||||
log.info(f"\n--- Top {limit} Tickers by Mention Count ---")
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
|
||||
|
Reference in New Issue
Block a user