Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if a youtube channel is currently live streaming without using search?

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}

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?

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!

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.

like image 861
TheSwindler44 Avatar asked May 30 '19 20:05

TheSwindler44


People also ask

How do you find if a YouTube channel is live?

Find and watch live streams and PremieresOpen the YouTube app. From the bottom, tap Explore. From the top, tap the Live destination.

How do you know if someone is going to live on YouTube?

Go to the channel page or watch page. If you're not subscribed, click Subscribe. When you subscribe to a channel, you'll automatically get personalized notifications. Click the Notification bell to switch between getting “All notifications” and “Personalized notifications.”

Can you watch a YouTube live stream anonymously?

The people on the live, including the person who's live it is, can see that you've JOINED their stream, but unless you request to share screen AND they accept it, you will be restricted to chatting only.


2 Answers

Adding to the answer by Bman70, I tried eliminating the need of making a costly search request after knowing that the channel is streaming live. I did this using two indicators in the HTML response from channels page who are streaming live.

function findLiveStreamVideoId(channelId, cb){
  $.ajax({
    url: 'https://www.youtube.com/channel/'+channelId,
    type: "GET",
    headers: {
      'Access-Control-Allow-Origin': '*',
      'Accept-Language': 'en-US, en;q=0.5'
  }}).done(function(resp) {
      
      //one method to find live video
      let n = resp.search(/\{"videoId[\sA-Za-z0-9:"\{\}\]\[,\-_]+BADGE_STYLE_TYPE_LIVE_NOW/i);

      //If found
      if(n>=0){
        let videoId = resp.slice(n+1, resp.indexOf("}",n)-1).split("\":\"")[1]
        return cb(videoId);
      }

      //If not found, then try another method to find live video
      n = resp.search(/https:\/\/i.ytimg.com\/vi\/[A-Za-z0-9\-_]+\/hqdefault_live.jpg/i);
      if (n >= 0){
        let videoId = resp.slice(n,resp.indexOf(".jpg",n)-1).split("/")[4]
        return cb(videoId);
      }

      //No streams found
      return cb(null, "No live streams found");
  }).fail(function() {
    return cb(null, "CORS Request blocked");
  });
}

However, there's a tradeoff. This method confuses a recently ended stream with currently live streams. A workaround for this issue is to get status of the videoId returned from Youtube API (costs a single unit from your quota).

like image 165
MMujtabaRoohani Avatar answered Oct 17 '22 06:10

MMujtabaRoohani


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:

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:

The number of live viewers tag, e.g. <li>753 watching</li>. The LIVE NOW badge tag: <span class="yt-badge yt-badge-live" >Live now</span>.

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.

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).

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'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 UPDATE The 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":" watching"}

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.

like image 27
Bman70 Avatar answered Oct 17 '22 07:10

Bman70