首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何检查YouTube频道是否正在直播

如何检查YouTube频道是否正在直播
EN

Stack Overflow用户
提问于 2015-09-08 09:36:15
回答 10查看 44.3K关注 0票数 28

我找不到任何信息来检查YouTube频道是否真的是流的。使用Twitch,您只需要通道名称,使用API,您可以检查是否有活动的。

我不想使用OAuth,通常一个公共API密钥就足够了。就像检查一个频道的视频一样,我想知道这个频道是否是流的。

EN

Stack Overflow用户

发布于 2022-05-23 05:39:32

我发现@VityaSchel的回答是非常有用的,但是它并没有区分那些有直播计划的频道和那些正在播放live now的频道。

为了区分计划和活动,我扩展了他的代码以访问YouTube数据API,以查找实时流详细信息:

代码语言:javascript
运行
复制
import { parse } from 'node-html-parser'
import fetch from 'node-fetch'

const youtubeAPIkey = 'YOUR_YOUTUBE_API_KEY'
const youtubeURLbase = 'https://www.googleapis.com/youtube/v3/videos?key=' + youtubeAPIkey + '&part=liveStreamingDetails,snippet&id='

const c = {cid: process.argv[2]}    // process.argv is array of arguments passed in console

const response = await fetch(`https://youtube.com/channel/${c.cid}/live`)
const text = await response.text()
const html = parse(text)
const canonicalURLTag = html.querySelector('link[rel=canonical]')
const canonicalURL = canonicalURLTag.getAttribute('href')

c.live = false
c.configured = canonicalURL.includes('/watch?v=')
if (!c.configured) process.exit()

c.vid = canonicalURL.match(/(?<==).*/)[0]

const data = await fetch(youtubeURLbase + c.vid).then(response => response.json())
if (data.error) {
    console.error(data)
    process.exit(1)
}
const i = data.items.pop()  // pop() grabs the last item
c.title = i.snippet.title
c.thumbnail = i.snippet.thumbnails.standard.url
c.scheduledStartTime = i.liveStreamingDetails.scheduledStartTime
c.live = i.liveStreamingDetails.hasOwnProperty('actualStartTime')
if (c.live) {
    c.actualStartTime = i.liveStreamingDetails.actualStartTime
}

console.log(c)

上述抽样输出如下:

代码语言:javascript
运行
复制
% node index.js UCNlfGuzOAKM1sycPuM_QTHg
{
  cid: 'UCNlfGuzOAKM1sycPuM_QTHg',
  live: true,
  configured: true,
  vid: '8yRgYiNH39E',
  title: ' Deep Focus 24/7 - Ambient Music For Studying, Concentration, Work And Meditation',
  thumbnail: 'https://i.ytimg.com/vi/8yRgYiNH39E/sddefault_live.jpg',
  scheduledStartTime: '2022-05-23T01:25:00Z',
  actualStartTime: '2022-05-23T01:30:22Z'
}
票数 0
EN
查看全部 10 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32454238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档