Twitter lib的fetchTweets()方法接受哪些参数 [英] What parameters will Twitter lib's fetchTweets() method accept

查看:144
本文介绍了Twitter lib的fetchTweets()方法接受哪些参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用创建了一个twitter推文转发器和喜欢推文,它使用用于搜索推文的 Twitter lib 库。我在我的项目中使用 Twitterlib 21版



似乎大部分都有效,但我有一个特殊的问题。当我在搜索中包含min_retweets:X参数时,它似乎不被识别。这不是一个正式记录的搜索参数,但它在您在网站上的正常Twitter搜索中使用它并且只返回推文已被推送X次的情况下有效。



如果我将min_retweets:X作为第一个搜索词,然后搜索将不返回结果。如果我把它作为最后一个搜索条件,我会得到结果,但它们不限于已被转推X次的推文。



我在fetchtweets()方法在Twitterlib中稍微有点,但目前我无法找出问题出在哪里。该图书馆的第21版表示,它已更新为使用:进行正确搜索的搜索,而且就我用过的其他搜索词而言,这似乎是准确的,而不是这一个。还有一个min_faves:X参数,但我没有测试过它是否适用于Twitterlib的搜索。



如果有人知道解决此问题的方法,我会很感激。下面是我用来从Twitter库调用函数和函数本身代码的代码:

  var tweets = twit。 fetchTweets(
TWITTER_SEARCH_PHRASE,函数(tweet){
if(!tweet.possibly_sensitive){
return tweet.id_str;
}
},{
multi:true,
lang:en,
count:5,
since_id:props.getProperty(SINCE_TWITTER_ID)
});






  OAuth .prototype.fetchTweets = function(search,tweet_processor,options){

var tweets,response,result = [],data,i,candidate;
var phrase = encodeString('lang:'+(options&&& option.lang ||'en')+''+ encodeString(search).replace(/%3A / g,:) ); //默认为英文语言

this.checkAccess();

if(options == null){
options = {};
}

var url = [
https://api.twitter.com/1.1/search/tweets.json?count=,
(options .count ||5),
options.filter? (& filter =+ encodeString(options.filter)):,
& include_entities =,
options.include_entities? encodeString(options.include_entities):false,
& result_type =,
options.result_type? encodeString(options.result_type):recent,
& q =,
短语,
options.since_id? & since_id =+ encodeString(options.since_id):
] .join();
var request_options =
{
method:get
};

尝试{

response = this.fetch(url,request_options);

if(response.getResponseCode()=== 200){

data = JSON.parse(response.getContentText());
if(data){

tweets = data.statuses;

if(!tweet_processor){
return options&& options.multi? tweets:tweets [tweets.length - 1];

for(i = tweets.length-1; i> = 0; i--){
candidate = tweet_processor(tweets [i]);
if(candidate === true)candidate = tweets [i];
if(candidate){
if(options&& option.multi){
result.push(candidate);
} else {
return candidate;
}
}
}
if(result.length){
return result;

if(i <0){
Logger.log(没有匹配的tweet this go-round);
}
}
} else {
Logger.log(response);
}
} catch(e){
Logger.log(JSON.stringify(e));
throw e;
}
返回结果;


解决方案

是的,它看起来像是你发现了一个合法的错误。如果您在使用min_retweets搜索查询运行fetchTweets()之后查看执行脚本,则会显示如下: ://api.twitter.com/1.1/search/tweets.json?count = 5& include_entities = false& result_type = recent& q = lang%3Aen%20min_retweets%3A100%2520swag

多余的%25 是百分号本身被重新转义,并且破坏了查询。我将不得不返回并全面重新审视所有特殊字符;我很确定有些人必须是双重逃脱的,有些人不得。与此同时,这是一个解决方法。您可以直接调用twit.fetch并直接输入URL,如下所示:

  var tweets = JSON.parse(
twit.fetch(
https://api.twitter.com/1.1/search/tweets.json?
+count = 5& result_type = recent& q =
+ Twitterlib.encodeString(lang:en min_retweets:100 #swag),
{method:get})。getContentText(UTF-8)
).statuses;

我也会回来,让你知道v22何时会下降并修复你的问题。感谢您使用我的图书馆!


I've created a twitter bot for retweeting and liking tweets using Agarwal's tutorial which uses the Twitter lib library for searching tweets. I'm using Twitterlib version 21 in my project.

It seems to work for the most part, but I have one particular problem. When I include the "min_retweets:X" parameter in my search, it doesn't seem to be recognized. This is not an officially documented search parameter but it does work when you use it in normal twitter searches on the site and returns only tweets that have been retweeted X times.

If I make "min_retweets:X" the first search term then the search will return no results. If I make it the last search term I get results but they are not limited to tweets that have been retweeted X times.

I've poked around in the fetchtweets() method inside Twitterlib a little but it's beyond me currently to figure out where the issue might be here. Version 21 of the library says it was updated to make searches with the ":" work properly, and that seems accurate as far as some other search terms I've used, just not this one. There's also a "min_faves:X" parameter but I haven't tested to see if it works with Twitterlib's search.

If anyone knows of a workaround to get this working, I'd appreciate it. Below is the code I use to call the function and the code for the function itself from Twitter lib:

var tweets = twit.fetchTweets( 
  TWITTER_SEARCH_PHRASE, function(tweet) {
    if (!tweet.possibly_sensitive) {
      return tweet.id_str;
    }
  }, {
    multi: true,
    lang: "en",
    count: 5,
    since_id: props.getProperty("SINCE_TWITTER_ID")
  });


OAuth.prototype.fetchTweets = function(search, tweet_processor, options) {

  var tweets, response, result = [], data, i, candidate;  
  var phrase = encodeString('lang:' + (options && options.lang || 'en') + ' ' + encodeString(search).replace(/%3A/g, ":")); // English language by default

  this.checkAccess();

  if(options == null) {
    options = {};
  }

  var url = [
    "https://api.twitter.com/1.1/search/tweets.json?count=", 
    (options.count || "5"),
    options.filter ? ("&filter=" + encodeString(options.filter)) : "",
    "&include_entities=",
    options.include_entities ? encodeString(options.include_entities) : "false",
    "&result_type=",
    options.result_type ? encodeString(options.result_type) : "recent",
    "&q=",
    phrase,
    options.since_id ? "&since_id=" + encodeString(options.since_id) : ""
    ].join("");
  var request_options =
  {
    "method": "get"
  };

try {

    response = this.fetch(url, request_options);

    if (response.getResponseCode() === 200) {

      data = JSON.parse(response.getContentText());
      if (data) {

        tweets = data.statuses;

        if(!tweet_processor) {
          return options && options.multi ? tweets : tweets[tweets.length - 1];
        }
        for (i=tweets.length-1; i>=0; i--) {
          candidate = tweet_processor(tweets[i]);
          if(candidate === true) candidate = tweets[i];
          if(candidate) {
            if(options && options.multi) {
              result.push(candidate);
            } else {
              return candidate;
            }
          }
        }
        if(result.length) {
          return result;
        }
        if(i < 0) {
          Logger.log("No matching tweets this go-round");
        }
      }
    } else {
      Logger.log(response);
    }
  } catch (e) {
    Logger.log(JSON.stringify(e));
    throw e;
  }
  return result;
}

解决方案

Yes, it looks like you've uncovered a legit bug on that one. If you look at the execution transcript after running fetchTweets() with a min_retweets search query, it comes out looking like this:

https://api.twitter.com/1.1/search/tweets.json?count=5&include_entities=false&result_type=recent&q=lang%3Aen%20min_retweets%3A100%2520swag

That extra %25 is the percent sign itself getting re-escaped, and it's breaking the query. I'm going to have to go back and comprehensively reexamine all of the special characters; I'm pretty sure some have to be double-escaped and some must not. In the meantime, here's a workaround. You can call twit.fetch and feed it the URL directly, like this:

var tweets = JSON.parse(
  twit.fetch(
     "https://api.twitter.com/1.1/search/tweets.json?"
     + "count=5&result_type=recent&q=" 
     + Twitterlib.encodeString("lang:en min_retweets:100 #swag"),     
    {method: "get"}).getContentText("UTF-8")
).statuses;

I'll also come back and let you know when v22 drops and fixes your issue. Thanks for using my library!

这篇关于Twitter lib的fetchTweets()方法接受哪些参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆