Added web dashboard.

This commit is contained in:
2025-07-21 20:13:18 +02:00
parent e6a853ed38
commit b82ba39aab
9 changed files with 321 additions and 60 deletions

View File

@@ -2,3 +2,4 @@ yfinance
praw
python-dotenv
nltk
Flask

54
rstat_tool/dashboard.py Normal file
View File

@@ -0,0 +1,54 @@
# rstat_tool/dashboard.py
from flask import Flask, render_template
from .database import (
get_overall_summary,
get_subreddit_summary,
get_all_scanned_subreddits
)
app = Flask(__name__, template_folder='../templates')
@app.template_filter('format_mc')
def format_market_cap(mc):
"""Formats a large number into a readable market cap string."""
if mc is None or mc == 0:
return "N/A"
if mc >= 1e12:
return f"${mc/1e12:.2f}T"
elif mc >= 1e9:
return f"${mc/1e9:.2f}B"
elif mc >= 1e6:
return f"${mc/1e6:.2f}M"
else:
return f"${mc:,}"
@app.context_processor
def inject_subreddits():
"""Makes the list of all scanned subreddits available to every template."""
subreddits = get_all_scanned_subreddits()
return dict(subreddits=subreddits)
@app.route("/")
def index():
"""The handler for the main dashboard page."""
# --- CHANGE HERE: Limit the data to the top 10 ---
tickers = get_overall_summary(limit=10)
return render_template("index.html", tickers=tickers)
@app.route("/subreddit/<name>")
def subreddit_dashboard(name):
"""A dynamic route for per-subreddit dashboards."""
# --- CHANGE HERE: Limit the data to the top 10 ---
tickers = get_subreddit_summary(name, limit=10)
return render_template("subreddit.html", tickers=tickers, subreddit_name=name)
def start_dashboard():
"""The main function called by the 'rstat-dashboard' command."""
print("Starting Flask server...")
print("Open http://127.0.0.1:5000 in your browser.")
print("Press CTRL+C to stop the server.")
app.run(debug=True)
if __name__ == "__main__":
start_dashboard()

View File

@@ -2,7 +2,6 @@
import sqlite3
import time
# --- IMPORT ADDED BACK IN ---
from .ticker_extractor import COMMON_WORDS_BLACKLIST
DB_FILE = "reddit_stocks.db"
@@ -14,9 +13,13 @@ def get_db_connection():
return conn
def initialize_db():
# ... (This function is unchanged)
"""
Initializes the database and creates the necessary tables if they don't exist.
"""
conn = get_db_connection()
cursor = conn.cursor()
# --- Create tickers table ---
cursor.execute("""
CREATE TABLE IF NOT EXISTS tickers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -25,12 +28,16 @@ def initialize_db():
last_updated INTEGER
)
""")
# --- Create subreddits table ---
cursor.execute("""
CREATE TABLE IF NOT EXISTS subreddits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE
)
""")
# --- Create mentions table ---
cursor.execute("""
CREATE TABLE IF NOT EXISTS mentions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -44,11 +51,11 @@ def initialize_db():
UNIQUE(ticker_id, post_id, sentiment_score)
)
""")
conn.commit()
conn.close()
print("Database initialized successfully.")
# --- CLEANUP FUNCTION ADDED BACK IN ---
def clean_stale_tickers():
"""
Removes tickers and their associated mentions from the database
@@ -58,7 +65,6 @@ def clean_stale_tickers():
conn = get_db_connection()
cursor = conn.cursor()
# Find ticker IDs that match the blacklist
placeholders = ','.join('?' for _ in COMMON_WORDS_BLACKLIST)
query = f"SELECT id, symbol FROM tickers WHERE symbol IN ({placeholders})"
@@ -74,11 +80,7 @@ def clean_stale_tickers():
ticker_id = ticker['id']
ticker_symbol = ticker['symbol']
print(f"Removing stale ticker '{ticker_symbol}' (ID: {ticker_id})...")
# 1. Delete all mentions associated with this ticker ID
cursor.execute("DELETE FROM mentions WHERE ticker_id = ?", (ticker_id,))
# 2. Delete the ticker itself
cursor.execute("DELETE FROM tickers WHERE id = ?", (ticker_id,))
deleted_count = conn.total_changes
@@ -88,7 +90,7 @@ def clean_stale_tickers():
def add_mention(conn, ticker_id, subreddit_id, post_id, timestamp, sentiment):
# ... (This function is unchanged)
"""Adds a new mention with its sentiment score to the database."""
cursor = conn.cursor()
try:
cursor.execute(
@@ -99,49 +101,44 @@ def add_mention(conn, ticker_id, subreddit_id, post_id, timestamp, sentiment):
except sqlite3.IntegrityError:
pass
# ... (get_or_create_entity, update_ticker_market_cap, get_ticker_info are unchanged)
def get_or_create_entity(conn, table_name, column_name, value):
# ...
"""Generic function to get or create an entity and return its ID."""
cursor = conn.cursor()
cursor.execute(f"SELECT id FROM {table_name} WHERE {column_name} = ?", (value,))
result = cursor.fetchone()
if result: return result['id']
if result:
return result['id']
else:
cursor.execute(f"INSERT INTO {table_name} ({column_name}) VALUES (?)", (value,))
conn.commit()
return cursor.lastrowid
def update_ticker_market_cap(conn, ticker_id, market_cap):
# ...
"""Updates the market cap and timestamp for a specific ticker."""
cursor = conn.cursor()
current_timestamp = int(time.time())
cursor.execute("UPDATE tickers SET market_cap = ?, last_updated = ? WHERE id = ?", (market_cap, current_timestamp, ticker_id))
conn.commit()
def get_ticker_info(conn, ticker_id):
# ...
"""Retrieves all info for a specific ticker by its ID."""
cursor = conn.cursor()
cursor.execute("SELECT * FROM tickers WHERE id = ?", (ticker_id,))
return cursor.fetchone()
def generate_summary_report(limit=20):
# ... (This function is unchanged)
"""Queries the DB to generate a summary for the command-line tool."""
print(f"\n--- Top {limit} Tickers by Mention Count ---")
conn = get_db_connection()
cursor = conn.cursor()
query = """
SELECT
t.symbol,
t.market_cap,
COUNT(m.id) as mention_count,
t.symbol, t.market_cap, COUNT(m.id) as mention_count,
SUM(CASE WHEN m.sentiment_score > 0.1 THEN 1 ELSE 0 END) as bullish_mentions,
SUM(CASE WHEN m.sentiment_score < -0.1 THEN 1 ELSE 0 END) as bearish_mentions,
SUM(CASE WHEN m.sentiment_score BETWEEN -0.1 AND 0.1 THEN 1 ELSE 0 END) as neutral_mentions
FROM mentions m
JOIN tickers t ON m.ticker_id = t.id
GROUP BY t.symbol, t.market_cap
ORDER BY mention_count DESC
LIMIT ?;
FROM mentions m JOIN tickers t ON m.ticker_id = t.id
GROUP BY t.symbol, t.market_cap ORDER BY mention_count DESC LIMIT ?;
"""
results = cursor.execute(query, (limit,)).fetchall()
header = f"{'Ticker':<8} | {'Mentions':<8} | {'Bullish':<8} | {'Bearish':<8} | {'Neutral':<8} | {'Market Cap':<15}"
@@ -156,3 +153,46 @@ def generate_summary_report(limit=20):
else: market_cap_str = f"${mc/1e6:.2f}M"
print(f"{row['symbol']:<8} | {row['mention_count']:<8} | {row['bullish_mentions']:<8} | {row['bearish_mentions']:<8} | {row['neutral_mentions']:<8} | {market_cap_str:<15}")
conn.close()
def get_overall_summary(limit=50):
"""Gets the top tickers across all subreddits for the dashboard."""
conn = get_db_connection()
query = """
SELECT
t.symbol, t.market_cap, COUNT(m.id) as mention_count,
SUM(CASE WHEN m.sentiment_score > 0.1 THEN 1 ELSE 0 END) as bullish_mentions,
SUM(CASE WHEN m.sentiment_score < -0.1 THEN 1 ELSE 0 END) as bearish_mentions,
SUM(CASE WHEN m.sentiment_score BETWEEN -0.1 AND 0.1 THEN 1 ELSE 0 END) as neutral_mentions
FROM mentions m JOIN tickers t ON m.ticker_id = t.id
GROUP BY t.symbol, t.market_cap ORDER BY mention_count DESC LIMIT ?;
"""
results = conn.execute(query, (limit,)).fetchall()
conn.close()
return results
def get_subreddit_summary(subreddit_name, limit=50):
"""Gets the top tickers for a specific subreddit for the dashboard."""
conn = get_db_connection()
query = """
SELECT
t.symbol, t.market_cap, COUNT(m.id) as mention_count,
SUM(CASE WHEN m.sentiment_score > 0.1 THEN 1 ELSE 0 END) as bullish_mentions,
SUM(CASE WHEN m.sentiment_score < -0.1 THEN 1 ELSE 0 END) as bearish_mentions,
SUM(CASE WHEN m.sentiment_score BETWEEN -0.1 AND 0.1 THEN 1 ELSE 0 END) as neutral_mentions
FROM mentions m
JOIN tickers t ON m.ticker_id = t.id
JOIN subreddits s ON m.subreddit_id = s.id
WHERE s.name = ?
GROUP BY t.symbol, t.market_cap ORDER BY mention_count DESC LIMIT ?;
"""
results = conn.execute(query, (subreddit_name, limit)).fetchall()
conn.close()
return results
def get_all_scanned_subreddits():
"""Gets a unique list of all subreddits we have data for."""
# --- THIS IS THE CORRECTED LINE ---
conn = get_db_connection()
results = conn.execute("SELECT DISTINCT name FROM subreddits ORDER BY name ASC;").fetchall()
conn.close()
return [row['name'] for row in results]

View File

@@ -5,44 +5,50 @@ import re
# A set of common English words and acronyms that look like stock tickers.
# This helps reduce false positives.
COMMON_WORDS_BLACKLIST = {
"401K", "403B", "457B", "ABOVE", "AI", "ALL", "ALPHA", "AMA", "AMEX",
"AND", "ANY", "AR", "ARE", "AROUND", "ASSET", "AT", "ATH", "ATL", "AUD",
"BE", "BEAR", "BELOW", "BETA", "BIG", "BIS", "BLEND", "BOE", "BOJ",
"BOND", "BRB", "BRL", "BTC", "BTW", "BULL", "BUT", "BUY", "BUZZ", "CAD",
"CAN", "CEO", "CFO", "CHF", "CIA", "CNY", "COME", "COST", "COULD", "CPI",
"401K", "403B", "457B", "ABOUT", "ABOVE", "ADAM", "AEDT", "AEST", "AH",
"AI", "ALL", "ALPHA", "ALSO", "AM", "AMA", "AMEX", "AND", "ANY", "AR",
"ARE", "AROUND", "ASAP", "ASS", "ASSET", "AT", "ATH", "ATL", "ATM",
"AUD", "BE", "BEAR", "BELOW", "BETA", "BIG", "BIS", "BLEND", "BOE",
"BOJ", "BOMB", "BOND", "BOTS", "BRB", "BRL", "BS", "BST", "BTC", "BTW",
"BULL", "BUT", "BUY", "BUZZ", "CAD", "CAN", "CEO", "CEST", "CET", "CFO",
"CHF", "CIA", "CLOSE", "CNY", "COME", "COST", "COULD", "CPI", "CST",
"CTB", "CTO", "CYCLE", "CZK", "DAO", "DATE", "DAX", "DAY", "DCA", "DD",
"DEBT", "DIA", "DIV", "DJIA", "DKK", "DM", "DO", "DOGE", "DR", "EACH",
"EARLY", "EARN", "ECB", "EDGAR", "EDIT", "EPS", "ESG", "ETF", "ETH",
"EU", "EUR", "EV", "EVERY", "FAQ", "FAR", "FAST", "FBI", "FDA", "FIHTX",
"FINRA", "FINT", "FINTX", "FINTY", "FOMC", "FOMO", "FOR", "FRAUD",
"FRG", "FSPSX", "FTSE", "FUD", "FULL", "FUND", "FXAIX", "FXIAX", "FY",
"FYI", "FZROX", "GAIN", "GDP", "GET", "GBP", "GO", "GOAL", "GPU", "GRAB",
"GTG", "HAS", "HAVE", "HATE", "HEAR", "HEDGE", "HINT", "HKD", "HODL",
"HOLD", "HOUR", "HSA", "HUF", "IMHO", "IMO", "IN", "INR", "IPO", "IRA",
"IRS", "IS", "ISM", "IT", "IV", "IVV", "IWM", "JPY", "JUST", "KNOW",
"KRW", "LARGE", "LAST", "LATE", "LATER", "LBO", "LIKE", "LMAO", "LOL",
"LONG", "LOOK", "LOSS", "LOVE", "M&A", "MAKE", "MAX", "MC", "MID", "MIGHT",
"MIN", "ML", "MOASS", "MONTH", "MUST", "MXN", "MY", "NATO", "NEAR",
"NEED", "NEW", "NEXT", "NFA", "NFT", "NGMI", "NIGHT", "NO", "NOK", "NONE",
"NOT", "NOW", "NSA", "NULL", "NZD", "NYSE", "OF", "OK", "OLD", "ON",
"OP", "OR", "OTC", "OUGHT", "OUT", "OVER", "PE", "PEAK", "PEG",
"PLAN", "PLN", "PMI", "PPI", "PRICE", "PROFIT", "PSA", "Q1", "Q2", "Q3",
"Q4", "QQQ", "RBA", "RBNZ", "REIT", "REKT", "RH", "RISK", "ROE", "ROFL",
"ROI", "ROTH", "RSD", "RUB", "SAVE", "SCALP", "SCAM", "SCHB", "SEC",
"DEBT", "DIA", "DIV", "DJIA", "DKK", "DM", "DO", "DOGE", "DONT", "DR",
"EACH", "EARLY", "EARN", "ECB", "EDGAR", "EDIT", "EDT", "END", "EPS",
"ER", "ESG", "EST", "ETF", "ETH", "EU", "EUR", "EV", "EVERY", "FAQ",
"FAR", "FAST", "FBI", "FDA", "FIHTX", "FINRA", "FINT", "FINTX", "FINTY",
"FOMC", "FOMO", "FOR", "FRAUD", "FRG", "FSPSX", "FTSE", "FUCK", "FUD",
"FULL", "FUND", "FXAIX", "FXIAX", "FY", "FYI", "FZROX", "GAIN", "GBP",
"GDP", "GET", "GMT", "GO", "GOAL", "GPU", "GRAB", "GTG", "HAS", "HAVE",
"HATE", "HEAR", "HEDGE", "HIGH", "HINT", "HKD", "HODL", "HOLD", "HOUR",
"HSA", "HUF", "IF", "IMHO", "IMO", "IN", "INR", "IP", "IPO", "IRA",
"IRS", "IS", "ISM", "IST", "IT", "ITM", "IV", "IVV", "IWM", "JPOW",
"JPY", "JST", "JUST", "KARMA", "KNOW", "KO", "KRW", "LARGE", "LAST",
"LATE", "LATER", "LBO", "LEAP", "LEAPS", "LETS", "LFG", "LIKE", "LIMIT",
"LMAO", "LOL", "LONG", "LOOK", "LOSS", "LOVE", "LOW", "M&A", "MAKE",
"MAX", "MC", "ME", "MID", "MIGHT", "MIN", "ML", "MOASS", "MONTH", "MSK",
"MUST", "MXN", "MY", "NATO", "NEAR", "NEED", "NEVER", "NEW", "NEXT",
"NFA", "NFT", "NGMI", "NIGHT", "NO", "NOK", "NONE", "NOT", "NOW", "NSA",
"NULL", "NZD", "NYSE", "OF", "OK", "OLD", "ON", "OP", "OPEN", "OR",
"OTC", "OTM", "OUGHT", "OUT", "OVER", "OWN", "PC", "PDT", "PE", "PEAK",
"PEG", "PEW", "PLAN", "PLN", "PM", "PMI", "POS", "PPI", "PR", "PRICE",
"PROFIT", "PSA", "PST", "Q1", "Q2", "Q3", "Q4", "QQQ", "RBA", "RBNZ",
"RE", "REAL", "REIT", "REKT", "RH", "RIP", "RISK", "ROE", "ROFL", "ROI",
"ROTH", "RSD", "RUB", "RULE", "SAVE", "SCALP", "SCAM", "SCHB", "SEC",
"SEE", "SEK", "SELL", "SEP", "SGD", "SHALL", "SHARE", "SHORT", "SO",
"SOME", "SOON", "SPAC", "SPEND", "SPLG", "SPX", "SPY", "STILL", "STOCK",
"SWING", "TAKE", "TERM", "THE", "THINK", "THIS", "TIME", "TL", "TL;DR",
"TLDR", "TODAY", "TO", "TOTAL", "TRADE", "TREND", "TRUE", "TRY", "TTYL",
"TWO", "UK", "UNDER", "UP", "US", "USA", "USD", "VTI", "VALUE", "VOO",
"VR", "WAGMI", "WANT", "WATCH", "WAY", "WE", "WEB3", "WEEK", "WHO",
"WHY", "WILL", "WORTH", "WOULD", "WSB", "YET", "YIELD", "YOLO", "YOU",
"SOME", "SOON", "SP", "SPAC", "SPEND", "SPLG", "SPX", "SPY", "START",
"STILL", "STOCK", "STOP", "SWING", "TAKE", "TERM", "THAT", "THE", "THINK",
"THIS", "TIME", "TITS", "TL", "TL;DR", "TLDR", "TO", "TODAY", "TOTAL",
"TRADE", "TREND", "TRUE", "TRY", "TTYL", "TWO", "UI", "UK", "UNDER",
"UP", "US", "USA", "USD", "UTC", "VTI", "VALUE", "VOO", "VR", "WAGMI",
"WANT", "WATCH", "WAY", "WE", "WEB3", "WEEK", "WHO", "WHY", "WILL",
"WORTH", "WOULD", "WSB", "WTF", "YET", "YIELD", "YES", "YOLO", "YOU",
"ZAR",
"KARMA", "OTM", "ITM", "ATM", "JPOW", "OPEN", "CLOSE", "HIGH", "LOW",
"RE", "BS", "ASAP", "RULE", "REAL", "LIMIT", "STOP", "END", "START", "BOTS",
"UTC", "AH", "PM", "PR", "GMT", "EST", "CST", "PST", "BST", "AEDT", "AEST",
"CET", "CEST", "EDT", "IST", "JST", "MSK", "PDT", "PST", "YES", "NO", "OWN",
"BOMB",
"YOUR", "BABY", "BAG", "BAGS", "GL", "GLHF", "EOD", "EOW", "EOY", "GOING", "KEEP",
"MORE", "PUT", "CALL", "YTD", "BOTH", "BUST", "EVEN", "FROM", "GOAT", "HALF",
"SL", "OS", "SOLIS", "OEM", "MA", "DOE", "II", "CHIPS"
}
def extract_tickers(text):
"""
Extracts potential stock tickers from a given piece of text.

View File

@@ -18,6 +18,7 @@ setup(
'console_scripts': [
# The path is now 'package_name.module_name:function_name'
'rstat=rstat_tool.main:main',
'rstat-dashboard=rstat_tool.dashboard:start_dashboard',
],
},
)

View File

@@ -2,6 +2,8 @@
"subreddits": [
"pennystocks",
"Shortsqueeze",
"smallstreetbets"
"smallstreetbets",
"wallstreetbets",
"Wallstreetbetsnew"
]
}

87
templates/base.html Normal file
View File

@@ -0,0 +1,87 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}Reddit Stock Dashboard{% endblock %}</title>
<style>
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
background-color: #f4f7f6;
color: #333;
margin: 0;
line-height: 1.6;
}
.navbar {
background-color: #ffffff;
padding: 1rem 2rem;
border-bottom: 1px solid #e0e0e0;
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.navbar a {
color: #555;
text-decoration: none;
font-weight: 600;
padding: 0.5rem 1rem;
border-radius: 6px;
transition: background-color 0.2s ease-in-out, color 0.2s ease-in-out;
}
.navbar a:hover {
background-color: #e9ecef;
color: #000;
}
.container {
max-width: 1000px;
margin: 2rem auto;
padding: 2rem;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}
h1 {
font-size: 1.75rem;
font-weight: 700;
margin-top: 0;
border-bottom: 1px solid #eee;
padding-bottom: 0.5rem;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 2rem;
font-size: 0.95rem;
}
th, td {
padding: 1rem;
text-align: left;
border-bottom: 1px solid #e0e0e0;
}
th {
font-weight: 600;
text-transform: uppercase;
font-size: 0.8rem;
letter-spacing: 0.05em;
color: #666;
}
tr:last-child td {
border-bottom: none;
}
.sentiment-bullish { color: #28a745; font-weight: 600; }
.sentiment-bearish { color: #dc3545; font-weight: 600; }
.sentiment-neutral { color: #6c757d; }
</style>
</head>
<body>
<header class="navbar">
<a href="/">Overall</a>
{% for sub in subreddits %}
<a href="/subreddit/{{ sub }}">r/{{ sub }}</a>
{% endfor %}
</header>
<main class="container">
{% block content %}{% endblock %}
</main>
</body>
</html>

35
templates/index.html Normal file
View File

@@ -0,0 +1,35 @@
{% extends "base.html" %}
{% block title %}Overall Dashboard{% endblock %}
{% block content %}
<h1>Top 10 Tickers (All Subreddits)</h1>
<table>
<thead>
<tr>
<th>Ticker</th>
<th>Mentions</th>
<th>Market Cap</th>
<th>Sentiment</th>
</tr>
</thead>
<tbody>
{% for ticker in tickers %}
<tr>
<td><strong>{{ ticker.symbol }}</strong></td>
<td>{{ ticker.mention_count }}</td>
<td>{{ ticker.market_cap | format_mc }}</td>
<td>
{% if ticker.bullish_mentions > ticker.bearish_mentions %}
<span class="sentiment-bullish">Bullish</span>
{% elif ticker.bearish_mentions > ticker.bullish_mentions %}
<span class="sentiment-bearish">Bearish</span>
{% else %}
<span class="sentiment-neutral">Neutral</span>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}

35
templates/subreddit.html Normal file
View File

@@ -0,0 +1,35 @@
{% extends "base.html" %}
{% block title %}r/{{ subreddit_name }} Dashboard{% endblock %}
{% block content %}
<h1>Top 10 Tickers in r/{{ subreddit_name }}</h1>
<table>
<thead>
<tr>
<th>Ticker</th>
<th>Mentions</th>
<th>Market Cap</th>
<th>Sentiment</th>
</tr>
</thead>
<tbody>
{% for ticker in tickers %}
<tr>
<td><strong>{{ ticker.symbol }}</strong></td>
<td>{{ ticker.mention_count }}</td>
<td>{{ ticker.market_cap | format_mc }}</td>
<td>
{% if ticker.bullish_mentions > ticker.bearish_mentions %}
<span class="sentiment-bullish">Bullish</span>
{% elif ticker.bearish_mentions > ticker.bullish_mentions %}
<span class="sentiment-bearish">Bearish</span>
{% else %}
<span class="sentiment-neutral">Neutral</span>
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}