sayem314 / hooman

http interceptor to hoomanize cloudflare requests
https://www.npmjs.com/package/hooman
MIT License
146 stars 18 forks source link

Download image throught clouflare with pipe send error 503 #12

Closed Clemv95 closed 4 years ago

Clemv95 commented 4 years ago

Here is my code : `var fs = require('fs') const request = require('request'); const emojis = require('./emojis.json'); const hooman = require('hooman');

var download = async function(uri, filename, callback) { var file = fs.createWriteStream(filename); var r = hooman.stream(uri).pipe(file); r.on('error', function(err) { console.log(err); }); r.on('finish', function() { file.close(callback) }); };

for (let i = 0; i < emojis.length; i++) { nom = emojis[i].image.slice(emojis[i].image.indexOf("emoji/") + 6, emojis[i].image.length); download(emojis[i].image, "./emojis/" + nom, function(err) { console.log(err); })

}`

And here is the error image

sayem314 commented 4 years ago

You are suppose to respect issue template when creating issue, the template exists for a reason. BTW stream example is documented here > https://github.com/sayem314/hooman#pipe-stream

sayem314 commented 4 years ago

Related issue you can read on to grasp more details: https://github.com/sayem314/hooman/issues/3

Clemv95 commented 4 years ago

Yeah sorry about that it's m'y first issue on github. I dont understand what is the await lign for pipe can you help ?

sayem314 commented 4 years ago

@Clemv95 no problem then.

you can use it like this:

const fs = require('fs');
const request = require('request');
const emojis = require('./emojis.json');
const hooman = require('hooman');

const download = async function(uri, filename, callback) {
  let file = fs.createWriteStream(filename);
  let r = hooman.stream(uri).pipe(file);
  r.on('error', function(err) {
    console.log(err);
  });
  r.on('finish', function() {
    file.close(callback);
  });
};

hooman
  .get(siteUrl)
  .then((res) => {
    for (let i = 0; i < emojis.length; i++) {
      nom = emojis[i].image.slice(emojis[i].image.indexOf('emoji/') + 6, emojis[i].image.length);
      download(emojis[i].image, './emojis/' + nom, function(err) {
        console.log(err);
      });
    }
  })
  .catch((err) => {
    console.log(err.message);
  });

siteUrl is the main base url of images where you will download images from. first you make dummy request to initialize cookie.

sayem314 commented 4 years ago

You can also use this function for image/small binary files.

const download = async function(uri, filename, callback) {
  try {
    const { body } = await hooman.get(uri, {
      responseType: 'buffer',
    });
    // Write image to file
    fs.writeFileSync('image.jpg', body);
    console.log('Done');
  } catch (err) {
    callback(err);
  }
};
sayem314 commented 4 years ago

Quick full example based your code:

const fs = require('fs');
const request = require('request');
const emojis = require('./emojis.json');
const hooman = require('hooman');

const download = async function(uri, filename, callback) {
  try {
    const { body } = await hooman.get(uri, {
      responseType: 'buffer',
    });
    // Write image to file
    fs.writeFileSync(filename, body);
    callback();
  } catch (err) {
    callback(err);
  }
};

for (let i = 0; i < emojis.length; i++) {
  nom = emojis[i].image.slice(emojis[i].image.indexOf('emoji/') + 6, emojis[i].image.length);
  download(emojis[i].image, './emojis/' + nom, function(err) {
    console.log(err);
  });
}