This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
116 lines (98 loc) · 2.77 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
var request = require('request');
var _ = require('lodash');
var sprintf = require('util').format;
var logUpdate = require('log-update');
var spinner = require('elegant-spinner');
var frame = spinner();
var organizers = ['rafaelrinaldi', 'lucasmazza', 'danielfilho'];
var ignore = organizers;
var options = {};
var animation;
function loading() {
// Will start a fancy loading animation
frame = spinner();
animation = setInterval(function() {
logUpdate(frame());
}, 100);
}
function loaded() {
clearInterval(animation);
logUpdate('');
}
function fetch(url) {
var requestOptions = {
/**
* Big arbitrary number so we always get a fresh full list.
* There's probably a better way to do it though.
*/
uri: url + '/999.json',
method: 'GET',
/**
* Use this flag so we don't need to handle w/ cert. signature.
* Ideally we should pass an API key and token, but that didn't worked at all.
* Lots of people w/ the same issue, I've posted on Discourse's main forum to see what's up.
*/
rejectUnauthorized: false,
json: true
};
return new Promise(function(resolve, reject) {
request(requestOptions, function(error, response, body){
if(error) {
reject(error);
}
resolve(body);
});
});
}
function parse(data) {
/**
* For some reason the API don't always return all the participants.
* So what I do here is to combine the results from "posts" and "participants".
*/
var posts = data.post_stream.posts;
var replies = data.details.participants;
var participants = posts.concat(replies);
participants = _.pluck(participants, 'username');
participants = _.uniq(participants);
if(options.ignore) {
ignore = ignore.concat(options.ignore.replace(/\s/g, '').split(','));
}
// Remove items that should be ignored if there are any
_.remove(participants, function(participant) {
return _.includes(ignore, participant);
});
return participants;
}
function select(list) {
// If "total" is not specified on the options, it will retrieve the full list
var total = options.total < list.length ? options.total : list.length;
return _.chain(list).shuffle().take(total).value();
}
function format(list) {
/**
* User can customize the output.
* By default it's the username followed by a new line.
*/
var template = options.format || '%s';
return list.map(function(item) {
return sprintf(template, item) + '\n';
}).toString().replace(/\,/gm, '').trim();
}
function output(input) {
loaded();
// Output the result to stdout
logUpdate(input);
}
module.exports = function(url, _options) {
options = _options;
loading();
fetch(url)
.catch(function(error) {
output(error);
})
.then(parse)
.then(select)
.then(format)
.then(output);
};