Created
February 19, 2015 19:44
-
-
Save aackerman/3ca20adbff9c5e165999 to your computer and use it in GitHub Desktop.
lebowski.coffee
This file contains hidden or 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
# Description: | |
# Get a random quote from The Big Lebowski. | |
# | |
# Dependencies: | |
# "htmlparser": "1.7.6" | |
# "soupselect": "0.2.0" | |
# "underscore": "1.3.3" | |
# "underscore.string": "2.3.0" | |
_ = require("underscore") | |
_s = require("underscore.string") | |
module.exports = (robot) -> | |
robot.hear /(lebowski|dude|abide|beverage)/i, (msg) -> | |
callback = (text) -> | |
msg.send text | |
if match = msg.message.text.match(/^lebowski me (.*)$/) | |
getLebowskiQuote robot, callback, match[1] | |
else | |
getLebowskiQuote robot, callback | |
getLebowskiQuote = (robot, callback, quote) -> | |
if quoteId = parseInt(quote) | |
url = "http://lebowski.me/api/quotes/#{quoteId}" | |
else if quote | |
url = "http://lebowski.me/api/quotes/search?term=#{encodeURIComponent(quote)}" | |
else | |
url = 'http://lebowski.me/api/quotes/random' | |
robot.http(url) | |
.header('User-Agent', 'The Dude') | |
.get() (err, res, body) -> | |
return callback "I can't get a quote right now, sorry." if err | |
try | |
data = JSON.parse(body) | |
catch error | |
return callback "Parse Error for lebowski.me" | |
pieces = [] | |
quote = _.values(data)[0] | |
quote = quote[0] if _.isArray(quote) | |
return unless quote.lines | |
quote.lines.forEach (line) -> | |
pieces.push("#{line.character.name}\n", "#{line.text}\n\n") | |
callback pieces.join("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment