From 6dceab8c70b319ee8c4a56cc863e3c5daf7331a8 Mon Sep 17 00:00:00 2001 From: Fred Wu Date: Thu, 11 Apr 2013 10:38:25 +1000 Subject: [PATCH] Added Bitcoin price lookup :) --- src/scripts/bitcoin.coffee | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/scripts/bitcoin.coffee diff --git a/src/scripts/bitcoin.coffee b/src/scripts/bitcoin.coffee new file mode 100644 index 000000000..c2b2c31af --- /dev/null +++ b/src/scripts/bitcoin.coffee @@ -0,0 +1,50 @@ +# Description: +# Find the latest Bitcoin price in specified currency +# +# Dependencies: +# "cheerio": "" +# +# Configuration: +# None +# +# Commands: +# hubot bitcoin price (in) +# +# Author: +# Fred Wu + +cheerio = require('cheerio') + +module.exports = (robot) -> + robot.respond /bitcoin price\s(in\s)?(.*)/i, (msg) -> + currency = msg.match[2].trim().toUpperCase() + bitcoinPrice(msg, currency) + +bitcoinPrice = (msg, currency) -> + msg + .send "Looking up... sit tight..." + msg + .http("http://bitcoinprices.com/") + .get() (err, res, body) -> + msg.send "#{getPrice(currency, body)}" + +getPrice = (currency, body) -> + $ = cheerio.load(body) + + lastPrice = null + highPrice = null + lowPrice = null + priceSymbol = null + + $('table.currencies td.symbol').each (i) -> + if $(this).text() == currency + priceSymbol = $(this).next().next().next().next().next().next().text() + lastPrice = "#{priceSymbol}#{$(this).next().next().next().next().next().text()}" + highPrice = "#{priceSymbol}#{$(this).next().next().next().text()}" + lowPrice = "#{priceSymbol}#{$(this).next().next().next().next().text()}" + false + + if lastPrice == null + "Can't find the price for #{currency}. :(" + else + "#{currency}: #{lastPrice} (H: #{highPrice} | L: #{lowPrice})"