Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Allows us to retrieve the diff of users we're following and the one follows us.
var request = require('request-promise');
var Q = require('q');
var _ = require('lodash');
var ACCESS_TOKEN = {
PRIMARY: 'YOU_ACCESS_TOKEN'
};
var SELF_FOLLOWS_API_ENDPOINT = 'https://api.instagram.com/v1/users/self/follows?access_token={token}';
var FOLLOWED_BY_API_ENDPOINT = 'https://api.instagram.com/v1/users/self/followed-by?access_token={token}';
var promises = [];
var collectUsers = function(response) {
var deferred = Q.defer();
var users = [];
var remoteUserIterator = function(response) {
var parsedResponse = JSON.parse(response);
for (var i = 0; i < parsedResponse.data.length; i++) {
users.push(parsedResponse.data[i]);
}
if (parsedResponse.pagination.next_url != null) {
request(parsedResponse.pagination.next_url).then(function(data) {
remoteUserIterator(data);
});
} else {
deferred.resolve(users);
}
};
remoteUserIterator(response);
return deferred.promise;
};
var getUsersDiff = function (firstList, secondList) {
var list = {};
_.each(firstList, function (item) {
list[item.id] = item;
});
_.each(secondList, function (item) {
delete list[item.id];
});
return _.values(list);
};
var remoteCallDecorator = function (callback) {
return function (data) {
var deferred = Q.defer();
callback(data, deferred);
return deferred.promise;
};
};
_.each([SELF_FOLLOWS_API_ENDPOINT, FOLLOWED_BY_API_ENDPOINT], function(url) {
promises.push(request(url.replace('{token}', ACCESS_TOKEN.PRIMARY)).then(remoteCallDecorator(function(data, deferred) {
collectUsers(data).then(function(users) {
deferred.resolve(users);
});
})));
});
Q.all(promises).then(function(usersList) {
console.log(_.map(getUsersDiff(usersList[0], usersList[1]), function(user) { return user.username; }));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment