My extent of anything programming related is next to nothing. I basically only know VERY basic PHP, which I kind of picked up in order to make my own custom APIs for use with NightBot. This is the entire source of my UpTime command.
<?php // No name was entered for the broadcaster. if (empty($_GET["Broadcaster"])) { echo 'Channel broadcaster field is empty!'; die(); } // Set names to lowercase for URL injection. $BroadcasterLower = strtolower($_GET["Broadcaster"]); // Set timezone to match Twitch servers for JSON date/ time. date_default_timezone_set("Greenwich"); // Inject lowercase name. $ch = curl_init("https://api.twitch.tv/kraken/streams/" . $BroadcasterLower); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); // Decode the above JSON data. $StreamingData = json_decode($result, true); // Get a workable date and time out of the provided JSON. $UpTimeDateTime = date('Y-m-d H:i:s', strtotime($StreamingData['stream']['created_at']));
// Stream offline if($StreamingData['stream'] == null) { // Try to get the last broadcast via VOD. AKA past broadcasts. // Totally copy and pasted this from a few lines above... But it works. $ch = curl_init("https://api.twitch.tv/kraken/channels/" . $BroadcasterLower . "/videos?broadcasts=true&limit=1"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); // Decode the above JSON data. $StreamVideos = json_decode($result, true); if($StreamVideos['videos']) { // Get the start time of the last broadcast, add the length of broadcast (in seconds) to get time broadcast ended. $StreamEndPoint = date('Y-m-d H:i:s', strtotime($StreamVideos['videos'][0]['recorded_at']) + $StreamVideos['videos'][0]['length']); } // User doesn't have VOD enabled. We can only guess at this point now. else { // Totally copy and pasted this from a few lines above... But it works. $ch = curl_init("https://api.twitch.tv/kraken/channels/" . $BroadcasterLower . "/"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); // Decode the above JSON data. $StreamData = json_decode($result, true); if($StreamData['display_name'] == null) { echo 'Sorry, the broadcaster name is invalid!'; die(); } $OfflineGuess = date('Y-m-d H:i:s', strtotime($StreamData['updated_at'])); } // Print out the offline message. echo 'Sorry, ' . htmlspecialchars($_GET["Broadcaster"]) . ' has not been streaming for the past '; // Print out the precise time stream has been offline. if($StreamVideos['videos']) { echo GetDateDifference($StreamEndPoint); } // No precise time available. Guessing game it is. else { echo GetDateDifference($OfflineGuess); } echo '. Check back later or follow and enable notifications to know when ' . htmlspecialchars($_GET["Broadcaster"]) . ' streams next!'; die(); } // Stream online. else { // User is streaming. Print out info. echo $StreamingData['stream']['channel']['display_name'] . ' has been streaming ' . $StreamingData['stream']['channel']['game'] . ' for the past '; echo GetDateDifference($UpTimeDateTime); echo '.'; } // Function to get the date difference. 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) . '' : ''; } ?>
Or I also have it on pastebin since I share all of my APIs on the NightBot community forums. Pastebin source for UpTime.php