Guest User

Teamliquid Twitch.tv List Plugin Refactored (finished)

a guest
Dec 30th, 2012
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.23 KB | None | 0 0
  1. <html>
  2.         <head>
  3.                 <title>Streamlist</title>
  4.         </head>
  5.         <body>
  6.                 <?php
  7.                     /**
  8.                     * @version   0.1
  9.                     * @copyright (C) 2012/2013 Cees Middel (Avon) www.maegis.net
  10.                     * @Inspired by Syi http://mushroom-mayhem.com
  11.                     * @license   GNU/GPL v3 http://www.gnu.org/licenses/gpl.html
  12.                     */
  13.  
  14.                     /**
  15.                     * array with all stream names i.e. www.twitch.tv/"iksf".
  16.                     * To add more users just add their channel names with comma seperation
  17.                     * This is all there is, no futher editing required
  18.                     */
  19.                     $streamList = array("beyondthesummit", "maegis");
  20.  
  21.                     // This variable becomes one long url with the channel names stringed up behind it
  22.                     // This url then fetches a json file from twitch with all the selected channels information
  23.                     $userGrab = "http://api.justin.tv/api/stream/list.json?channel=";
  24.  
  25.                     //I use this array to compare with the members array. All users in this arrat are substracted from the members array and hence are //"offline"
  26.                     $checkedOnline = array();
  27.  
  28.                     foreach($streamList as $singleStream){
  29.                             $userGrab .= ",";
  30.                             $userGrab .= $singleStream;
  31.                     }
  32.  
  33.                     //grabs the channel data from twitch.tv streams
  34.                     $json_file = file_get_contents($userGrab);
  35.                     $json_array = json_decode($json_file, true);
  36.  
  37.                     //gets member names from stream url's and checks for online members
  38.                     foreach($streamList as $i => $singleStream){
  39.                             if(isset($json_array[$i]))
  40.                             {
  41.                                 $title = $json_array[$i]['channel']['channel_url'];
  42.                                 $array = explode('/', $title);
  43.                                 $online = end($array);
  44.                                 $viewers = $json_array[$i]['stream_count'];        
  45.                                 if(checkOnline($online, $viewers))
  46.                                 {
  47.                                     $checkedOnline[] = $online;
  48.                                 }  
  49.                             }
  50.                                                        
  51.                     }
  52.  
  53.                     //checks if player streams are online
  54.                     function checkOnline($online, $viewers)
  55.                     {  
  56.                             //If the variable online is not equal to null, there is a good change this person is currently streaming
  57.                             if ($online != null)
  58.                             {
  59.                                     echo '<a href="http://www.twitch.tv/'.$online.'"> <strong>'.$online.'</strong></a>';
  60.                                     echo '&nbsp <img src="images/online.png"><strong> Status:</strong> Online! </br>';
  61.                                     echo '<img src="images/viewers.png"><strong>Viewers:</strong> &nbsp' .$viewers.'</br>';
  62.                                     return true;
  63.                             }
  64.                     }              
  65.                 ?>
  66.                 <hr>
  67.                 <?php
  68.                     //This part list all the people currently offline. Here the array with online users is compared with the total users.
  69.                     //online users are then removed from the total users array.
  70.                     foreach ($streamList as $i => $singleStream)
  71.                     {
  72.                             if(in_array($singleStream, $checkedOnline))
  73.                             {
  74.                                     unset($streamList[$i]);
  75.                             }                                      
  76.                     }
  77.                     //print a nice list with people that can't currently be bothered with streaming their games    
  78.                     foreach ($streamList as $i => $singleStream) {
  79.                             echo '<a href="http://www.twitch.tv/'.$singleStream.'"> <strong>'.$singleStream.'</strong></a>';
  80.                             echo '&nbsp<img src="images/offline.png"> <strong> Status :</strong> Offline! </br>';
  81.                     }
  82.                 ?>
  83.         </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment