【问题标题】:Searching by #HashTag using the Twitter API with Node.js使用带有 Node.js 的 Twitter API 通过 #HashTag 进行搜索
【发布时间】:2019-07-10 10:20:44
【问题描述】:

我正在编写一个 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);
          }

【问题讨论】:

    标签: javascript node.js api twitter


    【解决方案1】:

    我对 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 或类似的东西)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-19
      • 2017-03-09
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多