最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Instagram API pagination - Previous Page - Stack Overflow

programmeradmin2浏览0评论

I am getting Instagram user's media with Jquery.

I need to get All user media, but Instagram API media limit is only 20 and API pagination returns only next link. There is no previous link.

I write code somethink like that, this is working with next but not working previous. And I stack here

function instaPhotos(page) {


var $limit = 1;
var $token = $('.instagram').data('instagramtoken');
var $nextId = $('.instagram').attr('data-instagramnext');
var $pageNumber = parseInt(localStorage.getItem('page'));


switch (page){      
    case 'next':

        localStorage.setItem('page', ($pageNumber+1));
        var $url = '/?access_token=' + $token + '&count=' + $limit + '&max_id=' + $nextId


    break;      
    case 'prev':

        localStorage.setItem('page', ($pageNumber-1));
        var $url = '/?access_token=' + $token + '&count=' + $limit + '&min_id=' + $nextId

    break;
    default:

        localStorage.setItem('page', 1);        
        var $url = '/?access_token=' + $token + '&count=' + $limit

}


console.log($pageNumber);
console.log($url);

$.ajax({
    method: "GET",
    url: $url,
    dataType: "jsonp",
    context: this,
    success: function (r) {

        if (r.pagination.next_max_id){
            $('.instagram').attr('data-instagramnext', r.pagination.next_max_id);
        }

        // photo actions here.
    }

});

}

I am getting Instagram user's media with Jquery.

I need to get All user media, but Instagram API media limit is only 20 and API pagination returns only next link. There is no previous link.

I write code somethink like that, this is working with next but not working previous. And I stack here

function instaPhotos(page) {


var $limit = 1;
var $token = $('.instagram').data('instagramtoken');
var $nextId = $('.instagram').attr('data-instagramnext');
var $pageNumber = parseInt(localStorage.getItem('page'));


switch (page){      
    case 'next':

        localStorage.setItem('page', ($pageNumber+1));
        var $url = 'https://api.instagram./v1/users/self/media/recent/?access_token=' + $token + '&count=' + $limit + '&max_id=' + $nextId


    break;      
    case 'prev':

        localStorage.setItem('page', ($pageNumber-1));
        var $url = 'https://api.instagram./v1/users/self/media/recent/?access_token=' + $token + '&count=' + $limit + '&min_id=' + $nextId

    break;
    default:

        localStorage.setItem('page', 1);        
        var $url = 'https://api.instagram./v1/users/self/media/recent/?access_token=' + $token + '&count=' + $limit

}


console.log($pageNumber);
console.log($url);

$.ajax({
    method: "GET",
    url: $url,
    dataType: "jsonp",
    context: this,
    success: function (r) {

        if (r.pagination.next_max_id){
            $('.instagram').attr('data-instagramnext', r.pagination.next_max_id);
        }

        // photo actions here.
    }

});

}
Share Improve this question asked Feb 1, 2016 at 12:52 YasirYasir 1,1201 gold badge14 silver badges31 bronze badges 2
  • Wild guess, did you try to change min_id to max_id? – Georgy Commented Feb 1, 2016 at 13:39
  • yes I changed it but not working – Yasir Commented Feb 1, 2016 at 15:54
Add a ment  | 

2 Answers 2

Reset to default 6

First of all: you can increase media limit up to 33 photos. You should use count parameter on your querystring:

https://api.instagram./v1/tags/nofilter/media/recent?count=33&access_token=YOUR_ACCESS_TOKEN

About to navigate to previous page it seems to me that Instagram response will always be with the most recent published photos.

From Instagram API:

MIN_TAG_ID  Return media before this min_tag_id.
MAX_TAG_ID  Return media after this max_tag_id.

So let's think of 3 pages (from id #1 to id #100):

First page:

next_min_id #0
photo #1
photo #2
photo #3
...
photo #33
next_max_tag_id #34

Second page:

next_min_id #34 (equals to next_max_tag_id of first page) 
photo #35
photo #36
photo #37
... 
photo #66
next_max_tag_id #67

Third page:

next_min_id #67 (equals to next_max_tag_id of second page)
photo #68
photo #69
photo #70
...
photo #99
next_max_tag_id #100

While the querystring parameter max_tag_id limits the top of your result, min_tag_id limits the bottom of it. So if you use only min_tag_id as parameter the response will be equal the first page despite of the page min_tag_id value came from. After all the photos of first page (#1, #2, #3...) e before the min_tag_id of any other page (#34 and #67) and are the newest ones.

I think to acplish this task the only way is using max_tag_id querystring parameter or next_url result (entire url ready to use - easier).

You should save or max_tag_id's or (even better and faster) the entire result and control the logic on your code. Working example:

var token = "<your_access_token>";
var nextURL = "https://api.instagram./v1/tags/riodejaneiro/media/recent?count=33&access_token=" + token;

var currentPage = 0;
var pages = new Array();

$(function() { requestInstagram(); });

function previousPage() {

   currentPage = (currentPage>0) ? currentPage-1 : 0;
   // show photos of currentPage

   console.log("Page " + currentPage);
   console.log("First photo:" + pages[currentPage][0].id);

 }

 function nextPage() { 

    if (++currentPage < pages.length ) {
       // show photos of currentPage
       console.log("Page " + currentPage);
       console.log("First photo:" + pages[currentPage][0].id);
    } else {
       requestInstagram(); 
    }
}

function requestInstagram() { 
 $.ajax({
    method: "GET",
    url: nextURL ,
    dataType: "jsonp",
    context: this,
    success: function (r) {

      if (r.meta.code == "200") {

        pages[pages.length] = r.data;
        // show photos here
        console.log("Page " + currentPage);
        console.log("First photo:" + pages[currentPage][0].id);

        nextURL = r.pagination.next_url; // you should implement a way to identify the last page
        console.log(nextURL);

      }
    }

  });

}

Good luck!

I tested min_id, looks like it is broken on sandbox mode, it does however work correctly on old APIs that are live like on http://gramfeed.

You can test sandbox mode here: http://www.gramfeed./accounts/sandbox

发布评论

评论列表(0)

  1. 暂无评论