-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcouchpotato.js
More file actions
243 lines (197 loc) · 6.17 KB
/
Copy pathcouchpotato.js
File metadata and controls
243 lines (197 loc) · 6.17 KB
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
/*jslint node: true */
'use strict';
var CouchPotatoAPI = require('./lib/couchpotato-api');
var TelegramBot = require('node-telegram-bot-api');
var NodeCache = require("node-cache");
var _ = require("lodash");
var config = require('./config.json');
var bot = new TelegramBot(config.telegram.botToken, {
polling: true
});
var couchpotato = new CouchPotatoAPI({
hostname: config.couchpotato.hostname,
apiKey: config.couchpotato.apiKey,
port: config.couchpotato.port,
urlBase: config.couchpotato.urlBase,
ssl: config.couchpotato.ssl,
username: config.couchpotato.username,
password: config.couchpotato.password
});
var cache = new NodeCache();
/*
get the bot name
*/
bot.getMe().then(function(msg) {
console.log('Welcome to the couchpotato bot %s!', msg.username);
}).catch(function(err) {
throw new Error(err);
});
/*
handle start command
*/
bot.onText(/\/start/, function(msg) {
var chatId = msg.chat.id;
var username = msg.from.username || msg.from.first_name;
var response = [];
response.push('Hello ' + username + ', use /s to search');
response.push('\n`/s [movie] to continue...`');
var opts = {
"parse_mode": "Markdown",
"selective": 2,
};
bot.sendMessage(chatId, response.join('\n'), opts);
});
/*
handle search command
*/
bot.onText(/\/[Ss](earch)? (.+)/, function(msg, match) {
var messageId = msg.message_id;
var chatId = msg.chat.id;
var fromId = msg.from.id;
var movieName = match[2];
couchpotato.get("movie.search", {
"q": movieName
}).then(function(result) {
if (result.movies === undefined) {
throw new Error('could not find ' + movieName + ', try searching again');
}
return result.movies;
})
.then(function(movies) {
console.log(fromId + ' requested to search for movie ' + movieName);
var movieList = [];
var response = ['*Found ' + movies.length + ' movies:*'];
_.forEach(movies, function(n, key) {
var id = key + 1;
var title = n.original_title;
var year = ("year" in n ? n.year : '');
var rating = ("rating" in n ? ("imdb" in n.rating ? n.rating.imdb[0] + '/10' : '') : '');
var movieId = ("imdb" in n ? n.imdb : n.tmdb_id);
var thumb = ("images" in n ? ("poster" in n.images ? n.images.poster[0] : '') : '');
var runtime = ("runtime" in n ? n.runtime : '');
var onIMDb = ("via_imdb" in n ? true : false);
movieList.push({
"id": id,
"title": title,
"year": year,
"rating": rating,
"movie_id": movieId,
"thumb": thumb,
"via_imdb": onIMDb
});
response.push(
'*' + id + '*) ' +
(onIMDb ? '[' + title + '](http://imdb.com/title/' + movieId + ')' : '[' + title + '](https://www.themoviedb.org/movie/' + movieId + ')') +
(year ? ' - _' + year + '_' : '') +
(rating ? ' - _' + rating + '_' : '') +
(runtime ? ' - _' + runtime + 'm_' : '')
);
});
response.push('\n`/m [number] to continue...`');
// set cache
cache.set(fromId, movieList);
return response.join('\n');
})
.then(function(response) {
var opts = {
"disable_web_page_preview": true,
"parse_mode": "Markdown",
"selective": 2,
};
bot.sendMessage(chatId, response, opts);
}).catch(function(err) {
bot.sendMessage(chatId, "Oh no! " + err);
});
});
/*
handle movie command
*/
bot.onText(/\/[mM](ovie)? ([\d]{1})/, function(msg, match) {
var messageId = msg.message_id;
var chatId = msg.chat.id;
var fromId = msg.from.id;
var movieId = match[2];
// set movie option to cache
cache.set(fromId + 'm', movieId);
couchpotato.get("profile.list")
.then(function(result) {
if (result.list === undefined) {
throw new Error("could not get profiles, try searching again");
}
if (cache.get(fromId) === undefined) {
throw new Error("could not get previous movie list, try searching again");
}
return result.list;
})
.then(function(profiles) {
console.log(fromId + ' requested to get profiles list');
var profileList = [];
var response = [];
_.forEach(profiles, function(n, key) {
profileList.push({
"id": key + 1,
"label": n.label,
"hash": n._id
});
response.push('*' + (key + 1) + '*) ' + n.label);
});
response.push('\n\n`/p [number] to continue...`');
// set cache
cache.set("profiles", profileList);
return response.join(' ');
})
.then(function(response) {
bot.sendMessage(chatId, response, {
"selective": 2,
"parse_mode": "Markdown"
});
})
.catch(function(err) {
bot.sendMessage(chatId, "Oh no! " + err);
});
});
/*
handle quality profile command
*/
bot.onText(/\/[pP](rofile)? ([\d]{1})/, function(msg, match) {
var messageId = msg.message_id;
var chatId = msg.chat.id;
var fromId = msg.from.id;
var profileId = match[2];
var profileList = cache.get("profiles");
var movieId = cache.get(fromId + 'm');
var movieList = cache.get(fromId);
if (profileList === undefined || movieList === undefined || movieId === undefined) {
bot.sendMessage(chatId, 'Oh no! Error: something went wrong, try searching again');
}
var movie = _.filter(movieList, function(item) {
return item.id == movieId;
})[0];
var profile = _.filter(profileList, function(item) {
return item.id == profileId;
})[0];
couchpotato.get("movie.add", {
"identifier": movie.movie_id,
"title": movie.title,
"profile_id": profile.hash
})
.then(function(result) {
console.log(fromId + ' added movie ' + movie.title);
if (result.success === false) {
throw new Error("could not add movie, try searching again.");
}
bot.sendMessage(chatId, '[Movie added!](' + movie.thumb + ')', {
"selective": 2,
"parse_mode": "Markdown"
});
})
.catch(function(err) {
bot.sendMessage(chatId, "Oh no! " + err);
})
.finally(function() {
// delete cache items
cache.del(fromId);
cache.del(fromId + 'm');
cache.del("profiles");
});
});