forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin.coffee
50 lines (43 loc) · 1.28 KB
/
bitcoin.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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})"