forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
capgun.coffee
68 lines (59 loc) · 1.91 KB
/
capgun.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Description
# Grab web screens/thumbs of URLs using the capgun.io service
#
# Requires a CapGun API token to be set in the env var HUBOT_CAPGUN_TOKEN
#
# Dependencies:
# none
#
# Configuration:
# HUBOT_CAPGUN_TOKEN
#
# Commands:
# hubot cap <url> - Get a web screen of the url
#
# Notes:
# none
#
# Author:
# monde
module.exports = (robot) ->
robot.respond /cap (.*)/i, (msg) ->
Capgun.start msg, msg.match[1]
Capgun =
interval: 2500
token: process.env.HUBOT_CAPGUN_TOKEN
start: (msg, url) ->
this.submitOrder(msg, url)
submitOrder: (msg, url) ->
capgun = this
data = JSON.stringify({"url": url})
msg.http('https://api.capgun.io/v1/orders.json')
.headers
'Authorization': capgun.token,
'Accept': 'application/json'
.post(data) (err, res, body) ->
result = JSON.parse(body)
if err || res.statusCode != 200
message = result.message || "???"
msg.send "Capgun job failed with message '" + message + "', I'm bailing out!"
else
setTimeout (-> capgun.checkJob(msg, url, result.order.id, 0)), capgun.interval
checkJob: (msg, url, order_id, duration) ->
capgun = this
if duration > 90000
msg.send "Capgun job hung past 90 seconds for URL " + url + " , I'm bailing out!"
else
msg.http('https://api.capgun.io/v1/orders/' + order_id + '.json')
.headers
'Authorization': capgun.token,
'Accept': 'application/json'
.get() (err, res, body) ->
result = JSON.parse(body)
state = result.order.job.state
if state.search(/failed/) >= 0
msg.send "Capgun order " + order_id + " failed for URL " + url
else if state.search(/completed/) >= 0
msg.send result.order.asset_urls['xlarge']
else
setTimeout (-> capgun.checkJob(msg, url, result.order.id, duration + capgun.interval)), capgun.interval