Advertisement
kyroskoh

Twitch CustomAPI - Random User

May 7th, 2016
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.65 KB | None | 0 0
  1. <?php
  2. // Set he times to fail when looking for a random follower
  3. $TimesToFail = 10;
  4. // Set timezone to match Twitch servers
  5. date_default_timezone_set("Greenwich");
  6. // Set strings to lowercase... Easier that way
  7. $GeneralQueryLower = strtolower($_GET["GeneralQuery"]);
  8. $BroadcasterLower = strtolower($_GET["Broadcaster"]);
  9. // Pull & decode broadcaster chatter array(s).
  10. $ChatterData = json_decode(@file_get_contents("https://tmi.twitch.tv/group/user/$BroadcasterLower/chatters"), true);
  11. // Call the Random Viewer function.
  12. RandomViewer($GeneralQueryLower, $BroadcasterLower, $ChatterData, $TimesToFail);
  13.  
  14. function RandomViewer($GeneralQueryLower, $BroadcasterLower, $ChatterData, $TimesToFail)
  15. {
  16.     // Set an empty helper array.
  17.     $ViewerArray = array();
  18.     // Check if it is a valid $GeneralQueryLower.
  19.     if($GeneralQueryLower != "follower" && $GeneralQueryLower != "all" && $GeneralQueryLower != "follower all" && $GeneralQueryLower != "mod" && $GeneralQueryLower != "moderator")
  20.     {
  21.     echo 'Invalid Request. Please indicate with: follower/mod/all...';
  22.     }
  23.     // Everyone in the stream.
  24.     if($GeneralQueryLower == "all" || $GeneralQueryLower == "follower all")
  25.     {
  26.         $ViewerArray = array_merge($ViewerArray,$ChatterData['chatters']['moderators'],$ChatterData['chatters']['staff'],$ChatterData['chatters']['admins'],$ChatterData['chatters']['global_mods'],$ChatterData['chatters']['viewers']);
  27.     }
  28.     // Just the mods, well, anyone above the average viewer.
  29.     if($GeneralQueryLower == "mod" || $GeneralQueryLower == "moderator")
  30.     {
  31.         $ViewerArray = array_merge($ViewerArray,$ChatterData['chatters']['moderators'],$ChatterData['chatters']['staff'],$ChatterData['chatters']['admins'],$ChatterData['chatters']['global_mods']);
  32.     }
  33.     // Just the normal viewers, catch any other invalid input.
  34.     //else // (empty($_GET["GeneralQuery"]) || $GeneralQueryLower == "follower" || $GeneralQueryLower == null || $GeneralQueryLower != "all" || $GeneralQueryLower != "follower all" || $GeneralQueryLower != "mod")
  35.     //{
  36.         //$ViewerArray = $ChatterData['chatters']['viewers'];
  37.     //}
  38.  
  39.     // Pick a random user from chat after counting the array size.
  40.     $RandomUser = $ViewerArray[mt_rand(0, count($ViewerArray) - 1)];
  41.  
  42.     // Check if following is required.
  43.     if($GeneralQueryLower == "follower" || $GeneralQueryLower == "all" || $GeneralQueryLower == "follower all" || $GeneralQueryLower == "mod" || $GeneralQueryLower == "moderator")
  44.     {
  45.         // Try and pick a few more viewers.
  46.         for($FailedToFindFollower = 1; $FailedToFindFollower <= $TimesToFail; $FailedToFindFollower++)
  47.         {
  48.             $FailedToFindFollower++;
  49.             $RandomUser = $ViewerArray[mt_rand(0, count($ViewerArray) - 1)];
  50.             $FollowerCheckData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/users/$RandomUser/follows/channels/$BroadcasterLower"), true);
  51.             // Found a follower, print the name and how long they've benn following.
  52.             if($FollowerCheckData['created_at'] != null && $GeneralQueryLower != "follower" || $GeneralQueryLower != "all" || $GeneralQueryLower != "follower all" || $GeneralQueryLower != "mod" || $GeneralQueryLower != "moderator")
  53.             {
  54.                 echo $RandomUser . ' (following for ';
  55.                 // Get the date and time to be worked with.
  56.                 $CreatedDateTime = date('Y-m-d H:i:s', strtotime($FollowerCheckData['created_at']));
  57.                 // Call function to print follower date.
  58.                 echo GetDateDifference($CreatedDateTime);
  59.                 echo ').';
  60.                 break;
  61.             }
  62.         }
  63.         // Added a check, in the severe bad luck case no random follower was picked after $TimesToFail trys.
  64.         if($FollowerCheckData['created_at'] == null && $GeneralQueryLower != "follower" || $GeneralQueryLower != "all" || $GeneralQueryLower != "follower all" || $GeneralQueryLower != "mod" || $GeneralQueryLower != "moderator")
  65.         {
  66.             echo 'No follower picked after ';
  67.             echo $TimesToFail;
  68.             echo ' attempts! Please retry.';
  69.         }
  70.     }
  71.     // Just print a user, following not required.
  72.     //else
  73.     //{
  74.         //echo $RandomUser;
  75.     //}
  76. }
  77.  
  78. // Function to get the date difference. Found at StackOerflow, modified for my use.
  79. function GetDateDifference($datetime)
  80. {
  81.     $now = new DateTime;
  82.     $ago = new DateTime($datetime);
  83.     $diff = $now->diff($ago);
  84.     $diff->w = floor($diff->d / 7);
  85.     $diff->d -= $diff->w * 7;
  86.     $string = array
  87.     (
  88.         'y' => 'year',
  89.         'm' => 'month',
  90.         'w' => 'week',
  91.         'd' => 'day',
  92.         'h' => 'hour',
  93.         'i' => 'minute',
  94.         's' => 'second',
  95.     );
  96.     foreach ($string as $k => &$v) {
  97.         if ($diff->$k) {
  98.             $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  99.         }
  100.         else
  101.         {
  102.             unset($string[$k]);
  103.         }
  104.     }
  105.     return $string ? implode(', ', $string) . '' : '';
  106. }
  107. ?>
  108.  
  109. Example:
  110. RandomUser.php?Broadcaster=esl_CSGO&GeneralQuery=
  111. or
  112. RandomUser.php?Broadcaster=esl_CSGO&GeneralQuery=follower
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement