Advertisement
kyroskoh

Twitch CustomAPI - Follower Check

May 7th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.11 KB | None | 0 0
  1. <?php
  2. // Convert Get data to lowercase.
  3. $BroadcasterLower = strtolower($_GET["Broadcaster"]);
  4. $ToLower = strtolower($_GET["To"]);
  5. // Get and decode Follower data.
  6. $FollowerCheckData = json_decode(@file_get_contents("https://api.twitch.tv/kraken/users/$ToLower/follows/channels/$BroadcasterLower"), true);
  7. // Get and decode the correct capitalization of the user in question.
  8. $FollowerCheckName = json_decode(@file_get_contents("https://api.twitch.tv/kraken/channels/$ToLower"), true);
  9. // Get the date and time to be worked with.
  10. $CreatedDateTime = date('Y-m-d H:i:s', strtotime($FollowerCheckData['created_at']));
  11. // No name was entered to see follower relationship with.
  12. if (empty($_GET["To"]))
  13. {
  14.         echo 'You to need to specify a user to see if they\'re following!';
  15.         die();
  16. }
  17. // No such Twitch user exists, yet.
  18. if($FollowerCheckName['display_name'] == null)
  19. {
  20.     echo 'Sorry, ' . htmlspecialchars($_GET["To"]) . ' isn\'t a valid Twitch Account!';
  21.     die();
  22. }
  23. // User isn't following.
  24. if($FollowerCheckData['created_at'] == null)
  25. {
  26.     echo 'Sorry, ' . $FollowerCheckName["display_name"] . ' isn\'t following ' . htmlspecialchars($_GET["Broadcaster"]) . '... Bastard';
  27.     die();
  28. }
  29. // User is following :D!
  30. else
  31. {
  32.     echo 'Yeah! ' . $FollowerCheckName["display_name"] . ' has been following ' . htmlspecialchars($_GET["Broadcaster"]) . ' for ';
  33.     echo GetDateDifference($CreatedDateTime);
  34.     echo ' <3';
  35. }
  36. // Function to get the date difference, grabbed from SO, modified for this use.
  37. function GetDateDifference($datetime)
  38. {
  39.     $now = new DateTime;
  40.     $ago = new DateTime($datetime);
  41.     $diff = $now->diff($ago);
  42.  
  43.     $diff->w = floor($diff->d / 7);
  44.     $diff->d -= $diff->w * 7;
  45.  
  46.     $string = array
  47.     (
  48.         'y' => 'year',
  49.         'm' => 'month',
  50.         'w' => 'week',
  51.         'd' => 'day',
  52.         'h' => 'hour',
  53.         'i' => 'minute',
  54.         's' => 'second',
  55.     );
  56.     foreach ($string as $k => &$v) {
  57.         if ($diff->$k) {
  58.             $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  59.         }
  60.         else
  61.         {
  62.             unset($string[$k]);
  63.         }
  64.     }
  65.     return $string ? implode(', ', $string) . '' : '';
  66. }
  67. ?>
  68.  
  69. Example: FollowerCheck.php?Broadcaster=TwitchBroadcaster&To=TwitchUser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement