Added a way to download grooveshark songs and decode them.
This commit is contained in:
parent
4a2bbb08a5
commit
787e0ebb3c
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@ node_modules
|
|||
*sublime*
|
||||
mumblemusicbot
|
||||
*.pem
|
||||
*.mp3
|
||||
|
|
54
index.js
54
index.js
|
@ -2,6 +2,10 @@
|
|||
var mumble = require('mumble');
|
||||
var GS = require('grooveshark-streaming');
|
||||
var fs = require('fs');
|
||||
var http = require('http');
|
||||
var lame = require('lame');
|
||||
|
||||
var connection;
|
||||
|
||||
var options = {
|
||||
key : fs.readFileSync(__dirname + '/private.pem'),
|
||||
|
@ -11,15 +15,24 @@ var options = {
|
|||
console.log('Connecting');
|
||||
mumble.connect('mumble://wallpiece:9648', options, function (err, res) {
|
||||
if (err) throw new Error(err);
|
||||
connection = res;
|
||||
|
||||
res.authenticate('MusicBot', 'yolo');
|
||||
|
||||
res.on('initialized', function () {
|
||||
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...
|
||||
res.on( 'textMessage', function (data) {
|
||||
console.log(data);
|
||||
if (data.message.split(' ')[0] == '/m' || data.message.split(' ')[0] == '/music') {
|
||||
var command = data.message.split(' ').splice(1);
|
||||
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);
|
||||
}
|
Reference in a new issue