mirror of
https://gitlab.com/simple-stock-bots/simple-telegram-stock-bot.git
synced 2025-06-16 15:06:53 +00:00
improved performance of getting crypto prices
This commit is contained in:
parent
6baf893823
commit
ae0075b986
18
cg_Crypto.py
18
cg_Crypto.py
@ -117,7 +117,7 @@ class cg_Crypto:
|
||||
self.searched_symbols[search] = symbol_list
|
||||
return symbol_list
|
||||
|
||||
def price_reply(self, symbol: Coin) -> str:
|
||||
def price_reply(self, coin: Coin) -> str:
|
||||
"""Returns current market price or after hours if its available for a given coin symbol.
|
||||
|
||||
Parameters
|
||||
@ -133,22 +133,22 @@ class cg_Crypto:
|
||||
"""
|
||||
|
||||
response = r.get(
|
||||
f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false",
|
||||
f"https://api.coingecko.com/api/v3/simple/price?ids={coin.id}&vs_currencies={self.vs_currency}&include_24hr_change=true",
|
||||
timeout=5,
|
||||
)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
|
||||
try:
|
||||
name = data["name"]
|
||||
price = data["market_data"]["current_price"][self.vs_currency]
|
||||
change = data["market_data"]["price_change_percentage_24h"]
|
||||
data = response.json()[coin.id]
|
||||
|
||||
price = data[self.vs_currency]
|
||||
change = data[self.vs_currency + "_24h_change"]
|
||||
if change is None:
|
||||
change = 0
|
||||
except KeyError:
|
||||
return f"{symbol} returned an error."
|
||||
return f"{coin.id} returned an error."
|
||||
|
||||
message = f"The current price of {name} is $**{price:,}**"
|
||||
message = f"The current price of {coin.name} is $**{price:,}**"
|
||||
|
||||
# Determine wording of change text
|
||||
if change > 0:
|
||||
@ -159,7 +159,7 @@ class cg_Crypto:
|
||||
message += ", the coin hasn't shown any movement today."
|
||||
|
||||
else:
|
||||
message = f"The Coin: {symbol.name} was not found."
|
||||
message = f"The Coin: {coin.name} was not found."
|
||||
|
||||
return message
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user