Python: Automatically updating Twitch moderator list using API

435 views Asked by At

I'm trying to auto update a moderator list using this API: https://tmi.twitch.tv/group/user/ice3lade/chatters

I am accessing and storing it with

from urllib.request import urlopen
response = urlopen('https://tmi.twitch.tv/group/user/ice3lade/chatters')
chatlist = response.read()

But attempting to simply use it as a dictionary e.g.

print(chatlist("chatters"))

Returns an error

TypeError: 'bytes' object is not callable

I'm a total python noob so any help is appreciated. How do I either access this as a dictionary directly from the API, or how to store the data I get from reading the API as a proper dictionary?

1

There are 1 answers

0
Ice3lade On

Made a fairly reasonable solution, chatlist gives the full dictionary, chatters gives all the keys and values within the chatters dictionary, and moderators gives the list of moderators.

from urllib.request import urlopen
from json import loads
response = urlopen('https://tmi.twitch.tv/group/user/xflixx_teampokerstars/chatters')
readable = response.read().decode('utf-8')
chatlist = loads(readable)
chatters = chatlist['chatters']
moderators = chatters['moderators']

Didn't know json was required to decode the API.