This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #883 from fredwu/bitcoin
Added Bitcoin price lookup :)
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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})" |