From 35b7eea34b00acaa17d1635c557400a8e9ff390d Mon Sep 17 00:00:00 2001 From: Amando Xu <83917723+PrettyAX@users.noreply.github.com> Date: Mon, 1 May 2023 23:42:52 -0400 Subject: [PATCH] Update to Cryptotracker Implemented a new API from API-ninjas that is able to return the favorite pairs of cryptocurrency and their USD prices. If the user inputs a pair that is not supported, it will query a different API to print out all of the pairs that are supported. --- jarviscli/plugins/cryptotracker.py | 42 ++++++++++++------------------ 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/jarviscli/plugins/cryptotracker.py b/jarviscli/plugins/cryptotracker.py index be309f4ad..1f29a481d 100644 --- a/jarviscli/plugins/cryptotracker.py +++ b/jarviscli/plugins/cryptotracker.py @@ -13,27 +13,13 @@ ] -def print_in_color(change): - """ - Prints the price change with color. If the price is down, print in red. - If the price is up, print in green. - Parameters - ---------- - change: str - The price change. - """ - if float(change) < 0: - return Fore.RED + str(change) + Fore.RESET - else: - return Fore.GREEN + str(change) + Fore.RESET def check_prices(base, target): """" It requires the base and target currency symbols, - e.g. [BTC, XRP], to build the URL and print the price - and the price change. + e.g. [BTC, XRP], to build the URL and print the price. Parameters ---------- @@ -44,38 +30,44 @@ def check_prices(base, target): """ # build the api url - url = 'https://api.cryptonator.com/api/ticker/{}-{}'.format( + url = 'https://api.api-ninjas.com/v1/cryptoprice?symbol={}{}'.format( base.lower(), target.lower()) try: response = requests.get( - url, headers={ - 'User-Agent': 'Jarvis'}).json() - price = response['ticker']['price'] - change = response['ticker']['change'] + url, headers={'X-Api-Key': 'NtgQxUQ02CDzcA5It1e/0w==25CTvSFoQ2nBBCSm'}).json() + price = response['price'] + # this error occurs if the pair is non-existent except KeyError: print( "{WARNING}Wrong pair {}/{}!{COLOR_RESET} " "\nFull list of symbols is here: " - "https://coinmarketcap.com/all/views/all/" "\n".format( base, target, WARNING=Fore.RED, COLOR_RESET=Fore.RESET)) - + api_url = 'https://api.api-ninjas.com/v1/cryptosymbols' + response = requests.get(api_url, headers={'X-Api-Key': 'NtgQxUQ02CDzcA5It1e/0w==25CTvSFoQ2nBBCSm'}).json() + if response['symbols']: + print("All possible pairs are: ") + for symbol in response['symbols']: + print(symbol) + else: + print("No symbols found") + print("Please format the query as 'cryptotracker XXX/XXX'") # results else: - print("\t{}/{}\nPrice: {}\nChange: {}\n".format(base.upper(), - target.upper(), price, print_in_color(change))) + print("\t{}/{}\nPrice: {}\n".format(base.upper(), + target.upper(), price)) @plugin("cryptotracker") def main(jarvis, s): """ - Finds the price and the change of the price, for + Finds the price for a pair of currencies or for the default list of favorite pairs. -- Example: cryptotracker BTC/USD