Jump to content
Tactically Inept

Twitch Streaming Live Status Button


kuhla

Recommended Posts

NOTE: June, 19, 2020: This code below does not work after Twitch made some major changes to how their API works. I am only leaving it here for reference. See my post farther down below from June 19th.

This is not new content. This is just taken out of the previous thread and moved in here to remain consistent with the youtube streaming thread I just made.

 

Ok. Just got it working. You should see it now in the top left of the window where there is a graphic that says "Twitch Stream" and then whether it is offline or online. It is a clickable link at all times. The only issue that occurs is that when a stream has just started or ended, for about 30 seconds it flips between offline and online repeatedly. I'm assuming this is something to do with Twitch's API. After 20-30 seconds it levels out and stays correctly online or offline no matter how many times you refresh.

 

I figured I would catalog how I implemented it for those that might be curious. A little knowledge of html/css/php will probably help....

 

In the IPB global template (Customization > Themes > TheThemeYouAreEditing > Edit HTML CSS > Templates > Core > Front > Global > Logo) right after this code....

{{if \IPS\Theme::i()->logo['front']['url'] !== null }}
{{$logo = \IPS\File::get( 'core_Theme', \IPS\Theme::i()->logo['front']['url'] )->url;}}
<a href='{setting="base_url"}' id='elLogo' accesskey='1'><img src="{$logo}" alt='{setting="board_name" escape="true"}'></a>

....I put in this code....

<span style="padding-left:70px;"></span>
<iframe src="http://www.tactinept.com/twitchbut/twitch.php" frameborder="0" seamless scrolling="no" height="30" width="100"></iframe>
&nbsp;
<iframe src="http://www.tactinept.com/youtubebut/youtube.php" frameborder="0" seamless scrolling="no" height="30" width="100"></iframe>

....and the contents of twitch.php are....

<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/helix/streams?user_login=***CHANNEL* TO*CHECK*HERE***");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Client-ID: ***CLIENT*ID*HERE***"
));
$profile_data = json_decode(curl_exec($ch), true);
curl_close($ch);

if (!isset($profile_data['data'][0])) {
    echo "<a href=https://www.twitch.tv/tacinept target=newvindow><img src=https://www.tactinept.com/twitchbut/twitch_offline.png></a>";
} else {
    echo "<a href=https://www.twitch.tv/tacinept target=newvindow><img src=https://www.tactinept.com/twitchbut/twitch_online.png></a>";
}

 ?>

Initially I didn't want to implement it initially as an iframe but I ran into too many issues trying to use a php include. I also later ran into some issues trying to have the image output of a php function be a clickable link so instead I fell back to an echo command with html links.

 

You should see some minor graphical changes to it over the next few days/hours but then it should stay.

 

EDIT: Code updated 6-21-17

EDIT: Code updated 9-19-19

EDIT: Disclaimer added June 19, 2020

Link to comment
Share on other sites

  • 1 year later...

This is broken right now and I think I know why.

 

Twitch blog article from May 5, 2016 - https://blog.twitch.tv/client-id-required-for-kraken-api-calls-afbb8e95f843

 

....

 

Our Kraken API has always had a requirement of including the Client-ID header in order to not be rate limited. We’ve been lenient about this policy thus far and would allow requests without the header. On Monday, August 8th, 2016, we’re going to start requiring the Client-ID header. After the change, any request without the Client-ID header will be rejected.

.....

 

Looks easy to fix but will take a little time to troubleshoot. It's probably been broken for months but no one has been streaming so it has not really mattered. I will take a look at this when I have a little time.

Link to comment
Share on other sites

Buttons are working now if you have not noticed. Code in post above has been updated. The reason for the update was (1) the forum update and (2) see my post above on April 5th twitch API changes. Pretty small change but you need a client ID now which means you need to generate one and use it.

Link to comment
Share on other sites

  • 2 years later...

So the Twitch button has been broken since May 1st. They actually sent out an email before that date about the change so I knew it was coming. I did spend a little bit of time looking into what it would take to fix it and I honestly did not fully understand at the time.

As of last night, I will tentatively state that I am 95% sure I have fixed it but what I smushed together is so disgusting that I don't want to post it here right now and this was even after trying to do it a completely different way first. It's not even production worthy and more proof of concept level rough. I need to take what I have now and dramatically clean it up and may at that point I will maybe post it here.

However I do want to sort of share parts of where the logic is going in the off chance someone cares. This is sort of pseudo code-ish. I dunno. I'm not even sure if this is right.

EDIT: Yes I know there isn't really anything there for handling errors. I know.

Quote

function generatetoken

curl https://id.twitch.tv/oauth2/token

post with client id, secret, token type

write what gets returned to thefile

function tokenagecheck

read maxage from thefile

read token from thefile

if currenttime < currenttime + maxage then pass token

else pass error

function checklive

curl https://api.twitch.tv/helix/streams?user_login=STREAMNAME

header with client id and token

json decode check for live status return true or false

If tokenagecheck != error

if checklive with token = true

stream is live

else

stream is offline

endif

else

run generatetoken

if checklive = true

stream is live

else

stream is offline

endif

endif

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...