-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (100 loc) · 3.4 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
var http = require('http');
var sickbeard = function(){
this.name = 'sickbeard';
this.displayname = 'Sick Beard';
this.description = 'Send commands to Sick Beard';
this.defaultPrefs = [{
name: 'hostname',
type: 'text',
value: 'localhost'
},{
name: 'port',
type: 'text',
value: '8081'
},{
name: 'api_key',
type: 'text',
value: ''
}];
}
sickbeard.prototype.init = function(){
var self = this;
this.listen('sickbeard add (:<tv show>.+?)', 'standard', function(from, interface, params){
self.findShow(params[0], interface, from)
});
}
sickbeard.prototype.findShow = function(name, interface, from){
var self = this;
this.getPrefs().done(function(prefs){
var options = {
hostname: prefs.hostname,
port: prefs.port,
path: '/api/'+prefs.api_key+'/?cmd=sb.searchtvdb&name=' + encodeURIComponent(name) + '&lang=en',
headers: {
'user-agent': 'Woodhouse Bot - https://github.com/Woodhouse-bot/woodhouse'
}
};
var data = "";
var req = http.get(options, function(res) {
res.on('data', function (response) {
data += String(response);
});
res.on('end', function() {
var obj = JSON.parse(data);
var shows = obj.data.results;
self.checkShow(shows, interface, from);
});
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});
}
sickbeard.prototype.checkShow = function(shows, interface, from){
var self = this;
if(shows.length > 0){
var show = shows.shift();
var message = 'Did you mean: ' + show.name + ' (First Aired: ' + show.first_aired + ') - http://thetvdb.com/?tab=series&id=' + show.tvdbid;
this.sendMessage(message, interface, from);
this.api.yesNo.addYesNoQuestion(
from,
message,
function(){
self.addShow(show, interface, from);
},
function(){
self.checkShow(shows, interface, from)
}
);
} else {
this.sendMessage('No more results', interface, from);
}
}
sickbeard.prototype.addShow = function(show, interface, from){
var self = this;
this.getPrefs().done(function(prefs){
var options = {
hostname: prefs.hostname,
port: prefs.port,
path: '/api/'+prefs.api_key+'/?cmd=show.addnew&tvdbid='+show.tvdbid,
headers: {
'user-agent': 'Woodhouse Bot - https://github.com/Woodhouse-bot/woodhouse'
}
}, data = '';
var req = http.get(options, function(res) {
res.on('data', function (response) {
data += response
});
res.on('end', function() {
var obj = JSON.parse(data);
if (obj.result === 'success') {
self.sendMessage(show.name + ' added', interface, from);
} else {
self.sendMessage('There was an error adding the show. Message: ' + obj.message, interface, from);
}
});
}).on('error', function(e) {
console.log('problem with request: ' + e.message);
});
});
}
module.exports = sickbeard;