Skip to content

Commit

Permalink
Fixed tweets to going to correct channel
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanAkkerman committed Aug 25, 2024
1 parent 55bd54a commit bbe872b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 37 deletions.
11 changes: 9 additions & 2 deletions src/cogs/loops/overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import util.vars
from api.http_client import get_json_data
from constants.config import config
from constants.logger import logger
from util.disc import get_channel, get_guild, loop_error_catcher
from util.formatting import format_change

Expand Down Expand Up @@ -181,10 +182,16 @@ async def make_overview(self, category: str, tickers: list, last_sentiment: str)

if category == "crypto":
# Delete previous message
await self.crypto_channel.purge(limit=1)
try:
await self.crypto_channel.purge(limit=1)
except discord.errors.NotFound:
logger.warn("Could not delete previous crypto overview message.")
await self.crypto_channel.send(embed=e)
else:
await self.stocks_channel.purge(limit=1)
try:
await self.stocks_channel.purge(limit=1)
except discord.errors.NotFound:
logger.warn("Could not delete previous stock overview message.")
await self.stocks_channel.send(embed=e)


Expand Down
42 changes: 23 additions & 19 deletions src/cogs/loops/timeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import asyncio
import datetime
import traceback
from typing import List, Optional
Expand Down Expand Up @@ -136,25 +137,27 @@ async def get_latest_tweet(self) -> None:
tweets = await get_tweet()
logger.debug(f"Got {len(tweets)} tweets.")

# Process tweets concurrently
tasks = []

# Loop from oldest to newest tweet
for tweet in reversed(tweets):
tweet = tweet["content"]

# Skip if the tweet is not a timeline item
if "entryType" in tweet:
if tweet["entryType"] != "TimelineTimelineItem":
continue
# Ignore popups about X Premium
else:
if "itemContent" in tweet:
if "itemType" in tweet["itemContent"]:
if (
tweet["itemContent"]["itemType"]
== "TimelineMessagePrompt"
):
continue

await self.on_data(tweet, update_tweet_id=True)
for tweet_data in reversed(tweets):
tweet = tweet_data["content"]

# Skip tweets that are not timeline items
if not (
tweet.get("entryType") == "TimelineTimelineItem"
and tweet.get("itemContent", {}).get("itemType")
!= "TimelineMessagePrompt"
):
continue

# Collect tasks for concurrent processing
tasks.append(self.on_data(tweet, update_tweet_id=True))

# Run tasks concurrently for faster processing
if tasks:
await asyncio.gather(*tasks)

async def on_data(self, tweet: dict, update_tweet_id: bool = False) -> None:
"""This method is called whenever data is received from the stream.
Expand Down Expand Up @@ -195,7 +198,8 @@ async def on_data(self, tweet: dict, update_tweet_id: bool = False) -> None:
self.bot,
)

# Upload the tweet to the Discord.
# Upload the tweet to the Discord..
logger.debug(f"Uploading {user_screen_name}'s tweet to {category}")
await self.upload_tweet(e, category, media, user_screen_name, base_symbols)

async def upload_tweet(
Expand Down
56 changes: 40 additions & 16 deletions src/util/tweet_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,27 @@ async def make_tweet_embed(
# Ensure the tickers are unique
symbols = get_clean_symbols(tickers, hashtags)[:24]

e = make_embed(symbols, url, text, profile_pic, images, e_title, media_types)
# Check for difference
if symbols != tickers + hashtags:
logger.debug(
f"Removed following symbols: {set(tickers + hashtags) - set(symbols)}"
)

e = make_embed(
symbols=symbols,
url=url,
text=text,
profile_pic=profile_pic,
images=images,
e_title=e_title,
media_types=media_types,
)

# Max 25 fields
if symbols:
logger.debug("Adding financials to tweet embed...")
logger.debug(f"Adding financials for symbols: {symbols}")
e, category, base_symbols = await add_financials(
e, symbols, tickers, text, user_name, bot
e=e, symbols=symbols, tickers=tickers, text=text, user=user_name, bot=bot
)

return e, category, base_symbols
Expand Down Expand Up @@ -185,6 +199,9 @@ async def add_financials(
The base symbols of the tickers.
"""
global tweet_overview
logger.debug(
f"Adding financials to the embed. For symbols: {symbols}, tickers: {tickers}"
)

# In case multiple tickers get send
crypto = stocks = forex = 0
Expand All @@ -200,7 +217,8 @@ async def add_financials(
util.vars.classified_tickers = remove_old_rows(util.vars.classified_tickers, 3)
classified_tickers = util.vars.classified_tickers["ticker"].tolist()

for ticker in symbols[24:]:
for symbol in symbols:
logger.debug(f"Symbol: {symbol}")
if crypto > stocks and crypto > forex:
majority = "crypto"
elif stocks > crypto and stocks > forex:
Expand All @@ -211,8 +229,10 @@ async def add_financials(
majority = "Unknown"

# Get the information about the ticker
if ticker not in classified_tickers:
ticker_info = await classify_ticker(ticker, majority)
if symbol not in classified_tickers:
logger.debug(f"Classifying ticker: {symbol} with majority: {majority}")
ticker_info = await classify_ticker(symbol, majority)

if ticker_info:
(
_,
Expand All @@ -224,20 +244,23 @@ async def add_financials(
one_d_ta,
base_symbol,
) = ticker_info
logger.debug(
f"Classified ticker: {symbol} as {base_symbol}. Website: {website}"
)

# Skip if this ticker has been done before, for instance in tweets containing Solana and SOL
if base_symbol in base_symbols:
continue

if exchanges is None:
exchanges = []
logger.warn("No exchanges found for", ticker)
logger.warn(f"No exchanges found for ticker: {symbol}")

# Convert info to a dataframe
df = pd.DataFrame(
[
{
"ticker": ticker,
"ticker": symbol,
"website": website,
# Db cannot handle lists, so we convert them to strings
"exchanges": (
Expand All @@ -255,17 +278,18 @@ async def add_financials(
)

else:
if ticker in tickers:
e.add_field(name=f"${ticker}", value=majority)
logger.debug(
f"No crypto or stock match found for ${ticker} in {user}'s tweet at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}"
)
if symbol in tickers:
e.add_field(name=f"${symbol}", value=majority)
logger.debug(
f"No crypto or stock match found for ${symbol} in {user}'s tweet at {datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}"
)

# Go to next in symbols
continue
else:
logger.debug(f"Found ticker {symbol} in previously classified tickers.")
ticker_info = util.vars.classified_tickers[
util.vars.classified_tickers["ticker"] == ticker
util.vars.classified_tickers["ticker"] == symbol
]
website = ticker_info["website"].values[0]
exchanges = ticker_info["exchanges"].values[0]
Expand All @@ -274,9 +298,9 @@ async def add_financials(
base_symbol = ticker_info["base_symbol"].values[0]

# Still need the price, change, TA info
price, change, four_h_ta, one_d_ta = await get_financials(ticker, website)
price, change, four_h_ta, one_d_ta = await get_financials(symbol, website)

title = f"${ticker}"
title = f"${symbol}"

# Add to base symbol list to prevent duplicates
base_symbols.append(base_symbol)
Expand Down

0 comments on commit bbe872b

Please sign in to comment.