Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #883 from fredwu/bitcoin
Browse files Browse the repository at this point in the history
Added Bitcoin price lookup :)
  • Loading branch information
Tom Bell committed Apr 11, 2013
2 parents 1924f8d + 6dceab8 commit 1a8581e
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 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) <currency>
#
# 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})"

0 comments on commit 1a8581e

Please sign in to comment.