Terms of Service-YouTube Please be sure to check the YouTube Terms of Service as it may conflict with the YouTube Terms of Service depending on how you use it.
This is a problem I encountered during the recent development of personal apps. Now that YouTube livestreaming is active every day, it is often the case that you want to get the latest livestreaming information for that channel while having a channel ID. However, there are the following two problems when trying to get the latest live distribution information using the YouTube Data API.
channels
resource, the live distribution information (information during live distribution or scheduled to be released) is not included. (Currently as of May 2020)search
resource with the channel ID, it will be treated like a write operation, so the quota cost will be dozens of times higher than other information acquisition operations list
(https://developers.google). .com / youtube / v3 / getting-started? hl = ja) will be consumed.Since it doesn't make sense if you don't get and update the presence or absence of live distribution information frequently, using the search
resource will soon eliminate the quota cost allocation that can be used for one day. On the other hand, if you have a way to get the live stream ID videoId
, you can specify the videoId
to access the live stream information from the videos
resource at a low quota cost.
Therefore, it is necessary to get the latest live distribution ID of the channel, videoId
, without using the YouTube Data API.
It may not be used much, but if you have livestream information on your channel, you can embed the latest livestream player on your page using the url below.
url = "https://www.youtube.com/embed/live_stream?channel=<Channel ID>"
It will be easier to talk if you display the latest live stream. The next thing to do is to strip the page ~~ and retrieve the required information videoId
.
content = Net::HTTP.get_response(URI.parse(url)).entity
unless content.match(/watch\?.+/) == nil
match = content.match(/watch\?.+/)[0]
videoId = match.sub("watch?v=","").sub("\">","")
end
Since it is basically written in the form of watch? V = <videoId>
on YouTube pages and urls, you can use the match
and sub
methods from the obtained content
to get videoId
. Easy to get. (Thanks to YouTube who maintained it properly)
Now that you have the videoId
, you can use thevideos (videos)
resource of the YouTube Data API to get the distribution status and distribution time.
https://stackoverflow.com/questions/58040154/how-to-get-live-video-id-from-from-youtube-channel-html
Recommended Posts