Retrieving all custom images using Instagram card

I would like to get all my photos using the instagram gem ( https://github.com/Instagram/instagram-ruby-gem ), but I cannot find out how.

This is what I am trying to do so far:

client = Instagram.client(access_token: token)
client.user_recent_media.each do |media|
    puts media.images.thumbnail.url
end

      

It looks like it works (these are my Instagram images) but I can't get all of them or select any content I want from Instagram.

For example, I see that the user_recent_media method takes a "count" attribute, which should allow me to use some sort of pagination:

(from https://github.com/Instagram/instagram-ruby-gem/blob/master/lib/instagram/client/users.rb#L150)
@option options [Integer] :count (nil) Limits the number of results returned per page

      

Unfortunately I don't understand how these paginations are supposed to work. If for example I am requesting 1000 media items (just say all) it doesn't work and returns fewer items, at this point I get stuck because I have no idea how to request page 2

Has anyone used Instagram gem for something like this? Any help is appreciated

+3


source to share


3 answers


Below is the functional code of an old application that imports users (I just ran it with 1.1.5 Instagram Gem and it still works) that also uses a cursor. You should be able to change multiple variables and lines and be in your path:



  def import_followers
    response = user.client.user_follows
    followers = [].concat(response)
    next_cursor = response.pagination[:next_cursor]
    while !(next_cursor.to_s.empty?) do
      response = Instagram.user_follows(uid, {:cursor => next_cursor})
      next_cursor = response.pagination[:next_cursor]
      followers.concat(response)
    end
    return followers
  end

      

+4


source


Based on @nrowegt answer, I tweaked the code.

First we initialize the Instagram Gem (the access token will display the Instagram user id)

client = Instagram.client(:access_token => session[:access_token])

      

Then we will run the user_recent_media method and store the response in a variable

response = client.user_recent_media

      

We need to create an empty array to store all the images and add our response. Remember that the Instagram API only returns 20 items per call, so we need to make an array and "bye" below.

album = [].concat(response)

      

If the user has more image, the response variable will have the next_max_id variable. It's inside the pagination method; so we are storing next_max_id for the variable.



max_id = response.pagination.next_max_id

      

This is the tricky part. You need to run the above code as long as the max_id variable exists. It will be empty if we get to the last page of custom snapshots. So:

while !(max_id.to_s.empty?) do
        response = client.user_recent_media(:max_id => max_id)
        max_id = response.pagination.next_max_id
        album.concat(response)
end

      

With ONLY the other, this time we run the user_recent_media method passing in the max_id variable so Instagram can figure out where to start fetching images.

And we will send the final variable to the view

@album = album

      

Here is the complete code.

client = Instagram.client(:access_token => session[:access_token])
        response = client.user_recent_media
        album = [].concat(response)
        max_id = response.pagination.next_max_id

        while !(max_id.to_s.empty?) do
            response = client.user_recent_media(:max_id => max_id)
            max_id = response.pagination.next_max_id
            album.concat(response)
        end

        @album = album

      

+4


source


According to the documentation, you should get the hash pagination

in whatever response you get from the API. Something like the following (taken from the documentation):

{
    ...
    "pagination": {
        "next_url": "https://api.instagram.com/v1/tags/puppy/media/recent?access_token=fb2e77d.47a0479900504cb3ab4a1f626d174d2d&max_id=13872296",
        "next_max_id": "13872296"
    }
}

      

You will need to call the url from the attribute next_url

to get the next dataset.

So, I assume you can get it like this:

client.user_recent_media.pagination.next_url

      

+1


source







All Articles