Skip to content

Instantly share code, notes, and snippets.

@ireade
Forked from Akamaozu/payback.js
Created April 18, 2015 14:58
Show Gist options
  • Save ireade/c3b235aaa5cd82a33dc1 to your computer and use it in GitHub Desktop.
Save ireade/c3b235aaa5cd82a33dc1 to your computer and use it in GitHub Desktop.
$(document).ready(function(){
// required vars
var accessToken = window.location.hash.split("=")[1];
var userID = accessToken.split(".")[0];
// step 1: get users followed
getFollowedUsers(userID, accessToken, function(followed_users){
// step 2a: check for non-mutual follows
var completed_checks = 0;
var total_nonmutuals = 0;
var nonmutual_follows = [];
$.each(followed_users, function(index, followed_user){
isFollowershipMutual(followed_user.id, accessToken, function(is_mutual){
if(!is_mutual){
// step 2b: get metadata of non-mutual follows
total_nonmutuals += 1;
getUserMetadata(followed_user.id, accessToken, function(user_data){
// step 2c: render markup for non-mutual follow
var username = user_data.username;
var profile = user_data.profile_picture;
nonmutual_follows.push(
'<li class="unfollower" data-unfollowerId="'+followed_user.id+'">' +
'<a href="https://www.instagram.com/'+username+'" target="_blank">' +
'<img src="'+profile+'" alt="'+username+'">' +
'<span>'+username+'</span>' +
'</a>' +
'</li>'
);
});
}
completed_checks += 1;
})
});
// step 3: render list of nonmutual follows on an interval
var rendered_nonmutuals = 0;
var render_checker = setInterval(render_nonmutual_followers, 1000);
function render_nonmutual_followers(){
var markup = nonmutual_follows.join('');
$('#payback').append( markup );
$('#unfollowers-count').html(rendered_nonmutuals+" people don't follow back");
rendered_nonmutuals += nonmutual_follows.length;
nonmutual_follows = [];
if(cancel_render_checker() === true){
clearInterval( render_checker );
}
}
function cancel_render_checker(){
if(completed_checks < followed_users.length) return false;
if(rendered_nonmutuals < total_nonmutuals) return false;
return true;
}
});
function getFollowedUsers(userID, accessToken, callback){
var api_endpoint = 'https://api.instagram.com/v1/users/'+userID+'/follows?access_token='+accessToken+'&count=500&callback=?';
$.getJSON(api_endpoint, {}, function(response){
callback( response.data );
});
}
function isFollowershipMutual(followerID, accessToken, callback){
var api_endpoint = 'https://api.instagram.com/v1/users/'+followerID+'/relationship?access_token='+accessToken+'&callback=?';
$.getJSON(api_endpoint, {}, function(response){
var is_mutual = (response.data.incoming_status !== 'none' ? true : false);
callback( is_mutual );
});
}
function getUserMetadata(userID, accessToken, callback){
var api_endpoint = 'https://api.instagram.com/v1/users/'+userID+'/?access_token='+accessToken+'&callback=?';
$.getJSON(api_endpoint, {}, function(response){
var user_metadata = response.data;
callback( user_metadata );
})
}
});
@ireade
Copy link
Author

ireade commented Apr 18, 2015

Fixed a couple of typos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment