Add option to not fetch financial data to make the script and process more effective.
This commit is contained in:
@@ -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,28 +115,13 @@ 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)
|
||||||
|
|
||||||
|
if fetch_financials:
|
||||||
for ticker_symbol in all_tickers_found_in_post:
|
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_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.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}...")
|
log.info(f" -> Fetching financial data for {ticker_symbol}...")
|
||||||
financials = get_financial_data_via_fetcher(ticker_symbol)
|
financials = get_financial_data_via_fetcher(ticker_symbol)
|
||||||
log.debug(f" -> Fetched data: {financials}")
|
|
||||||
database.update_ticker_financials(
|
database.update_ticker_financials(
|
||||||
conn, ticker_id,
|
conn, ticker_id,
|
||||||
financials.get('market_cap'),
|
financials.get('market_cap'),
|
||||||
@@ -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__":
|
||||||
|
Reference in New Issue
Block a user