Skip to content

Instantly share code, notes, and snippets.

@zoutepopcorn
Created August 10, 2017 15:36
Show Gist options
  • Save zoutepopcorn/2a2e0838755889dfab9b71e27c6e43db to your computer and use it in GitHub Desktop.
Save zoutepopcorn/2a2e0838755889dfab9b71e27c6e43db to your computer and use it in GitHub Desktop.
Node.js
const fs = require('fs');
const download = require('download');
const htmlparser = require("htmlparser2");
const get = require('simple-get');
const pUrl = require('parse-url');
const url = process.argv[2];
const pu = pUrl(url);
const fsPath = require('fs-path');
let base = `${pu.protocol}://${pu.resource}` ;
if(pu.port) {
base += `:${pu.port}` ;
}
console.log(base);
const path = pu.pathname;
console.log(path);
let downAtt = (dest) => {
console.log(`Dest: ${dest}`);
fsPath.writeFile(dest, 'jojo', function(err){
if(err) {
throw err;
} else {
console.log('wrote a file like DaVinci drew machines');
}
});
}
let parseData = (data) => {
var parser = new htmlparser.Parser({
onopentag: function(name, att){
const src = att.src;
if(src) {
let url = "";
//console.log(`${name} - ${src}`);
if(src.indexOf("http://") == 0 || src.indexOf("https://") == 0) {
url = src;
} else {
if(src.indexOf("/") == 0) {
url = base + src;
} else {
url = `${base}/${path}/${src}`;
}
console.log(`==> ${url} `);
downAtt(data.pathname);
}
if(name == "script") {
console.log(url);
}
}
},
ontext: function(text){
},
onclosetag: function(tagname){
}
}, {decodeEntities: true});
parser.write(data);
parser.end();
}
let getCode = () => {
get.concat(url, function (err, res, data) {
if (err) throw err;
console.log(res.statusCode); // 200
let out = data.toString();
parseData(out);
})
}
if(url) {
getCode();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment