有什么方法可以使用 Python 检查 Twitch 流是否在线?

新手上路,请多包涵

我只是想知道是否有任何方法可以编写 python 脚本来检查 twitch.tv 流是否直播?

我不确定为什么我的应用引擎标签被删除,但这将使用应用引擎。

原文由 suitegamer 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 808
2 个回答

看起来 Twitch 提供了一个 API(文档 在这里),它提供了一种获取该信息的方法。获取提要的一个非常简单的示例是:

 import urllib2

url = 'http://api.justin.tv/api/stream/list.json?channel=FollowGrubby'
contents = urllib2.urlopen(url)

print contents.read()

这将转储所有信息,然后您可以使用 JSON 库 解析这些信息(XML 看起来也可用)。如果流不是活动的,看起来该值返回空(根本没有测试过这么多,我也没有读过任何东西:))。希望这可以帮助!

原文由 RocketDonkey 发布,翻译遵循 CC BY-SA 3.0 许可协议

由于截至 2020-05-02 所有答案实际上都已过时,我会试一试。您现在需要注册开发人员应用程序(我相信),现在您必须使用需要用户 ID 而不是用户名(因为它们可以更改)的端点。

请参阅 https://dev.twitch.tv/docs/v5/reference/users

https://dev.twitch.tv/docs/v5/reference/streams

首先你需要 注册一个应用程序

从那里你需要得到你的 Client-ID

这个例子中的那个不是真实的

TWITCH_STREAM_API_ENDPOINT_V5 = "https://api.twitch.tv/kraken/streams/{}"

API_HEADERS = {
    'Client-ID' : 'tqanfnani3tygk9a9esl8conhnaz6wj',
    'Accept' : 'application/vnd.twitchtv.v5+json',
}

reqSession = requests.Session()

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

原文由 Cherona 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏