diff --git a/rstat_tool/database.py b/rstat_tool/database.py index 15aab5c..158a744 100644 --- a/rstat_tool/database.py +++ b/rstat_tool/database.py @@ -419,4 +419,34 @@ def get_top_weekly_ticker_symbols(): """ results = conn.execute(query, (seven_days_ago_timestamp,)).fetchall() conn.close() - return [row['symbol'] for row in results] # Return a simple list of strings \ No newline at end of file + return [row['symbol'] for row in results] # Return a simple list of strings + +def get_top_daily_ticker_symbols_for_subreddit(subreddit_name): + """Gets a list of the Top 10 daily ticker symbols for a specific subreddit.""" + conn = get_db_connection() + one_day_ago = datetime.now(timezone.utc) - timedelta(days=1) + one_day_ago_timestamp = int(one_day_ago.timestamp()) + query = """ + SELECT t.symbol FROM mentions m JOIN tickers t ON m.ticker_id = t.id + JOIN subreddits s ON m.subreddit_id = s.id + WHERE LOWER(s.name) = LOWER(?) AND m.mention_timestamp >= ? + GROUP BY t.symbol ORDER BY COUNT(m.id) DESC LIMIT 10; + """ + results = conn.execute(query, (subreddit_name, one_day_ago_timestamp,)).fetchall() + conn.close() + return [row['symbol'] for row in results] + +def get_top_weekly_ticker_symbols_for_subreddit(subreddit_name): + """Gets a list of the Top 10 weekly ticker symbols for a specific subreddit.""" + conn = get_db_connection() + seven_days_ago = datetime.now(timezone.utc) - timedelta(days=7) + seven_days_ago_timestamp = int(seven_days_ago.timestamp()) + query = """ + SELECT t.symbol FROM mentions m JOIN tickers t ON m.ticker_id = t.id + JOIN subreddits s ON m.subreddit_id = s.id + WHERE LOWER(s.name) = LOWER(?) AND m.mention_timestamp >= ? + GROUP BY t.symbol ORDER BY COUNT(m.id) DESC LIMIT 10; + """ + results = conn.execute(query, (subreddit_name, seven_days_ago_timestamp,)).fetchall() + conn.close() + return [row['symbol'] for row in results] \ No newline at end of file diff --git a/rstat_tool/main.py b/rstat_tool/main.py index b88600a..b375190 100644 --- a/rstat_tool/main.py +++ b/rstat_tool/main.py @@ -200,18 +200,37 @@ def main(): database.initialize_db() if args.update_top_tickers: - # --- Mode 1: Update Top Tickers --- log.critical("--- Starting Financial Data Update for Top Tickers ---") - top_daily = database.get_top_daily_ticker_symbols() - top_weekly = database.get_top_weekly_ticker_symbols() - unique_top_tickers = sorted(list(set(top_daily + top_weekly))) + + # 1. Start with an empty set to hold all unique tickers + tickers_to_update = set() + + # 2. Get the overall top tickers + log.info("-> Checking overall top daily and weekly tickers...") + top_daily_overall = database.get_top_daily_ticker_symbols() + top_weekly_overall = database.get_top_weekly_ticker_symbols() + tickers_to_update.update(top_daily_overall) + tickers_to_update.update(top_weekly_overall) + + # 3. Get all subreddits and loop through them + all_subreddits = database.get_all_scanned_subreddits() + log.info(f"-> Checking top tickers for {len(all_subreddits)} individual subreddit(s)...") + for sub_name in all_subreddits: + log.debug(f" -> Checking r/{sub_name}...") + top_daily_sub = database.get_top_daily_ticker_symbols_for_subreddit(sub_name) + top_weekly_sub = database.get_top_weekly_ticker_symbols_for_subreddit(sub_name) + tickers_to_update.update(top_daily_sub) + tickers_to_update.update(top_weekly_sub) + + unique_top_tickers = sorted(list(tickers_to_update)) if not unique_top_tickers: log.info("No top tickers found in the last week. Nothing to update.") else: - log.info(f"Found {len(unique_top_tickers)} unique tickers to update: {', '.join(unique_top_tickers)}") + log.info(f"Found {len(unique_top_tickers)} unique top tickers to update: {', '.join(unique_top_tickers)}") conn = database.get_db_connection() for ticker_symbol in unique_top_tickers: + # 4. Find the ticker's ID to perform the update ticker_info = database.get_ticker_by_symbol(ticker_symbol) if ticker_info: log.info(f" -> Updating financials for {ticker_info['symbol']}...") @@ -222,6 +241,7 @@ def main(): financials.get('closing_price') ) conn.close() + log.critical("--- Top Ticker Financial Data Update Complete ---") elif args.update_financials_only: