Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using since_id and max_id in Twitter API

I hope I'm overthinking this and there's an obvious solution.

From the API (GET statuses/user_timeline)

max_id - Returns results with an ID less than (that is, older than) or equal to the specified ID.

"or equal to" means it will include the tweet with ID that I sent as my max_id parameter.

--

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?

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.

Details about the snowflake update: https://dev.twitter.com/docs/twitter-ids-json-and-snowflake

Posible solutions:

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.

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.

thanks!

like image 694
jneiku Avatar asked Jan 18 '23 05:01

jneiku


1 Answers

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);
}
like image 168
Bob Lauer Avatar answered Jan 20 '23 13:01

Bob Lauer