asjon/scripts/memes.coffee
2015-09-28 18:56:28 +02:00

61 lines
1.7 KiB
CoffeeScript

# Description:
# makes dank maymays
#
# Commands:
# hubot meme <template>,<upper title>,<lower title> - genera un meme
# hubot memes [-v] - mostra i templates disponibili
#
# Dependencies:
# "async": "1.4.2"
#
# Configuration:
# None
#
# Author:
# Enrico Fasoli (fazo96)
async = require 'async'
module.exports = (robot) ->
memegen = 'http://memegen.link/'
usage = 'non si usa così: prova a chiedermi "asjon help meme" per
sapere sapere come funziona il comando'
escape = (str) ->
return '_' unless str.length
str.replace( /-/g, '--')
.replace(/\ /g, '-')
.replace( /_/g, '__')
.replace(/\?/g, '~q')
.replace( /%/g, '~p')
.toLowerCase()
gen_meme = (template, title1, title2) ->
"#{memegen}#{template}/#{escape title1}/#{escape title2}.jpg"
send_example = (url, res, callback) ->
cb = if callback? then (-> callback()) else (-> return)
robot.http(url).get() (err, r, body) ->
template = JSON.parse body
title = template.name
names = template.aliases
res.send (gen_meme names[0], title, names.join ', '),
if robot.adapterName is 'tg' then cb else ''
cb() if robot.adapterName isnt 'tg'
robot.respond /meme (.+)/i, (res) ->
args = (i.trim() for i in res.match[1].split ',')
return res.send usage if args.length != 3
res.send gen_meme args...
robot.respond /memes( -v)?/i, (res) ->
if not res.match[1]?
res.send 'guarda qui: http://memegen.link/overview'
else
robot.http(memegen + 'templates/').get() (err, r, body) ->
templates = (url for _, url of JSON.parse body)
async.eachSeries templates, (url, done) ->
send_example url, res, -> done()