-
Notifications
You must be signed in to change notification settings - Fork 0
/
sonarrFunctions.js
271 lines (252 loc) · 10.5 KB
/
sonarrFunctions.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
var Sonarr = require('./sonarr'),
Utils = require('./utilFunctions'),
Deferred = require('bluebird'),
Conversation = require('./conversation'),
_ = require('lodash');
/**
* @constructor
*/
function SonarrFunctions() {
"use strict";
function setup(message, data) {
return new Deferred(function (resolve, reject) {
var qualitiesDeferred = Sonarr.qualities(),
seriesDeferred = Sonarr.lookup(message.match[1]),
rootFolderDeferred = Sonarr.rootFolders();
Deferred.join(qualitiesDeferred, seriesDeferred, rootFolderDeferred, function (qualities, series, rootFolders) {
data.qualities = qualities;
data.series = series;
data.rootFolders = rootFolders;
resolve();
}).catch(function (err) {
reject(err);
});
});
}
function finish(data) {
return new Deferred(function (resolve, reject) {
Sonarr.download(data)
.then(resolve("Downloaded with Success"), reject("Failed to download"));
});
}
function askSeries(data) {
var question = [];
question.push("Choose Series");
_.forEach(data.series, function (show, i) {
question.push("[" + (i + 1) + "] - " + show.title + " (" + show.year + ")");
});
question.push("[0] - Quit");
return question.join("\n");
}
function confirmSeries(conversation) {
return function (response, convo) {
var isNumber = /^(\d)+$/,
choice,
show;
if (response && response.text && response.text && response.text.match(isNumber)) {
choice = parseInt(response.text, 10);
if (choice === 0) {
conversation.end(response, convo);
} else {
choice = choice - 1;
show = conversation.data.series[choice];
if (show) {
convo.say("Selected " + show.title + " (" + show.year + ")");
conversation.data.show = show;
conversation.nextQuestion(response, convo);
} else {
convo.say(response.text + ": option not found");
conversation.silentRepeat(response, convo);
}
}
} else {
convo.say(response.text + ": expected number");
conversation.silentRepeat(response, convo);
}
};
}
function askQuality(data) {
var question = [];
question.push("Choose Quality");
_.forEach(data.qualities, function (quality, i) {
question.push("[" + (i + 1) + "] - " + quality.name);
});
question.push("[0] - Quit");
return question.join("\n");
}
function confirmQuality(conversation) {
return function (response, convo) {
var isNumber = /^(\d)+$/,
choice,
quality;
if (response && response.text && response.text && response.text.match(isNumber)) {
choice = parseInt(response.text, 10);
if (choice === 0) {
conversation.end(response, convo);
} else {
choice = choice - 1;
quality = conversation.data.qualities[choice];
if (quality) {
convo.say("Selected " + quality.name);
conversation.data.quality = quality;
conversation.nextQuestion(response, convo);
} else {
convo.say(response.text + ": option not found");
conversation.silentRepeat(response, convo);
}
}
} else {
convo.say(response.text + ": expected number");
conversation.silentRepeat(response, convo);
}
};
}
function askFolder(data) {
var question = [];
question.push("What folder should I save this in?");
_.forEach(data.rootFolders, function (folder, i) {
question.push("[" + (i + 1) + "] - " + folder.path);
});
question.push("[0] - Quit");
return question.join("\n");
}
function confirmFolder(conversation) {
return function (response, convo) {
var isNumber = /^(\d)+$/,
choice,
folder;
if (response && response.text && response.text && response.text.match(isNumber)) {
choice = parseInt(response.text, 10);
if (choice === 0) {
conversation.end(response, convo);
} else {
choice = choice - 1;
folder = conversation.data.rootFolders[choice];
if (folder) {
convo.say("Selected " + folder.path);
conversation.data.folder = folder;
conversation.nextQuestion(response, convo);
} else {
convo.say(response.text + ": option not found");
conversation.silentRepeat(response, convo);
}
}
} else {
convo.say(response.text + ": expected number");
conversation.silentRepeat(response, convo);
}
};
}
function preconditionFolder(data) {
if (data.rootFolders && data.rootFolders.length === 1) {
data.folder = data.rootFolders[0];
return false;
}
return true;
}
function askSeasons(data) {
var question = [];
question.push("Which seasons should I download? (separate with space e.g. 1 2 3)");
_.forEach(data.show.seasons, function (season, i) {
if (season.seasonNumber !== 0) { // don't allow specials
question.push("[" + season.seasonNumber + "] - Season " + season.seasonNumber);
}
});
question.push("[" + (data.show.seasonCount + 1) + "] - ALL");
question.push("[0] - Quit");
return question.join('\n');
}
function confirmSeasons(conversation) {
return function (response, convo) {
var isNumbers = /^( *\d+ *)+$/,
seasonsSelected,
res;
if (response && response.text && response.text && response.text.match(isNumbers)) {
response.text = response.text.replace(/ +/g, " "); //remove multiple spaces
seasonsSelected = response.text.split(" ");
// ALL SELECTED
if (_.find(seasonsSelected, function (season) {
return parseInt(season, 10) === (conversation.data.show.seasonCount + 1);
})) {
_.forEach(conversation.data.show.seasons, function (season) {
if (season.seasonNumber !== 0) {
season.monitored = true;
}
});
convo.say("Added all seasons");
conversation.nextQuestion(response, convo);
// QUIT SELECTED
} else if (_.find(seasonsSelected, function (season) {
return parseInt(season, 10) === 0;
})) {
conversation.end(response, convo);
// SPECIFIC SEASONS SELECTED
} else {
res = [];
res.push("Added Seasons: ");
_.forEach(seasonsSelected, function (season) {
var foundSeason = _.find(conversation.data.show.seasons, function (showSeason) {
return showSeason.seasonNumber === parseInt(season, 10);
});
if (foundSeason) {
foundSeason.monitored = true;
res.push(foundSeason.seasonNumber);
}
});
if (res.length === 1) { //no seasons added
convo.say(response.text + ": option not found");
conversation.silentRepeat(response, convo);
} else {
convo.say(res.join("\n"));
conversation.nextQuestion(response, convo);
}
}
} else {
convo.say(response.text + ": option not found");
conversation.silentRepeat(response, convo);
}
};
}
function askSummary(data) {
var question = [],
seasonsSelected = data.show.seasons.filter(function (season) {
return season.monitored === true;
}).map(function (season) {
return season.seasonNumber;
}).join(", ");
question.push("Does this look correct?");
question.push("SHOW: " + data.show.title + " (" + data.show.year + ")");
question.push("QUALITY: " + data.quality.name);
question.push("FOLDER: " + data.folder.path);
question.push("SEASONS: " + seasonsSelected);
return question.join('\n');
}
function confirmSummary(conversation) {
return function (response, convo) {
if (response && response.text && response.text.match(conversation.bot.utterances.yes)) {
convo.say("Downloading");
conversation.nextQuestion(response, convo);
} else if (response && response.text && response.text.match(conversation.bot.utterances.no)) {
conversation.end(response, convo);
} else {
convo.say(response.text + ": option not found");
conversation.silentRepeat(response, convo);
}
};
}
function initialiseConversation(bot, message) {
var conversation = new Conversation();
conversation.setSetup(setup)
.addStep(askSeries, confirmSeries)
.addStep(askSeasons, confirmSeasons)
.addStep(askQuality, confirmQuality)
.addStep(askFolder, confirmFolder, preconditionFolder)
.addStep(askSummary, confirmSummary)
.setFinish(finish)
.start(bot, message);
}
return {
initialiseConversation: initialiseConversation
};
}
module.exports = new SonarrFunctions();