Fetching all user's pictures with the Instagram gem

青春壹個敷衍的年華 提交于 2020-01-23 10:09:43

问题


I'd like to fetch all my pictures using the instagram gem (https://github.com/Instagram/instagram-ruby-gem) but I'm not able to find out how.

This is what I'm trying so far:

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

It apparently works (those are my Instagram images) but I'm not able to fetch all of them or to select in any way which content I want from Instagram.

For example I see that the method user_recent_media accepts a "count" attribute that should let me 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 get how this paginations is supposed to work. If for example I request 1000 media elements (just to say all of them) it doesn't work and returns way less elements, at that point I'm stuck because I have no idea how to requeste page 2

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


回答1:


Below is functioning code from an old app that imports users (I just ran it with the 1.1.5 Instagram Gem and it still works), which also uses the cursor. You should be able to change a few variables and lines and be on your way:

  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



回答2:


Based on the answer of @nrowegt, I customised the code.

First, we initialize the Instagram Gem (the access token will map the Instagram user ID)

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

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

response = client.user_recent_media

We need to create an empty array to save all the pictures and add our response. Remember, Instagram API only returns 20 items per call, that's why we need to do the array and the "while" below.

album = [].concat(response)

If the user has more picture, the response variable will have a variable called next_max_id. This is inside of pagination method; so we store the next_max_id on a variable.

max_id = response.pagination.next_max_id

This is the tricky part. You need to run the above's code while the max_id variable exists. It will be empty if we reach the last page of the user's pictures. 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 the ONLY differente that this time we run the user_recent_media method passing the max_id variable so Instagram can figure out where to start fetching the pictures.

And we send the final variable to the view

@album = album

Here is the full 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



回答3:


According to the documentation you should receive a pagination hash in any response you'll receive 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'll have to call the URL from the next_url attribute to retrieve the next set of data.

So, I'm guessing that you should be able to retrieve that as follows:

client.user_recent_media.pagination.next_url


来源:https://stackoverflow.com/questions/30241061/fetching-all-users-pictures-with-the-instagram-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!