instagram-api icon indicating copy to clipboard operation
instagram-api copied to clipboard

Handling pagination

Open stephen-lazarionok opened this issue 8 years ago • 4 comments

What's the best way to handle pagination?

instagramApi.getMediasByTag(tag).then(function(data){
  // one page is not enough for me, how can I get more?
  var pagination = data.pagination;
}, errorHandler)

stephen-lazarionok avatar Sep 10 '16 02:09 stephen-lazarionok

you must pass a second argument (https://github.com/guilhermefarias/instagram-api#getmediasbytagtagname-params)

the argument should be next_max_id and you could got it with data.max_id. (maybe this endpoint have a different name for next cursor, so pls cosoling data to get the correct name)

then just call recursively getMediasByTag

ex:

let arrayOfData = [];      
function paginate() {
        return new Promise((resolve, reject) => {
            (function paginate(nextPage) {
                var instagramOptions = {};
                if (nextPage) {
                    instagramOptions[pageKey] = nextPage;
                }
                instagramApi.getMediasByTag(tag, instagramOptions).then(result => {
                    arrayOfData = arrayOfData.concat(result.data);
                    if (result.pagination && result.pagination[nextPageKey]) {
                        paginate(result.pagination[nextPageKey]);
                    } else {
                        resolve(arrayOfData);
                    }
                }, reject);
            }());
    });
}

OBS: change nextPage and nextPageKey for the argument that comes from instagram

gcaraciolo avatar Sep 10 '16 02:09 gcaraciolo

Not clear for me. Let's say I want to take 10 first pages of results. How can I get this?'

stephen-lazarionok avatar Sep 10 '16 13:09 stephen-lazarionok

Well you can have a variable "count" and you will increment that variable each time the recursively function was called.

gcaraciolo avatar Sep 10 '16 14:09 gcaraciolo

@guilhermefarias close this issue, as it does not seem a problem.

gcaraciolo avatar Nov 30 '16 19:11 gcaraciolo