下載音樂檔
去免費的音樂音效網站「魔王魂」下載mp3檔案
var request = require('request'),
cheerio = require('cheerio'),
fs = require('fs');
var audioDir = './audios';
var url = 'http://maoudamashii.jokersounds.com/list/bgm3.html';
request(url, function(err, res, body) {
if (!err && res.statusCode === 200) {
var $ = cheerio.load(body);
var audioUrls = [];
$('audio').each(function() {
audioUrls.push($(this).attr('src'));
});
if (!fs.existsSync(audioDir)) {
fs.mkdirSync(audioDir);
}
console.log("Detect " + audioUrls.length + " audio files.");
audioUrls.forEach(function(img, i) {
//取得檔名,因為檔案的名吃包含路徑
//像是http://maoudamashii.jokersounds.com/music/bgm/mp3/bgm_maoudamashii_acoustic51.mp3
//所以必須將前面的路徑給去除
var filename = audioUrls[i].split('/').slice(-1)[0];
console.log("Downloading [ " + filename + " ]");
request(audioUrls[i]).pipe(fs.createWriteStream(audioDir + '/' + filename));
});
}
});