I'm writing a Node.js application that will delete my tweets after 24 hours. I want to add a param to allow me to delete only the tweets with an hashtag of "#SnappyTweet".
function snappyTweet () {
client.get('statuses/user_timeline', {trim_user: true, count: 20}, function(error, tweets, response){
if(error) throw error;
var i = 0;
var len = tweets.length;
for (i; i < len; i++) {
var id = tweets[i].id_str;
var favd = tweets[i].favorited;
var hashtag = // I want to a add var here for hash tags
var tweetDate = new
Date(Date.parse(tweets[i].created_at.replace(/( \+)/, ' UTC$1')));
var expiryDate = moment(tweetDate).add(2, 'minutes')._d;
var now = moment();
// And instead of favoited I want to check for the hashtag.
if (moment(now).isAfter(expiryDate) && moment(tweetDate).isAfter('2018-01-01') && favd === false) {
deleteTweet(id);
}
I don't know much about the Twitter API. But one thought that comes to mind is you could search the tweet for the desired hashtag. If a match occurs, you'd know to delete that tweet.
Example:
let hashtag = "#SnappyTweet"; // hashtag to match
function hashMatch(tweet) {
let matches = [];
let pattern = /(^|\s)(#[a-z\d-]+)/ig; // this matches strings starting with a #
while ((match = pattern.exec(tweet))) {
matches.push(match[0].replace(/^\s+|\s+$/g, ""));
}
return (matches.includes(hashtag));
}
let tweet1 = 'test tweet 123 #SnappyTweet';
let tweet2 = 'test tweet 123 #NoMatchHere blah blah';
console.log(hashMatch(tweet1)); // first tweet is a match, so you can delete it
console.log(hashMatch(tweet2)); // second tweet isn't a match
So to implement this in your code, you might change your if
statement to something like:
if (moment(now).isAfter(expiryDate) && moment(tweetDate).isAfter('2018-01-01') && hashMatch(tweet[i].text)) {
deleteTweet(id);
}
Where tweet[i].text
is the text string of the tweet. I'm not that familiar with the API, but I'm assuming there's probably a property or method to get the text string (e.g., tweet[i].text
or something similar).