asjon/scripts/deepfry.coffee

95 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2018-02-07 00:15:51 +01:00
# Description:
# deep fry an image
#
# Commands:
# hubot deepfry/friggi[mi] <url>
#
# 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()
2018-02-07 13:40:23 +01:00
.noise('uniform')
.quality(rand(30,80))
.noise('uniform')
2018-02-07 00:15:51 +01:00
.toBuffer callback
resize = (image, callback) ->
gm(image)
2018-02-07 13:40:23 +01:00
.scale('95%')
.scale('105%')
2018-02-07 00:15:51 +01:00
.toBuffer callback
colorize = (image, callback) ->
gm(image)
2018-02-07 13:40:23 +01:00
.modulate(rand(100,110), rand(100,150))
2018-02-07 00:15:51 +01:00
.toBuffer callback
deepFry = (res, image) ->
script = concat [
init(image),
resize,
colorize
2018-02-07 13:40:23 +01:00
times(50, compress),
2018-02-07 00:15:51 +01:00
]
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