Sign in to follow this  
Followers 0
kuhla

Youtube Streaming Live Status Button

2 posts in this topic

Some of you have already noticed the new, clickable button a the top of the forum showing the Youtube stream's status.

 

In this thread I'll document how it was implemented if anyone else out there wants to copy it or just learn what I did.

 

Documentation of how the button was implemented:

1) Enabled the API by registering with Google developers console.

https://developers.google.com/youtube/v3/live/getting-started
https://console.developers.google.com/

The one you are looking to enable is the "YouTube Data API".

2) Get your Youtube channel ID.

https://support.google.com/youtube/answer/3250431?hl=en
http://www.youtube.com/account_advanced

You are going to need it later on.

3) Generate the browser key.

In the Google developers console there, in the same "APIs & auth" section where you enabled the API, there is a subsection "Credentials". Here is where you generate an API browser key. Make a note of it.

You can test the output with this URL (see the two sections where you need to substitute your info?):

https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=<CHANNEL_ID_GOES_HERE>&eventType=live&type=video&key=<API_KEY_GOES_HERE>

4) Query the API.

The youtube live status button works very similarly to the twitch one, JSON decode, but with some changes for extra functionality. Here is the PHP code being used. The button sits in an iFrame at the top of the forum in the same way as the Twitch button.

<?php

$json_array = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=<CHANNEL_ID_GOES_HERE>&eventType=live&type=video&key=<API_KEY_GOES_HERE>"), true);

if ($json_array['pageInfo']['totalResults'] == 0) 
	{
		echo "<a href=https://www.youtube.com/user/tacinept target=newvindow><img src=http://www.tactinept.com/youtubebut/youtube_offline.png></a>";
}
else 
	{
		echo "<a href=https://www.youtube.com/watch?v=";
		echo $json_array[items][0][id][videoId];
		echo " target=newvindow><img src=http://www.tactinept.com/ytstream/online.gif></a>";
}
?>

It ain't pretty, just like the twitch code, but it works. The "online" part of the logic statement above is linking directly to the currently live stream, not just to the channel. It's getting the videoID from the API and putting it into the link.

 

5) Notes

 

The API is slow. As in, it doesn't appear as "online" or "offline" until almost 5 minutes after you have started/stopped streaming.

 

EDIT: CODE UPDATED: 6-21-2017

Share this post


Link to post
Share on other sites

Code updated. Almost no change. Needed an update because of the forum.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0