-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
310 lines (273 loc) · 8.76 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
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
#!/usr/bin/env node
const server = require('./lib/server.js');
const config = require('./lib/config.js');
const yts = require('./lib/yts-api/yts-api.js');
const tv = require('./lib/tv-api/tv-api.js');
const debug = require('debug')('main');
const humanize = require('humanize');
const inquirer = require('inquirer');
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
const clivas = require('clivas');
const imdb = require('imdb-api');
const program = require('commander');
const parseTorrent = require('parse-torrent');
const WebTorrent = require('webtorrent');
const img2ascii = require('image-to-ascii');
var buffered = false;
const TRACKERS_LIST = config.get('trackers');
const BUFFERING_SIZE = 10 * 1024 * 1024;
const client = new WebTorrent({
maxConns: 500,
});
const ui = new inquirer.ui.BottomBar();
const apiKey = program.apiKey ?
program.apiKey
: config.get('apiKey') === null ? '' : config.get('apiKey');
program
.version('0.0.1')
.usage('<command>')
.option('--api-key [key]', 'API key for Open Movie Database.');
function movieQuestionarreHandler(req) {
imdb
.getReq(Object.assign(req, { opts: {apiKey} }))
.then((movie) => {
clivas.clear();
printMovie(movie);
yts.listMovies({query_term: movie.title}).then((res) => {
var allTorrents = res.data.movies[0].torrents;
var torrentsList = allTorrents.map((item, i) => {
return { name : `${item.quality} | ${item.size} | ${item.seeds} seeds | ${item.peers} leeches`, value: i };
});
inquirer.prompt([
{
type: 'list',
name: 'torrent',
message: 'Which torrent would you like to stream?',
choices: torrentsList
}
]).then(function(answer){
if (answer.trailer) {
var trailer = res.data.movies[0].yt_trailer_code;
openUrl(`http://youtube.com/watch?v=${trailer}`);
}
magnet = parseTorrent.toMagnetURI({
infoHash: allTorrents[answer.torrent].hash,
announce: TRACKERS_LIST,
name: res.data.movies[0].title
});
// console.log(magnet);
client.add(magnet);
});
});
}).catch(err => clivas.line(err));
}
program
.command('movie <name>')
.description('Search for a movie by its title.')
.action(name => {
movieQuestionarreHandler({ name });
});
function seriesQuestionnareHandler(req) {
imdb
.getReq(Object.assign(req, { opts: { apiKey } }))
.then((show) => {
clivas.clear();
printShow(show);
tv.getShow(show.imdbid)
.then((show) => {
var seasonsList = {};
for (var i = 0; i < show.episodes.length; i++) {
var episode = show.episodes[i];
var season = show.episodes[i].season; //arrays are 0-index, so normalize the season number
if (!Array.isArray(seasonsList[season]))
{
seasonsList[season] = [];
}
seasonsList[season].push(episode);
}
var prompts = [
{
type: 'list',
name: 'season',
message: 'Which season would you like to stream?',
choices: (answers) => {
var seasons = [];
for (let key of Object.keys(seasonsList))
{
seasons.push({ name: `Season ${key}`, value: key });
}
return seasons;
}
},
{
type: 'list',
name: 'episode',
message: 'Which episode would you like to stream?',
choices: (answers) => {
return seasonsList[answers.season].map((item, i) => {
return { name: `${i + 1}. ${item.title}`, value: i };
});
}
},
{
type: 'list',
name: 'torrent',
message: 'Which torrent would you like to stream?',
when: (answers) => {
return Object.keys(seasonsList[answers.season][answers.episode].torrents).length > 1;
},
choices: (answers) => {
var torrents = [];
var i = 0;
for (let key of Object.keys(seasonsList[answers.season][answers.episode].torrents)) {
torrents.push({ name: `${key}`, value: key});
i++;
}
torrents.splice(0, 1);
return torrents;
}
}
];
inquirer.prompt(prompts).then(function(answers){
var torrent = '0';
if (answers.torrent) torrent = answers.torrent;
client.add(seasonsList[answers.season][answers.episode].torrents[torrent].url);
});
});
}).catch(err => clivas.line(err));
}
program
.command('television <name>')
.alias('tv')
.description('Search for a series by its title.')
.action(name => {
seriesQuestionnareHandler({ name });
});
program
.command('search [term]')
.alias('s')
.description('Begin interactive session.')
.action((term) => {
inquirer.prompt([{
type: 'autocomplete',
name: 'title',
message: 'Search for a show or movie',
source: function(answersSoFar, input) {
return new Promise((resolve, reject) => {
imdb.search({ title: input || '' }, { apiKey }).then((res) => {
let list = res.results.map((result) => { return { name: `${ result.title } | ${ result.type } | ${ result.year }`, value: result }; });
if (list.length > 0) {
resolve(list);
}
else {
resolve([]);
}
}).catch(err => resolve([]));
});
}
}]).then(function(answers) {
if (answers.title.type === 'movie') {
movieQuestionarreHandler({ id: answers.title.imdbid });
}
else if (answers.title.type === 'series') {
seriesQuestionnareHandler({ id: answers.title.imdbid });
}
}).catch(err => console.log(err));
});
program
.command('link <name>')
.alias('l')
.description('Just add a magnet link.')
.action(link => {
client.add(link);
});
program.command('apikey <key>')
.alias('api')
.description('Sets and saves your OMDB api key.')
.action(value => {
config.set('apiKey', value).then(() => {
clivas.line('Saved API key.');
process.emit('SIGINT');
});
});
program.parse(process.argv);
process.on('SIGINT', () => {
client.destroy();
process.exit(0);
});
client.on('error', err => {
clivas.line(err);
});
client.on('torrent', (torrent) => {
var vids = server.findMovieInTorrent(torrent.files);
var file;
inquirer.prompt([
{
type: 'list',
name: 'file',
message: 'Which file would you like to stream?',
choices: function() {
return vids.map((file, i) => {
return { name: file.name, value: i };
});
},
when: function() {
return vids.length > 1;
},
default: 0
}
]).then(function(answer) {
debug('creating server...');
var f = Object.keys(answer).length == 0 ? 0 : answer.file;
var s = server.createServer(vids[f]);
s.on('listening', () => {
var port = s.address().port;
clivas.line(`{underline+bold:Serving movie at http://localhost:${ port }}`);
debug('torrent downloading...');
torrent.on('download', (chunk) => {
var buffered_percent = torrent.downloaded / (torrent.length / 100);
avgSpeed(torrent.downloadSpeed);
ui.updateBottomBar(`Buffering... ${Math.floor(buffered_percent)}%`);
if (buffered_percent >= 20)
{
ui.updateBottomBar(`${ Math.floor(100 * torrent.progress) }% downloaded at ${ humanize.filesize(avgSpeed(torrent.downloadSpeed)) }/s`);
if (buffered == false)
{
buffered = true;
}
}
});
});
});
});
let speedBuffer = Array();
let avgSpeed = (speed) => {
debug('average speed %o', speedBuffer);
if (speedBuffer.length < 11) {
speedBuffer.push(speed);
}
speedBuffer.splice(0, 1);
speedBuffer.push(speed);
return speedBuffer.reduce((a, b) => (a + b) / 2);
};
var printShow = function printShow(show)
{
debug('printing show info for %o', show.title);
clivas.clear();
clivas.line(`{gray+bold:${ show.title }}`);
show.ratings.forEach((rating) => {
clivas.line(`{yellow:${ rating.Source } rating: ${ rating.Value }}`);
});
clivas.line(`{gray:${show.plot}}`);
};
var printMovie = function printMovie(movie)
{
debug('printing movie info for movie %o', movie.title);
clivas.clear();
clivas.line(`{gray+bold:${ movie.title }}`);
if (movie.tomato) {
clivas.line(`{red+bold:Rotten Tomatoes Score: ${ movie.tomato.meter }%}`);
clivas.line(`{red+bold:Rotten Tomatoes User Score: ${ movie.tomato.userMeter }%}`);
}
clivas.line(`{gray:${movie.plot}}`);
};