C# - Instasharp - How to use Pagination

故事扮演 提交于 2019-12-11 06:59:43

问题


I am creating a small program to get a instagram users following list using C# and instasharp. The code below will get me the first 50. I believe I have to use the pagination option to get the next url to get to the next page. Thats where Im stuck. How do I use pagination to loop through all of a users following?

var config = new InstaSharp.InstagramConfig("api", "oauth", "xxxxxx", "xxxxxxx", "xxxxxx");

var config1 = new InstaSharp.Endpoints.Relationships.Unauthenticated(config);
var result = config1.Follows(000000);

dynamic dyn = JsonConvert.DeserializeObject(result.Json);
foreach (var data in dyn.data)
{
    listBox1.Items.Add(data.username);
}

回答1:


Based on my response here: https://stackoverflow.com/a/25139236/88217

If you look at the unit tests on the InstaSharp github, you can see an example of how to use Pagination:

    public async Task Follows_NextCursor()
    {
        //This test will fail if testing with an account with less than one page of follows
        var result = await relationships.Follows();
        result = await relationships.Follows(457273003/*ffujiy*/, result.Pagination.NextCursor);
        Assert.IsTrue(result.Data.Count > 0);
    }

In the case where you want to loop through and get all of them, I would expect you to do something like this:

int userID = 000000;
var result = await relationships.Follows(userID);

while(result.Data.Count > 0)
{
  dynamic dyn = JsonConvert.DeserializeObject(result.Json);
  foreach (var data in dyn.data)
  {
    listBox1.Items.Add(data.username); 
  }

  result = await relationships.Follows(userID, result.Pagination.NextCursor)
}

Because this code uses await, you will have to mark the method as async, for more information on what that entails, I would suggest looking at the async/await keywords, a good introduction can be found at Stephen Cleary's Blog



来源:https://stackoverflow.com/questions/25137768/c-sharp-instasharp-how-to-use-pagination

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