Add deep dive function to get more into each single stock.

This commit is contained in:
2025-07-21 21:33:49 +02:00
parent b82ba39aab
commit 0c90fed0eb
9 changed files with 270 additions and 99 deletions

View File

@@ -52,6 +52,20 @@ def initialize_db():
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
post_id TEXT NOT NULL UNIQUE,
title TEXT NOT NULL,
post_url TEXT,
subreddit_id INTEGER,
post_timestamp INTEGER,
comment_count INTEGER,
avg_comment_sentiment REAL,
FOREIGN KEY (subreddit_id) REFERENCES subreddits (id)
)
""")
conn.commit()
conn.close()
print("Database initialized successfully.")
@@ -195,4 +209,40 @@ def get_all_scanned_subreddits():
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]
return [row['name'] for row in results]
def add_or_update_post_analysis(conn, post_data):
"""
Inserts a new post analysis record or updates an existing one.
This prevents duplicate entries for the same post.
"""
cursor = conn.cursor()
# Use the UNIQUE post_id to replace old data with new on conflict
cursor.execute(
"""
INSERT INTO posts (post_id, title, post_url, subreddit_id, post_timestamp, comment_count, avg_comment_sentiment)
VALUES (:post_id, :title, :post_url, :subreddit_id, :post_timestamp, :comment_count, :avg_comment_sentiment)
ON CONFLICT(post_id) DO UPDATE SET
comment_count = excluded.comment_count,
avg_comment_sentiment = excluded.avg_comment_sentiment;
""",
post_data
)
conn.commit()
def get_deep_dive_details(ticker_symbol):
"""
Gets all analyzed posts that mention a specific ticker.
"""
conn = get_db_connection()
query = """
SELECT DISTINCT p.*, s.name as subreddit_name FROM posts p
JOIN mentions m ON p.post_id = m.post_id
JOIN tickers t ON m.ticker_id = t.id
JOIN subreddits s ON p.subreddit_id = s.id
WHERE t.symbol = ?
ORDER BY p.post_timestamp DESC;
"""
results = conn.execute(query, (ticker_symbol,)).fetchall()
conn.close()
return results