0

I am trying to get a list of subscribers via cursor based pagination. This is my first time using cursor based pagination, so I may be over complicating & I hope you'll identify that if so!

Here is my function for reference:

def get_subscribers():
    subscribers = [] # Create empty list for appending collected

    url = 'https://api.twitch.tv/helix/subscriptions'

    token = refresh_token(config.refresh_token)

    headers = {
        'Client-id':config.client_id,
        'Authorization': 'Bearer {}'.format(token)
    }

    params = {
        'broadcaster_id':config.channel_id,
    }

    req = requests.request('GET', url, params=params, headers=headers)
    response = req.json()
    subs = response['data']
    next = response['pagination']['cursor']
    for sub in subs:
        print('Appending {0} to Subscriber List.'.format(sub['user_name']))
        subscribers.append(sub)

    while subs is not None:
        params = {
            'broadcaster_id':config.channel_id,
            'after':next
        }

        req = requests.request('GET', url, params=params, headers=headers)
        response = req.json()
        subs = response['data']
        next = response['pagination']['cursor']

        for sub in subs:
            print('Appending {0} to Subscriber List.'.format(sub['user_name']))
            subscribers.append(sub)

    return subscribers

Here's an example response on the first request:

{
  "data": [
    {
      "broadcaster_id": "67961343",
      "broadcaster_name": "xJOKERx",
      "is_gift": false,
      "plan_name": "Channel Subscription (xJOKERx): $24.99 Sub",
      "tier": "3000",
      "user_id": "67961343",
      "user_name": "xJOKERx"
    },
    {
      "broadcaster_id": "67961343",
      "broadcaster_name": "xJOKERx",
      "is_gift": false,
      "plan_name": "Channel Subscription (xJOKERx)",
      "tier": "1000",
      "user_id": "147283917",
      "user_name": "killming"
    },
    {
      "broadcaster_id": "67961343",
      "broadcaster_name": "xJOKERx",
      "is_gift": false,
      "plan_name": "Channel Subscription (xJOKERx)",
      "tier": "1000",
      "user_id": "100417968",
      "user_name": "King_gilbster"
    },
    {
      "broadcaster_id": "67961343",
      "broadcaster_name": "xJOKERx",
      "is_gift": false,
      "plan_name": "Channel Subscription (xJOKERx)",
      "tier": "1000",
      "user_id": "81318617",
      "user_name": "ParagonWing"
    },
  ],
  "pagination": {
    "cursor": "eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MjB9fQ"
  }
}

Here is an example response on the "final" page:

{
  "data": [],
  "pagination": {
    "cursor": "eyJiIjp7Ik9mZnNldCI6MTYwfSwiYSI6eyJPZmZzZXQiOjIwMH19"
  }
}

So what I'm doing is making my initial request then saying while the list is not None continue making requests while using the last cursor for offset.

The problem with this is the list is never None. What condition could be met for this to stop the while loop?

I am also open to alternatives not requiring a while loop.

CodeSpent
  • 1,684
  • 4
  • 23
  • 46

1 Answers1

2

Instead of checking if sub is None, check if the list is empty:

while subs:   # This checks for both None and empty
    # do things
drum
  • 5,416
  • 7
  • 57
  • 91