问题描述
我正在开发一个网站来加载多个 YouTube 频道直播.起初,我试图找出一种不使用 youtube api 的方法来做到这一点,但我决定放弃.
I'm working on a website to load multiple youtube channels live streams. At first i was trying to figure out a way to do this without utilizing youtube's api but have decided to give in.
要查找某个频道是否正在直播并获取我一直在使用的直播链接:
To find whether a channel is live streaming and to get the live stream links I've been using:
https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={CHANNEL_ID}&eventType=live&maxResults=10&type=video&key={API_KEY}
但是,最小配额为 10000,每次搜索价值 100,在超出配额限制之前,我只能进行大约 100 次搜索,这根本没有帮助.我最终在大约 10 分钟内超过了配额限制.:(
However with the minimum quota being 10000 and each search being worth 100, Im only able to do about 100 searches before I exceed my quota limit which doesn't help at all. I ended up exceeding the quota limit in about 10 minutes. :(
有没有人知道使用尽可能少的配额点来确定频道当前是否正在直播以及直播链接是什么的更好方法?
Does anyone know of a better way to figure out if a channel is currently live streaming and what the live stream links are, using as minimal quota points as possible?
我想每 3 分钟为每个用户重新加载 youtube 数据,将其保存到数据库中,并使用我自己的 api 显示信息以节省服务器资源以及配额点.
I want to reload youtube data for each user every 3 minutes, save it into a database, and display the information using my own api to save server resources as well as quota points.
希望有人能很好地解决这个问题!
Hopefully someone has a good solution to this problem!
如果对链接无能为力,只需确定用户是否在线,而无需每次使用 100 个配额点,这将是一个很大的帮助.
If nothing can be done about links just determining if the user is live without using 100 quota points each time would be a big help.
推荐答案
由于该问题仅指定不应使用 Search API 配额来确定频道是否正在流式传输,因此我想我会分享一种解决方法方法.与简单的 API 调用相比,它可能需要更多的工作,但它可以将 API 配额的使用量减少到几乎为零:
Since the question only specified that Search API quotas should not be used in finding out if the channel is streaming, I thought I would share a sort of work-around method. It might require a bit more work than a simple API call, but it reduces API quota use to practically nothing:
我使用了一个简单的 Perl GET
请求来检索 Youtube 频道的主页.在直播的频道页面的 HTML 中可以找到几个独特的元素:
I used a simple Perl GET
request to retrieve a Youtube channel's main page. Several unique elements are found in the HTML of a channel page that is streaming live:
实时观众标签的数量,例如
753 观看
LIVE NOW
徽章标签:.要确定某个频道当前是否正在直播,需要通过简单的匹配来查看唯一的 HTML 标记是否包含在 GET 请求结果中.类似于:if ($get_results =~/$unique_html/)
(Perl).然后,可以只对实际正在流式传输的通道ID进行API调用,以获取该流的视频ID.
To ascertain whether a channel is currently streaming live requires a simple match to see if the unique HTML tag is contained in the GET request results. Something like: if ($get_results =~ /$unique_html/)
(Perl). Then, an API call can be made only to a channel ID that is actually streaming, in order to obtain the video ID of the stream.
这样做的好处是您已经知道频道正在流式传输,而不是使用数千个配额点来查找.我的测试脚本通过在 HTML 代码中查找以下内容,成功识别频道是否正在流式传输:<span class="yt-badge yt-badge-live";>
(注意来自 Youtube 的代码中奇怪的额外空格).
The advantage of this is that you already know the channel is streaming, instead of using thousands of quota points to find out. My test script successfully identifies whether a channel is streaming, by looking in the HTML code for: <span class="yt-badge yt-badge-live" >
(note the weird extra spaces in the code from Youtube).
我不知道 OP 正在使用什么语言,或者我会帮助处理该语言的基本 GET 请求.我使用 Perl,并包含浏览器标头、用户代理和 cookie,看起来像正常的计算机访问.
I don't know what language OP is using, or I would help with a basic GET request in that language. I used Perl, and included browser headers, User Agent and cookies, to look like a normal computer visit.
Youtube 的 robots.txt 似乎并没有禁止抓取频道的主页,只是频道的社区页面.
Youtube's robots.txt doesn't seem to forbid crawling a channel's main page, only the community page of a channel.
让我知道您对这种方法的优缺点的看法,如果您发现缺陷,请评论可以改进的地方而不是不喜欢的地方.谢谢,编码愉快!
Let me know what you think about the pros and cons of this method, and please comment with what might be improved rather than disliking if you find a flaw. Thanks, happy coding!
2020 年更新yt-badge-live
似乎已被弃用,它不再可靠地显示频道是否正在流式传输.相反,我现在检查此字符串的 HTML:
2020 UPDATEThe yt-badge-live
seems to have been deprecated, it no longer reliably shows whether the channel is streaming. Instead, I now check the HTML for this string:
{"text":";观看}
如果我得到匹配项,则表示该页面正在流式传输.(非流媒体频道不包含此字符串.)再次注意奇怪的额外空格.因为我使用的是 Perl,所以我也避开了所有的引号.
If I get a match, it means the page is streaming. (Non-streaming channels don't contain this string.) Again, note the weird extra whitespace. I also escape all the quotation marks since I'm using Perl.
这篇关于如何在不使用搜索的情况下查找 youtube 频道当前是否正在直播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!