Last active
March 11, 2016 18:19
-
-
Save mikerudolph/9201696 to your computer and use it in GitHub Desktop.
I wanted to see how easy it was to download Rdio songs as you listen to them, turns out its just as easy as I though.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
url = require('url'), | |
fs = require('fs'); | |
var port = 9000; | |
var httpServer = http.createServer(function(request, response) { | |
var parsedUrl = url.parse(request.url); | |
var requestData; | |
var requestOpts = { | |
headers: request.headers, | |
hostname: request.headers.host, | |
method: request.method, | |
path: parsedUrl.path | |
}; | |
var mp3FileName = 'temp-mp3-file.mp3'; | |
var mp3File = null; | |
var proxy = http.request(requestOpts, function(res) { | |
console.log(res); | |
// Check to see if this is an mp3 file | |
// TODO: verify its from rdio and not something random | |
if (parsedUrl.path.indexOf('.mp3') !== -1) { | |
console.log('FOUND MP3 FILE!!!'); | |
mp3File = new Buffer(''); | |
} | |
res.on('data', function(chunk) { | |
if (mp3File) { | |
mp3File = Buffer.concat([mp3File, chunk]); | |
} | |
response.write(chunk, 'binary'); | |
}); | |
res.on('end', function() { | |
if (mp3File) { | |
fs.writeFile('./mp3/' + mp3FileName, mp3File, function(err) { | |
if (err) { | |
return console.log('Error writing file', err); | |
} | |
console.log('FINISHED GETTING MP3 FILE'); | |
}); | |
mp3File = null; | |
} | |
response.end(); | |
}); | |
res.on('error', function(err) { | |
console.log('ERROR SOMEWHERE'); | |
}); | |
response.writeHead(res.statusCode, res.headers); | |
}); | |
request.on('data', function(chunk) { | |
requestData = requestData + chunk; | |
proxy.write(chunk, 'binary'); | |
}); | |
request.on('end', function() { | |
proxy.end(); | |
}); | |
request.on('error', function(err) { | |
console.log('Error'); | |
}); | |
}); | |
httpServer.listen(port, function() { | |
console.log('HTTP Proxy Server is listening on port: ' + port); | |
}); |
Are you the author of Allavsoft?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Apples are not banana's