I need to download a remote image, that its path is saved on the wineTweet.label
attribut.
And then POST a tweet with this image.
My implementation is working, but I first save the image to a file and then read it before posting it.
Here is the code :
var file = fs.createWriteStream("file.jpg");
https.get(wineTweet.label, function (response) {
response.pipe(file);
file.on('finish', function () {
var data = require('fs').readFileSync('file.jpg');
// Make post request on media endpoint. Pass file data as media parameter
client.post('media/upload', {media: data}, function (error, media, response) {
if (!error) {
// If successful, a media object will be returned. // Lets tweet it
var status = {
status : 'I am a tweet',
media_ids: media.media_id_string // Pass the media id string
}
client.post('statuses/update', status, function (error, tweet, response) {
if (!error) {
console.log(tweet);
});
}
});
});
});
How to directly connect ReadingStream to the POST request ?
Just use the response
directly, which is already a Readable stream, instead of writing/reading a temporary file:
https.get(wineTweet.label, function(res) {
// Make post request on media endpoint. Pass file data as media parameter
client.post('media/upload', {media: res}, function(error, media, response) {
if (!error) {
// If successful, a media object will be returned. // Lets tweet it
var status = {
status : 'I am a tweet',
media_ids: media.media_id_string // Pass the media id string
};
client.post('statuses/update', status, function(error, tweet, response) {
if (!error)
console.log(tweet);
});
}
});
});
On an unrelated note, you should handle errors appropriately instead of ignoring them.