Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Set he times to fail when looking for a random follower
- $TimesToFail = 10;
- // Set timezone to match Twitch servers
- date_default_timezone_set("Greenwich");
- // Set strings to lowercase... Easier that way
- $GeneralQueryLower = strtolower($_GET["GeneralQuery"]);
- $BroadcasterLower = strtolower($_GET["Broadcaster"]);
- // Pull & decode broadcaster chatter array(s).
- $ChatterData = json_decode(@file_get_contents("https://tmi.twitch.tv/group/user/$BroadcasterLower/chatters"), true);
- // Call the Random Viewer function.
- RandomViewer($GeneralQueryLower, $BroadcasterLower, $ChatterData, $TimesToFail);
- function RandomViewer($GeneralQueryLower, $BroadcasterLower, $ChatterData, $TimesToFail)
- {
- // Set an empty helper array.
- $ViewerArray = array();
- // Check if it is a valid $GeneralQueryLower.
- if($GeneralQueryLower != "follower" && $GeneralQueryLower != "all" && $GeneralQueryLower != "follower all" && $GeneralQueryLower != "mod" && $GeneralQueryLower != "moderator")
- {
- echo 'Invalid Request. Please indicate with: follower/mod/all...';
- }
- // Everyone in the stream.
- if($GeneralQueryLower == "all" || $GeneralQueryLower == "follower all")
- {
- $ViewerArray = array_merge($ViewerArray,$ChatterData['chatters']['moderators'],$ChatterData['chatters']['staff'],$ChatterData['chatters']['admins'],$ChatterData['chatters']['global_mods'],$ChatterData['chatters']['viewers']);
- }
- // Just the mods, well, anyone above the average viewer.
- if($GeneralQueryLower == "mod" || $GeneralQueryLower == "moderator")
- {
- $ViewerArray = array_merge($ViewerArray,$ChatterData['chatters']['moderators'],$ChatterData['chatters']['staff'],$ChatterData['chatters']['admins'],$ChatterData['chatters']['global_mods']);
- }
- // Just the normal viewers, catch any other invalid input.
- //else // (empty($_GET["GeneralQuery"]) || $GeneralQueryLower == "follower" || $GeneralQueryLower == null || $GeneralQueryLower != "all" || $GeneralQueryLower != "follower all" || $GeneralQueryLower != "mod")
- //{
- //$ViewerArray = $ChatterData['chatters']['viewers'];
- //}
- // Pick a random user from chat after counting the array size.
- $RandomUser = $ViewerArray[mt_rand(0, count($ViewerArray) - 1)];
- // Check if following is required.
- if($GeneralQueryLower == "follower" || $GeneralQueryLower == "all" || $GeneralQueryLower == "follower all" || $GeneralQueryLower == "mod" || $GeneralQueryLower == "moderator")
- {
- // Try and pick a few more viewers.
- for($FailedToFindFollower = 1; $FailedToFindFollower <= $TimesToFail; $FailedToFindFollower++)
- {
- $FailedToFindFollower++;
- $RandomUser = $ViewerArray[mt_rand(0, count($ViewerArray) - 1)];
- $FollowerCheckData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/users/$RandomUser/follows/channels/$BroadcasterLower"), true);
- // Found a follower, print the name and how long they've benn following.
- if($FollowerCheckData['created_at'] != null && $GeneralQueryLower != "follower" || $GeneralQueryLower != "all" || $GeneralQueryLower != "follower all" || $GeneralQueryLower != "mod" || $GeneralQueryLower != "moderator")
- {
- echo $RandomUser . ' (following for ';
- // Get the date and time to be worked with.
- $CreatedDateTime = date('Y-m-d H:i:s', strtotime($FollowerCheckData['created_at']));
- // Call function to print follower date.
- echo GetDateDifference($CreatedDateTime);
- echo ').';
- break;
- }
- }
- // Added a check, in the severe bad luck case no random follower was picked after $TimesToFail trys.
- if($FollowerCheckData['created_at'] == null && $GeneralQueryLower != "follower" || $GeneralQueryLower != "all" || $GeneralQueryLower != "follower all" || $GeneralQueryLower != "mod" || $GeneralQueryLower != "moderator")
- {
- echo 'No follower picked after ';
- echo $TimesToFail;
- echo ' attempts! Please retry.';
- }
- }
- // Just print a user, following not required.
- //else
- //{
- //echo $RandomUser;
- //}
- }
- // Function to get the date difference. Found at StackOerflow, modified for my use.
- function GetDateDifference($datetime)
- {
- $now = new DateTime;
- $ago = new DateTime($datetime);
- $diff = $now->diff($ago);
- $diff->w = floor($diff->d / 7);
- $diff->d -= $diff->w * 7;
- $string = array
- (
- 'y' => 'year',
- 'm' => 'month',
- 'w' => 'week',
- 'd' => 'day',
- 'h' => 'hour',
- 'i' => 'minute',
- 's' => 'second',
- );
- foreach ($string as $k => &$v) {
- if ($diff->$k) {
- $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
- }
- else
- {
- unset($string[$k]);
- }
- }
- return $string ? implode(', ', $string) . '' : '';
- }
- ?>
- Example:
- RandomUser.php?Broadcaster=esl_CSGO&GeneralQuery=
- or
- RandomUser.php?Broadcaster=esl_CSGO&GeneralQuery=follower
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement