Skip to content

Commit

Permalink
Added crypto categories
Browse files Browse the repository at this point in the history
close #439
Also some bug fixes
  • Loading branch information
StephanAkkerman committed Dec 11, 2023
1 parent ca0b314 commit 05602de
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 35 deletions.
48 changes: 25 additions & 23 deletions src/cogs/loops/nfts.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,29 @@ def __init__(self, bot: commands.Bot) -> None:
self.bot = bot

if config["LOOPS"]["NFTS"]["ENABLED"]:
self.top_channel = get_channel(
self.bot,
config["LOOPS"]["NFTS"]["TOP"],
config["CATEGORIES"]["NFTS"],
)

self.upcoming_channel = get_channel(
self.bot,
config["LOOPS"]["NFTS"]["UPCOMING"],
config["CATEGORIES"]["NFTS"],
)

self.p2e_channel = get_channel(
self.bot,
config["LOOPS"]["NFTS"]["P2E"],
config["CATEGORIES"]["NFTS"],
)

self.top_nfts.start()
self.upcoming_nfts.start()
self.top_p2e.start()
if config["LOOPS"]["NFTS"]["TOP"]:
self.top_channel = get_channel(
self.bot,
config["LOOPS"]["NFTS"]["TOP"]["CHANNEL"],
config["CATEGORIES"]["NFTS"],
)
self.top_nfts.start()

if config["LOOPS"]["NFTS"]["UPCOMING"]:
self.upcoming_channel = get_channel(
self.bot,
config["LOOPS"]["NFTS"]["UPCOMING"]["CHANNEL"],
config["CATEGORIES"]["NFTS"],
)
self.upcoming_nfts.start()

if config["LOOPS"]["NFTS"]["P2E"]:
self.p2e_channel = get_channel(
self.bot,
config["LOOPS"]["NFTS"]["P2E"]["CHANNEL"],
config["CATEGORIES"]["NFTS"],
)
self.top_p2e.start()

if config["LOOPS"]["TRENDING"]["NFTS"]:
self.trending_channel = get_channel(
Expand Down Expand Up @@ -264,7 +266,7 @@ async def top_p2e(self):
color=data_sources["playtoearn"]["color"],
timestamp=datetime.datetime.now(datetime.timezone.utc),
)

e.add_field(
name="Game",
value="\n".join(p2e["name"].tolist()),
Expand All @@ -282,7 +284,7 @@ async def top_p2e(self):
value="\n".join(p2e["status"].tolist()),
inline=True,
)

e.set_footer(
text="\u200b",
icon_url=data_sources["playtoearn"]["icon"],
Expand Down
14 changes: 12 additions & 2 deletions src/cogs/loops/timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,18 @@ async def get_latest_tweet(self) -> None:
tweet = tweet["content"]

# Skip if the tweet is not a timeline item
if tweet["entryType"] != "TimelineTimelineItem":
continue
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)

Expand Down
82 changes: 79 additions & 3 deletions src/cogs/loops/trending.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import datetime

# > 3rd party dependencies
import yahoo_fin.stock_info as si
import pandas as pd

# > Discord dependencies
import discord
from discord.ext import commands
from discord.ext.tasks import loop

# Local dependencies
from util.vars import config, get_json_data
from util.vars import config, get_json_data, data_sources
from util.disc_util import get_channel
from util.afterhours import afterHours
from util.formatting import format_embed
from util.cg_data import get_trending_coins
from util.formatting import (
format_embed,
format_change,
human_format,
format_embed_length,
)
from util.cg_data import get_trending_coins, get_top_categories


class Trending(commands.Cog):
Expand All @@ -32,6 +40,14 @@ def __init__(self, bot: commands.Bot) -> None:

self.crypto.start()

if config["LOOPS"]["CRYPTO_CATEGORIES"]["ENABLED"]:
self.crypto_categories_channel = get_channel(
self.bot,
config["LOOPS"]["CRYPTO_CATEGORIES"]["CHANNEL"],
)

self.crypto_categories.start()

if config["LOOPS"]["TRENDING"]["STOCKS"]["ENABLED"]:
self.stocks_channel = get_channel(
self.bot,
Expand Down Expand Up @@ -87,6 +103,66 @@ async def crypto(self) -> None:
await self.crypto_channel.send(embed=cg_e)
await self.crypto_channel.send(embed=cmc_e)

@loop(hours=1)
async def crypto_categories(self) -> None:
df = await get_top_categories()

# Only use top 10
df = df.head(10)

# Format the dataframe
# Merge name and link
df["Name"] = "[" + df["Name"] + "](" + df["Link"] + ")"

# Format 24h change
df["24h Change"] = df["24h Change"].apply(lambda x: format_change(x))

# Format the volume
df["Volume"] = df["Volume"].apply(lambda x: "$" + human_format(x))

# Format the market cap
df["Market Cap"] = df["Market Cap"].apply(lambda x: "$" + human_format(x))

# Get lists of each column
categories = "\n".join(df["Name"].tolist())
change = "\n".join(df["24h Change"].tolist())
volume = "\n".join(df["Volume"].tolist())

# Prevent possible overflow
categories, change, volume = format_embed_length([categories, change, volume])

e = discord.Embed(
title=f"Top {len(df)} Crypto Categories",
url="https://www.coingecko.com/en/categories",
description="",
color=data_sources["coingecko"]["color"],
timestamp=datetime.datetime.now(datetime.timezone.utc),
)

e.add_field(
name="Category",
value=categories,
inline=True,
)

e.add_field(
name="24h Change",
value=change,
inline=True,
)

e.add_field(
name="Volume",
value=volume,
inline=True,
)

# Set empty text as footer, so we can see the icon
e.set_footer(text="\u200b", icon_url=data_sources["coingecko"]["icon"])

await self.crypto_categories_channel.purge(limit=1)
await self.crypto_categories_channel.send(embed=e)

@loop(hours=1)
async def stocks(self) -> None:
"""
Expand Down
12 changes: 6 additions & 6 deletions src/util/cg_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ async def get_top_categories():

soup = BeautifulSoup(html, "html.parser")

table = soup.find("table", {"class": "sort table tw-mb-0 text-sm table-scrollable"})
table = soup.find("table")

data = []
for tr in table.find_all("tr")[1:]:
Expand All @@ -292,19 +292,19 @@ async def get_top_categories():
# i == 0 -> rank

if i == 1:
coin_data["name"] = td.find("a").text
coin_data["link"] = "https://www.coingecko.com/" + td.find("a")["href"]
coin_data["Name"] = td.find("a").text
coin_data["Link"] = "https://www.coingecko.com/" + td.find("a")["href"]

# 24h
if i == 4:
coin_data["24h"] = float(td["data-sort"])
coin_data["24h Change"] = td["data-sort"]

# Market cap
if i == 6:
coin_data["market_cap"] = float(td["data-sort"])
coin_data["Market Cap"] = td["data-sort"]

if i == 7:
coin_data["volume"] = float(td["data-sort"])
coin_data["Volume"] = td["data-sort"]

if coin_data != {}:
data.append(coin_data)
Expand Down
16 changes: 16 additions & 0 deletions src/util/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ def format_change(change: float) -> str:
str
The formatted change.
"""
if type(change) == str:
# Try to convert to float
try:
change = float(change)
except ValueError:
return 0

# Round to 2 decimals
change = round(change, 2)

return f"+{change}% 📈" if change > 0 else f"{change}% 📉"

Expand All @@ -47,6 +56,13 @@ def human_format(number: float, absolute: bool = False, decimals: int = 0) -> st
The formatted number as a string.
"""

# Try to convert to float
if type(number) == str:
try:
number = float(number)
except ValueError:
number = 0

if number == 0:
return "0"

Expand Down
2 changes: 1 addition & 1 deletion src/util/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"yahoo": {"color": 0x720E9E, "icon": icon_url + "yahoo.png"},
"binance": {"color": 0xF0B90B, "icon": icon_url + "binance.png"},
"investing": {"color": 0xDC8F02, "icon": icon_url + "investing.png"},
"coingecko": {"color": 0x0D3EFD, "icon": icon_url + "coingecko.png"},
"coingecko": {"color": 0x8AC14B, "icon": icon_url + "coingecko.png"},
"opensea": {"color": 0x3685DF, "icon": icon_url + "opensea.png"},
"coinmarketcap": {"color": 0x0D3EFD, "icon": icon_url + "cmc.ico"},
"playtoearn": {"color": 0x4792C9, "icon": icon_url + "playtoearn.png"},
Expand Down

0 comments on commit 05602de

Please sign in to comment.