BuddyDev

Search

Twitch Buddypress Integration

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #6658

    Hello community. I’ve tinkered with this in the past but I wanted to share my knowledge and maybe @brajesh and his team could expand it into a nice premium plugin.

    My idea was to have users have a custom Twitch.tv tab in buddypress with the stream/chat. I wanted to expand this so that it would have a rating system for logged in users to give the user a star rating or a thumbs up like type system. This would then allow the live streamer to show up higher in the “all online streams” page.

    Add Twitch.tv tab to buddypress:
    https://premium.wpmudev.org/forums/topic/buddypress-add-twitch-channelchat-api

    Here is the Twitch.tv API:
    https://github.com/justintv/Twitch-API/blob/master/embed-video.md

    Here is the code for a page of live streamers:

    Add this to the theme’s function.php :

    /**
    					 * GET CHANNEL STREAM INFO
    					 *
    					 * sends a request to the twitch api and retrieve the stream data
    					 * full list of returned data here: https://github.com/justintv/Twitch-API/blob/master/v3_resources/streams.md#get-streamschannel
    					 *
    					 * #param	{string}	$channel	(required)	slug of the channel
    					 *
    					 * #return	{array}	$stream_data			array with info related to the channel
    					 */
    
    					function ap_get_channel_stream($channel) {
    							// send a request to twitch streams api
    							$twitch_call = file_get_contents('https://api.twitch.tv/kraken/streams/'.$channel);
    							$stream_data = json_decode($twitch_call, true);
    							// return the data
    							return $stream_data;
    					}
    
    					/**
    					 * CHECK IF CHANNEL IS LIVE
    					 *
    					 * #uses ap_get_channel_stream()	to get the stream data for the given channel
    					 *
    					 * #param	{string}	$channel	(required)	the slug of the channel
    					 *
    					 * #return	{bool}		true if channel is live, false if not
    					 */
    
    					function ap_is_channel_live( $channel ) {
    						$stream_data = ap_get_channel_stream($channel);
    						return ( ! is_null( $stream_data['stream'] ) ) ? TRUE : FALSE;
    					}
    
    					/**
    					 * DISPLAY THE LIVE STREAM
    					 *
    					 * embed the video stream of a given channel
    					 *
    					 * #param	{string}	$channel	(required) the slug of the channel
    					 *
    					 * #return	{echo}		print the iframe for the video player
    					 */
    
    					function ap_display_channel_stream($channel){
    						echo '<iframe src="http://player.twitch.tv/?channel='. $channel .'" height="720" width="1280" frameborder="0" scrolling="no" allowfullscreen></iframe>';                        
    
    					}

    then in a custom page template add this:

    // get all users and loop them
    					$users = get_users();
    					foreach ( $users as $user ){
    						// get the channel
    						unset($channel);
    						$channel = bp_get_profile_field_data('field=Channel Name&user_id=' . $user->ID );	
    
    						// if we've got a channel name, check if online and eventually display something
    						if ( $channel ){
    							if ( ap_is_channel_live($channel) ){
    
    								// you could display the video stream directly with ap_display_channel_stream($channel);
    								// but then you'll have multiple videos on the same page..
    
    								// you can retrieve and use various stream info
    								unset($channel_data);
    								$channel_data = ap_get_channel_stream($channel);
    
    								// then by instance you could display the name of the channel and the viewers
    								echo $channel_data['stream']['channel']['display_name'];
    								echo 'viewers: '.$channel_data['stream']['viewers'];
    
    								// or the preview image, linking to their profile on your site
    								echo '<a href="'.bp_core_get_user_domain( $user->ID ).'"><img src="'.$channel_data['preview']['medium'].'"></a>';
    
    							}
    						}
    					}
  • Participant
    Level: Enlightened
    Posts: 69
    Christopher Niedzwiecki on #6666

    I would support anything Twitch since I’m trying to make a social network for gamers. I don’t think that would be a big audience for this website though lol. Vote for me though 😀

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #6667

    Honestly I’d like to see a discord integration as well in addition to a good twitch streamer plugin. Both would be great to integrate with buddypress to expand the community feel.

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #6680

    Hi Chris, Joshua,
    Thank you.

    I am sure that the above code is beneficial for our members. We do have plans for some gaming based theme next year, so we will certainly explore all the options. We don’t have any plan for this at the moment though. Sorry about that.

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #6682

    Hey Brajesh,

    No problem. It is just some basic code to get the tab in buddypress profiles and a page that will display online streamers that are on the website.

    I would love to see this expanded so that a rating system can be added to the user’s twitch tv buddypress tab. So if I’m a registered user who visit the site, I can rate that twitch streamer on their profile page. Just some ideas.

    Love to see your gaming themes! Looking forward to seeing what you guys come up with.

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #6684

    Thank you Joshua. Yes, The integration to profile for twitch/discord will make sense. We will still need to explore it when we start but I am going to keep your ideas in mind.

    I am looking forward to an exciting 2017 🙂

  • Participant
    Level: Enlightened
    Posts: 69
    Christopher Niedzwiecki on #6687

    Brajesh for president

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #6695

    hahaha 🙂

You must be logged in to reply to this topic.

This topic is: not resolved