node-twitter icon indicating copy to clipboard operation
node-twitter copied to clipboard

Download images and videos from DMs

Open AaronStackBarnes opened this issue 6 years ago • 2 comments

I need to be able to download DM images and videos. Can this library do that? I couldn't figure out how
so I edited the package locally to add this functionality. I am a total beginner but I am happy to push up a PR if it is deemed worthwhile. Sorry if this the wrong place to ask questions like this. Thanks!

AaronStackBarnes avatar Jul 06 '18 18:07 AaronStackBarnes

Hi, can you share the code please ? Thank you

haddadnidal avatar Jul 06 '18 18:07 haddadnidal

in the __ request method

  // Download Media
  if (typeof params.getMedia !== 'undefined') {
    downloadImage = true;
    getMedia = params.getMedia;
    imagePath = params.imagePath;
    delete params.getMedia;
    delete params.imagePath;
    var options = {
      method: method.toLowerCase(),  // Request method - get || post
      url: path
    };
  } else {
    // Build the options to pass to our custom request object
    var options = {
      method: method.toLowerCase(),  // Request method - get || post
      url: this.__buildEndpoint(path, base) // Generate url
    };
  }

and then where you call request

  // Callback version
  if (downloadImage){
    var ws = fs.createWriteStream(imagePath);
    ws.on('error', function(err) { console.log(err); });
    this.request(options).pipe(ws);
    downloadImage = false;
    imagePath = "";
    callback(null, null, null);
  } else {
    this.request(options, function(error, response, data) {

I am happy to refactor/improve if you think the functionality is needed. I just need something for a demo quick.

AaronStackBarnes avatar Jul 06 '18 19:07 AaronStackBarnes