使用 Python 和 TwitchAPI 测试 Twitch 流是否可用

问题描述

我正在寻找一种解决方案,可以在上线/离线时自动切换 OBS 中的场景。 我从较早的 question 中找到了此代码,但它对我不起作用。 它只是返回“无”。 坦克!

sudo apt-get install PHP-mongodb

解决方法

这里有一种更简单、更容易调试的方法:

import requests
import json

def is_live_stream(streamer_name,client_id):

    twitch_api_stream_url = "https://api.twitch.tv/kraken/streams/" \
                    + streamer_name + "?client_id=" + client_id

    streamer_html = requests.get(twitch_api_stream_url)
    streamer = json.loads(streamer_html.content)

    return streamer["stream"] is not None

# Call with is_live_stream("bikestreaming",your-client-id)

和以前一样,如果主播处于直播状态,则为 true,如果主播未直播,则为 false。带有 try catch 语句的代码很难调试。此链接解释了 twitch 客户端 ID:https://dev.twitch.tv/docs#client-id