From 6baf893823153dc445f3f1b1df255e25141941b7 Mon Sep 17 00:00:00 2001 From: MisterBiggs Date: Tue, 22 Jun 2021 00:08:04 -0700 Subject: [PATCH] Closes #70 --- IEX_Symbol.py | 7 +++++-- cg_Crypto.py | 28 +++++++++++++++++++++------- symbol_router.py | 4 ++-- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/IEX_Symbol.py b/IEX_Symbol.py index 5e9c0c7..3463ecd 100644 --- a/IEX_Symbol.py +++ b/IEX_Symbol.py @@ -476,7 +476,7 @@ class IEX_Symbol: return pd.DataFrame() def trending(self) -> list[str]: - """Gets current coins trending on IEX. Only returns when market is open. + """Gets current coins trending on IEX. Only returns when market is open. Returns ------- @@ -489,6 +489,9 @@ class IEX_Symbol: timeout=5, ) if stocks.status_code == 200: - return [f"${s['symbol']}: {s['companyName']}" for s in stocks.json()] + return [ + f"`${s['symbol']}`: {s['companyName']}, {s['changePercent']:.2f}%" + for s in stocks.json() + ] else: return ["Trending Stocks Currently Unavailable."] diff --git a/cg_Crypto.py b/cg_Crypto.py index 4318ced..8c5a37b 100644 --- a/cg_Crypto.py +++ b/cg_Crypto.py @@ -292,13 +292,27 @@ class cg_Crypto: "https://api.coingecko.com/api/v3/search/trending", timeout=5, ) - if coins.status_code == 200: - return [ - f"$${c['item']['symbol'].upper()}: {c['item']['name']}" - for c in coins.json()["coins"] - ] - else: - return ["Trending Coins Currently Unavailable."] + try: + trending = [] + if coins.status_code == 200: + for coin in coins.json()["coins"]: + c = coin["item"] + + sym = c["symbol"].upper() + name = c["name"] + change = r.get( + f"https://api.coingecko.com/api/v3/simple/price?ids={c['id']}&vs_currencies={self.vs_currency}&include_24hr_change=true" + ).json()[c["id"]]["usd_24h_change"] + + msg = f"`$${sym}`: {name}, {change:.2f}%" + + trending.append(msg) + + except Exception as e: + print(e) + trending = ["Trending Coins Currently Unavailable."] + + return trending def batch_price(self, coins: list[Coin]) -> list[str]: query = ",".join([c.id for c in coins]) diff --git a/symbol_router.py b/symbol_router.py index 8da9a44..a69f78c 100644 --- a/symbol_router.py +++ b/symbol_router.py @@ -326,7 +326,7 @@ class Router: stocks = self.stock.trending() coins = self.crypto.trending() - reply = "`Trending Stocks:\n" + reply = "Trending Stocks:\n" reply += "-" * len("Trending Stocks:") + "\n" for stock in stocks: reply += stock + "\n" @@ -336,7 +336,7 @@ class Router: for coin in coins: reply += coin + "\n" - return reply + "`" + return reply def random_pick(self) -> str: