Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter API 1.1 favorites/following/followers backup
/* Twitter API 1.1 favorites/following/followers backup (a textarea will appear so you can copy/paste to save data).
** Get your access keys to use Twitter API 1.1: https://dev.twitter.com/docs/auth/tokens-devtwittercom
** Format JSON in Firefox: https://addons.mozilla.org/EN-us/firefox/addon/jsonview/
** You can change Twitter API URL and Twitter screen_name, then execute script from a trusted web page without CSP protection like about:blank in Firefox Scratchpad or any other browser console.
*/
var url = "https://api.twitter.com/1.1/favorites/list.json";
//var url = "https://api.twitter.com/1.1/friends/list.json";
//var url = "https://api.twitter.com/1.1/followers/list.json";
var accessor = {
token: "XXX",
tokenSecret: "XXX",
consumerKey : "XXX",
consumerSecret: "XXX"
};
var message = {
action: url,
method: "GET",
parameters: {
screen_name: "baptx",
count: 200,
callback: "getJSONP"}
};
var out = [];
var length = -1;
var start = 0;
var next = -1;
var res;
function getJSONP(data)
{
res = data;
}
function loadAPI_favorites()
{
if (length != 1)
{
if (next != -1)
message.parameters.max_id = next;
OAuth.completeRequest(message, accessor);
OAuth.SignatureMethod.sign(message, accessor);
var script3 = document.createElement("script");
script3.setAttribute("src", url + '?' + OAuth.formEncode(message.parameters));
document.body.appendChild(script3);
script3.addEventListener("load", function() {
length = res.length;
for (var i = 0; i < length; i++)
out[start + i] = res[i].created_at + "\t@" + res[i].user.screen_name + ": " + res[i].text + "\t" + "(tweet ID: " + res[i].id_str + ")";
next = res[length - 1].id_str;
start += length - 1;
loadAPI_favorites();
});
}
else
{
displayData();
}
}
function loadAPI_following_followers()
{
if (length == -1 || res.next_cursor_str != 0)
{
message.parameters.cursor = next;
OAuth.completeRequest(message, accessor);
OAuth.SignatureMethod.sign(message, accessor);
var script3 = document.createElement("script");
script3.setAttribute("src", url + '?' + OAuth.formEncode(message.parameters));
document.body.appendChild(script3);
script3.addEventListener("load", function() {
length = res.users.length;
for (var i = 0; i < length; i++)
out[start + i] = res.users[i].screen_name;
next = res.next_cursor_str;
start += length;
loadAPI_following_followers();
});
}
else
{
displayData();
}
}
function displayData()
{
var box = document.createElement("textarea");
box.value = out.join('\n');
document.body.appendChild(box);
}
var script = document.createElement("script");
script.setAttribute("src", "https://pastebin.com/raw/HFjqYLdG"); // http://oauth.googlecode.com/svn/code/javascript/oauth.js (down)
document.body.appendChild(script);
script.addEventListener("load", function() {
var script2 = document.createElement("script");
script2.setAttribute("src", "https://pastebin.com/raw/M0N8JKwf"); // http://pajhome.org.uk/crypt/md5/sha1.js
document.head.appendChild(script2);
script2.addEventListener("load", function() {
(url == "https://api.twitter.com/1.1/favorites/list.json") ? loadAPI_favorites() : loadAPI_following_followers();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment