This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/coins.py

58 lines
2.6 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
from util import http, hook
2013-11-29 12:21:26 +01:00
@hook.command("butt", autohelp=False)
2013-11-28 12:36:54 +01:00
@hook.command("btc", autohelp=False)
2011-11-20 10:23:31 +01:00
@hook.command(autohelp=False)
2013-11-28 12:36:54 +01:00
def bitcoin(inp):
2013-11-29 12:21:26 +01:00
"bitcoin <exchange> -- Gets current exchange rate for bitcoins from several exchanges, default is Blockchain. Supports MtGox, Bitpay, Coinbase and BitStamp."
2013-11-28 12:36:54 +01:00
exchanges = {
2013-11-29 12:05:55 +01:00
"blockchain": {
"api_url": "https://blockchain.info/ticker",
"func": lambda data: u"Blockchain // Buy: \x0307${:,.2f}\x0f - Sell: \x0307${:,.2f}\x0f".format(data["USD"]["buy"], \
data["USD"]["sell"])
},
2013-11-28 12:36:54 +01:00
"mtgox": {
"api_url": "https://mtgox.com/api/1/BTCUSD/ticker",
2013-11-28 23:55:07 +01:00
"func": lambda data: u"MtGox // Current: \x0307{}\x0f - High: \x0307{}\x0f - Low: \x0307{}\x0f - Best Ask: \x0307{}\x0f - Volume: {}".format(data['return']['last']['display'], \
2013-11-28 12:36:54 +01:00
data['return']['high']['display'], data['return']['low']['display'], data['return']['buy']['display'], \
data['return']['vol']['display'])
},
2013-11-29 12:21:26 +01:00
"coinbase":{
"api_url": "https://coinbase.com/api/v1/prices/spot_rate",
"func": lambda data: u"Coinbase // Current: \x0307${:,.2f}\x0f".format(float(data['amount']))
},
2013-11-28 12:36:54 +01:00
"bitpay": {
"api_url": "https://bitpay.com/api/rates",
2013-11-29 04:18:18 +01:00
"func": lambda data: u"Bitpay // Current: \x0307${:,.2f}\x0f".format(data[0]['rate'])
2013-11-28 12:36:54 +01:00
},
"bitstamp": {
"api_url": "https://www.bitstamp.net/api/ticker/",
2013-11-29 04:18:18 +01:00
"func": lambda data: u"BitStamp // Current: \x0307${:,.2f}\x0f - High: \x0307${:,.2f}\x0f - Low: \x0307${:,.2f}\x0f - Volume: {:,.2f} BTC".format(float(data['last']), float(data['high']), float(data['low']), \
2013-11-28 12:36:54 +01:00
float(data['volume']))
}
}
2013-11-28 12:36:54 +01:00
inp = inp.lower()
if inp:
if inp in exchanges:
exchange = exchanges[inp]
else:
return "Invalid Exchange"
else:
2013-11-29 12:05:55 +01:00
exchange = exchanges["blockchain"]
2013-11-28 12:36:54 +01:00
data = http.get_json(exchange["api_url"])
func = exchange["func"]
return func(data)
2013-11-28 12:04:28 +01:00
@hook.command(autohelp=False)
def litecoin(inp, message=None):
"""litecoin -- gets current exchange rate for litecoins from BTC-E"""
data = http.get_json("https://btc-e.com/api/2/ltc_usd/ticker")
ticker = data['ticker']
2013-11-29 04:18:18 +01:00
message("Current: \x0307${:,.2f}\x0f - High: \x0307${:,.2f}\x0f"
" - Low: \x0307${:,.2f}\x0f - Volume: {:,.2f} LTC".format(ticker['buy'], ticker['high'], ticker['low'], ticker['vol_cur']))