Skip to content

Instantly share code, notes, and snippets.

@M-Igashi
Created November 19, 2017 10:48
Show Gist options
  • Save M-Igashi/393ecefe4b3ab5bba44af225b69db480 to your computer and use it in GitHub Desktop.
Save M-Igashi/393ecefe4b3ab5bba44af225b69db480 to your computer and use it in GitHub Desktop.
function Delete_Old_Tweets() {
var destroy_count = 0;
var tweets = fetchTweets(0);
var stoptweets = 0;
var run_date = new Date();
var tweet_date = new Date();
var tweet_age = 0;
while (tweets.length>1) {
max_id = tweets[tweets.length-1].id_str
for (var i=tweets.length-1; i>=0; i--) {
tweet_date = new Date(tweets[i].created_at);
//age of the tweet in days
tweet_age = tweet_date*1+ 0.1 * 24*60*60*1000;
/////////////ALTER CRITERIA HERE TO TWEAK WHAT GETS DELETED
if (tweet_age < run_date && ((tweets[i].retweeted_status != undefined) || (/*tweets[i].retweet_count == 0 && tweets[i].favorite_count == 0 &&*/ tweets[i].entities.hashtags.length > 0)))
{
destroyTweet(tweets[i].id_str);
destroy_count+=1;
}
}
tweets = fetchTweets(max_id+1);
Logger.log(destroy_count);
}
}
function fetchTweets(max_id) {
/////////////////////////SET YOU TWITTER SCREENNAME HERE
var twitter_handle = "btc_AP";
var search = "https://api.twitter.com/1.1/statuses/user_timeline.json";
search = search + "?include_entities=true&include_rts=true&screen_name=" + twitter_handle + "&count=200";
if (max_id>0) {
search = search + "&since_id=" + max_id;
}
var options =
{
"method": "get",
"oAuthServiceName":"twitter",
"oAuthUseToken":"always"
};
var service = twitter.getService();
try {
var result = service.fetch(search, options);
}
catch(e) {
Logger.log(e.toString());
}
if (result.getResponseCode() === 200) {
var data = JSON.parse(result.getContentText());
if (data) {
Logger.log("Fetched " + data.length + " tweets.");
return data;
}
}
}
function destroyTweet(tweet_id) {
var options =
{
"method": "POST",
"oAuthServiceName":"twitter",
"oAuthUseToken":"always"
};
var destroy = "https://api.twitter.com/1.1/statuses/destroy/" + tweet_id + ".json";
var service = twitter.getService();
try {
var result = service.fetch(destroy, options);
}
catch (e) {
Logger.log(e.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment