<filename>CmcAPI.py
import os
from coinmarketcapapi import CoinMarketCapAPI, CoinMarketCapAPIError

from dotenv import load_dotenv
load_dotenv()

cmc = CoinMarketCapAPI(os.getenv("CMC_API_KEY"))


def getCoinMap(limit=100):
    """Gets map of top 100 listed coins on CMC"""

    # TODO cache coin map
    try:
        response = cmc.cryptocurrency_map(
            limit=limit, sort="cmc_rank", aux="is_active")
        return response.data

    except CoinMarketCapAPIError as e:
        print(e.rep.error)
        return


def getCoinId(symbol: str) -> int:
    """Gets id of cryptocurrency symbol

    Parameters:
    symbol (str): Symbol of cryptocurrency

    Returns:
    int: Coin id
    """
    coin_map = getCoinMap()
    for coin in coin_map:
        if coin["symbol"] == symbol:
            return coin["id"]
    return None


def queryCoinInfo(id: str, currency="USD") -> dict:
    """Gets basic financial information of one coin"""

    try:
        latest_quote = cmc.cryptocurrency_quotes_latest(
            id=id, convert=currency)
        return latest_quote.data

    except CoinMarketCapAPIError as e:
        print(e.rep.error)
        return


# if __name__ == '__main__':
    # pass
    # id = getCoinId("ETH")
    # query = queryCoinInfo(id)
    # print(query["1027"]["name"])

<filename>CmcAPI.py
import os
from coinmarketcapapi import CoinMarketCapAPI, CoinMarketCapAPIError

from dotenv import load_dotenv
load_dotenv()

cmc = CoinMarketCapAPI(os.getenv("CMC_API_KEY"))


def getCoinMap(limit=100):
    """Gets map of top 100 listed coins on CMC"""

    # TODO cache coin map
    try:
        response = cmc.cryptocurrency_map(
            limit=limit, sort="cmc_rank", aux="is_active")
        return response.data

    except CoinMarketCapAPIError as e:
        print(e.rep.error)
        return


def getCoinId(symbol: str) -> int:
    """Gets id of cryptocurrency symbol

    Parameters:
    symbol (str): Symbol of cryptocurrency

    Returns:
    int: Coin id
    """
    coin_map = getCoinMap()
    for coin in coin_map:
        if coin["symbol"] == symbol:
            return coin["id"]
    return None


def queryCoinInfo(id: str, currency="USD") -> dict:
    """Gets basic financial information of one coin"""

    try:
        latest_quote = cmc.cryptocurrency_quotes_latest(
            id=id, convert=currency)
        return latest_quote.data

    except CoinMarketCapAPIError as e:
        print(e.rep.error)
        return


# if __name__ == '__main__':
    # pass
    # id = getCoinId("ETH")
    # query = queryCoinInfo(id)
    # print(query["1027"]["name"])

Gets map of top 100 listed coins on CMC # TODO cache coin map Gets id of cryptocurrency symbol      Parameters:     symbol (str): Symbol of cryptocurrency      Returns:     int: Coin id Gets basic financial information of one coin # if __name__ == '__main__': # pass # id = getCoinId("ETH") # query = queryCoinInfo(id) # print(query["1027"]["name"])