我使用Agarwal 的教程创建了一个 twitter 机器人,用于转发和喜欢推文,该教程使用Twitter库来搜索推文。我在我的项目中使用Twitterlib 版本 21。
它似乎在大多数情况下都有效,但我有一个特别的问题。当我在搜索中包含“min_retweets:X”参数时,它似乎无法被识别。这不是官方记录的搜索参数,但当您在网站上的普通推特搜索中使用它并仅返回已转发 X 次的推文时,它确实有效。
如果我将“min_retweets:X”作为第一个搜索词,那么搜索将不会返回任何结果。如果我将它作为最后一个搜索词,我会得到结果,但它们不仅限于已被转发 X 次的推文。
我已经在 Twitterlib 中的 fetchtweets() 方法中进行了一些探索,但目前我无法弄清楚问题可能出在哪里。该库的第 21 版表示,它已更新为使用“:”进行搜索正常工作,就我使用的其他一些搜索词而言,这似乎是准确的,只是不是这个。还有一个“min_faves:X”参数,但我还没有测试它是否适用于 Twitterlib 的搜索。
如果有人知道使此工作正常的解决方法,我将不胜感激。下面是我用来调用函数的代码以及来自 Twitter 库的函数本身的代码:
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;
}