Skip to content

Instantly share code, notes, and snippets.

@adamyeats-zz
Last active October 9, 2015 02:47
Show Gist options
  • Save adamyeats-zz/3426030 to your computer and use it in GitHub Desktop.
Save adamyeats-zz/3426030 to your computer and use it in GitHub Desktop.
Get all Twitter followers and status objects (old v1 API)
function sendRequest() {
var username = encodeURIComponent(jQuery('#username').val()),
reqUrl = 'https://api.twitter.com/1/friends/ids.json?screen_name=' + username + '&cursor=-1&stringify_ids=true&callback=?';
return jQuery.getJSON(reqUrl);
}
function getUsers(data) {
var seperatedArray = unflatten(data.ids, 100),
promises = [];
// TODO: check to see if data is good
for (var i = 0; i < seperatedArray.length; i++) {
var queryString = seperatedArray[i].join(',');
promises[i] = jQuery.getJSON('http://api.twitter.com/1/users/lookup.json?user_id=' + queryString + '&callback=?');
}
return jQuery.when.apply(jQuery, promises);
}
function sortUsers() {
var deferred = new jQuery.Deferred(),
promise = deferred.promise(),
tempData = arguments,
finalData = [];
finalData = (function () {
if (tempData[1] === "success") {
return tempData[0];
}
else {
for (var i = 0; i < tempData.length; i++) {
finalData = finalData.concat(tempData[i][0]);
}
}
return finalData;
}());
finalData = finalData.sort(function (a, b) {
if (a.status && b.status) {
return (new Date(a.status.created_at).getTime() - new Date(b.status.created_at).getTime());
}
if (a.status && !b.status) {
return 1;
}
if (!a.status && b.status) {
return -1;
}
});
deferred.resolve(finalData);
return promise;
}
function unflatten(array, per) {
var results = [],
i, y, x;
for (i = 0; i < array.length; i++) {
y = Math.floor(i / per);
x = i % per;
if (!results[y]) {
results[y] = [];
}
results[y][x] = array[i];
};
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment