update reddit tracker

This commit is contained in:
MuslemRahimi 2024-11-22 15:48:56 +01:00
parent 1c2e850b48
commit 3003a2fb20
3 changed files with 84 additions and 56 deletions

View File

@ -123,58 +123,72 @@ def compute_daily_statistics(file_path):
def compute_trending_tickers(daily_stats): def compute_trending_tickers(daily_stats):
today = datetime.now().date() today = datetime.now().date()
seven_days_ago = today - timedelta(days=14) period_list = [2,7,30,90]
res_dict = {}
trending = defaultdict(lambda: {'total': 0, 'PUT': 0, 'CALL': 0, 'sentiment': []})
for date, stats in daily_stats.items():
if seven_days_ago <= date <= today:
for ticker, counts in stats['ticker_mentions'].items():
trending[ticker]['total'] += counts['total']
trending[ticker]['PUT'] += counts['PUT']
trending[ticker]['CALL'] += counts['CALL']
trending[ticker]['sentiment'].extend(counts['sentiment'])
trending_list = [
{
'symbol': symbol,
'count': counts['total'],
'put': counts['PUT'],
'call': counts['CALL'],
'avgSentiment': round(sum(counts['sentiment']) / len(counts['sentiment']),2) if counts['sentiment'] else 0
}
for symbol, counts in trending.items() if symbol in total_symbols
]
trending_list.sort(key=lambda x: x['count'], reverse=True)
for item in trending_list: for time_period in period_list:
symbol = item['symbol'] res_list = []
try:
with open(f'json/quote/{symbol}.json') as f:
data = json.load(f)
name = data['name']
price = round(data['price'],2)
changes_percentage = round(data['changesPercentage'],2)
except Exception as e:
print(e)
name = None
price = None
changes_percentage = None
if symbol in stock_symbols: N_day_ago = today - timedelta(days=time_period)
item['assetType'] = 'stocks'
item['name'] = name trending = defaultdict(lambda: {'total': 0, 'PUT': 0, 'CALL': 0, 'sentiment': []})
item['price'] = price
item['changesPercentage'] = changes_percentage for date, stats in daily_stats.items():
elif symbol in etf_symbols: if N_day_ago <= date <= today:
item['assetType'] = 'etf' for ticker, counts in stats['ticker_mentions'].items():
item['name'] = name trending[ticker]['total'] += counts['total']
item['price'] = price trending[ticker]['PUT'] += counts['PUT']
item['changesPercentage'] = changes_percentage trending[ticker]['CALL'] += counts['CALL']
else: trending[ticker]['sentiment'].extend(counts['sentiment'])
item['assetType'] = ''
res_list = [
return trending_list {
'symbol': symbol,
'count': counts['total'],
'put': counts['PUT'],
'call': counts['CALL'],
'avgSentiment': round(sum(counts['sentiment']) / len(counts['sentiment']),2) if counts['sentiment'] else 0
}
for symbol, counts in trending.items() if symbol in total_symbols
]
res_list.sort(key=lambda x: x['count'], reverse=True)
for item in res_list:
symbol = item['symbol']
try:
with open(f'json/quote/{symbol}.json') as f:
data = json.load(f)
name = data['name']
price = round(data['price'],2)
changes_percentage = round(data['changesPercentage'],2)
except Exception as e:
print(e)
name = None
price = None
changes_percentage = None
if symbol in stock_symbols:
item['assetType'] = 'stocks'
item['name'] = name
item['price'] = price
item['changesPercentage'] = changes_percentage
elif symbol in etf_symbols:
item['assetType'] = 'etf'
item['name'] = name
item['price'] = price
item['changesPercentage'] = changes_percentage
else:
item['assetType'] = ''
if time_period == 2:
res_dict['oneDay'] = res_list
elif time_period == 7:
res_dict['oneWeek'] = res_list
elif time_period == 30:
res_dict['oneMonth'] = res_list
elif time_period == 90:
res_dict['threeMonths'] = res_list
return res_dict
# Usage # Usage
file_path = 'json/reddit-tracker/wallstreetbets/data.json' file_path = 'json/reddit-tracker/wallstreetbets/data.json'

View File

@ -48,8 +48,17 @@ data_changed = False
subreddit = reddit.subreddit("wallstreetbets") subreddit = reddit.subreddit("wallstreetbets")
# Iterate through new submissions # Iterate through new submissions
for submission in subreddit.new(limit=1000): for submission in subreddit.hot(limit=1000):
post_id = submission.id post_id = submission.id
# Check if the post was deleted by moderators
if submission.removed_by_category == "mod":
# Remove post from existing data if it was deleted by moderators
if post_id in existing_posts:
del existing_posts[post_id]
data_changed = True
continue # Skip this post
# Check if this post is already in our data # Check if this post is already in our data
if post_id in existing_posts: if post_id in existing_posts:
# Update existing post # Update existing post
@ -57,14 +66,20 @@ for submission in subreddit.new(limit=1000):
existing_posts[post_id]['num_comments'] = submission.num_comments existing_posts[post_id]['num_comments'] = submission.num_comments
data_changed = True data_changed = True
else: else:
# Try to get a high-quality thumbnail URL
thumbnail = None
if hasattr(submission, 'preview'):
thumbnail = submission.preview['images'][0]['source']['url']
# Extract the required fields for new post # Extract the required fields for new post
extracted_post = { extracted_post = {
"id": post_id, "id": post_id,
"permalink": submission.permalink, "permalink": submission.permalink,
"title": submission.title, "title": submission.title,
"thumbnail": thumbnail,
"selftext": submission.selftext, "selftext": submission.selftext,
"created_utc": int(submission.created_utc), "created_utc": int(submission.created_utc),
"upvote_ratio": submission.upvote_ratio, "upvote_ratio": round(submission.upvote_ratio * 100, 2),
"num_comments": submission.num_comments, "num_comments": submission.num_comments,
"link_flair_text": submission.link_flair_text, "link_flair_text": submission.link_flair_text,
"author": str(submission.author), "author": str(submission.author),
@ -74,7 +89,6 @@ for submission in subreddit.new(limit=1000):
existing_posts[post_id] = extracted_post existing_posts[post_id] = extracted_post
data_changed = True data_changed = True
if data_changed: if data_changed:
# Convert the dictionary back to a list and sort by created_utc # Convert the dictionary back to a list and sort by created_utc
updated_data = list(existing_posts.values()) updated_data = list(existing_posts.values())
@ -84,4 +98,4 @@ if data_changed:
save_data(updated_data) save_data(updated_data)
print(f"Data updated and saved to {file_path}") print(f"Data updated and saved to {file_path}")
else: else:
print("No new data to add or update.") print("No new data to add or update.")

View File

@ -3444,9 +3444,9 @@ async def get_reddit_tracker(api_key: str = Security(get_api_key)):
try: try:
with open(f"json/reddit-tracker/wallstreetbets/trending.json", 'rb') as file: with open(f"json/reddit-tracker/wallstreetbets/trending.json", 'rb') as file:
trending = orjson.loads(file.read())[0:5] trending = orjson.loads(file.read())
except: except:
trending = [] trending = {}
res = {'posts': latest_post, 'stats': stats, 'trending': trending} res = {'posts': latest_post, 'stats': stats, 'trending': trending}