我正在编写一个Node.js应用程序,该应用程序将在24小时后删除我的推文。我想添加一个参数,以允许我仅删除具有“ #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);
}
答案 0 :(得分:0)
我对Twitter API不太了解。但是想到的一个想法是,您可以在推文中搜索所需的主题标签。如果发生匹配,您将删除该推文。
示例:
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
因此,要在代码中实现此目的,您可以将if
语句更改为以下内容:
if (moment(now).isAfter(expiryDate) && moment(tweetDate).isAfter('2018-01-01') && hashMatch(tweet[i].text)) {
deleteTweet(id);
}
tweet[i].text
是推文的文本字符串。我对API不太熟悉,但是我假设可能存在一种属性或方法来获取文本字符串(例如tweet[i].text
或类似名称)。