2012-03-15 110 views
4

我希望我能够反覆揣测,并且有一个明显的解决方案。在Twitter中使用since_id和max_id API

从API(GET状态/ user_timeline)

max_id - 返回的结果与一个ID小于(即,大于以上)或等于指定的ID。

“或等于”意味着它将包括与ID的鸣叫,我发送作为我的max_id参数。

-

我的问题是这样的:如果我存储我的大鸣叫的ID(从之前的请求),我怎么能从此ID中减去1,从在我的下返回排除请求?

显而易见的解决方案是做这样的'& max_id ='+ lastID-1,但叽叽喳喳ID是这样的数学运算和JavaScript的四舍五入的结果。关于雪花更新

详情: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake

更多钞票的解决方案:

已经提到,我可以使用BigInteger的JavaScript库:http://silentmatt.com/biginteger/,但在我看来这是多余的,如小任务。

是否必须对字符串(id_str)使用递归并将其递增或递减?我讨厌使用黑客,例如应该工作的小细节。

-

如果你有这个问题,请分享你的解决方案。

谢谢!

+0

遇到同样的问题,在JS – 2013-05-10 06:38:33

回答

4

我遇到了同样的问题,最后通过从最后一位减去1来解决这个问题,然后通过递归从0中减去1来解释场景。

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

事实上,除非我们减少max_id参数,否则Twitter API会以重复推文响应。

这里是max_id一个不错的Twitter API的文章:https://dev.twitter.com/docs/working-with-timelines 论JavaScritp与大(超过53位)的工作号码的总体思路:http://www.2ality.com/2012/07/large-integers.html

回到问题:使用库似乎像除非你把它用于别的东西,否则就会过度杀伤。@鲍勃 - 劳尔具有良好的轻量级的解决方案,但我已经写我自己的函数没有递归

function decStrNum (n) { 
    n = n.toString(); 
    var result=n; 
    var i=n.length-1; 
    while (i>-1) { 
     if (n[i]==="0") { 
     result=result.substring(0,i)+"9"+result.substring(i+1); 
     i --; 
     } 
     else { 
     result=result.substring(0,i)+(parseInt(n[i],10)-1).toString()+result.substring(i+1); 
     return result; 
     } 
    } 
    return result; 
} 

为了测试它与下面的数字/字符串运行:

console.log("290904187124985850"); 
console.log(decStrNum("290904187124985850")); 
console.log("290904187124985851"); 
console.log(decStrNum("290904187124985851")); 
console.log("290904187124985800"); 
console.log(decStrNum("290904187124985800")); 
console.log("000000000000000001"); 
console.log(decStrNum("0000000000000000001")); 
+0

中有这个感觉很烦,非常感谢分享这个。对于decStrNum(“0000000000000000001”),它给出了0000000000000000000(最后增加了一位数字),我不确定它能否挑起Twitter API的麻烦? – 2013-05-10 06:45:07

+0

@alexanderb,不客气。我想不是,除非你试图检索0000000000000000001 -1这是没有多大意义的元素;) – Azat 2013-06-21 20:15:05

0

这里@ Azat的一个quick'n'dirty PHP版本的非递归函数的答案,用于递减长字符串格式(非负)整数。

<?php 
function decStrNum($n) 
{ 
    $n = (string)$n; 
    if ((int)$n == 0 || 0 === strpos($n, '-')) 
    return false; 
    $result = $n; 
    $len = strlen($n); 
    $i = $len - 1; 
    while ($i > -1) { 
    if ($n[$i] === "0") { 
     $end = substr($result, $i + 1); 
     if ($end === false) $end = ''; 
     $result = substr($result, 0, -($len - $i)) . "9" . $end; 
     $i--; 
    } else { 
     $end = substr($result, $i + 1); 
     if ($end === false) $end = ''; 
     return substr($result, 0, -($len - $i)) . ((int)$n[$i] - 1) . $end; 
    } 
    } 
    return $result; 
} 
相关问题