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

后端 未结 7 475
被撕碎了的回忆
被撕碎了的回忆 2021-02-04 18:44

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

7条回答
  •  失恋的感觉
    2021-02-04 19:43

    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',
    }
    
    
    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
    

提交回复
热议问题