[RUBY] How to get the latest live stream ID for a channel without using the YouTube Data API

Caution

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.

Background

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.

  1. Acquire the entire channel information Even if you try to acquire the latest channel video from the channels resource, the live distribution information (information during live distribution or scheduled to be released) is not included. (Currently as of May 2020)
  2. If you use the 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.

Method (Ruby)

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.

Reference (How to use PHP)

https://stackoverflow.com/questions/58040154/how-to-get-live-video-id-from-from-youtube-channel-html

Recommended Posts

How to get the latest live stream ID for a channel without using the YouTube Data API
How to output array values without using a for statement
How to get the contents of Map using for statement Memorandum
[Java] How to get to the front of a specific string using the String class
[Java] How to operate List using Stream API
How to get the ID of a user authenticated with Firebase in Swift
[Java] How to easily get the longest character string of ArrayList using stream
[For beginners] How to operate Stream API after Java 8
How to store Rakuten API data in a table
How to make a mod for Slay the Spire
[Swift] How to get the document ID of Firebase
SDWebImage: How to clear the cache for a particular UIImageView
[Must-see for apprentice java engineer] How to use Stream API
How to install JDK 8 on Windows without using the installer
How to join a table without using DBFlute and sql
Now is the time to get started with the Stream API
[jsoup] How to get the full amount of a document
How to play MIDI files using the Java Sound API
How to create a jar file or war file using the jar command
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
Easy way to create a mapping class when using the API
How to get the id of PRIMAY KEY auto_incremented in MyBatis
[For beginners] How to get the Ruby delayed railway line name
How to check for the contents of a java fixed-length string
How to make a groundbreaking diamond using Java for statement wwww
[java8] To understand the Stream API
How to configure high availability (HA) without the need for a R3 Corda 3rd party load balancer
How to return a value from Model to Controller using the [Swift5] protocol
How to build a Ruby on Rails environment using Docker (for Docker beginners)
A story I was addicted to when testing the API using MockMVC
Try to issue or get a card from Jave to Trello using API
How to convert an array of Strings to an array of objects with the Stream API
How to get the current date as a string in yyyyMMdd format
How to give MAX + 1 ID to registered data when adding a new record
How to get the absolute path of a directory running in Java
[Java] Generate a narrowed list from multiple lists using the Stream API
[Swift] How to set an image in the background without using UIImageView.
How to execute a contract using web3j
How to sort a List using Comparator
How to get date data in Ruby
Data processing using stream API from Java 8
Try using the Stream API in Java
[IOS] How to get data from DynamoDB
How to create a Maven repository for 2020
[Java] How to get the current directory
How to get the date in java
I tried to summarize the Stream API
[Rails] How to handle data using enum
How to get the query string to actually issue when using PreparedStatement with JDBC
How to get articles using News API, New York Times API, Wikipedia gem, etc.
How to play MIDI files using the Java Sound API (specify the MIDI device to use)
[Swift] How to generate an ID to uniquely identify a certain thing (using UUID)
[Java] How to get HashMap elements by loop control using extended for statement
[Ruby] Sending a POST HTTP request to the Web API using OAuth authentication
How to make a unique combination of data in the rails intermediate table