Search code examples
javainstagraminstagram-api

Instagram API Pagination : Next page


I am trying to get all the recent media by tag using this Instagram endpoint. The purpose here is to track all the recent media for tags. I have configured a scheduled task (with Java and Spring) (to execute every hour) that sends requests and gets data. Below is the execution sequence:

  • Send the GET request to Instagram and previously stored max_tag_id (send with null if there's no previous id)
  • Iterate through results, extract next_max_tag_id from pagination element and store it in the database against corresponding tag
  • Send GET request again with new max_tag_id and continue
  • Stop if next_url in the result is null or number of media returned is less than 20 (configured)

Once the execution finishes, next execution (after let's say an hour) will start with previously stored max_tag_id.

The issue I am seeing is, I never get 'recent' documents in subsequent executions. As per the documentation, passing max_tag_id in the request should return all the media after that id, however it's not happening. I keep getting old media.

If I want to get the recent documents in every execution, do I need to pass null max_id in the first request of every execution? If I do that, will I not get redundant documents in every execution? I tried asking Instagram but haven't got any response. Also, their documentation explains little about pagination. Not sure whether pagination for recent media endpoint works backwards.


Solution

  • If you want most recent don't use max_tag_id, If you use max_tag_id it will return all media dated before that.

    You need to get the min_tag_id and store it, in the next hour start by making call with only min_tag_id, if there is pagination.next_url, use that to get next set of 20, until pagination.next_url does not exist.... use the stored min_tag_id to make calls the next hour.

    The very first time you make call without max_tag_id or min_tag_id

    You can also set the &count=32, to get 32 posts with every API call, (32 is max from my experience)