multistream/scripts/update_channels.py
File Type: text/x-script.python
#update_channels
from django.conf import settings
from twitchAPI.twitch import Twitch
from frontend import models
import pprint
pp = pprint.PrettyPrinter(indent=4)
def run():
t_client = Twitch(settings.TWITCH_CLIENT_ID, settings.TWITCH_CLIENT_SECRET)
channels = models.Channel.objects.filter(active=True)
try:
t_channels = t_client.get_users(logins=[c.name for c in channels])
except:
t_channels = None
pp.pprint("Error communicating with Twitch API")
if t_channels:
for t_c in t_channels['data']:
c = channels.get(name__iexact=t_c["login"].lower())
if c:
pp.pprint(c.name + " -> " + t_c["login"])
c.logo = t_c["profile_image_url"]
c.tid = t_c["id"]
c.save()