2011-11-20 22:23:31 +13:00
from util import http , hook
2013-12-02 23:16:34 +13:00
## CONSTANTS
exchanges = {
" blockchain " : {
" api_url " : " https://blockchain.info/ticker " ,
2014-02-13 20:47:47 +13:00
" func " : lambda data : u " Blockchain // Buy: \x03 07$ {:,.2f} \x0f - "
u " Sell: \x03 07$ {:,.2f} \x0f " . format ( data [ " USD " ] [ " buy " ] , data [ " USD " ] [ " sell " ] )
2013-12-02 23:16:34 +13:00
} ,
2014-02-14 17:03:08 +13:00
" coinbase " : {
2013-12-02 23:16:34 +13:00
" api_url " : " https://coinbase.com/api/v1/prices/spot_rate " ,
" func " : lambda data : u " Coinbase // Current: \x03 07$ {:,.2f} \x0f " . format ( float ( data [ ' amount ' ] ) )
} ,
" bitpay " : {
" api_url " : " https://bitpay.com/api/rates " ,
" func " : lambda data : u " Bitpay // Current: \x03 07$ {:,.2f} \x0f " . format ( data [ 0 ] [ ' rate ' ] )
} ,
" bitstamp " : {
" api_url " : " https://www.bitstamp.net/api/ticker/ " ,
2014-02-13 20:47:47 +13:00
" func " : lambda data : u " BitStamp // Current: \x03 07$ {:,.2f} \x0f - High: \x03 07$ {:,.2f} \x0f - "
u " Low: \x03 07$ {:,.2f} \x0f - Volume: {:,.2f} BTC " . format ( float ( data [ ' last ' ] ) ,
float ( data [ ' high ' ] ) ,
float ( data [ ' low ' ] ) ,
float ( data [ ' volume ' ] ) )
2013-12-02 23:16:34 +13:00
}
}
## HOOK FUNCTIONS
2011-11-20 22:23:31 +13:00
2013-11-29 00:36:54 +13:00
@hook.command ( " btc " , autohelp = False )
2011-11-20 22:23:31 +13:00
@hook.command ( autohelp = False )
2013-11-29 00:36:54 +13:00
def bitcoin ( inp ) :
2014-02-13 14:34:50 +13:00
""" bitcoin <exchange> -- Gets current exchange rate for bitcoins from several exchanges, default is Blockchain.
Supports MtGox , Bitpay , Coinbase and BitStamp . """
2013-11-29 00:36:54 +13:00
inp = inp . lower ( )
if inp :
if inp in exchanges :
exchange = exchanges [ inp ]
else :
return " Invalid Exchange "
else :
2013-11-30 00:05:55 +13:00
exchange = exchanges [ " blockchain " ]
2013-11-29 00:36:54 +13:00
data = http . get_json ( exchange [ " api_url " ] )
func = exchange [ " func " ]
return func ( data )
2013-11-29 00:04:28 +13:00
2013-12-02 23:16:34 +13:00
@hook.command ( " ltc " , autohelp = False )
2013-11-28 18:48:11 +08: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 16:18:18 +13:00
message ( " Current: \x03 07$ {:,.2f} \x0f - High: \x03 07$ {:,.2f} \x0f "
2014-02-13 20:47:47 +13:00
" - Low: \x03 07$ {:,.2f} \x0f - Volume: {:,.2f} LTC " . format ( ticker [ ' buy ' ] , ticker [ ' high ' ] , ticker [ ' low ' ] ,
ticker [ ' vol_cur ' ] ) )