-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
363 lines (295 loc) · 7.38 KB
/
app.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"use strict";
var request = require('request-promise');
var Base = require('./base.js');
var webhook_id;
/**
*
* Support planned:
* __Download__ "movie title" __in the best quality__
* __Download__ "movie title" __in HD quality__
* __Do I have__ "movie title" __in my watchlist__?
* __Do I have__ "movie title" __in my collection__?
* __Search for all wanted movies__
*
*/
var debug = false;
var log = function(message){
if(debug){
Homey.log('Saying: ' + message)
}
else {
Homey.manager('speech-output').say(message);
}
};
var App = Base.extend({
constructor: function(){
this.updateSettings();
this.listenToSettingsChange();
this.listenToSpeech();
this.listenToActions();
},
/**
* Listen to speech events
*/
listenToSpeech: function(){
var self = this;
Homey.log('listenToSpeech');
Homey.manager('speech-input').on('speech', function(speech) {
Homey.log(speech);
if(!Homey.manager('settings').get('use_default')) return;
// Trigger leds while searching here
// loop all triggers
speech.triggers.forEach(function(trigger){
// trigger.id == download, add
if(['download', 'add'].indexOf(trigger.id) > -1){
var movie_name = speech.transcript.substring(trigger.position + trigger.text.length);
Homey.log(movie_name);
self.searchAndAdd(movie_name);
}
else if(['to_watchlist'].indexOf(trigger.id) > -1){
self.askAndAdd();
}
// trigger.id == watchlist
// trigger.id == do i have
// Search library and give answer "yeah, in HD quality", "no, add it?"
// Allow answering to add it
});
});
},
/**
* Listen to settings
*/
listenToSettingsChange: function(){
var self = this;
Homey.log('listenToSettingsChange');
Homey.manager('settings').on('set', function(){
self.updateSettings();
});
},
/**
* Listen to actions
*/
listenToActions: function(){
var self = this;
// Search for all movies in the wanted list
Homey.manager('flow').on('action.search_all', function(callback){
self.request('movie.searcher.full_search')
.then(function(){
log(__('messages.search_all_wanted'));
callback(null, true);
});
});
// Ask what movie the user wants added
Homey.manager('flow').on('action.ask', function(callback, args){
Homey.log('action.ask', args);
self.askAndAdd();
callback(null, true);
});
// Filter out words before adding
Homey.manager('flow').on('action.filter_and_add', function(callback, args){
Homey.log('action.filter_and_add', args);
self.doFilter(args)
.then(function(movie_name){
Homey.log(movie_name);
self.searchAndAdd(movie_name);
callback(null, true);
})
.catch(function(){
callback(true, false);
})
});
},
/**
* When updating settings, re-register the webhook
*/
updateSettings: function(){
Homey.log('updateSettings');
// Register initial webhook
this.registerWebhook();
},
/**
* Register webhook
*/
registerWebhook: function(){
var self = this;
Homey.log('registerWebhook');
var url = Homey.manager('settings').get('webhook_url'),
id = Homey.manager('settings').get('webhook_id'),
secret = Homey.manager('settings').get('webhook_secret');
// Unregister if changed
if(webhook_id && webhook_id != id) Homey.manager('cloud').unregisterWebhook(webhook_id);
// Don't register if not all values are set
if(!url || !id || !secret) return;
Homey.manager('cloud').registerWebhook(
id,
secret,
{},
self.incomingWebhook,
function(){}
);
// Store used webhook internally
webhook_id = id;
},
/**
* Catch incoming webhook
* @param args
*/
incomingWebhook: function(args){
Homey.log('incomingWebhook: ', args.body.type);
var message = args.body.message,
type = args.body.type,
movie = args.body.movie;
if (type == 'movie.snatched'){
Homey.manager('flow').trigger('snatched', {
movie: movie
});
}
else if (type == 'renamer.after'){
Homey.manager('flow').trigger('downloaded', {
movie: movie
});
}
else if (type == 'media.available'){
Homey.manager('flow').trigger('dashboard', {
movie: movie
});
}
else if (type == 'test'){
log(message);
}
},
doSearch: function(q){
Homey.log('Search for: ' + q);
return this.request('search', {
q: q,
type: 'movie',
limit: 1
});
},
searchAndAdd: function(movie_name){
var self = this;
return self.doSearch(movie_name)
.then(self.doConfirmResult.bind(self))
.catch(function(err){ log(err); })
.then(self.doAdd.bind(self));
},
doAsk: function(){
Homey.log('doAsk');
return new Promise(function(resolve, reject){
Homey.manager( 'speech-input').ask(__('messages.what_movie?'), function(err, result){
Homey.log('doAsk results', err, result);
if(result){
resolve(result);
}
else {
reject();
}
});
});
},
askAndAdd: function(){
var self = this;
return self.doAsk()
.then(function(movie_name){
return self.doSearch(movie_name)
})
.catch(function(){
log(__('messages.sorry_couldnt_add'));
})
.then(self.doConfirmResult.bind(self))
.catch(function(err){ log(err); })
.then(self.doAdd.bind(self));
},
doFilter: function(args){
Homey.log('doFilter');
return new Promise(function(resolve){
var remove = (args.remove || '').toLowerCase().split(' '),
sentence = (args.droptoken || '').toLowerCase().split(' ');
var movie_name = sentence.filter(function(item) {
return remove.indexOf(item) === -1;
}).join(' ');
resolve(movie_name);
});
},
doConfirmResult: function(data){
Homey.log('doConfirmResult');
if(data && data.movies && data.movies.length > 0){
var movie = data.movies[0];
return new Promise(function(resolve, reject){
if(Homey.manager('settings').get('skip_confirm')){
resolve(movie);
}
else {
var question = __('messages.confirm', {'title': movie.original_title});
Homey.manager( 'speech-input').confirm(question, function(err, confirmed){
//var confirmed = true;
Homey.log('Adding ' + movie.original_title + ': ' + confirmed );
if(confirmed){
resolve(movie);
}
else {
reject(__('messages.not_adding'));
}
});
}
});
}
else {
throw __('messages.sorry_couldnt_add');
}
},
doAdd: function(movie){
Homey.manager('flow').trigger('added', {
movie: movie.original_title
});
return this.request('movie.add', {
identifier: movie.imdb
});
},
//doCheckWatchlist: function(){
// Homey.log('doCheckWatchlist');
//},
//
//doCheckCollection: function(){
// Homey.log('doCheckCollection');
//},
/**
* Request the CouchPotato api
* @param endpoint
* @param args
* @param callback
*/
request: function(endpoint, args){
Homey.log('request');
var host = Homey.manager('settings').get('host'),
api = Homey.manager('settings').get('api');
var url = host + '/api/' + api + '/' + endpoint;
url += '?' + this.requestSerialize(args);
Homey.log(url);
return request.get({
url: url,
json: true
});
},
/**
* Object to querystring
* @param obj
* @returns {string}
*/
requestSerialize: function(obj) {
Homey.log('requestSerialize');
var str = [];
for(var p in obj) {
if (obj.hasOwnProperty(p)) {
var v = obj[p];
str.push(typeof v == 'object' ?
serialize(v, p) :
encodeURIComponent(p) + "=" + encodeURIComponent(v));
}
}
return str.join("&");
}
});
module.exports.init = function(){
new App();
};