2018-08-05 20:30:51 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
Queries coinlib.io for information about various cryptocurrencies.
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2019-10-08 12:39:13 -04:00
|
|
|
import config
|
2018-08-05 20:30:51 -04:00
|
|
|
from module import commands, example, require_admin
|
|
|
|
|
|
|
|
URI = "https://coinlib.io/api/v1"
|
|
|
|
|
|
|
|
@commands("crypto", "coin")
|
|
|
|
@example(".crypto btc",
|
|
|
|
"[CRYPTO] Name: Bitcoin | Symbol: BTC | Rank: 1 | Price: 7067.62 | "
|
|
|
|
+ "Markets: 3 | Market Cap: 121511525662.62")
|
|
|
|
def crypto(bot, trigger):
|
|
|
|
"""
|
|
|
|
Queries coinlib.io for information about various crytocurrencies.
|
|
|
|
"""
|
|
|
|
params = {
|
2019-10-08 12:39:13 -04:00
|
|
|
"key": config.coinlib_api_key,
|
2018-08-05 20:30:51 -04:00
|
|
|
"pref": "USD",
|
|
|
|
}
|
2020-01-07 18:58:19 -05:00
|
|
|
if len(trigger.args) < 2:
|
|
|
|
return bot.reply("What coin?")
|
|
|
|
symbol = trigger.args[1]
|
2018-08-05 20:30:51 -04:00
|
|
|
if symbol:
|
|
|
|
params["symbol"] = symbol
|
2018-12-31 01:20:25 -05:00
|
|
|
res = requests.get(URI + "/coin", params=params)
|
2018-08-05 20:30:51 -04:00
|
|
|
if res.status_code in [400, 401]:
|
|
|
|
return bot.msg(f"Error: {res.json()['error']}")
|
|
|
|
res.raise_for_status()
|
|
|
|
data = res.json()
|
|
|
|
msg = f"[\x0304CRYPTO\x03] \x0310Name\x03: \x0312{data['name']}\x03 | "
|
|
|
|
msg += f"\x0310Symbol\x03: \x0312{data['show_symbol']}\x03 | "
|
|
|
|
msg += f"\x0310Rank\x03: \x0312{data['rank']}\x03 | "
|
|
|
|
msg += f"\x0310Price\x03: \x0308"
|
2018-12-31 01:20:25 -05:00
|
|
|
msg += data['price'] + "\x03 | "
|
2018-08-05 20:30:51 -04:00
|
|
|
msg += f"\x0310Markets\x03: \x0312{len(data['markets'])}\x03 | "
|
|
|
|
msg += f"\x0310Market Cap\x03: \x0308"
|
|
|
|
msg += re.sub(r'(^.+\.\d\d).*', r'\1', data['market_cap']) + "\x03"
|
|
|
|
else:
|
|
|
|
res = requests.get(URI + "/global", params=params)
|
|
|
|
if res.status_code in [400, 401]:
|
|
|
|
return bot.msg(f"Error: {res.json()['error']}")
|
|
|
|
res.raise_for_status()
|
|
|
|
data = res.json()
|
|
|
|
msg = f"[\x0304CRYPTO\x03] \x0310Total Coins\x03: \x0312{data['coins']}\x03 | "
|
|
|
|
msg += f"\x0310Total Markets\x03: \x0312{data['markets']}\x03 | "
|
|
|
|
msg += f"\x0310Total Market Cap\x03: \x0308"
|
|
|
|
msg += re.sub(r'(^.+\.\d\d).*', r'\1', data['total_market_cap']) + "\x03"
|
|
|
|
bot.msg(msg)
|