-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.coffee
64 lines (53 loc) · 1.74 KB
/
bot.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
read = require 'read'
{spawn} = require 'child_process'
argv = require('yargs')
.alias(term: 't', 'base': 'b', 'var': 'v')
.default(t: 1, b: 20, v: 5)
.argv
credential = {}
require('async').series [
(callback) ->
credentialFilename = require('path').join(__dirname, 'credential.yml')
rawYaml = require('fs').readFileSync(credentialFilename, 'utf8')
credential = require('js-yaml').safeLoad(rawYaml)
if not credential.userid
read prompt: 'Userid: ', (err, userid) ->
credential.userid = userid
callback(err, 'reading userid')
else
callback(null, 'reading userid')
,
(callback) ->
read prompt: 'Password: ', silent: true, (err, password) ->
credential.password = password
callback(err, 'reading password')
], (err, actions) ->
if err
[..., lastAction] = actions
console.error 'Error when ' + lastAction + ':'
console.error err
else
enroll = ->
console.log 'term', argv.t
console.log 'delay base', argv.b
console.log 'delay variation', argv.v
child = spawn 'node_modules/casperjs/bin/casperjs', [
'enroll.coffee',
argv.t
]
child.stdin.write credential.userid + '\n'
child.stdin.write credential.password + '\n'
child.stdout.on 'data', (data) ->
console.log data.toString()
child.stderr.on 'data', (data) ->
console.error data.toString()
child.on 'close', (code) ->
return if code == 0
# Wait argv.b min +/- a random number of mins < argv.v.
retryDelayMin = argv.b + Math.random() * 2 * argv.v - argv.v
console.log 'Retrying in ' + retryDelayMin + ' mins.'
setTimeout ->
enroll()
,
retryDelayMin * 60 * 1000
enroll()