From f44edfd5947e6074ab30eda8f6a67f181879f4e5 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Sat, 27 Jul 2024 21:39:00 +0200 Subject: [PATCH] update reddit statistics --- app/cron_reddit_statistics.py | 63 ++++++++++++++++++++++++++++++----- app/main.py | 8 ++++- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/app/cron_reddit_statistics.py b/app/cron_reddit_statistics.py index dc32bbf..9c26f7a 100644 --- a/app/cron_reddit_statistics.py +++ b/app/cron_reddit_statistics.py @@ -2,18 +2,34 @@ import re import requests import praw -from datetime import datetime +from datetime import datetime, timedelta from collections import defaultdict - import os from dotenv import load_dotenv +import sqlite3 + +con = sqlite3.connect('stocks.db') + +cursor = con.cursor() +cursor.execute("PRAGMA journal_mode = wal") +cursor.execute("SELECT DISTINCT symbol FROM stocks") +stock_symbols = [row[0] for row in cursor.fetchall()] + +etf_con = sqlite3.connect('etf.db') +etf_cursor = etf_con.cursor() +etf_cursor.execute("PRAGMA journal_mode = wal") +etf_cursor.execute("SELECT DISTINCT symbol FROM etfs") +etf_symbols = [row[0] for row in etf_cursor.fetchall()] + +con.close() +etf_con.close() + load_dotenv() client_key = os.getenv('REDDIT_API_KEY') client_secret = os.getenv('REDDIT_API_SECRET') user_agent = os.getenv('REDDIT_USER_AGENT') - # Initialize Reddit instance reddit = praw.Reddit( client_id=client_key, @@ -22,11 +38,10 @@ ) # Function to save data -def save_data(data): - with open('json/reddit-tracker/wallstreetbets/stats.json', 'w', encoding='utf-8') as f: +def save_data(data, filename): + with open(f'json/reddit-tracker/wallstreetbets/{filename}', 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) - def compute_daily_statistics(file_path): # Load the data from the JSON file with open(file_path, 'r', encoding='utf-8') as f: @@ -72,9 +87,39 @@ def compute_daily_statistics(file_path): 'tickerMentions': dict(stats['ticker_mentions']) # Optional: include detailed ticker mentions }) - return formatted_stats + return formatted_stats, daily_stats + +# Function to compute trending tickers +def compute_trending_tickers(daily_stats): + today = datetime.now().date() + seven_days_ago = today - timedelta(days=14) + + trending = defaultdict(int) + + for date, stats in daily_stats.items(): + if seven_days_ago <= date <= today: + for ticker, count in stats['ticker_mentions'].items(): + trending[ticker] += count + + trending_list = [{'symbol': symbol, 'count': count} for symbol, count in trending.items()] + trending_list.sort(key=lambda x: x['count'], reverse=True) + + for item in trending_list: + symbol = item['symbol'] + if symbol in stock_symbols: + item['assetType'] = 'stocks' + elif symbol in etf_symbols: + item['assetType'] = 'etf' + else: + item['assetType'] = '' + + return trending_list # Usage file_path = 'json/reddit-tracker/wallstreetbets/data.json' -daily_statistics = compute_daily_statistics(file_path) -save_data(daily_statistics) \ No newline at end of file +daily_statistics, daily_stats_dict = compute_daily_statistics(file_path) +save_data(daily_statistics, 'stats.json') + +# Compute and save trending tickers +trending_tickers = compute_trending_tickers(daily_stats_dict) +save_data(trending_tickers, 'trending.json') \ No newline at end of file diff --git a/app/main.py b/app/main.py index fe9d84d..4a5046b 100755 --- a/app/main.py +++ b/app/main.py @@ -3295,7 +3295,13 @@ async def get_reddit_tracker(api_key: str = Security(get_api_key)): except: stats = [] - res = {'posts': latest_post, 'stats': stats} + try: + with open(f"json/reddit-tracker/wallstreetbets/trending.json", 'rb') as file: + trending = orjson.loads(file.read())[0:10] + except: + trending = [] + + res = {'posts': latest_post, 'stats': stats, 'trending': trending} data = orjson.dumps(res) compressed_data = gzip.compress(data)