This commit is contained in:
H.J.A. Timmermans Timmermans 2022-03-17 13:41:34 +01:00
parent e5e9b61cc3
commit 3c865e5d50
1 changed files with 36 additions and 12 deletions

View File

@ -1,26 +1,50 @@
# Description:
# Returns the URL of the first bing hit for a query
# Find the latest Bitcoin price in specified currency
#
# Dependencies:
# None
# "cheerio": ""
#
# Configuration:
# None
#
# Commands:
# hubot bing me <query> - Bings <query> & returns 1st result's URL
# hubot bitcoin price (in) <currency>
#
# Author:
# Brandon Satrom
# Fred Wu
cheerio = require('cheerio')
module.exports = (robot) ->
robot.respond /(bing)( me)? (.*)/i, (msg) ->
bingMe msg, msg.match[3], (url) ->
msg.send url
robot.respond /bitcoin price\s(in\s)?(.*)/i, (msg) ->
currency = msg.match[2].trim().toUpperCase()
bitcoinPrice(msg, currency)
bingMe = (msg, query, cb) ->
msg.http('http://www.bing.com/search')
.query(q: query)
bitcoinPrice = (msg, currency) ->
msg
.send "Looking up... sit tight..."
msg
.http("http://bitcoinprices.com/")
.get() (err, res, body) ->
cb body.match(/<div class="sb_tlst"><h3><a href="([^"]*)"/)?[1] || "Sorry, Bing had zero results for '#{query}'"
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})"