I'm attempting to work through a database of "events" I have and pull photos from the Instagram API based on each event's location, radius, start time, and end time. I've set up the following code on my Node server, but it is not behaving as I would expect.
The first thing I see when I run this code is sending request to Instagram for [name] with min_timestamp: [timestamp] printed for every event. I did not expect this. I would have expected to see this line logged for the first event, then updated with a new timestamp over and over until that event reaches its end time. Then event 2, iterate through timestamps, and so forth.
I end up with the same block of photos repeated over and over for each event. It's as if my code is firing off one request to Instagram (with the initial timestamp) over and over and then stopping.
Notes on my timestamp variable: For each event I set my minTimestamp variable to initially equal event.start from my database. This is used within the request sent to Instagram. Instagram returns up to 20 photos to me. Each photo has a created_time variable. I grab the most recent created_time variable and set my minTimestamp variable to equal it (minTimestamp = images[0].created_time;) for my next request sent to Instagram (to grab the next 20 photos). This continues until minTimestamp is no longer less than endTimestamp (event.end for that event from my db).
server.js code:
// modules =================================================
var express = require('express.io');
var app = express();
var port = process.env.PORT || 6060;
var io = require('socket.io').listen(app.listen(port));
var request = require('request');
var Instagram = require('instagram-node-lib');
var mongoose = require('mongoose');
var async = require('async');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var db = require('./config/db');
var Event = require('./app/models/event');
// configuration ===========================================
mongoose.connect(db.url); // connect to our mongoDB database
// get all data/stuff of the body (POST) parameters
app.use(bodyParser.json()); // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form- urlencoded
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method- Override header in the request. simulate DELETE/PUT
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users
var baseUrl = 'https://api.instagram.com/v1/media/search?lat=';
var clientId = CLIENT-ID;
Event.find({}, function(err, events) {
async.eachSeries(events, function(event, callback) {
var name = event.event;
var latitude = event.latitude;
var longitude = event.longitude;
var distance = event.radius;
var minTimestamp = Math.floor(new Date(event.start).getTime()/1000);
var endTimestamp = Math.floor(new Date(event.end).getTime()/1000);
async.whilst(
function () { return minTimestamp < Math.floor(Date.now() / 1000) && minTimestamp < endTimestamp; },
function(callback) {
console.log('sending request to Instagram for ' + name + ' with min_timestamp: ' + minTimestamp);
request(baseUrl + latitude + '&lng=' + longitude + '&distance=' + distance + '&min_timestamp=' + minTimestamp + '&client_id=' + clientId,
function (error, response, body) {
if (error) {
console.log('error');
return;
}
//JSON object with all the info about the image
var imageJson = JSON.parse(body);
var images = imageJson.data;
var numImages = images.length;
console.log(numImages + ' images returned with starting time ' + images[(numImages - 1)].created_time + ' and ending time ' + images[0].created_time);
async.eachSeries(images, function(image, callback) {
//Save the new object to DB
Event.findOneAndUpdate( { $and: [{latitude: latitude}, {radius: distance}] }, { $push: {'photos':
{ img: image.images.standard_resolution.url,
link: image.link,
username: image.user.username,
profile: image.user.profile_picture,
text: image.caption ? image.caption.text : '',
longitude: image.location.longitude,
latitude: image.location.latitude
}}},
{ safe: true, upsert: false },
function(err, model) {
console.log(err);
}
);
console.log(numImages + ' images saved to db');
callback();
}, function(err){
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('Images failed to process');
} else {
console.log('All images have been processed successfully');
}
});
minTimestamp = images[0].created_time;
console.log('min_timestamp incremented to: ' + minTimestamp);
}
);
},
function (err) {
}
);
callback();
}, function(err){
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('An event failed to process');
} else {
console.log('All events have been processed successfully');
}
});
});
// routes ==================================================
require('./app/routes')(app); // configure our routes
// start app ===============================================
console.log('Magic happens on port ' + port); // shoutout to the user
exports = module.exports = app; // expose app