-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
57 lines (48 loc) · 1.33 KB
/
main.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
const spawn = require('child_process').spawn;
const PlugAPI = require('plugapi');
const youtubedl = require('youtube-dl');
const omx = require('node-omxplayer');
const bot = new PlugAPI({
email: process.env.PLUG_EMAIL,
password: process.env.PLUG_PASSWORD
});
bot.connect('survival-machines'); // The part after https://plug.dj
bot.on(PlugAPI.events.ROOM_JOIN, (room) => {
console.log(`Joined ${room}`);
main();
});
bot.on(PlugAPI.events.CHAT, (data) => {
console.log(`${data.from} said ${data.message}`);
});
function main()
{
bot.sendChat('Coucou, je suis le bot de td8 !');
catchCurrentVideo(bot);
}
function catchCurrentVideo(bot)
{
var timeElapsed = bot.getTimeElapsed();
var cid = bot.getMedia().cid;
console.log('video current time : ' + timeElapsed);
readVideo(bot, cid, timeElapsed);
}
function readVideo(bot,cid,time=0)
{
console.log('read ' + cid);
var video = youtubedl(cid, ['-f mp4'], {cwd: __dirname});
video.on('info', function(info) {
console.log('video started : ' + cid);
if (time != 0)
{
stream(bot, info.url, bot.getTimeElapsed());
}
});
}
function stream(bot,url, time=0)
{
console.log('stream ' + bot.getMedia());
let omxProcess = spawn('omxplayer', ['-l '+ time, url]);
omxProcess.on('close', function() {
console.log('player closed');
});
}