From 649b45c084d02598a44571d40edeb3e5660ff48e Mon Sep 17 00:00:00 2001
From: Bram van der Veen <96aa48@gmail.com>
Date: Fri, 5 Jun 2015 18:00:20 +0200
Subject: [PATCH] Made the application work, can now lookup and play songs on a
mumble server
---
app.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 68 insertions(+), 15 deletions(-)
diff --git a/app.js b/app.js
index d287681..af844c6 100644
--- a/app.js
+++ b/app.js
@@ -2,6 +2,11 @@
var lame = require('lame');
var mumble = require('mumble');
+var yt = require('youtube-search');
+var dl = require('youtube-dl');
+var fs = require('fs');
+
+var lastresults = [];
mumble.connect('mumble://wallpiece:9648', function(error, client) {
if(error) { throw new Error(error); }
@@ -12,31 +17,79 @@ mumble.connect('mumble://wallpiece:9648', function(error, client) {
});
});
-var start = function( client ) {
+var start = function(client) {
var input = client.inputStream();
var decoder = new lame.Decoder();
- client.channelByName('Gaming').join()
-
client.on('textMessage', function (data) {
- // console.log(client.userBySession(data.session[0]));
- if (data.message == 'play') {
- var stream;
- decoder.on( 'format', function( format ) {
- // console.log( format );
+ var command = data.message.split(' ');
- stream.pipe( client.inputStream({
+ if (command[0] == '/join') {
+ client.channelById(client.userBySession(data.actor).channel.id).join();
+ }
+ else if (command[0] == '/select'){
+ if (command[1] && parseInt(command[1]) != NaN) {
+ if (lastresults.length > 0) {
+ command[1] = parseInt(command[1]);
+ dl.exec(lastresults[command[1]].link, ['-x', '--audio-format', 'mp3', '-o', 'music/%(title)s.%(ext)s'], {}, function(err, output) {
+ if (err) throw err;
+ play(client, lastresults[command[1]].title);
+ });
+ }
+ else {
+ sendMessage(client, 'You didn\'t do a song lookup yet, try /play (<songname>). Or you didn\'t fill in a number.');
+ }
+ }
+ else {
+ sendMessage(client, 'You need to specify which song you want to play : (<songnumber>)')
+ }
+ }
+ else if (command[0] == '/play') {
+ if (command[1]) {
+ if (command[1].substr(0,6) == 'http://' || command[1].substr(0, 7) == 'https://') {
+
+ }
+ else if (command[1] == 'offline') {
+ }
+ else {
+ command.splice(0,1);
+ yt(command.join(' '), {key: fs.readFileSync('apikey').toString()}, function (err, results) {
+ lastresults = results;
+ if (err) console.warn(err);
+ var list = '
What song do you wanna play?
';
+ for (var i = 0; i < results.length; i++) {
+ list += '[' + i +'] ' + results[i].title + '
';
+
+ }
+ list += '
Type what song you want with /select <number of the song>'
+ sendMessage(client, list);
+ });
+ }
+ }
+ else {
+ sendMessage(client, 'You need to specify what you would want to play : (offline/<songname>/<youtubelink>)');
+ }
+ }
+
+ console.log(data);
+
+ });
+
+ function sendMessage(client, message) {
+ client.sendMessage('TextMessage', {'message': message, channelId: [client.user.channel.id]})
+ }
+
+ function play(client, song) {
+ var stream;
+ decoder.on( 'format', function(format) {
+ stream.pipe(client.inputStream({
channels: format.channels,
sampleRate: format.sampleRate,
gain: 0.1
}));
});
- stream = require('fs').createReadStream(__dirname + '/test.mp3').pipe( decoder );
- }
- client.channelByName('Gaming').join()
- // if (data.)
- // client.sendMessage('Received message');
- });
+ stream = require('fs').createReadStream(__dirname + '/music/' + song + '.mp3').pipe(decoder);
+ }
};