我只是想知道是否有任何方法来编写python脚本来检查twitch.tv流是否存在?任何和所有的想法都表示赞赏!
编辑:我不知道为什么我的应用引擎标签被删除,但这将使用应用程序引擎.
Gre*_* 27 10
我讨厌必须经历制作 api 密钥的过程以及所有这些只是为了检查频道是否处于活动状态,所以我尝试找到一种解决方法:
截至 2021 年 6 月,如果您向类似 的 url 发送 http get 请求https://www.twitch.tv/CHANNEL_NAME
,则响应中将显示“"isLiveBroadcast": true
如果流是直播的”,如果流不是直播的,则不会有类似内容。
所以我在nodejs中编写了这段代码作为示例:
const fetch = require('node-fetch');
const channelName = '39daph';
async function main(){
let a = await fetch(`https://www.twitch.tv/${channelName}`);
if( (await a.text()).includes('isLiveBroadcast') )
console.log(`${channelName} is live`);
else
console.log(`${channelName} is not live`);
}
main();
Run Code Online (Sandbox Code Playgroud)
这也是一个Python示例:
import requests
channelName = '39daph'
contents = requests.get('https://www.twitch.tv/' +channelName).content.decode('utf-8')
if 'isLiveBroadcast' in contents:
print(channelName + ' is live')
else:
print(channelName + ' is not live')
Run Code Online (Sandbox Code Playgroud)
由于截至 2020 年 5 月 2 日,所有答案实际上都已过时,我会试一试。您现在需要注册一个开发者应用程序(我相信),现在您必须使用需要用户 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
Run Code Online (Sandbox Code Playgroud)
看起来Twitch提供了一个API(此处提供文档),它提供了获取该信息的方法.获取Feed的一个非常简单的示例是:
import urllib2
url = 'http://api.justin.tv/api/stream/list.json?channel=FollowGrubby'
contents = urllib2.urlopen(url)
print contents.read()
Run Code Online (Sandbox Code Playgroud)
这将转储所有信息,然后您可以使用JSON库进行解析(XML看起来也可用).看起来如果流不是活的话,该值返回空(没有测试过这么多,也没有读过任何东西:)).希望这可以帮助!
RocketDonkey的好答案现在似乎已经过时了,所以我发布了一个更新的答案,对于像我这样的人来说,谷歌会遇到这个问题.您可以通过解析检查用户EXAMPLEUSER的状态
https://api.twitch.tv/kraken/streams/EXAMPLEUSER
Run Code Online (Sandbox Code Playgroud)
条目"stream":null将告诉您用户是否离线,如果该用户存在.这是一个小的Python脚本,您可以在命令行上使用该脚本将为用户在线打印0,为用户脱机打印1,为未找到用户打印2.
#!/usr/bin/env python3
# checks whether a twitch.tv userstream is live
import argparse
from urllib.request import urlopen
from urllib.error import URLError
import json
def parse_args():
""" parses commandline, returns args namespace object """
desc = ('Check online status of twitch.tv user.\n'
'Exit prints are 0: online, 1: offline, 2: not found, 3: error.')
parser = argparse.ArgumentParser(description = desc,
formatter_class = argparse.RawTextHelpFormatter)
parser.add_argument('USER', nargs = 1, help = 'twitch.tv username')
args = parser.parse_args()
return args
def check_user(user):
""" returns 0: online, 1: offline, 2: not found, 3: error """
url = 'https://api.twitch.tv/kraken/streams/' + user
try:
info = json.loads(urlopen(url, timeout = 15).read().decode('utf-8'))
if info['stream'] == None:
status = 1
else:
status = 0
except URLError as e:
if e.reason == 'Not Found' or e.reason == 'Unprocessable Entity':
status = 2
else:
status = 3
return status
# main
try:
user = parse_args().USER[0]
print(check_user(user))
except KeyboardInterrupt:
pass
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14555 次 |
最近记录: |