Skip to content

Commit 02ae829

Browse files
committed
Switching to CoinMarketCap's official API
1 parent b7641df commit 02ae829

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

plugins/cryptocurrency.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
Created By:
77
- Luke Rogers <https://github.com/lukeroge>
88
9-
Special Thanks:
10-
- https://coinmarketcap-nexuist.rhcloud.com/
11-
129
License:
1310
GPL v3
1411
"""
@@ -19,29 +16,29 @@
1916

2017
from cloudbot import hook
2118

22-
API_URL = "https://coinmarketcap-nexuist.rhcloud.com/api/{}"
19+
API_URL = "https://api.coinmarketcap.com/v1/ticker/{}?convert={}"
2320

2421

2522
# aliases
2623
@hook.command("bitcoin", "btc", autohelp=False)
2724
def bitcoin(text):
2825
""" -- Returns current bitcoin value """
2926
# alias
30-
return crypto_command(" ".join(["btc", text]))
27+
return crypto_command(" ".join(["bitcoin", text]))
3128

3229

3330
@hook.command("litecoin", "ltc", autohelp=False)
3431
def litecoin(text):
3532
""" -- Returns current litecoin value """
3633
# alias
37-
return crypto_command(" ".join(["ltc", text]))
34+
return crypto_command(" ".join(["litecoin", text]))
3835

3936

4037
@hook.command("dogecoin", "doge", autohelp=False)
4138
def dogecoin(text):
4239
""" -- Returns current dogecoin value """
4340
# alias
44-
return crypto_command(" ".join(["doge", text]))
41+
return crypto_command(" ".join(["dogecoin", text]))
4542

4643

4744
# main command
@@ -53,12 +50,13 @@ def crypto_command(text):
5350

5451
try:
5552
if not args:
56-
currency = 'usd'
53+
currency = 'USD'
5754
else:
58-
currency = args.pop(0).lower()
55+
currency = args.pop(0).upper()
5956

60-
encoded = quote_plus(ticker)
61-
request = requests.get(API_URL.format(encoded))
57+
encoded_ticker = quote_plus(ticker)
58+
encoded_currency = quote_plus(currency)
59+
request = requests.get(API_URL.format(encoded_ticker, encoded_currency))
6260
request.raise_for_status()
6361
except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError) as e:
6462
return "Could not get value: {}".format(e)
@@ -68,30 +66,32 @@ def crypto_command(text):
6866
if "error" in data:
6967
return "{}.".format(data['error'])
7068

71-
updated_time = datetime.fromtimestamp(float(data['timestamp']))
69+
updated_time = datetime.fromtimestamp(float(data[0]['last_updated']))
7270
if (datetime.today() - updated_time).days > 2:
7371
# the API retains data for old ticker names that are no longer updated
7472
# in these cases we just return a "not found" message
7573
return "Currency not found."
7674

77-
change = float(data['change'])
75+
change = float(data[0]['percent_change_24h'])
7876
if change > 0:
7977
change_str = "\x033 {}%\x0f".format(change)
8078
elif change < 0:
8179
change_str = "\x035 {}%\x0f".format(change)
8280
else:
8381
change_str = "{}%".format(change)
8482

85-
if currency == 'gbp':
83+
if currency == 'GBP':
8684
currency_sign = '£'
87-
elif currency == 'eur':
85+
elif currency == 'EUR':
8886
currency_sign = '€'
89-
else:
87+
elif currency == 'USD':
9088
currency_sign = '$'
89+
else:
90+
currency_sign = ''
9191

92-
return "{} // \x0307{}{:,.2f}\x0f {} - {:,.7f} BTC // {} change".format(data['symbol'].upper(),
92+
return "{} // \x0307{}{:,.2f}\x0f {} - {:,.7f} BTC // {} change".format(data[0]['symbol'],
9393
currency_sign,
94-
float(data['price'][currency]),
94+
float(data[0]['price_'+currency.lower()]),
9595
currency.upper(),
96-
float(data['price']['btc']),
96+
float(data[0]['price_btc']),
9797
change_str)

0 commit comments

Comments
 (0)