diff --git a/scripts/deepfry.coffee b/scripts/deepfry.coffee new file mode 100644 index 0000000..530dec9 --- /dev/null +++ b/scripts/deepfry.coffee @@ -0,0 +1,94 @@ +# Description: +# deep fry an image +# +# Commands: +# hubot deepfry/friggi[mi] +# +# Dependencies: +# "async": "^1.4.2", +# "hubot-matrix": "rnhmjoj/hubot-matrix", +# "needle": "^0.10.0", +# +# Configuration: +# None +# +# Author: +# Michele Guerini Rocco (rnhmjoj) + +ne = require 'needle' +async = require 'async' +gm = require 'gm' +url = require 'url' + +module.exports = (robot) -> + # utilities + concat = (x) -> [].concat.apply([], x) + init = (x) -> (f) -> f(null, x) + times = (n, x) -> x for i in [1..n] + rand = (min, max) -> Math.random() * (max - min) + min + + # effects + compress = (image, callback) -> + gm(image) + .noProfile() + .quality(rand(25,40)) + .colorize(rand(0,5),rand(0,5),rand(0,5)) + .toBuffer callback + + resize = (image, callback) -> + gm(image) + .resize(260000, '@>') + .resize('40%') + .resize('500%') + .toBuffer callback + + colorize = (image, callback) -> + gm(image) + .modulate(rand(110,200), rand(100,110)) + .toBuffer callback + + deepFry = (res, image) -> + script = concat [ + init(image), + resize, + times(40, compress), + colorize + ] + + async.waterfall script, (err, image) -> + if err? + robot.logger.warning "frying failed:\n#{err}" + return res.send 'scusa, si è rotta la friggitrice.' + + # send image + robot.logger.info 'image fried successfully' + robot.adapter.sendImage + room: res.message.room + , image, 'fried.jpeg', -> + res.reply res.random [ 'meme fritto per te.', 'eccoti servito.'] + + + robot.respond /(?:deepfry|friggi(?:mi)?) (questo|.+)/i, (res) -> + if not url.parse(res.match[1]).hostname + return res.send res.random [ 'non si usa così.', 'è un url questo?', 'cosa?' ] + imageURL = res.match[1] + + # supported image mime types + accepted = ['image/jpeg', 'image/png', 'image/tiff'] + + # fetch headers + ne.head imageURL, follow_max: 5, (err, re) -> + if err? + robot.logger.warning "headers download failed:\n#{err}" + return res.send 'non riesco ad aprire il link.' + + mimetype = re.headers['content-type'].split(';')[0] + if not (mimetype in accepted) + robot.logger.info 'url is not an image' + return res.send "non è un'immagine questo!" + + # download and process + robot.logger.info 'downloading image...' + ne.get imageURL, follow_max: 5, (err, re, body) -> + robot.logger.info 'image downloaded' + deepFry res, body