instagram-api
instagram-api copied to clipboard
Handling pagination
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)
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
Not clear for me. Let's say I want to take 10 first pages of results. How can I get this?'
Well you can have a variable "count" and you will increment that variable each time the recursively function was called.
@guilhermefarias close this issue, as it does not seem a problem.