Instagram API Mapping - Previous Page

I get custom Instagram media with jQuery.

I need to get all user media, but the Instagram API media limit is only 20, and for the API pagination link, only the following link. No previous link.

I write the code something like this, it works with the following, but the previous does not work. And I fold 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.com/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.com/v1/users/self/media/recent/?access_token=' + $token + '&count=' + $limit + '&min_id=' + $nextId

    break;
    default:

        localStorage.setItem('page', 1);        
        var $url = 'https://api.instagram.com/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.
    }

});

}
+4
source share
2 answers

First of all: you can increase the multimedia limit to 33 photos. You should use the count parameter in your query:

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

, , Instagram .

API- Instagram:

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

, 3 ( id # 1 id # 100):

:

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

:

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

:

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

querystring max_tag_id , min_tag_id . , min_tag_id , , min_tag_id . (# 1, # 2, # 3...) min_tag_id (# 34 # 67) .

, querystring max_tag_id next_url ( URL- - ).

max_tag_id ( ) . :

var token = "<your_access_token>";
var nextURL = "https://api.instagram.com/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);

      }
    }

  });

}

!

+6

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


All Articles