Thanks to visit codestin.com
Credit goes to github.com

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

updated to use new memecaptain API #1649

Merged
merged 1 commit into from
Aug 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 108 additions & 40 deletions src/scripts/meme_captain.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,110 +24,178 @@
# hubot Not sure if <something> or <something else> - Generates a Futurama Fry meme
# hubot <text>, AND IT'S GONE - Bank Teller
# hubot WHAT IF I TOLD YOU <text> - Morpheus What if I told you
# hubot WTF <text> - Picard WTF
# hubot IF <text> THAT'D BE GREAT - Generates Lumberg
# hubot MUCH <text> (SO|VERY) <text> - Generates Doge
# hubot <text> EVERYWHERE - Generates Buzz Lightyear
#
# Author:
# bobanj
# bobanj, ericjsilva

module.exports = (robot) ->
robot.respond /Y U NO (.+)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/y_u_no.jpg', 'Y U NO', msg.match[1], (url) ->
memeGenerator msg, 'NryNmg', 'Y U NO', msg.match[1], (url) ->
msg.send url

robot.respond /iron price (.+)/i, (msg) ->
memeGenerator msg, 'http://imgur.com/nqVZQel.jpg', msg.match[1], 'Pay the iron price', (url) ->
memeGenerator msg, 'q06KuA', msg.match[1], 'Pay the iron price', (url) ->
msg.send url

robot.respond /aliens guy (.+)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/aliens.jpg', msg.match[1], '', (url) ->
memeGenerator msg, 'sO-Hng', msg.match[1], '', (url) ->
msg.send url

robot.respond /brace yourself (.+)/i, (msg) ->
memeGenerator msg, 'http://i.imgur.com/cOnPlV7.jpg', 'Brace Yourself', msg.match[1], (url) ->
memeGenerator msg, '7KY5sQ', 'Brace Yourself', msg.match[1], (url) ->
msg.send url

robot.respond /(.*) (ALL the .*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/all_the_things.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'cKjh_w', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(I DON'?T ALWAYS .*) (BUT WHEN I DO,? .*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/most_interesting.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'V8QnRQ', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(.*)(SUCCESS|NAILED IT.*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/success_kid.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'AbNPRQ', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(.*) (\w+\sTOO DAMN .*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/too_damn_high.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'RCkv6Q', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(NOT SURE IF .*) (OR .*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/fry.png', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'CsNF8w', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(YO DAWG .*) (SO .*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/xzibit.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'Yqk_kg', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(All your .*) (are belong to .*)/i, (msg) ->
memeGenerator msg, 'http://i.imgur.com/gzPiQ8R.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'Ss_hXw', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(.*)\s*BITCH PLEASE\s*(.*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/yao_ming.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'jo9J0Q', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(.*)\s*COURAGE\s*(.*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/courage_wolf.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'IMQ72w', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /ONE DOES NOT SIMPLY (.*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/boromir.jpg', 'ONE DOES NOT SIMPLY', msg.match[1], (url) ->
memeGenerator msg, 'da2i4A', 'ONE DOES NOT SIMPLY', msg.match[1], (url) ->
msg.send url

robot.respond /(IF YOU .*\s)(.* GONNA HAVE A BAD TIME)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/bad_time.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'lfSVJw', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(.*)TROLLFACE(.*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/troll_face.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'dGAIFw', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(IF .*), ((ARE|CAN|DO|DOES|HOW|IS|MAY|MIGHT|SHOULD|THEN|WHAT|WHEN|WHERE|WHICH|WHO|WHY|WILL|WON\'T|WOULD)[ \'N].*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/philosoraptor.jpg', msg.match[1], msg.match[2] + (if msg.match[2].search(/\?$/)==(-1) then '?' else ''), (url) ->
memeGenerator msg, '-kFVmQ', msg.match[1], msg.match[2] + (if msg.match[2].search(/\?$/)==(-1) then '?' else ''), (url) ->
msg.send url

robot.respond /(.*)(AND IT\'S GONE.*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/src_images/uIZe3Q.jpg', msg.match[1], msg.match[2], (url) ->
memeGenerator msg, 'uIZe3Q', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /WHAT IF I TOLD YOU (.*)/i, (msg) ->
memeGenerator msg, 'http://memecaptain.com/src_images/fWle1w.png', 'WHAT IF I TOLD YOU', msg.match[1], (url) ->
memeGenerator msg, 'fWle1w', 'WHAT IF I TOLD YOU', msg.match[1], (url) ->
msg.send url

memeGenerator = (msg, imageName, text1, text2, callback) ->
imageUrl = imageName
robot.respond /WTF (.*)/i, (msg) ->
memeGenerator msg, 'z8IPtw', 'WTF', msg.match[1], (url) ->
msg.send url

robot.respond /(IF .*)(THAT'D BE GREAT)/i, (msg) ->
memeGenerator msg, 'q1cQXg', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(MUCH .*) ((SO|VERY) .*)/i, (msg) ->
memeGenerator msg, 'AfO6hw', msg.match[1], msg.match[2], (url) ->
msg.send url

robot.respond /(.*)(EVERYWHERE.*)/i, (msg) ->
memeGenerator msg, 'yDcY5w', msg.match[1], msg.match[2], (url) ->
msg.send url


memeGeneratorUrl = 'http://memecaptain.com/gend_images'

getDataPayload = (imageName, topText, botText) ->
data = getDataPayloadAdv(imageName, topText, botText, 0.05, 0, 0.9, 0.25, 0.05, 0.75, 0.9, 0.25)
return data


getDataPayloadAdv = (imageName, topText, botText, topX, topY, topW, topH, botX, botY, botW, botH) ->
data = {
src_image_id: imageName,
private: true,
captions_attributes: [
{
text: topText,
top_left_x_pct: topX,
top_left_y_pct: topY,
width_pct: topW,
height_pct: topH
},
{
text: botText,
top_left_x_pct: botX,
top_left_y_pct: botY,
width_pct: botW,
height_pct: botH
}
]
}
return JSON.stringify(data)


memeGenerator = (msg, imageName, topText, botText, callback) ->
processResult = (err, res, body) ->
return msg.send err if err
if res.statusCode == 301
msg.http(res.headers.location).get() processResult
if res.statusCode == 303
callback res.headers.location
if res.statusCode == 202
timer = setInterval(->
msg.http(res.headers.location).get() (err, res, body) ->
if res.statusCode == 303
callback res.headers.location
clearInterval(timer)
, 2000)
if res.statusCode > 300
msg.reply "Sorry, I couldn't generate that meme. Unexpected status from memecaptain.com: #{res.statusCode}"
return

msg.http(memeGeneratorUrl)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.post(getDataPayload(imageName, topText, botText)) processResult


memeGeneratorAdv = (msg, imageName, topText, botText, topX, topY, topW, topH, botX, botY, botW, botH, callback) ->
processResult = (err, res, body) ->
return msg.send err if err
if res.statusCode == 303
callback res.headers.location
if res.statusCode == 202
timer = setInterval(->
msg.http(res.headers.location).get() (err, res, body) ->
if res.statusCode == 303
callback res.headers.location
clearInterval(timer)
, 2000)
if res.statusCode > 300
msg.reply "Sorry, I couldn't generate that meme. Unexpected status from memecaption.com: #{res.statusCode}"
msg.reply "Sorry, I couldn't generate that meme. Unexpected status from memecaptain.com: #{res.statusCode}"
return
try
result = JSON.parse(body)
catch error
msg.reply "Sorry, I couldn't generate that meme. Unexpected response from memecaptain.com: #{body}"
if result? and result['imageUrl']?
callback result['imageUrl']
else
msg.reply "Sorry, I couldn't generate that meme."

msg.http("http://memecaptain.com/g")
.query(
u: imageUrl,
t1: text1,
t2: text2
).get() processResult

msg.http(memeGeneratorUrl)
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.post(getDataPayloadAdv(imageName, topText, botText, topX, topY, topW, topH, botX, botY, botW, botH)) processResult