0

I am trying to get a python script to say whether a twitch channel is live but haven't been able to do it, any and all help would be appreciated.

here are the docs I've been able to find https://dev.twitch.tv/docs/api/guide

This is what I have atm but I keep on getting "'set' object has no attribute 'items'". This is modified code from "Is There Any Way To Check if a Twitch Stream Is Live Using Python?" however it is now outdated because of the new API.

import requests
def checkUser(): 
    API_HEADERS = {
        'Client-ID : [client id here from dev portal]',
        'Accept : application/vnd.twitchtv.v5+json',
    }

    url = "https://api.twitch.tv/helix/streams/[streamer here]"

    req = requests.Session().get(url, headers=API_HEADERS)
    jsondata = req.json()
    print(jsondata)

checkUser()
4

1 回答 1

0

您的“'set'对象没有属性'items'”问题的答案只是一个简单的错字。它应该是

API_HEADERS = {
'Client-ID' : '[client id here from dev portal]',
'Accept' : 'application/vnd.twitchtv.v5+json'
}

注意冒号现在不是文本的一部分

为了回答您关于如何判断频道是否在线的首要问题,您可以查看我制作的示例代码。

import requests

URL = 'https://api.twitch.tv/helix/streams?user_login=[Channel_Name_Here]'
authURL = 'https://id.twitch.tv/oauth2/token'
Client_ID = [Your_client_ID]
Secret  = [Your Client_Secret]

AutParams = {'client_id': Client_ID,
             'client_secret': Secret,
             'grant_type': 'client_credentials'
             }


def Check():
    AutCall = requests.post(url=authURL, params=AutParams) 
    access_token = AutCall.json()['access_token']

    head = {
    'Client-ID' : Client_ID,
    'Authorization' :  "Bearer " + access_token
    }

    r = requests.get(URL, headers = head).json()['data']

    if r:
        r = r[0]
        if r['type'] == 'live':
            return True
        else:
            return False
    else:
        return False

print(Check())
于 2020-12-02T23:29:47.220 回答