1

I wrote a node.js app that displays in real time pictures from Instagram with a specific hashtag. The app works very well and is very quick. What i would like to add, is the possibility to save all displayed images into a specific folder (on my computer) for printing (then adding a script to do that).

Do you know if there is a library that would allow me to save those images into a folder ? Thank you

Amine
  • 29
  • 3

1 Answers1

0

The fact that it is an Instagram photo and you are getting it in a real-time is not really important in saving the photo on your local hard drive.

Regardless of you using any npm module or simply using your own code some where at the end you should have a URL to the photo like this:

"standard_resolution": {
                "url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg",
                "width": 612,
                "height": 612
            }

So you can simply use:

var http = require('http-get');
var options = {url: 'http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg'};
http.get(options, '/path/to/where/you/want/myPic1.jpg', function (error, result) {
    if (error) {
        console.error(error);
    } else {
        console.log('Photo successfully downloaded  and saved at: ' + result.file);
    }
});

or I have seen this too (I can't remember where but I have it as snippet):

var http = require('http')
  , fs = require('fs')
  , options

options = {
    host: 'www.google.com'
  , port: 80
  , path: '/images/logos/ps_logo2.png'
}

var request = http.get(options, function(res){
    var imagedata = ''
    res.setEncoding('binary')

    res.on('data', function(chunk){
        imagedata += chunk
    })

    res.on('end', function(){
        fs.writeFile('logo.png', imagedata, 'binary', function(err){
            if (err) throw err
            console.log('File saved.')
        })
    })

})

Since it is an http request the respond time depends on so many factors (from the speed of your internet, Instagram server respond time, all the way to your hard drive speed).

So if you can make a queue with Redis or other message queue tools you can make sure all the photos eventually will be saved locally if your app is running 24x7 with lots of subscriptions.

Good luck!

Update: I found the URL to my code snippets: Writing image to local server

Appreciate these guys for their codes! I just pointed out.

Community
  • 1
  • 1
Maziyar
  • 1,913
  • 2
  • 18
  • 37