From 787e0ebb3c7b4786cc51dd9617892cf5a4561a75 Mon Sep 17 00:00:00 2001 From: Bram van der Veen <96aa48@gmail.com> Date: Sat, 21 Feb 2015 12:44:33 +0100 Subject: [PATCH] Added a way to download grooveshark songs and decode them. --- .gitignore | 1 + index.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 33a4a5f..ce29665 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules *sublime* mumblemusicbot *.pem +*.mp3 diff --git a/index.js b/index.js index b272593..d84099c 100644 --- a/index.js +++ b/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()); }); - // On text message... + // 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); } \ No newline at end of file