如何使用Twitter API在Node.js中获得扩展的REtweets? (最多只能输入140个字符)

时间:2018-09-24 00:13:10

标签: javascript json node.js twitter

我正在尝试获取扩展的转发,但我似乎无法获得名为 retweeted_status 的额外字段作为结果中的参数。包含名为“ retweeted_status”的参数时,我没有任何错误;我也不知道它是否甚至作为Node.js中的参数存在。因为Twitter API文档未包含任何类型的示例或清晰的信息进行转推,我尝试实现用我在Google中搜索过的其他编程语言编写的方法,但最终没有获得任何好的结果。 (注意:tweet_mode仅适用于Tweets,不适用于RETweets!

我尝试过的事情:

retweeted_status: 'extended' //doesn't work but node.js doesn't give an error
retweeted_status: true       //doesn't work but node.js doesn't give an error
retweeted_status: 1          //doesn't work but node.js doesn't give an error

我的代码如下(它从不输入if条件,如果确实出现了我在代码中提到的错误):

var params = {q: '@Avengers', count: 100, include_rts: 0, tweet_mode: 'extended', retweeted_status: 'extended'}; 
client.get('search/tweets', params, function(error, tweets, response) {
    if (!error) {
      console.log(tweets);
      for(var i=0; i < params.count; i++){

        if (tweets.retweeted_status){ //It never enters this condition
                                      //even if it does I get this error
               //TypeError: Cannot read property 'statuses' of undefined tweets.statuses[i]
          tweets = tweets.retweeted_status;
          var tweet = (JSON.stringify(tweets.statuses[i].full_text)+"\n");
          var string = tweet.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
          console.log('hi');
        }
        else{
          var tweet = (JSON.stringify(tweets.statuses[i].full_text)+"\n");
          var string = tweet.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
          console.log('hi2');
        }

        fs.appendFile("tweet.txt", string, function (err) {
          if (err) throw err;
          console.log('Saved!');
        }); 
      }
    }
});

我在这个网站https://dev.to/kehers/formatting-tweets-a-look-at-extended-tweets-retweets-and-quotes-n5j中找了几个小时,试图编写一些代码(我已经在上面提供了),但是由于IF语句被忽略,所以这些代码无法正常工作。获取推文时,应提供一个额外的参数“ retweeted_status”,如下所示:

{
  "created_at": "Sun Mar 11 12:00:27 +0000 2018",
  "id": 972804442667003900,
  "full_text": "RT @jasongorman: As a physics grad, I understand how snooker works at a level I imagine a lot of pro snooker players don't. But I suck at s…",
  "truncated": false,
  "display_text_range": [...],
  "entities": {...},
  "retweeted_status": { // <======================= I am not getting this extra parameter!
    "created_at": "Sun Mar 11 08:10:46 +0000 2018",
    "id": 972746641957642200,
    "full_text": "As a physics grad, I understand how snooker works at a level I imagine a lot of pro snooker players don't. But I suck at snooker. Understanding != ability.",
    "truncated": false,
    "display_text_range": [0, 155],
    "entities": {
      "hashtags": [],
      "symbols": [],
      "user_mentions": [],
      "urls": []
    },
  }
}

0 个答案:

没有答案