Advertisement
kyroskoh

Twitch CustomAPI - YouTube Chan's Subs, Views, Videos, Last

May 7th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.65 KB | None | 0 0
  1. <?php
  2. // Available commands (link, subs, views, videos, last, last3, date, datetime, datediff, datetimediff)
  3. // obtain your own API key at
  4. // https://console.developers.google.com
  5. $MyKey = xxxxx_xxxxx_xxxxx;
  6. // Convert the GET data into variables.
  7. $YouTubeID = $_GET["Channel_ID"];
  8. $Command = $_GET["Command"];
  9. $User = $_GET["User"];
  10.  
  11.  
  12. // Get the channel ID and Upload ID from the YouTube name inputted
  13. $IDs = json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=$YouTubeID&key=$MyKey"), true);
  14. if($IDs['items'][0]['id'] == null)
  15. {
  16.     echo 'No such YouTube channel exists.';
  17.     die();
  18. }
  19. // Set variables.
  20. $Channel_ID = $IDs['items'][0]['id'];
  21. $Uploads_ID = $IDs['items'][0]['contentDetails']['relatedPlaylists']['uploads'];
  22.  
  23. // Get the Subscribers, views and videos.
  24. $ChannelInfo = json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=contentDetails%2C+statistics%2Csnippet&id=$Channel_ID&key=$MyKey"), true);
  25. // Set variables.
  26. $CorrectName = $ChannelInfo['items'][0]['snippet']['title'];
  27.  
  28. $ViewSubsDenied = $ChannelInfo['items'][0]['statistics']['hiddenSubscriberCount'];
  29. $TotalSubs = $ChannelInfo['items'][0]['statistics']['subscriberCount'];
  30. $NextSub = number_format($TotalSubs) + 1;
  31. $TotalViews = $ChannelInfo['items'][0]['statistics']['viewCount'];
  32. $TotalVideos = $ChannelInfo['items'][0]['statistics']['videoCount'];
  33.  
  34. $CreatedDate = date('Y-m-d', strtotime($ChannelInfo['items'][0]['snippet']['publishedAt']));
  35. $CreatedDateTime = date('Y-m-d H:i:s', strtotime($ChannelInfo['items'][0]['snippet']['publishedAt']));
  36.  
  37. // Get the last3 video uploaded.
  38. $Last3Videos = json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?playlistId=$Uploads_ID&key=$MyKey&fields=items&part=snippet&maxResults=3"), true);
  39. // Get the last video uploaded.
  40. $LastVideo = json_decode(@file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?playlistId=$Uploads_ID&key=$MyKey&fields=items&part=snippet&maxResults=1"), true);
  41. // Set variable.
  42. $Last3VideosURL = $Last3Videos['items'][0]['snippet']['resourceId']['videoId'];
  43. $Last3VideosTitle = $Last3Videos['items'][0]['snippet']['title'];
  44. $Last2VideosURL = $Last3Videos['items'][1]['snippet']['resourceId']['videoId'];
  45. $Last2VideosTitle = $Last3Videos['items'][1]['snippet']['title'];
  46. $Last1VideosURL = $Last3Videos['items'][2]['snippet']['resourceId']['videoId'];
  47. $Last1VideosTitle = $Last3Videos['items'][2]['snippet']['title'];
  48. $LastVideoURL = $LastVideo['items'][0]['snippet']['resourceId']['videoId'];
  49. $LastVideoTitle = $LastVideo['items'][0]['snippet']['title'];
  50.  
  51. // Figure out what to display, and display it.
  52. if($Command == "link")
  53. {
  54.     echo $User . ": You can be " . $CorrectName . "'s NEXT " . number_format($NextSub) . " YouTube Subscriber right now! Subscribe to " . $CorrectName . "'s YouTube URL: https://www.youtube.com/channel/" . $YouTubeID . "?sub_confirmation=1";
  55. }
  56. if($Command == "subs")
  57. {  
  58.     if($ViewSubsDenied == "true")
  59.     {
  60.         echo $CorrectName . ' has hidden their number of subscribers!';
  61.     }
  62.     else
  63.     {
  64.         echo $CorrectName . ' has ' . number_format($TotalSubs) . ' YouTube Subscribers!';
  65.     }
  66. }
  67. if($Command == "views")
  68. {
  69.     echo $CorrectName . ' has a total of ' . number_format($TotalViews) . ' YouTube Videos Views.';
  70. }
  71. if($Command == "videos")
  72. {
  73.     echo $CorrectName . ' has ' . number_format($TotalVideos) . ' YouTube videos.';
  74. }
  75. if($Command == "last3")
  76. {
  77.     echo $CorrectName . "'s last 3 YouTube videos: ($Last3VideosTitle) youtu.be/" . $Last3VideosURL . " | ($Last2VideosTitle) youtu.be/" . $Last2VideosURL . " | ($Last1VideosTitle) youtu.be/" . $Last1VideosURL;
  78. }
  79. if($Command == "last")
  80. {
  81.     echo $CorrectName . "'s YouTube latest video: ($LastVideoTitle) youtu.be/" . $LastVideoURL;
  82. }
  83. if($Command == "date" || $Command == "datetime" || $Command == "datediff" || $Command == "datetimediff")
  84. {
  85.     echo $CorrectName . ' created their YouTube account on ';
  86.     if($Command == "date" || $Command == "datediff")
  87.     {
  88.         echo $CreatedDate;
  89.         if($Command == "datediff")
  90.         {
  91.             echo ' (';
  92.             echo GetDateDifference($CreatedDate, $Command);
  93.             echo ')';
  94.         }
  95.     }
  96.     if($Command == "datetime" || $Command == "datetimediff")
  97.     {
  98.         echo $CreatedDateTime;
  99.         if($Command == "datetimediff")
  100.         {
  101.             echo ' (';
  102.             echo GetDateDifference($CreatedDateTime, $Command);
  103.             echo ')';
  104.         }
  105.     }
  106. }
  107.  
  108. // Function to get the date difference.
  109. function GetDateDifference($datetime, $Command)
  110. {
  111.     $now = new DateTime;
  112.     $ago = new DateTime($datetime);
  113.     $diff = $now->diff($ago);
  114.  
  115.     $diff->w = floor($diff->d / 7);
  116.     $diff->d -= $diff->w * 7;
  117.     if($Command == "datetimediff")
  118.     {
  119.         $string = array
  120.         (
  121.             'y' => 'year',
  122.             'm' => 'month',
  123.             'w' => 'week',
  124.             'd' => 'day',
  125.             'h' => 'hour',
  126.             'i' => 'minute',
  127.             's' => 'second',
  128.         );
  129.     }
  130.     if($Command == "datediff")
  131.     {
  132.         $string = array
  133.         (
  134.             'y' => 'year',
  135.             'm' => 'month',
  136.             'w' => 'week',
  137.             'd' => 'day',
  138.         );
  139.     }
  140.     foreach ($string as $k => &$v) {
  141.         if ($diff->$k) {
  142.             $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
  143.         }
  144.         else
  145.         {
  146.             unset($string[$k]);
  147.         }
  148.     }
  149.     return $string ? implode(', ', $string) . '' : '';
  150. }
  151. ?>
  152.  
  153. Example:
  154. Youtube.php?Channel_ID=YT_Channel_ID&Command=subs
  155. or
  156. Youtube.php?Channel_ID=YT_Channel_ID&Command=subs&User=username
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement