Added a way to download grooveshark songs and decode them.

This commit is contained in:
Bram van der Veen 2015-02-21 12:44:33 +01:00
parent 4a2bbb08a5
commit 787e0ebb3c
2 changed files with 53 additions and 2 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ node_modules
*sublime* *sublime*
mumblemusicbot mumblemusicbot
*.pem *.pem
*.mp3

View file

@ -2,6 +2,10 @@
var mumble = require('mumble'); var mumble = require('mumble');
var GS = require('grooveshark-streaming'); var GS = require('grooveshark-streaming');
var fs = require('fs'); var fs = require('fs');
var http = require('http');
var lame = require('lame');
var connection;
var options = { var options = {
key : fs.readFileSync(__dirname + '/private.pem'), key : fs.readFileSync(__dirname + '/private.pem'),
@ -11,15 +15,24 @@ var options = {
console.log('Connecting'); console.log('Connecting');
mumble.connect('mumble://wallpiece:9648', options, function (err, res) { mumble.connect('mumble://wallpiece:9648', options, function (err, res) {
if (err) throw new Error(err); if (err) throw new Error(err);
connection = res;
res.authenticate('MusicBot', 'yolo'); res.authenticate('MusicBot', 'yolo');
res.on('initialized', function () { res.on('initialized', function () {
console.log('Connected!'); console.log('Connected!');
// var speaker = new require('speaker')();
// var decoder = new lame.Decoder();
// fs.createReadStream(__dirname + '/song.mp3').pipe(decoder).pipe(res.inputStream());
}); });
// message('Hey there, I\'m music bot!');
// On text message... // On text message...
res.on( 'textMessage', function (data) { res.on( 'textMessage', function (data) {
console.log(data);
if (data.message.split(' ')[0] == '/m' || data.message.split(' ')[0] == '/music') { if (data.message.split(' ')[0] == '/m' || data.message.split(' ')[0] == '/music') {
var command = data.message.split(' ').splice(1); var command = data.message.split(' ').splice(1);
if (command[0] == 'g' || command[0] == 'grooveshark') { if (command[0] == 'g' || command[0] == 'grooveshark') {
@ -34,6 +47,43 @@ mumble.connect('mumble://wallpiece:9648', options, function (err, res) {
// }); // });
}); });
function grooveshark() { function message(message) {
console.log('sending message', message);
connection.sendMessage('TextMessage', {'message': message, channelId: [0]});
}
function grooveshark(command) {
var lookup = command.splice(1).join(' ');
GS.Tinysong.getSongInfo(lookup, null, function (err, info) {
GS.Grooveshark.getStreamingUrl(info.SongID, function (err, url) {
if (fs.existsSync(__dirname + '/song.mp3')) fs.unlinkSync(__dirname + '/song.mp3');
message('Got the song, downloading it now!');
http.get(url, function (res) {
res.on("data", function (data){
if (fs.existsSync(__dirname + '/song.mp3')) {
fs.appendFileSync(__dirname + '/song.mp3', data);
}
else {
fs.writeFileSync(__dirname + '/song.mp3', data);
}
});
res.on('end', function () {
play(__dirname + '/song.mp3');
message('Done downloading song!');
})
})
});
});
}
function play(song) {
var speaker = new require('speaker')();
var decoder = new lame.Decoder();
fs.createReadStream(__dirname + '/song.mp3').pipe(decoder).pipe(connection.inputStream());
// fs.createReadStream(__dirname + '/song.mp3').pipe(decoder).pipe(speaker);
} }