-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (76 loc) · 2.38 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
#!/usr/bin/env node
var nodecast = require('nodecast');
var inquirer = require('inquirer');
var getYouTubeID = require('get-youtube-id');
var fetchVideoInfo = require('youtube-info');
var prettyjson = require('prettyjson');
var stop = false;
var stopall = false;
var id;
if(process.argv[2] === "stop") {
stop = true;
} else if(process.argv[2] === "stopall") {
stopall = true;
} else {
id = getYouTubeID(process.argv[2])
if(id === null) {
console.log('Invalid Youtube link')
process.exit()
}
}
var devices = nodecast.find();
var knownDevices = []
devices.on('device', function(device) {
knownDevices.push(device)
});
setTimeout(function(){
if(stopall) {
for (var i = 0; i < knownDevices.length; i++){
let device = knownDevices[i];
let yt = device.app('YouTube');
yt.stop(function(err){
console.log('Stopped', device.name)
if(i === knownDevices.length) {
process.exit()
}
})
}
} else {
var names = []
for (var i = 0; i < knownDevices.length; i++) {
names.push(knownDevices[i].name)
}
var question = {
type:'list',
name:'Select device',
choices:names
}
inquirer.prompt([question]).then(answers => {
var device = knownDevices.find(obj => {
return obj.name === answers['Select device']
})
var yt = device.app('YouTube');
if(stop) {
yt.stop(function(err){
if(!err) {
console.log(device.name,'casting stopped')
}
process.exit()
})
} else {
yt.start('v='+id, function(err){
fetchVideoInfo(id, function (err, videoInfo) {
if (err) throw new Error(err);
delete videoInfo.description;
delete videoInfo.regionsAllowed;
delete videoInfo.channelThumbnailUrl;
delete videoInfo.channelId;
delete videoInfo.embedURL;
console.log(prettyjson.render(videoInfo));
process.exit()
});
})
}
});
}
},1000)