Process comments.

This commit is contained in:
2025-07-21 15:21:41 +02:00
parent b0125c35c3
commit 76e95e5373
3 changed files with 80 additions and 80 deletions

75
main.py
View File

@@ -11,29 +11,28 @@ from dotenv import load_dotenv
import database
from ticker_extractor import extract_tickers
from sentiment_analyzer import get_sentiment_score # <-- IMPORT OUR NEW MODULE
from sentiment_analyzer import get_sentiment_score
load_dotenv()
MARKET_CAP_REFRESH_INTERVAL = 86400
# ... (load_subreddits, get_market_cap, get_reddit_instance functions are unchanged)
# (load_subreddits, get_market_cap, get_reddit_instance functions are unchanged)
def load_subreddits(filepath):
# ...
try:
with open(filepath, 'r') as f: return json.load(f).get("subreddits", [])
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
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")
@@ -42,12 +41,12 @@ def get_reddit_instance():
return None
return praw.Reddit(client_id=client_id, client_secret=client_secret, user_agent=user_agent)
def scan_subreddits(reddit, subreddits_list, post_limit=25):
"""Scans subreddits, performs sentiment analysis, and stores results in the database."""
# --- UPDATED: Function now accepts post_limit and comment_limit ---
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 for top {post_limit} posts...")
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)
@@ -55,34 +54,43 @@ def scan_subreddits(reddit, subreddits_list, post_limit=25):
print(f"Scanning r/{subreddit_name}...")
for submission in subreddit.hot(limit=post_limit):
# We analyze the title for sentiment as it's often the most concise summary.
# Analyzing all comments could be a future enhancement.
text_to_analyze = submission.title
tickers_in_post = extract_tickers(text_to_analyze + " " + submission.selftext)
# --- NEW: Get sentiment score for the post's title ---
sentiment = get_sentiment_score(text_to_analyze)
# --- 1. Process the Post Title and Body ---
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)
# --- NEW: Pass the sentiment score to the database ---
database.add_mention(
conn,
ticker_id=ticker_id,
subreddit_id=subreddit_id,
post_id=submission.id,
timestamp=int(submission.created_utc),
sentiment=sentiment # Pass the score here
)
# (The market cap update logic remains the same)
database.add_mention(conn, ticker_id, subreddit_id, submission.id, int(submission.created_utc), post_sentiment)
# (Market cap logic remains the same)
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'])
# --- 2. Process the Comments ---
# Expand "MoreComments" objects. limit=None means we try to get all, but PRAW is protective.
# A limit of 32 is the max PRAW will do in a single call. We'll iterate to be safe.
submission.comments.replace_more(limit=10)
comment_count = 0
for comment in submission.comments.list():
if comment_count >= comment_limit:
break # Stop processing comments for this post if we hit our limit
tickers_in_comment = extract_tickers(comment.body)
if not tickers_in_comment:
continue # Skip comments that don't mention any tickers
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)
# We use the submission.id as the post_id to group mentions correctly
database.add_mention(conn, ticker_id, subreddit_id, submission.id, int(comment.created_utc), comment_sentiment)
comment_count += 1
except Exception as e:
print(f"Could not scan r/{subreddit_name}. Error: {e}")
@@ -91,10 +99,6 @@ def scan_subreddits(reddit, subreddits_list, post_limit=25):
print("\n--- Scan Complete ---")
def main():
# --- IMPORTANT: Delete your old DB file before running! ---
# Since we changed the schema and logic, old data won't have sentiment.
# It's best to start fresh. Delete the `reddit_stocks.db` file now.
parser = argparse.ArgumentParser(description="Analyze stock ticker mentions on Reddit.")
parser.add_argument("config_file", help="Path to the JSON file containing subreddits.")
args = parser.parse_args()
@@ -105,7 +109,8 @@ def main():
reddit = get_reddit_instance()
if not reddit: return
scan_subreddits(reddit, subreddits)
# We now pass the limits to the scan function
scan_subreddits(reddit, subreddits, post_limit=25, comment_limit=100)
database.generate_summary_report()
if __name__ == "__main__":