112 lines
4.4 KiB
Python
112 lines
4.4 KiB
Python
# main.py
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import time
|
|
|
|
import praw
|
|
import yfinance as yf
|
|
from dotenv import load_dotenv
|
|
|
|
import database
|
|
from ticker_extractor import extract_tickers
|
|
from sentiment_analyzer import get_sentiment_score # <-- IMPORT OUR NEW MODULE
|
|
|
|
load_dotenv()
|
|
MARKET_CAP_REFRESH_INTERVAL = 86400
|
|
|
|
# ... (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", [])
|
|
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.")
|
|
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."""
|
|
conn = database.get_db_connection()
|
|
|
|
print(f"\nScanning {len(subreddits_list)} subreddits for top {post_limit} posts...")
|
|
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):
|
|
# 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)
|
|
|
|
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)
|
|
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'])
|
|
|
|
except Exception as e:
|
|
print(f"Could not scan r/{subreddit_name}. Error: {e}")
|
|
|
|
conn.close()
|
|
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()
|
|
|
|
database.initialize_db()
|
|
subreddits = load_subreddits(args.config_file)
|
|
if not subreddits: return
|
|
reddit = get_reddit_instance()
|
|
if not reddit: return
|
|
|
|
scan_subreddits(reddit, subreddits)
|
|
database.generate_summary_report()
|
|
|
|
if __name__ == "__main__":
|
|
main() |