Is There Any Way To Check if a Twitch Stream Is Live Using Python?

suitegamer picture suitegamer · Aug 22, 2012 · Viewed 23k times · Source

I'm just wondering if there is any way to write a python script to check to see if a twitch.tv stream is live?

I'm not sure why my app engine tag was removed, but this would be using app engine.

Answer

Cherona picture Cherona · Feb 5, 2020

Since all answers are actually outdated as of 2020-05-02, i'll give it a shot. You now are required to register a developer application (I believe), and now you must use an endpoint that requires a user-id instead of a username (as they can change).

See https://dev.twitch.tv/docs/v5/reference/users

and https://dev.twitch.tv/docs/v5/reference/streams

First you'll need to Register an application

From that you'll need to get your Client-ID.

The one in this example is not a real

TWITCH_STREAM_API_ENDPOINT_V5 = "https://api.twitch.tv/kraken/streams/{}"

API_HEADERS = {
    'Client-ID' : 'tqanfnani3tygk9a9esl8conhnaz6wj',
    'Accept' : 'application/vnd.twitchtv.v5+json',
}

reqSession = requests.Session()

def checkUser(userID): #returns true if online, false if not
    url = TWITCH_STREAM_API_ENDPOINT_V5.format(userID)

    try:
        req = reqSession.get(url, headers=API_HEADERS)
        jsondata = req.json()
        if 'stream' in jsondata:
            if jsondata['stream'] is not None: #stream is online
                return True
            else:
                return False
    except Exception as e:
        print("Error checking user: ", e)
        return False