Skip to content

Commit dc605c1

Browse files
committed
Merge pull request #44 from zobalogh/instagram-location-extension
Adding metadata to Instagram payload
2 parents 27e023f + 26ac954 commit dc605c1

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

Diff for: instagram/instagram.html

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
photos the user has liked. Each message sent by the node contains a single
8787
photo in its payload, either as a Buffer containing the photo or its URL.</p>
8888

89+
<p>When the metadata is available within Instagram's service, the photo's capture time and location
90+
are also forwarded in the form of msg.time, msg.lat and msg.lon.</p>
91+
8992
<p>Videos are currently not supported and are ignored.</p>
9093
</script>
9194

@@ -123,6 +126,9 @@
123126
photos the user has liked. Each message sent by the node contains a single
124127
photo in its payload, either as a Buffer containing the photo or its URL.</p>
125128

129+
<p>When the metadata is available within Instagram's service, the photo's capture time and location
130+
are also forwarded in the form of msg.time, msg.lat and msg.lon.</p>
131+
126132
<p>Videos are currently not supported and are ignored.</p>
127133
</script>
128134

Diff for: instagram/instagram.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module.exports = function(RED) {
2929

3030
function InstagramCredentialsNode(n) {
3131
RED.nodes.createNode(this,n);
32-
// this.clientID = n.clientID;
3332
}
3433

3534
function downloadImageAndSendAsBuffer(node, url, msg) {
@@ -112,7 +111,7 @@ module.exports = function(RED) {
112111
}
113112

114113
if(medias) {
115-
for(var i = 0; i < medias.length; i++) {
114+
for(var i = 0; i < medias.length; i++) {
116115
if (node.inputType === "like") { // like is a special case as per Instagram API behaviour
117116
if(areWeInPaginationRecursion === false) { // need to set the pointer of latest served liked image before pagination occurs
118117
idOfLikedReturned = medias[0].id;
@@ -132,6 +131,20 @@ module.exports = function(RED) {
132131
//deliberate no-op
133132
} else if(medias[i].type === IMAGE) {
134133
var url = medias[i].images.standard_resolution.url;
134+
135+
if(medias[i].location) {
136+
if(medias[i].location.latitude) {
137+
msg.lat = medias[i].location.latitude;
138+
}
139+
if(medias[i].location.longitude) {
140+
msg.lon = medias[i].location.longitude;
141+
}
142+
}
143+
144+
if(medias[i].created_time) {
145+
msg.time = new Date(medias[i].created_time * 1000);
146+
}
147+
135148
if (node.outputType === "link") {
136149
msg.payload = url;
137150
node.send(msg);

Diff for: test/instagram/instagram_spec.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -329,17 +329,23 @@ describe('instagram nodes', function() {
329329
fetchUploadedPhotos(done, true, false);
330330
});
331331

332-
it('handles like with init', function(done) {
332+
it('handles like with init and gets metadata', function(done) {
333333

334334
var newPhotoURL = "http://new_liked_photo_standard.jpg";
335335
var oldID = "MY_OLD_MEDIA_ID";
336336

337+
var injectedLat = "51.025115599";
338+
var injectedLon = "-1.396541077";
339+
var injectedTime = "1411724651";
340+
341+
var timeAsJSDate = new Date(injectedTime * 1000);
342+
337343
// need to fake the HTTP requests of the init sequence, then straight away the sequence of getting a second photo
338344
var scope = nock('https://api.instagram.com')
339345
.get('/v1/users/self/media/liked?count=1&access_token=AN_ACCESS_TOKEN')
340346
.reply(200, {"pagination":{},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","user_has_liked":true,"id":oldID}]})
341347
.get('/v1/users/self/media/liked?access_token=AN_ACCESS_TOKEN')
342-
.reply(200,{"pagination":{},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","images":{"standard_resolution":{"url":newPhotoURL}}}, {"attribution":null,"tags":[],"type":"image","id":oldID}]});
348+
.reply(200,{"pagination":{},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","images":{"standard_resolution":{"url":newPhotoURL}}, "created_time":injectedTime, "location":{"latitude": injectedLat, "name": "IBM Hursley", "longitude": injectedLon}}, {"attribution":null,"tags":[],"type":"image","id":oldID}]});
343349

344350
helper.load(instagramNode, [{id:"instagramCredentials1", type:"instagram-credentials"},
345351
{id:"instagramNode1", type:"instagram", instagram: "instagramCredentials1","inputType":"like","outputType":"link", wires:[["helperNode1"]]},
@@ -363,6 +369,9 @@ describe('instagram nodes', function() {
363369
if (testInterval !== null) {
364370
clearInterval(testInterval);
365371
}
372+
msg.lat.should.equal(injectedLat);
373+
msg.lon.should.equal(injectedLon);
374+
msg.time.toString().should.equal(timeAsJSDate.toString());
366375
msg.payload.should.equal(newPhotoURL);
367376
done();
368377
} catch(err) {
@@ -381,18 +390,24 @@ describe('instagram nodes', function() {
381390
});
382391
});
383392

384-
it('manages to buffer an image', function(done) {
393+
it('manages to buffer an image and handles metadata', function(done) {
385394
var photo = '/photo.jpg';
386395
var apiURI = 'https://api.instagram.com';
387396
var photoURI = apiURI + photo;
388397

389398
var replyText = "Hello World";
390399

400+
var injectedLat = "51.025115599";
401+
var injectedLon = "-1.396541077";
402+
var injectedTime = "1411724651";
403+
404+
var timeAsJSDate = new Date(injectedTime * 1000);
405+
391406
var scope = nock('https://api.instagram.com')
392407
.get('/v1/users/self/media/recent?count=1&access_token=AN_ACCESS_TOKEN') // request to get the initial photos uploaded by the user
393408
.reply(200, {"pagination":{},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","id":"MY_OLD_MEDIA_ID"}]})
394409
.get('/v1/users/self/media/recent?min_id=MY_OLD_MEDIA_ID&access_token=AN_ACCESS_TOKEN')
395-
.reply(200, {"pagination":{},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","images":{"standard_resolution":{"url":photoURI,"width":640,"height":640}},"id":"A_NEW_PHOTO_ID"}]})
410+
.reply(200, {"pagination":{},"meta":{"code":200},"data":[{"attribution":null,"tags":[],"type":"image","images":{"standard_resolution":{"url":photoURI,"width":640,"height":640}},"id":"A_NEW_PHOTO_ID", "created_time":injectedTime, "location":{"latitude": injectedLat, "name": "IBM Hursley", "longitude": injectedLon}}]})
396411
.get(photo)
397412
.reply(200, replyText);
398413

@@ -418,6 +433,11 @@ describe('instagram nodes', function() {
418433
if (testInterval !== null) {
419434
clearInterval(testInterval);
420435
}
436+
437+
msg.lat.should.equal(injectedLat);
438+
msg.lon.should.equal(injectedLon);
439+
msg.time.toString().should.equal(timeAsJSDate.toString());
440+
421441
msg.payload.toString().should.equal(replyText);
422442
done();
423443
} catch(err) {

0 commit comments

Comments
 (0)