Last active
October 9, 2015 02:47
-
-
Save adamyeats-zz/3426030 to your computer and use it in GitHub Desktop.
Get all Twitter followers and status objects (old v1 API)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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