carbocation / go-instagram

Go library for accessing Instagram REST and Search APIs
BSD 2-Clause "Simplified" License
51 stars 20 forks source link

Pagination support #3

Closed Volox closed 9 years ago

Volox commented 9 years ago

Hi, first of all thanks for this package, then i have a question, it is possible to handle pagination? if so how? From the docs i see that i have a ResponsePagination struct with the next url to call, but i have to handle the request myself or is it possible to re-use some of the package code?

janez87 commented 9 years ago

+1

carbocation commented 9 years ago

On my way to work, but essentially if you create your call with an 'params := &instagram.Parameters{}' object in the call, when you make a query and get a pagination object, you can do things like

'params.MaxID = pagination.NextMaxID'

Then when you make your next call including the updated params object, you will start from where you left off.

Can't show more code since out of time, but maybe that gives a sense of how to do it.

carbocation commented 9 years ago

Going to close this because it is already supported - but if there is confusion, please let me know so I can improve the documentation.

rcshubhadeep commented 9 years ago

There is actually no documentation for this feature. Neither the test files give a lead. Can you please post something on this please?

carbocation commented 9 years ago

Reasonable request. I use it internally and can definitely whip up an example tomorrow.

On Wednesday, June 10, 2015, Shubhadeep Roychowdhury < notifications@github.com> wrote:

There is actually no documentation for this feature. Neither the test files give a lead. Can you please post something on this please?

— Reply to this email directly or view it on GitHub https://github.com/carbocation/go-instagram/issues/3#issuecomment-110860193 .

carbocation commented 9 years ago

As you say, I'll need to add an example in the tests.

First, here is a demo where we search for media tagged with "purple". In this example, ig is of type *instagram.Client.

// Define the params outside of the loop
params := &instagram.Parameters{}

// Loop indefinitely while we iterate over the pages
i := 0
for {
    // For demo purposes, to prevent indefinite looping for tags with millions of images
    if i > 4 {
        break
    }
    i++

    // Fetch the media
    media, pagination, err := ig.Tags.RecentMedia("purple", params)
    if err != nil {
        fmt.Println(err)
        break
    }
    if len(media) < 1 {
        //We have exhausted available media for this tag
        break
    }

    // Do something with the media
    fmt.Println(media)

    // Update params with the pagination values before the next iteration of this loop
    params.MaxID = pagination.NextMaxID
}
rcshubhadeep commented 9 years ago

That is a great example. But I have noticed that in the param Instagram actually supports a count. Which you have not made a provision of providing. So I forked your code base and put it there. This already gives a good amount of result. But this example here is very good. Will implement that as well. Thanks for the prompt answer :) and for the great work!

On Fri, Jun 12, 2015 at 6:30 PM, carbocation notifications@github.com wrote:

As you say, I'll need to add an example in the tests.

First, here is a demo where we search for media tagged with "purple":

// Define the params outside of the loopparams := &instagram.Parameters{} // Loop indefinitely while we iterate over the pagesi := 0for { media, pagination, err := ig.Tags.RecentMedia("purple", params) if err != nil { fmt.Println(err) break } if len(media) < 1 { //We have exhausted available media for this tag break }

// Do something with the media
fmt.Println(media)

// Update params with the pagination values before the next iteration of this loop
params.MaxID = pagination.NextMaxID

}

— Reply to this email directly or view it on GitHub https://github.com/carbocation/go-instagram/issues/3#issuecomment-111484328 .

Shubhadeep

carbocation commented 9 years ago

Count is already supported in Parameters{} and is documented in the main Readme for this library. Note, however, that the Count parameter is not supported on all endpoints by Instagram.

Your fork hardcodes a count on the Media Search endpoint, but as you can see on the official instagram developer site, there is no support for "count" on that endpoint. So, if the Instagram documentation is correct, your count parameter will have no effect.

rcshubhadeep commented 9 years ago

This is the most strange part. Because actually the count does make an effect(even in the API console you can try it). Anyway, I did it because I wanted to test and not to touch your main code base. Now I will use your style I guess.

On Fri, Jun 12, 2015 at 11:56 PM, carbocation notifications@github.com wrote:

Count may be specified in Parameters{} but it is not supported on all endpoints. Your fork hardcodes a count on the Media Search endpoint, but as you can see on the official instagram developer site https://instagram.com/developer/endpoints/media/, there is no support for "count" on that endpoint. So, if the Instagram documentation is correct, your count parameter will have no effect.

— Reply to this email directly or view it on GitHub https://github.com/carbocation/go-instagram/issues/3#issuecomment-111581183 .

Shubhadeep

carbocation commented 9 years ago

Bizarre but good find! I'd be happy to review and merge in that change if you want to submit a pull request. Might be worth commenting that the Count parameter isn't documented by Instagram on that endpoint so people aren't surprised if it stops working in the future.

rcshubhadeep commented 9 years ago

I did create a pull request now. Thanks for your help again :)

On Sat, Jun 13, 2015 at 12:00 AM, carbocation notifications@github.com wrote:

Bizarre but good find! I'd be happy to review and merge in that change if you want to submit a pull request. Might be worth commenting that the Count parameter isn't documented by Instagram on that endpoint so people aren't surprised if it stops working in the future.

— Reply to this email directly or view it on GitHub https://github.com/carbocation/go-instagram/issues/3#issuecomment-111581998 .

Shubhadeep

carbocation commented 9 years ago

I believe this is all merged in, so I'm closing this issue. Thanks!