Created
May 28, 2018 11:00
-
-
Save je3f0o/2fec16f1142f773f34f546c72a8c4e58 to your computer and use it in GitHub Desktop.
Person requested to share some script I used.
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
/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. | |
* File Name : index.js | |
* Created at : 2018-05-27 | |
* Updated at : 2018-05-27 | |
* Author : jeefo | |
* Purpose : | |
* Description : | |
_._._._._._._._._._._._._._._._._._._._._.*/ | |
// ignore:start | |
"use strict"; | |
/* globals */ | |
/* exported */ | |
// ignore:end | |
var fs = require("fs"); | |
var async = require("async"); | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var url = 'https://doc.lagout.org/Others/Game%20Development/Programming/'; | |
var download = function(url, filename, callback){ | |
request.get(url) | |
.on('error', function(err) { console.log(err); }) | |
.pipe(fs.createWriteStream(filename)) | |
.on('close', callback); | |
}; | |
request(url, function(err, resp, body){ | |
var $ = cheerio.load(body); | |
var links = $('pre > a'); //jquery get all hyperlinks | |
var books = []; | |
$(links).each(function(i, link){ | |
if (i === 0) { return; } | |
var uri = $(link).attr('href'); | |
books.push({ | |
url : `${ url }${ uri }`, | |
name : decodeURIComponent(uri) | |
}); | |
}); | |
async.eachSeries(books, function (book, callback) { | |
download(book.url, book.name, function (err) { | |
if (err) { | |
console.error("ERROR"); | |
console.error(book); | |
console.error(err); | |
process.exit(); | |
} else { | |
console.log(`Download: ${ book.name }`); | |
callback(); | |
} | |
}); | |
}, function () { | |
console.log("Done"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment