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

Commit 6dceab8

Browse files
committed
Added Bitcoin price lookup :)
1 parent 1924f8d commit 6dceab8

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/scripts/bitcoin.coffee

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Description:
2+
# Find the latest Bitcoin price in specified currency
3+
#
4+
# Dependencies:
5+
# "cheerio": ""
6+
#
7+
# Configuration:
8+
# None
9+
#
10+
# Commands:
11+
# hubot bitcoin price (in) <currency>
12+
#
13+
# Author:
14+
# Fred Wu
15+
16+
cheerio = require('cheerio')
17+
18+
module.exports = (robot) ->
19+
robot.respond /bitcoin price\s(in\s)?(.*)/i, (msg) ->
20+
currency = msg.match[2].trim().toUpperCase()
21+
bitcoinPrice(msg, currency)
22+
23+
bitcoinPrice = (msg, currency) ->
24+
msg
25+
.send "Looking up... sit tight..."
26+
msg
27+
.http("http://bitcoinprices.com/")
28+
.get() (err, res, body) ->
29+
msg.send "#{getPrice(currency, body)}"
30+
31+
getPrice = (currency, body) ->
32+
$ = cheerio.load(body)
33+
34+
lastPrice = null
35+
highPrice = null
36+
lowPrice = null
37+
priceSymbol = null
38+
39+
$('table.currencies td.symbol').each (i) ->
40+
if $(this).text() == currency
41+
priceSymbol = $(this).next().next().next().next().next().next().text()
42+
lastPrice = "#{priceSymbol}#{$(this).next().next().next().next().next().text()}"
43+
highPrice = "#{priceSymbol}#{$(this).next().next().next().text()}"
44+
lowPrice = "#{priceSymbol}#{$(this).next().next().next().next().text()}"
45+
false
46+
47+
if lastPrice == null
48+
"Can't find the price for #{currency}. :("
49+
else
50+
"#{currency}: #{lastPrice} (H: #{highPrice} | L: #{lowPrice})"

0 commit comments

Comments
 (0)