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

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

问题描述

我使用 Agarwal 的教程创建了一个推特机器人,用于转发和喜欢推文,它使用用于搜索推文的 Twitter lib 库.我在我的项目中使用 Twitterlib 版本 21.

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.

它似乎在大多数情况下都有效,但我有一个特别的问题.当我在搜索中包含min_retweets:X"参数时,它似乎无法识别.这不是官方记录的搜索参数,但当您在网站上的正常 Twitter 搜索中使用它时它确实有效,并且仅返回已转发 X 次的推文.

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.

如果我将min_retweets:X"设为第一个搜索词,那么搜索将不会返回任何结果.如果我把它作为最后一个搜索词,我会得到结果,但它们不仅限于已转发 X 次的推文.

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.

我已经在 Twitterlib 中的 fetchtweets() 方法中摸索了一点,但目前我无法弄清楚问题可能出在哪里.库的第 21 版说它已更新以使使用:"的搜索正常工作,并且就我使用的其他一些搜索词而言,这似乎是准确的,只是不是这个.还有一个min_faves:X"参数,但我还没有测试过它是否适用于 Twitterlib 的搜索.

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.

如果有人知道解决此问题的方法,我将不胜感激.下面是我用来调用函数的代码以及来自 Twitter 库的函数本身的代码:

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;
}

推荐答案

是的,您似乎在该方案上发现了一个合法的错误.如果您在使用 min_retweets 搜索查询运行 fetchTweets() 后查看执行记录,结果如下所示:

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

额外的 %25 是百分号本身被重新转义,它破坏了查询.我将不得不回去全面重新审视所有的特殊字符;我很确定有些必须被双重转义,有些则不能.与此同时,这里有一个解决方法.您可以调用 twit.fetch 并直接向其提供 URL,如下所示:

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;

我也会在 v22 发布并解决您的问题时回来通知您.感谢您使用我的图书馆!

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

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

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