# Path: apps/subscribers/models.py
# class Ticket(models.Model):
#     twid = models.CharField(blank=True, max_length=40)
#     name = models.CharField(max_length=200)
#     display_name = models.CharField(blank=True, max_length=200)
#     created = models.DateTimeField(default=timezone.now)
#     updated = models.DateTimeField(default=timezone.now)
# 
#     # Store the months if there's a substreak (defaults to 1 month).
#     streak = models.IntegerField(default=1)
# 
#     # Is this subscription active?
#     is_active = models.BooleanField(default=True,
#         help_text='Is this subscription active?')
#     is_paid = models.BooleanField(default=True,
#         help_text='Is this a paid subscription? (e.g., Not a bot.)')
# 
#     # Custom manager.
#     objects = TicketManager()
# 
#     class Meta:
#         ordering = ['updated']
#         get_latest_by = 'updated'
# 
#     def __str__(self):
#         return '{}'.format(self.name)
# 
#     def update(self, **kwargs):
#         allowed_attributes = {'twid', 'display_name', 'updated', 'is_active'}
#         for name, value in kwargs.items():
#             assert name in allowed_attributes
#             setattr(self, name, value)
#         self.save()


import logging
import requests
import os
from django.core.management.base import NoArgsCommand
from apps.subscribers.models import Ticket
        count = r.json().get('_total')  # Total number of tickets.
        limit = 100  # Maximum number of tickets we can fetch at once.

        while url:
            # To keep our dyno-hour usage down, we have to make sure that
            # requests aren't hung up. So try the request and if a `Timeout`
            # is thrown, bail.
            try:
                response = requests.get(url, headers=headers, params={'limit': limit}, timeout=1)
            except requests.exceptions.RequestException as e:
                logger.exception(e)
                break

            data = response.json()
            tickets = data['subscriptions']

            # The Twitch API doesn't stop offering `next` URLs when no results
            # are available. So if we don't have tickets, shut it down.
            if not tickets:
                break

            # We have tickets. Let's check each ticket and mark if that person
            # as active if their ticket still exists in Twitch's API.
            for ticket in tickets:
                name = ticket['user']['name']
                updates = {
                        'display_name': ticket['user']['display_name'],
                        'is_active': True,
                        'updated': ticket['created_at'],
                        'twid': ticket['_id'] }

# Path: apps/subscribers/models.py
# class Ticket(models.Model):
#     twid = models.CharField(blank=True, max_length=40)
#     name = models.CharField(max_length=200)
#     display_name = models.CharField(blank=True, max_length=200)
#     created = models.DateTimeField(default=timezone.now)
#     updated = models.DateTimeField(default=timezone.now)
# 
#     # Store the months if there's a substreak (defaults to 1 month).
#     streak = models.IntegerField(default=1)
# 
#     # Is this subscription active?
#     is_active = models.BooleanField(default=True,
#         help_text='Is this subscription active?')
#     is_paid = models.BooleanField(default=True,
#         help_text='Is this a paid subscription? (e.g., Not a bot.)')
# 
#     # Custom manager.
#     objects = TicketManager()
# 
#     class Meta:
#         ordering = ['updated']
#         get_latest_by = 'updated'
# 
#     def __str__(self):
#         return '{}'.format(self.name)
# 
#     def update(self, **kwargs):
#         allowed_attributes = {'twid', 'display_name', 'updated', 'is_active'}
#         for name, value in kwargs.items():
#             assert name in allowed_attributes
#             setattr(self, name, value)
#         self.save()


# Path: apps/subscribers/management/commands/updatetickets.py
import logging
import requests
import os
from django.core.management.base import NoArgsCommand
from apps.subscribers.models import Ticket

        count = r.json().get('_total')  # Total number of tickets.
        limit = 100  # Maximum number of tickets we can fetch at once.

        while url:
            # To keep our dyno-hour usage down, we have to make sure that
            # requests aren't hung up. So try the request and if a `Timeout`
            # is thrown, bail.
            try:
                response = requests.get(url, headers=headers, params={'limit': limit}, timeout=1)
            except requests.exceptions.RequestException as e:
                logger.exception(e)
                break

            data = response.json()
            tickets = data['subscriptions']

            # The Twitch API doesn't stop offering `next` URLs when no results
            # are available. So if we don't have tickets, shut it down.
            if not tickets:
                break

            # We have tickets. Let's check each ticket and mark if that person
            # as active if their ticket still exists in Twitch's API.
            for ticket in tickets:
                name = ticket['user']['name']
                updates = {
                        'display_name': ticket['user']['display_name'],
                        'is_active': True,
                        'updated': ticket['created_at'],
                        'twid': ticket['_id'] }

                t, created = Ticket.objects.update_or_create(name=name, defaults=updates)