Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Cryptotracker #1103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions jarviscli/plugins/cryptotracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand All @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This api key, is it yours? Hmm, not so nice.

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
Expand Down