Using the from_id and max_id commands in the Twitter API

I hope I rethought this and there is a clear solution.

From API (GET / user_timeline statuses)

max_id - Returns results with an identifier less than (i.e. older) or equal to the specified ID.

"or equal" means that it will include a tweet with the identifier that I sent as the max_id parameter.

-

My question is this: if I store the identifier of my oldest tweet (from the previous request), how can I subtract 1 from this identifier to exclude it from the one returned in my next request?

The obvious solution would be to do something like this '& max_id =' + lastID-1, but twitter ids are great for such math operations and javascript rounds the results.

Snowflake update details: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake

Positive decisions:

It was mentioned that I can use the BigInteger JavaScript library: http://silentmatt.com/biginteger/ , but in my opinion this is redundant for such as small tasks.

Do I need to use recursion in the string (id_str) and increase or decrease it by one? I hate using a hack for such small details that should just work.

-

If you have this problem, share your solution.

thanks!

+4
source share
3 answers

I ran into the same problem and decided to solve it by subtracting 1 from the last digit, and then learning the scenario when we subtract 1 from 0 through 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); } 
+4
source

Indeed, the Twitter API will respond to duplicate tweets if we do not reduce the max_id parameter.

Here is a good Twitter API article on max_id: https://dev.twitter.com/docs/working-with-timelines About the general concept of working with large (over 53-bit) numbers in JavaScritp: http://www.2ality.com /2012/07/large-integers.html

Back to the question: using the library seems redundant if you are not using it for something else. @bob-lauer has a nice lightweight solution, but I wrote my own function without recursion :

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

To test it, do the following numbers / lines:

 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")); 
+4
source

Here is a short version of the PHP version of @Azat's answer from a non-recursive function to reduce a long string (non-negative) integer.

 <?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; } 
0
source

Source: https://habr.com/ru/post/1401561/


All Articles