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

后端 未结 7 564
被撕碎了的回忆
被撕碎了的回忆 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条回答
  •  萌比男神i
    2021-02-04 19:27

    This solution doesn't require registering an application

    import requests
    
    HEADERS = { 'client-id' : 'kimne78kx3ncx6brgo4mv6wki5h1ko' }
    GQL_QUERY = """
    query($login: String) {
        user(login: $login) {
            stream {
                id
            }
        }
    }
    """
    
    def isLive(username):
        QUERY = {
            'query': GQL_QUERY,
            'variables': {
                'login': username
            }
        }
    
        response = requests.post('https://gql.twitch.tv/gql',
                                 json=QUERY, headers=HEADERS)
        dict_response = response.json()
        return True if dict_response['data']['user'] else False
    
    
    if __name__ == '__main__':
        USERS = ['forsen', 'offineandy', 'dyrus']
        for user in USERS:
            IS_LIVE = isLive(user)
            print(f'User {user} live: {IS_LIVE}')
    

提交回复
热议问题