Skip to content

Instantly share code, notes, and snippets.

@aditya2272sharma
Last active January 4, 2020 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aditya2272sharma/26ef711315deff58bb62cc08ac41676b to your computer and use it in GitHub Desktop.
Save aditya2272sharma/26ef711315deff58bb62cc08ac41676b to your computer and use it in GitHub Desktop.
var urls;
var writeStream;
var prettifier;
var specialCharRegExp = /[^a-zA-Z0-9 ]/g;
var fs = require('fs');
var Transform = require('stream').Transform;
var request = require('request');
var pretty = require('pretty');
urls = [
'http://www.google.com',
'https://timesofindia.indiatimes.com/world/middle-east/irans-gen-soleimani-killed-in-airstrike-at-baghdad-airport/articleshow/73077598.cms'
];
prettifier = new Transform({
decodeStrings: false
});
prettifier._transform = (chunk, enconding, done) => {
var textChunk = chunk.toString();
var prettyTextChunk = pretty(textChunk);
done(null, prettyTextChunk);
};
urls.forEach((url, index) => {
writeStream = fs.createWriteStream(url.replace(specialCharRegExp, '') + '.txt');
request
.get(url)
.on('response', function(response) {
console.log('==========================================================');
console.log(response.statusCode);
console.log(response.headers['content-type']);
})
.pipe(prettifier)
.pipe(writeStream);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment