Process comments.
This commit is contained in:
75
main.py
75
main.py
@@ -11,29 +11,28 @@ from dotenv import load_dotenv
|
|||||||
|
|
||||||
import database
|
import database
|
||||||
from ticker_extractor import extract_tickers
|
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()
|
load_dotenv()
|
||||||
MARKET_CAP_REFRESH_INTERVAL = 86400
|
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):
|
def load_subreddits(filepath):
|
||||||
# ...
|
|
||||||
try:
|
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:
|
except (FileNotFoundError, json.JSONDecodeError) as e:
|
||||||
print(f"Error loading config: {e}")
|
print(f"Error loading config: {e}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_market_cap(ticker_symbol):
|
def get_market_cap(ticker_symbol):
|
||||||
# ...
|
|
||||||
try:
|
try:
|
||||||
ticker = yf.Ticker(ticker_symbol)
|
ticker = yf.Ticker(ticker_symbol)
|
||||||
return ticker.fast_info.get('marketCap')
|
return ticker.fast_info.get('marketCap')
|
||||||
except Exception: return None
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
def get_reddit_instance():
|
def get_reddit_instance():
|
||||||
# ...
|
|
||||||
client_id = os.getenv("REDDIT_CLIENT_ID")
|
client_id = os.getenv("REDDIT_CLIENT_ID")
|
||||||
client_secret = os.getenv("REDDIT_CLIENT_SECRET")
|
client_secret = os.getenv("REDDIT_CLIENT_SECRET")
|
||||||
user_agent = os.getenv("REDDIT_USER_AGENT")
|
user_agent = os.getenv("REDDIT_USER_AGENT")
|
||||||
@@ -42,12 +41,12 @@ def get_reddit_instance():
|
|||||||
return None
|
return None
|
||||||
return praw.Reddit(client_id=client_id, client_secret=client_secret, user_agent=user_agent)
|
return praw.Reddit(client_id=client_id, client_secret=client_secret, user_agent=user_agent)
|
||||||
|
|
||||||
|
# --- UPDATED: Function now accepts post_limit and comment_limit ---
|
||||||
def scan_subreddits(reddit, subreddits_list, post_limit=25):
|
def scan_subreddits(reddit, subreddits_list, post_limit=25, comment_limit=100):
|
||||||
"""Scans subreddits, performs sentiment analysis, and stores results in the database."""
|
"""Scans subreddits, analyzes posts and comments, and stores results in the database."""
|
||||||
conn = database.get_db_connection()
|
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:
|
for subreddit_name in subreddits_list:
|
||||||
try:
|
try:
|
||||||
subreddit_id = database.get_or_create_entity(conn, 'subreddits', 'name', subreddit_name)
|
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}...")
|
print(f"Scanning r/{subreddit_name}...")
|
||||||
|
|
||||||
for submission in subreddit.hot(limit=post_limit):
|
for submission in subreddit.hot(limit=post_limit):
|
||||||
# We analyze the title for sentiment as it's often the most concise summary.
|
# --- 1. Process the Post Title and Body ---
|
||||||
# Analyzing all comments could be a future enhancement.
|
post_text = submission.title + " " + submission.selftext
|
||||||
text_to_analyze = submission.title
|
tickers_in_post = extract_tickers(post_text)
|
||||||
tickers_in_post = extract_tickers(text_to_analyze + " " + submission.selftext)
|
post_sentiment = get_sentiment_score(submission.title)
|
||||||
|
|
||||||
# --- NEW: Get sentiment score for the post's title ---
|
|
||||||
sentiment = get_sentiment_score(text_to_analyze)
|
|
||||||
|
|
||||||
for ticker_symbol in set(tickers_in_post):
|
for ticker_symbol in set(tickers_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)
|
||||||
|
database.add_mention(conn, ticker_id, subreddit_id, submission.id, int(submission.created_utc), post_sentiment)
|
||||||
# --- NEW: Pass the sentiment score to the database ---
|
# (Market cap logic remains the same)
|
||||||
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)
|
|
||||||
ticker_info = database.get_ticker_info(conn, ticker_id)
|
ticker_info = database.get_ticker_info(conn, ticker_id)
|
||||||
current_time = int(time.time())
|
current_time = int(time.time())
|
||||||
if not ticker_info['last_updated'] or (current_time - ticker_info['last_updated'] > MARKET_CAP_REFRESH_INTERVAL):
|
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}...")
|
print(f" -> Fetching market cap for {ticker_symbol}...")
|
||||||
market_cap = get_market_cap(ticker_symbol)
|
market_cap = get_market_cap(ticker_symbol)
|
||||||
database.update_ticker_market_cap(conn, ticker_id, market_cap or ticker_info['market_cap'])
|
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:
|
except Exception as e:
|
||||||
print(f"Could not scan r/{subreddit_name}. Error: {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 ---")
|
print("\n--- Scan Complete ---")
|
||||||
|
|
||||||
def main():
|
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 = argparse.ArgumentParser(description="Analyze stock ticker mentions on Reddit.")
|
||||||
parser.add_argument("config_file", help="Path to the JSON file containing subreddits.")
|
parser.add_argument("config_file", help="Path to the JSON file containing subreddits.")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@@ -105,7 +109,8 @@ def main():
|
|||||||
reddit = get_reddit_instance()
|
reddit = get_reddit_instance()
|
||||||
if not reddit: return
|
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()
|
database.generate_summary_report()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@@ -1,9 +1,7 @@
|
|||||||
{
|
{
|
||||||
"subreddits": [
|
"subreddits": [
|
||||||
"wallstreetbets",
|
|
||||||
"stocks",
|
|
||||||
"investing",
|
|
||||||
"pennystocks",
|
"pennystocks",
|
||||||
"Shortsqueeze"
|
"Shortsqueeze",
|
||||||
|
"smallstreetbets"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@@ -5,47 +5,44 @@ import re
|
|||||||
# A set of common English words and acronyms that look like stock tickers.
|
# A set of common English words and acronyms that look like stock tickers.
|
||||||
# This helps reduce false positives.
|
# This helps reduce false positives.
|
||||||
COMMON_WORDS_BLACKLIST = {
|
COMMON_WORDS_BLACKLIST = {
|
||||||
"A", "I", "DD", "CEO", "CFO", "CTO", "EPS", "IPO", "YOLO", "FOMO",
|
"401K", "403B", "457B", "ABOVE", "AI", "ALL", "ALPHA", "AMA", "AMEX",
|
||||||
"TLDR", "EDIT", "THE", "AND", "FOR", "ARE", "BUT", "NOT", "YOU",
|
"AND", "ANY", "AR", "ARE", "AROUND", "ASSET", "AT", "ATH", "ATL", "AUD",
|
||||||
"ALL", "ANY", "CAN", "HAS", "NEW", "NOW", "OLD", "SEE", "TWO",
|
"BE", "BEAR", "BELOW", "BETA", "BIG", "BIS", "BLEND", "BOE", "BOJ",
|
||||||
"WAY", "WHO", "WHY", "BIG", "BUY", "SELL", "HOLD", "BE", "GO",
|
"BOND", "BRB", "BRL", "BTC", "BTW", "BULL", "BUT", "BUY", "BUZZ", "CAD",
|
||||||
"ON", "AT", "IN", "IS", "IT", "OF", "OR", "TO", "WE", "UP",
|
"CAN", "CEO", "CFO", "CHF", "CIA", "CNY", "COME", "COST", "COULD", "CPI",
|
||||||
"OUT", "SO", "RH", "SEC", "IRS", "USA", "UK", "EU",
|
"CTB", "CTO", "CYCLE", "CZK", "DAO", "DATE", "DAX", "DAY", "DCA", "DD",
|
||||||
"AI", "ML", "AR", "VR", "NFT", "DAO", "WEB3", "ETH", "BTC", "DOGE",
|
"DEBT", "DIA", "DIV", "DJIA", "DKK", "DM", "DO", "DOGE", "DR", "EACH",
|
||||||
"USD", "EUR", "GBP", "JPY", "CNY", "INR", "AUD", "CAD", "CHF",
|
"EARLY", "EARN", "ECB", "EDGAR", "EDIT", "EPS", "ESG", "ETF", "ETH",
|
||||||
"RUB", "ZAR", "BRL", "MXN", "HKD", "SGD", "NZD", "RSD",
|
"EU", "EUR", "EV", "EVERY", "FAQ", "FAR", "FAST", "FBI", "FDA", "FIHTX",
|
||||||
"JPY", "KRW", "SEK", "NOK", "DKK", "PLN", "CZK", "HUF", "TRY",
|
"FINRA", "FINT", "FINTX", "FINTY", "FOMC", "FOMO", "FOR", "FRAUD",
|
||||||
"US", "IRA", "FDA", "SEC", "FBI", "CIA", "NSA", "NATO", "FINRA",
|
"FRG", "FSPSX", "FTSE", "FUD", "FULL", "FUND", "FXAIX", "FXIAX", "FY",
|
||||||
"NASDAQ", "NYSE", "AMEX", "FTSE", "DAX", "WSB", "SPX", "DJIA",
|
"FYI", "FZROX", "GAIN", "GDP", "GET", "GBP", "GO", "GOAL", "GPU", "GRAB",
|
||||||
"EDGAR", "GDP", "CPI", "PPI", "PMI", "ISM", "FOMC", "ECB", "BOE",
|
"GTG", "HAS", "HAVE", "HATE", "HEAR", "HEDGE", "HINT", "HKD", "HODL",
|
||||||
"BOJ", "RBA", "RBNZ", "BIS", "NFA", "P", "VOO", "CTB", "DR",
|
"HOLD", "HOUR", "HSA", "HUF", "IMHO", "IMO", "IN", "INR", "IPO", "IRA",
|
||||||
"ETF", "EV", "ESG", "REIT", "SPAC", "IPO", "M&A", "LBO", "PE",
|
"IRS", "IS", "ISM", "IT", "IV", "IVV", "IWM", "JPY", "JUST", "KNOW",
|
||||||
"Q1", "Q2", "Q3", "Q4", "FY", "FAQ", "ROI", "ROE", "EPS", "P/E", "PEG",
|
"KRW", "LARGE", "LAST", "LATE", "LATER", "LBO", "LIKE", "LMAO", "LOL",
|
||||||
"FRG", "FXAIX", "FXIAX", "FZROX", "BULL", "BEAR", "BULLISH", "BEARISH",
|
"LONG", "LOOK", "LOSS", "LOVE", "M&A", "MAKE", "MAX", "MC", "MID", "MIGHT",
|
||||||
"QQQ", "SPY", "DIA", "IWM", "VTI", "VOO", "IVV", "SCHB", "SPLG",
|
"MIN", "ML", "MOASS", "MONTH", "MUST", "MXN", "MY", "NATO", "NEAR",
|
||||||
"ROTH", "IRA", "401K", "403B", "457B", "SEP", "SIMPLE", "HSA",
|
"NEED", "NEW", "NEXT", "NFA", "NFT", "NGMI", "NIGHT", "NO", "NOK", "NONE",
|
||||||
"LONG", "SHORT", "LEVERAGE", "MARGIN", "HEDGE", "SWING", "DAY",
|
"NOT", "NOW", "NSA", "NULL", "NZD", "NYSE", "OF", "OK", "OLD", "ON",
|
||||||
"GRAB", "GPU", "MY", "PSA", "AMA", "DM", "OP", "SPAC", "FIHTX",
|
"OP", "OR", "OTC", "OUGHT", "OUT", "OVER", "PE", "PEAK", "PEG",
|
||||||
"FINTX", "FINT", "FINTX", "FINTY", "FSPSX", "TOTAL", "LARGE", "MID", "SMALL",
|
"PLAN", "PLN", "PMI", "PPI", "PRICE", "PROFIT", "PSA", "Q1", "Q2", "Q3",
|
||||||
"GROWTH", "VALUE", "BLEND", "INCOME", "DIV", "YIELD", "BETA", "ALPHA", "VOLATILITY",
|
"Q4", "QQQ", "RBA", "RBNZ", "REIT", "REKT", "RH", "RISK", "ROE", "ROFL",
|
||||||
"RISK", "RETURN", "SHARPE", "SORTINO", "MAX", "MIN", "STDDEV", "VARIANCE",
|
"ROI", "ROTH", "RSD", "RUB", "SAVE", "SCALP", "SCAM", "SCHB", "SEC",
|
||||||
"PDF", "FULL", "PEAK", "LATE", "EARLY", "MIDDAY", "NIGHT", "MORNING", "AFTERNOON",
|
"SEE", "SEK", "SELL", "SEP", "SGD", "SHALL", "SHARE", "SHORT", "SO",
|
||||||
"CYCLE", "TREND", "PATTERN", "BREAKOUT", "PULLBACK", "REVERSAL", "CONSOLIDATION",
|
"SOME", "SOON", "SPAC", "SPEND", "SPLG", "SPX", "SPY", "STILL", "STOCK",
|
||||||
"OTC", "TRUE", "FALSE", "NULL", "NONE", "ALL", "ANY", "SOME", "EACH", "EVERY",
|
"SWING", "TAKE", "TERM", "THE", "THINK", "THIS", "TIME", "TL", "TL;DR",
|
||||||
"STILL", "TERM", "TIME", "DATE", "YEAR", "MONTH", "WEEK", "HOUR", "MINUTE", "SECOND",
|
"TLDR", "TODAY", "TO", "TOTAL", "TRADE", "TREND", "TRUE", "TRY", "TTYL",
|
||||||
"JUST", "ALREADY", "STILL", "YET", "NOW", "LATER", "SOON", "EARLIER", "TODAY", "TOMORROW",
|
"TWO", "UK", "UNDER", "UP", "US", "USA", "USD", "VTI", "VALUE", "VOO",
|
||||||
"YESTERDAY", "TONIGHT", "THIS", "LAST", "NEXT", "WOULD", "SHOULD", "COULD", "MIGHT",
|
"VR", "WAGMI", "WANT", "WATCH", "WAY", "WE", "WEB3", "WEEK", "WHO",
|
||||||
"WILL", "CAN", "MUST", "SHALL", "OUGHT", "TAKE", "MAKE", "HAVE", "GET", "DO", "BE",
|
"WHY", "WILL", "WORTH", "WOULD", "WSB", "YET", "YIELD", "YOLO", "YOU",
|
||||||
"GO", "COME", "SEE", "LOOK", "WATCH", "HEAR", "YES", "NO", "OK", "LIKE", "LOVE", "HATE",
|
"ZAR",
|
||||||
"WANT", "NEED", "THINK", "BELIEVE", "KNOW", "PRICE", "COST", "VALUE", "WORTH",
|
"KARMA", "OTM", "ITM", "ATM", "JPOW", "OPEN", "CLOSE", "HIGH", "LOW",
|
||||||
"EXPENSE", "SPEND", "SAVE", "EARN", "PROFIT", "LOSS", "GAIN", "DEBT", "CREDIT",
|
"RE", "BS", "ASAP", "RULE", "REAL", "LIMIT", "STOP", "END", "START", "BOTS",
|
||||||
"BOND", "STOCK", "SHARE", "FUND", "ASSET", "LIABILITY", "BUZZ", "UNDER", "OVER", "BETWEEN",
|
"UTC", "AH", "PM", "PR", "GMT", "EST", "CST", "PST", "BST", "AEDT", "AEST",
|
||||||
"FRAUD", "SCAM", "RISK", "REWARD", "RETURN", "INVEST", "TRADE", "BUY", "SELL", "HOLD",
|
"CET", "CEST", "EDT", "IST", "JST", "MSK", "PDT", "PST", "YES", "NO", "OWN",
|
||||||
"SHORT", "LONG", "LEVERAGE", "MARGIN", "HEDGE", "SCALP", "POSITION",
|
"BOMB",
|
||||||
"PLAN", "GOAL", "WILL", "FAST", "HINT", "ABOVE", "BELOW", "AROUND", "NEAR", "FAR",
|
|
||||||
"TL",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def extract_tickers(text):
|
def extract_tickers(text):
|
||||||
"""
|
"""
|
||||||
Extracts potential stock tickers from a given piece of text.
|
Extracts potential stock tickers from a given piece of text.
|
||||||
@@ -54,7 +51,7 @@ def extract_tickers(text):
|
|||||||
# Regex to find potential tickers:
|
# Regex to find potential tickers:
|
||||||
# 1. Words prefixed with $: $AAPL, $TSLA
|
# 1. Words prefixed with $: $AAPL, $TSLA
|
||||||
# 2. All-caps words between 1 and 5 characters: GME, AMC
|
# 2. All-caps words between 1 and 5 characters: GME, AMC
|
||||||
ticker_regex = r"\$[A-Z]{1,5}\b|\b[A-Z]{1,5}\b"
|
ticker_regex = r"\$[A-Z]{1,5}\b|\b[A-Z]{2,5}\b"
|
||||||
|
|
||||||
potential_tickers = re.findall(ticker_regex, text)
|
potential_tickers = re.findall(ticker_regex, text)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user