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

后端 未结 7 554
被撕碎了的回忆
被撕碎了的回忆 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:41

    I'll try to shoot my shot, just in case someone still needs an answer to this, so here it goes

    import requests
    import time
    from twitchAPI.twitch import Twitch
    
    client_id = ""
    client_secret = ""
    
    twitch = Twitch(client_id, client_secret)
    twitch.authenticate_app([])
    
    TWITCH_STREAM_API_ENDPOINT_V5 = "https://api.twitch.tv/kraken/streams/{}"
    
    API_HEADERS = {
        'Client-ID' : client_id,
        'Accept' : 'application/vnd.twitchtv.v5+json',
    }
    
    def checkUser(user): #returns true if online, false if not
        userid = twitch.get_users(logins=[user])['data'][0]['id']
        url = TWITCH_STREAM_API_ENDPOINT_V5.format(userid)
        try:
            req = requests.Session().get(url, headers=API_HEADERS)
            jsondata = req.json()
            if 'stream' in jsondata:
                if jsondata['stream'] is not None: 
                    return True
                else:
                    return False
        except Exception as e:
            print("Error checking user: ", e)
            return False
    
    print(checkUser('michaelreeves'))
    

提交回复
热议问题