在Twitter API中使用since_id和max_id [英] using since_id and max_id in Twitter API
问题描述
我希望我对此有所考虑,并且有一个明显的解决方案.
I hope I'm overthinking this and there's an obvious solution.
从API(获取状态/用户时间轴)
From the API (GET statuses/user_timeline)
max_id-返回ID小于(即早于)或等于指定ID的结果.
max_id - Returns results with an ID less than (that is, older than) or equal to the specified ID.
或等于" 表示它将包含ID为我的max_id参数发送的tweet.
"or equal to" means it will include the tweet with ID that I sent as my max_id parameter.
-
我的问题是:如果我存储了我最早的tweet的ID(来自上一个请求),如何从该ID中减去1,以排除它在下一个请求中返回?
My question is this: if I store the id of my oldest tweet (from a previous request), how can I subtract 1 from this id to exclude it from being returned in my next request?
显而易见的解决方案是执行类似'& max_id ='+ lastID-1的操作,但对于此类数学运算,twitter ID很大,而javascript将结果四舍五入.
The obvious solution would be to do something like this '&max_id='+lastID-1, but twitter IDs are way to large for such math operations and javascript rounds off the results.
有关雪花更新的详细信息: https://dev.twitter.com/docs/twitter-ids-json和雪花
Details about the snowflake update: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake
可能的解决方案:
已经提到我可以使用BigInteger Javascript库: http://silentmatt.com/biginteger/,但在我看来,这对于诸如小任务来说是多余的.
It has been mentioned that I can use the BigInteger Javascript Library: http://silentmatt.com/biginteger/, but in my opinion this is redundant for such as small task.
我是否必须对字符串(id_str)使用递归并将其递增或递减1?我不喜欢使用hack来处理那些应该有效的小细节.
Do I have to use recursion on the string (id_str) and increment or decrement it by one? I hate to use a hack for such as small detail that should just work.
-
如果您遇到此问题,请分享您的解决方案.
If you've had this problem please share your solution.
谢谢!
推荐答案
我遇到了同样的问题,最后通过从最后一位减去1来解决了这个问题,然后考虑了从1减去1时的情况.通过递归0.
I ran into this same problem, and ended up solving it by subtracting 1 from the last digit, and then accounting for the scenario when we're subtracting 1 from 0 via recursion.
function decrementHugeNumberBy1(n) {
// make sure s is a string, as we can't do math on numbers over a certain size
n = n.toString();
var allButLast = n.substr(0, n.length - 1);
var lastNumber = n.substr(n.length - 1);
if (lastNumber === "0") {
return decrementHugeNumberBy1(allButLast) + "9";
}
else {
var finalResult = allButLast + (parseInt(lastNumber, 10) - 1).toString();
return trimLeft(finalResult, "0");
}
}
function trimLeft(s, c) {
var i = 0;
while (i < s.length && s[i] === c) {
i++;
}
return s.substring(i);
}
这篇关于在Twitter API中使用since_id和max_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!