Skip to content

Instantly share code, notes, and snippets.

@Cybourgeoisie
Created September 9, 2019 12:40
Show Gist options
  • Save Cybourgeoisie/773c87cf24d55dfb6e4c7fe42ea11d25 to your computer and use it in GitHub Desktop.
Save Cybourgeoisie/773c87cf24d55dfb6e4c7fe42ea11d25 to your computer and use it in GitHub Desktop.
A simple script to paginate through all the direct replies to a tweet, uses "twurl" CLI tool
var fs = require('fs');
var exec = require('child_process').exec;
/**
::NOTICE::
Requires `twurl` to be installed -> https://github.com/twitter/twurl
::Custom Parameters::
filename
name of the file to write to, defaults to replies_output.txt
user
user who is receving the replies
since_id
the ID of the tweet to get the replies from, should be a string since too large for int
max_id
the ID of the last tweet to get (newest tweet)
leave empty to ignore (will be set during pagination)
limit
the number of times to paginate, good to keep low for testing due to API rate limits
use 0 to ignore
**/
var filename = "replies_output.txt";
var user = "coin_artist";
var since_id = "1170675630838616064";
var max_id = "";
var limit = 0;
// Internal use vars
var count = 0;
var replies = [];
var stream = fs.createWriteStream(filename, {flags:'a'});
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
};
function formatRequest(user, since_id, max_id) {
let req = "twurl /1.1/search/tweets.json?q=to:" + user;
if (since_id) req += "\\&since_id=" + since_id;
if (max_id) req += "\\&max_id=" + max_id;
return req;
}
function getTweetReplies(user, since_id, max_id){
let cmd = formatRequest(user, since_id, max_id);
execute(cmd, function(out){
if (!out) {
console.log("No output");
stream.end();
return;
}
let res = JSON.parse(out);
if (!res.statuses || res.statuses.length === 0) {
console.log("No responses");
stream.end();
return;
}
if (limit > 0 && count++ >= limit) {
console.log("Limit hit");
stream.end();
return;
}
// Get the replies & append them to the output file
for (let i = 0; i < res.statuses.length; i++) {
// If the max_id == tweet ID, skip over
if (res.statuses[i].id_str === max_id) {
continue;
}
// Only those that are replies
if (res.statuses[i].in_reply_to_status_id_str === since_id) {
stream.write(res.statuses[i].text + "\n");
}
}
// If the current max ID is also the new max ID, we're done
if (res.statuses[res.statuses.length-1].id_str === max_id) {
console.log("Read all replies");
stream.end();
return;
}
let highest_id = res.statuses[0].id_str;
max_id = res.statuses[res.statuses.length-1].id_str;
console.log("Found statuses from " + highest_id + " to " + max_id);
getTweetReplies(user, since_id, max_id);
});
};
// Run
getTweetReplies(user, since_id, max_id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment