diff --git a/rstat_tool/main.py b/rstat_tool/main.py index f4ed819..d73671f 100644 --- a/rstat_tool/main.py +++ b/rstat_tool/main.py @@ -66,13 +66,16 @@ def get_financial_data_via_fetcher(ticker_symbol): return financials -def scan_subreddits(reddit, subreddits_list, post_limit=100, comment_limit=100, days_to_scan=1): +def scan_subreddits(reddit, subreddits_list, post_limit=100, comment_limit=100, days_to_scan=1, fetch_financials=True): """ Scans subreddits and uses the fetcher to get financial data. """ conn = database.get_db_connection() post_age_limit = days_to_scan * 86400 current_time = time.time() log.info(f"Scanning {len(subreddits_list)} subreddit(s) for NEW posts in the last {days_to_scan} day(s)...") + if not fetch_financials: + log.warning("NOTE: Financial data fetching is disabled for this run.") + for subreddit_name in subreddits_list: try: normalized_sub_name = subreddit_name.lower() @@ -112,33 +115,18 @@ def scan_subreddits(reddit, subreddits_list, post_limit=100, comment_limit=100, ticker_id = database.get_or_create_entity(conn, 'tickers', 'symbol', ticker_symbol) database.add_mention(conn, ticker_id, subreddit_id, submission.id, 'comment', int(comment.created_utc), comment_sentiment) - for ticker_symbol in all_tickers_found_in_post: - log.debug(f" DEBUG: Checking ticker '{ticker_symbol}' for financial update.") - ticker_id = database.get_or_create_entity(conn, 'tickers', 'symbol', ticker_symbol) - ticker_info = database.get_ticker_info(conn, ticker_id) - - # Log the state we are about to check - log.debug(f" -> Ticker Info from DB: last_updated = {ticker_info['last_updated']}") - - needs_update = False - if not ticker_info['last_updated']: - log.debug(" -> Condition MET: 'last_updated' is NULL. Needs update.") - needs_update = True - elif (current_time - ticker_info['last_updated'] > database.MARKET_CAP_REFRESH_INTERVAL): - log.debug(" -> Condition MET: Data is older than 24 hours. Needs update.") - needs_update = True - else: - log.debug(" -> Condition NOT MET: Data is fresh. Skipping update.") - - if needs_update: - log.info(f" -> Fetching financial data for {ticker_symbol}...") - financials = get_financial_data_via_fetcher(ticker_symbol) - log.debug(f" -> Fetched data: {financials}") - database.update_ticker_financials( - conn, ticker_id, - financials.get('market_cap'), - financials.get('closing_price') - ) + if fetch_financials: + for ticker_symbol in all_tickers_found_in_post: + ticker_id = database.get_or_create_entity(conn, 'tickers', 'symbol', ticker_symbol) + ticker_info = database.get_ticker_info(conn, ticker_id) + if not ticker_info['last_updated'] or (current_time - ticker_info['last_updated'] > database.MARKET_CAP_REFRESH_INTERVAL): + log.info(f" -> Fetching financial data for {ticker_symbol}...") + financials = get_financial_data_via_fetcher(ticker_symbol) + database.update_ticker_financials( + conn, ticker_id, + financials.get('market_cap'), + financials.get('closing_price') + ) all_comment_sentiments = [get_sentiment_score(c.body) for c in all_comments] avg_sentiment = sum(all_comment_sentiments) / len(all_comment_sentiments) if all_comment_sentiments else 0 @@ -173,6 +161,7 @@ def main(): parser.add_argument("-d", "--days", type=int, default=1, help="Number of past days to scan for new posts. (Default: 1)") parser.add_argument("-p", "--posts", type=int, default=200, help="Max posts to check per subreddit. (Default: 200)") parser.add_argument("-c", "--comments", type=int, default=100, help="Number of comments to scan per post. (Default: 100)") + parser.add_argument("--no-financials", action="store_true", help="Disable fetching of financial data during the Reddit scan.") parser.add_argument("--debug", action="store_true", help="Enable detailed debug logging to the console.") parser.add_argument("--stdout", action="store_true", help="Print all log messages to the console.") @@ -245,7 +234,8 @@ def main(): subreddits_to_scan, post_limit=args.posts, comment_limit=args.comments, - days_to_scan=args.days + days_to_scan=args.days, + fetch_financials=(not args.no_financials) # Pass the inverse of the flag ) if __name__ == "__main__":