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