mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 07:16:40 +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
|
self.searched_symbols[search] = symbol_list
|
||||||
return 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.
|
"""Returns current market price or after hours if its available for a given coin symbol.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
@ -133,22 +133,22 @@ class cg_Crypto:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
response = r.get(
|
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,
|
timeout=5,
|
||||||
)
|
)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
name = data["name"]
|
data = response.json()[coin.id]
|
||||||
price = data["market_data"]["current_price"][self.vs_currency]
|
|
||||||
change = data["market_data"]["price_change_percentage_24h"]
|
price = data[self.vs_currency]
|
||||||
|
change = data[self.vs_currency + "_24h_change"]
|
||||||
if change is None:
|
if change is None:
|
||||||
change = 0
|
change = 0
|
||||||
except KeyError:
|
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
|
# Determine wording of change text
|
||||||
if change > 0:
|
if change > 0:
|
||||||
@ -159,7 +159,7 @@ class cg_Crypto:
|
|||||||
message += ", the coin hasn't shown any movement today."
|
message += ", the coin hasn't shown any movement today."
|
||||||
|
|
||||||
else:
|
else:
|
||||||
message = f"The Coin: {symbol.name} was not found."
|
message = f"The Coin: {coin.name} was not found."
|
||||||
|
|
||||||
return message
|
return message
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user