Add option to not fetch financial data to make the script and process more effective.

This commit is contained in:
2025-07-25 11:40:50 +02:00
parent c9e754c9c9
commit 841f6a5305

View File

@@ -66,13 +66,16 @@ def get_financial_data_via_fetcher(ticker_symbol):
return financials 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. """ """ Scans subreddits and uses the fetcher to get financial data. """
conn = database.get_db_connection() conn = database.get_db_connection()
post_age_limit = days_to_scan * 86400 post_age_limit = days_to_scan * 86400
current_time = time.time() current_time = time.time()
log.info(f"Scanning {len(subreddits_list)} subreddit(s) for NEW posts in the last {days_to_scan} day(s)...") 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: for subreddit_name in subreddits_list:
try: try:
normalized_sub_name = subreddit_name.lower() 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) 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) 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: if fetch_financials:
log.debug(f" DEBUG: Checking ticker '{ticker_symbol}' for financial update.") for ticker_symbol in all_tickers_found_in_post:
ticker_id = database.get_or_create_entity(conn, 'tickers', 'symbol', ticker_symbol) ticker_id = database.get_or_create_entity(conn, 'tickers', 'symbol', ticker_symbol)
ticker_info = database.get_ticker_info(conn, ticker_id) 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 the state we are about to check log.info(f" -> Fetching financial data for {ticker_symbol}...")
log.debug(f" -> Ticker Info from DB: last_updated = {ticker_info['last_updated']}") financials = get_financial_data_via_fetcher(ticker_symbol)
database.update_ticker_financials(
needs_update = False conn, ticker_id,
if not ticker_info['last_updated']: financials.get('market_cap'),
log.debug(" -> Condition MET: 'last_updated' is NULL. Needs update.") financials.get('closing_price')
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')
)
all_comment_sentiments = [get_sentiment_score(c.body) for c in all_comments] 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 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("-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("-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("-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("--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.") parser.add_argument("--stdout", action="store_true", help="Print all log messages to the console.")
@@ -245,7 +234,8 @@ def main():
subreddits_to_scan, subreddits_to_scan,
post_limit=args.posts, post_limit=args.posts,
comment_limit=args.comments, 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__": if __name__ == "__main__":