kuhla Posted September 10, 2015 Report Share Posted September 10, 2015 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-startedhttps://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=enhttp://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 Quote Link to comment Share on other sites More sharing options...
kuhla Posted June 21, 2017 Author Report Share Posted June 21, 2017 Code updated. Almost no change. Needed an update because of the forum. Quote Link to comment Share on other sites More sharing options...
kuhla Posted April 2, 2019 Author Report Share Posted April 2, 2019 Looks like the button broke. Should I fix it? Nobody uses it.... ....or just turn it into a link? Quote Link to comment Share on other sites More sharing options...
Malaphax Posted April 2, 2019 Report Share Posted April 2, 2019 I'm of the opinion we just kill that button. We're getting use out of the twitch streaming button. Last upload to our shared youtube account was me ~3 years ago, so unless we were planning on utilizing the tacinept youtube channel for our various twitch highlights/uploads I don't think even a link is all that useful. Quote Link to comment Share on other sites More sharing options...
kuhla Posted April 2, 2019 Author Report Share Posted April 2, 2019 I'll just turn it into a link. That is easy. Quote Link to comment Share on other sites More sharing options...
kuhla Posted April 3, 2019 Author Report Share Posted April 3, 2019 Briefly looked into what might be wrong with it. Something changed with the API but not sure what. Button is just a button now. Does not show live status. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.