# rstat_tool/main.py import argparse import json import os import time import praw import yfinance as yf from dotenv import load_dotenv from . import database from .ticker_extractor import extract_tickers from .sentiment_analyzer import get_sentiment_score load_dotenv() MARKET_CAP_REFRESH_INTERVAL = 86400 def load_subreddits(filepath): try: with open(filepath, 'r') as f: return json.load(f).get("subreddits", []) except (FileNotFoundError, json.JSONDecodeError) as e: print(f"Error loading config: {e}") return None def get_market_cap(ticker_symbol): try: ticker = yf.Ticker(ticker_symbol) return ticker.fast_info.get('marketCap') except Exception: return None def get_reddit_instance(): client_id = os.getenv("REDDIT_CLIENT_ID") client_secret = os.getenv("REDDIT_CLIENT_SECRET") user_agent = os.getenv("REDDIT_USER_AGENT") if not all([client_id, client_secret, user_agent]): print("Error: Reddit API credentials not found in .env file.") return None # --- THIS IS THE CORRECTED LINE --- # The argument is 'client_secret', not 'secret_client'. return praw.Reddit(client_id=client_id, client_secret=client_secret, user_agent=user_agent) def scan_subreddits(reddit, subreddits_list, post_limit=25, comment_limit=100): """Scans subreddits, analyzes posts and comments, and stores results in the database.""" conn = database.get_db_connection() print(f"\nScanning {len(subreddits_list)} subreddits (Top {post_limit} posts, {comment_limit} comments/post)...") for subreddit_name in subreddits_list: try: subreddit_id = database.get_or_create_entity(conn, 'subreddits', 'name', subreddit_name) subreddit = reddit.subreddit(subreddit_name) print(f"Scanning r/{subreddit_name}...") for submission in subreddit.hot(limit=post_limit): # --- LOGIC PART 1: PROCESS INDIVIDUAL MENTIONS --- # 1a. Process the Post Title and Body for mentions post_text = submission.title + " " + submission.selftext tickers_in_post = extract_tickers(post_text) post_sentiment = get_sentiment_score(submission.title) for ticker_symbol in set(tickers_in_post): ticker_id = database.get_or_create_entity(conn, 'tickers', 'symbol', ticker_symbol) database.add_mention(conn, ticker_id, subreddit_id, submission.id, int(submission.created_utc), post_sentiment) ticker_info = database.get_ticker_info(conn, ticker_id) current_time = int(time.time()) if not ticker_info['last_updated'] or (current_time - ticker_info['last_updated'] > MARKET_CAP_REFRESH_INTERVAL): print(f" -> Fetching market cap for {ticker_symbol}...") market_cap = get_market_cap(ticker_symbol) database.update_ticker_market_cap(conn, ticker_id, market_cap or ticker_info['market_cap']) # 1b. Process Comments for mentions submission.comments.replace_more(limit=0) for comment in submission.comments.list()[:comment_limit]: tickers_in_comment = extract_tickers(comment.body) if not tickers_in_comment: continue comment_sentiment = get_sentiment_score(comment.body) for ticker_symbol in set(tickers_in_comment): ticker_id = database.get_or_create_entity(conn, 'tickers', 'symbol', ticker_symbol) database.add_mention(conn, ticker_id, subreddit_id, submission.id, int(comment.created_utc), comment_sentiment) # --- LOGIC PART 2: DEEP DIVE ANALYSIS --- all_comment_sentiments = [] for comment in submission.comments.list()[:comment_limit]: all_comment_sentiments.append(get_sentiment_score(comment.body)) avg_sentiment = sum(all_comment_sentiments) / len(all_comment_sentiments) if all_comment_sentiments else 0 post_analysis_data = { "post_id": submission.id, "title": submission.title, "post_url": f"https://reddit.com{submission.permalink}", "subreddit_id": subreddit_id, "post_timestamp": int(submission.created_utc), "comment_count": len(all_comment_sentiments), "avg_comment_sentiment": avg_sentiment } database.add_or_update_post_analysis(conn, post_analysis_data) except Exception as e: print(f"Could not scan r/{subreddit_name}. Error: {e}") conn.close() print("\n--- Scan Complete ---") def main(): parser = argparse.ArgumentParser(description="Analyze stock ticker mentions on Reddit.", formatter_class=argparse.RawTextHelpFormatter) parser.add_argument("config_file", help="Path to the JSON file containing subreddits.") parser.add_argument("-p", "--posts", type=int, default=25, help="Number of posts to scan per subreddit.\n(Default: 25)") parser.add_argument("-c", "--comments", type=int, default=100, help="Number of comments to scan per post.\n(Default: 100)") parser.add_argument("-l", "--limit", type=int, default=20, help="Number of tickers to show in the final report.\n(Default: 20)") args = parser.parse_args() database.initialize_db() database.clean_stale_tickers() subreddits = load_subreddits(args.config_file) if not subreddits: return reddit = get_reddit_instance() if not reddit: return scan_subreddits(reddit, subreddits, post_limit=args.posts, comment_limit=args.comments) database.generate_summary_report(limit=args.limit) if __name__ == "__main__": main()