myTS3/gecko.py

59 lines
1.2 KiB
Python

"""
TBD
"""
__author__ = "Lukas Mahler"
__version__ = "0.0.1"
__date__ = "23.09.2021"
__email__ = "m@hler.eu"
__status__ = "Development"
# Imports
from pycoingecko import CoinGeckoAPI
class GeckoAPI:
def __init__(self):
self.api = self.getAPI()
self.coinlist = self.getCoins()
@staticmethod
def getAPI():
"""
"""
api = CoinGeckoAPI()
if api.ping()['gecko_says'] == '(V3) To the Moon!':
return api
else:
raise ConnectionError("Couldnt connect to Coingecko API.")
def getCoins(self):
"""
"""
return self.api.get_coins_list()
def getSymbol(self, symbol, curr="EUR", decimal=2):
"""
"""
matching = [x for x in self.coinlist if x["symbol"] == symbol.lower()]
if len(matching) == 1:
resp_dict = self.api.get_price(ids=matching[0]["id"], vs_currencies=curr.lower())
price = resp_dict[matching[0]["id"]][curr.lower()]
if isinstance(price, int):
price = float(price) # Convert to always have floats
price = f"{price:.{decimal}f}"
return price
else:
return "No unique coin found"
if __name__ == "__main__":
exit()