asjon/scripts/memes.coffee

61 lines
1.7 KiB
CoffeeScript
Raw Normal View History

2015-08-28 19:58:22 +02:00
# Description:
# makes dank maymays
#
# Commands:
2015-09-26 17:26:09 +02:00
# hubot meme <template>,<upper title>,<lower title> - genera un meme
# hubot memes [-v] - mostra i templates disponibili
2015-08-28 19:58:22 +02:00
#
2015-09-28 04:17:40 +02:00
# Dependencies:
# "async": "1.4.2"
#
2015-08-28 19:58:22 +02:00
# Configuration:
# None
#
# Author:
# Enrico Fasoli (fazo96)
async = require 'async'
2015-08-28 19:58:22 +02:00
module.exports = (robot) ->
2015-09-28 17:06:57 +02:00
memegen = 'http://memegen.link/'
usage = 'non si usa così: prova a chiedermi "asjon help meme" per
sapere sapere come funziona il comando'
escape = (str) ->
2015-09-28 18:56:28 +02:00
return '_' unless str.length
str.replace( /-/g, '--')
.replace(/\ /g, '-')
.replace( /_/g, '__')
.replace(/\?/g, '~q')
.replace( /%/g, '~p')
.toLowerCase()
2015-09-28 17:06:57 +02:00
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
2015-09-28 17:06:57 +02:00
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'
2015-08-28 19:58:22 +02:00
robot.respond /meme (.+)/i, (res) ->
2015-09-28 17:06:57 +02:00
args = (i.trim() for i in res.match[1].split ',')
return res.send usage if args.length != 3
2015-09-28 17:06:57 +02:00
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()