Subversion Repositories yt_downloader

Rev

Rev 7 | Rev 13 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. // ViaThinkSoft YouTube Downloader Functions 2.1
  4. // Revision: 2021-07-15
  5. // Author: Daniel Marschall <www.daniel-marschall.de>
  6. // Licensed under the terms of the Apache 2.0 License
  7.  
  8. // Get API key:   https://console.developers.google.com/apis/credentials
  9. // Test API here: https://developers.google.com/apis-explorer/?hl=de#p/youtube/v3/youtube.playlistItems.list
  10.  
  11. $yt_apikey = null;
  12. $yt_apikey_callback = null;
  13.  
  14. function yt_set_apikey($apikey) {
  15.         global $yt_apikey;
  16.         $yt_apikey = $apikey;
  17. }
  18.  
  19. function yt_set_apikey_callback($apikey_callback) {
  20.         global $yt_apikey_callback;
  21.         $yt_apikey_callback = $apikey_callback;
  22. }
  23.  
  24. function yt_get_apikey() {
  25.         global $yt_apikey, $yt_apikey_callback;
  26.  
  27.         if (!empty($yt_apikey_callback)) {
  28.                 $apikey = call_user_func($yt_apikey_callback);
  29.                 if (!yt_check_apikey_syntax($apikey)) throw new Exception("Invalid API key '$apikey'");
  30.         } else if (!empty($yt_apikey)) {
  31.                 $apikey = $yt_apikey;
  32.                 if (!yt_check_apikey_syntax($apikey)) throw new Exception("Invalid API key '$apikey'");
  33.         } else {
  34.                 throw new Exception("This function requires a YouTube API key.\n");
  35.         }
  36.  
  37.         return $apikey;
  38. }
  39.  
  40. function yt_playlist_items($playlist_id, $maxresults=-1) {
  41.         $out = array();
  42.  
  43.         $next_page_token = '';
  44.  
  45.         do {
  46.                 $cont = file_get_contents('https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId='.urlencode($playlist_id).'&maxResults=50'.(($next_page_token!='') ? '&pageToken='.urlencode($next_page_token) : '').'&key='.urlencode(yt_get_apikey()));
  47.                 if (!$cont) return false;
  48.  
  49.                 $obj = json_decode($cont, true);
  50.                 if (!$obj) return false;
  51.  
  52.                 if (!isset($obj['items'])) return false;
  53.  
  54.                 foreach ($obj['items'] as $item) {
  55.                         if ($item['snippet']['resourceId']['kind'] == 'youtube#video') {
  56.                                 $title    = $item['snippet']['title'];
  57.                                 $video_id = $item['snippet']['resourceId']['videoId'];
  58.                                 $out[] = array($video_id, $title);
  59.                                 if (($maxresults != -1) && ($maxresults == count($out))) return $out;
  60.                         }
  61.                 }
  62.  
  63.                 $next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
  64.         } while ($next_page_token != '');
  65.  
  66.         return $out;
  67. }
  68.  
  69. function yt_get_channel_id($username) {
  70.         $cont = file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&forUsername='.urlencode($username).'&part=id');
  71.         if (!$cont) return false;
  72.  
  73.         $obj = json_decode($cont, true);
  74.         if (!$obj) return false;
  75.  
  76.         if (!isset($obj['items'])) return false;
  77.  
  78.         foreach ($obj['items'] as $item) {
  79.                 if ($item['kind'] == 'youtube#channel') {
  80.                         return $item['id'];
  81.                 }
  82.         }
  83. }
  84.  
  85. function yt_get_channel_id_and_stats($username) {
  86.         $cont = file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&forUsername='.urlencode($username).'&part=id,statistics');
  87.         if (!$cont) return false;
  88.  
  89.         $obj = json_decode($cont, true);
  90.         if (!$obj) return false;
  91.  
  92.         if (!isset($obj['items'])) return false;
  93.  
  94.         foreach ($obj['items'] as $item) {
  95.                 if ($item['kind'] == 'youtube#channel') {
  96.                         return array($item['id'], $item['statistics']);
  97.                 }
  98.         }
  99. }
  100.  
  101. function yt_get_channel_stats($channel_id) {
  102.         $cont = file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&id='.urlencode($channel_id).'&part=statistics');
  103.         if (!$cont) return false;
  104.  
  105.         $obj = json_decode($cont, true);
  106.         if (!$obj) return false;
  107.  
  108.         if (!isset($obj['items'])) return false; //totalResults could be 0
  109.  
  110.         foreach ($obj['items'] as $item) {
  111.                 if ($item['kind'] == 'youtube#channel') {
  112.                         return $item['statistics'];
  113.                 }
  114.         }
  115. }
  116.  
  117. function yt_get_playlist_stats($playlist_id) {
  118.         $cont = file_get_contents('https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&id='.urlencode($playlist_id).'&key='.urlencode(yt_get_apikey()));
  119.         if (!$cont) return false;
  120.  
  121.         $obj = json_decode($cont, true);
  122.         if (!$obj) return false;
  123.  
  124.         if (!isset($obj['items'])) return false;
  125.  
  126.         foreach ($obj['items'] as $item) {
  127.                 if ($item['kind'] == 'youtube#playlist') {
  128.                         return $item['contentDetails'];
  129.                 }
  130.         }
  131. }
  132.  
  133. function yt_channel_items($channel_id, $searchterms='', $maxresults=-1) {
  134.         $out = array();
  135.  
  136.         $next_page_token = '';
  137.  
  138.         do {
  139.                 $cont = file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId='.urlencode($channel_id).(($searchterms!='') ? '&q='.urlencode($searchterms) : '').'&maxResults=50'.(($next_page_token!='') ? '&pageToken='.urlencode($next_page_token) : '').'&key='.urlencode(yt_get_apikey()));
  140.                 if (!$cont) return false;
  141.  
  142.                 $obj = json_decode($cont, true);
  143.                 if (!$obj) return false;
  144.  
  145.                 if (!isset($obj['items'])) return false;
  146.  
  147.                 foreach ($obj['items'] as $item) {
  148.                         if ($item['id']['kind'] == 'youtube#video') {
  149.                                 $title    = $item['snippet']['title'];
  150.                                 $video_id = $item['id']['videoId'];
  151.                                 $out[] = array($video_id, $title);
  152.                                 if (($maxresults != -1) && ($maxresults == count($out))) return $out;
  153.                         }
  154.                 }
  155.  
  156.                 $next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
  157.         } while ($next_page_token != '');
  158.  
  159.         return $out;
  160. }
  161.  
  162. // Acceptable order values are: date, rating, relevance(default), title, videoCount, viewCount
  163. function yt_search_items($searchterms, $order='', $maxresults=-1) {
  164.         $out = array();
  165.  
  166.         $next_page_token = '';
  167.  
  168.         do {
  169.                 $cont = file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&q='.urlencode($searchterms).(($order!='') ? '&order='.urlencode($order) : '').'&maxResults=50'.(($next_page_token!='') ? '&pageToken='.urlencode($next_page_token) : '').'&key='.urlencode(yt_get_apikey()));
  170.                 if (!$cont) return false;
  171.  
  172.                 $obj = json_decode($cont, true);
  173.                 if (!$obj) return false;
  174.  
  175.                 if (!isset($obj['items'])) return false;
  176.  
  177.                 foreach ($obj['items'] as $item) {
  178.                         if ($item['id']['kind'] == 'youtube#video') {
  179.                                 $title    = $item['snippet']['title'];
  180.                                 $video_id = $item['id']['videoId'];
  181.                                 $out[] = array($video_id, $title);
  182.                                 if (($maxresults != -1) && ($maxresults == count($out))) return $out;
  183.                         }
  184.                 }
  185.  
  186.                 $next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
  187.         } while ($next_page_token != '');
  188.  
  189.         return $out;
  190. }
  191.  
  192. function getVideoIDFromURL($url) {
  193.         // Extract video ID from the URL
  194.  
  195.         $vid = false;
  196.         $m = null;
  197.  
  198.         # Usual format
  199.         if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/watch(.*)(/|&|\\?)v=([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
  200.                 $vid = $m[4];
  201.         }
  202.  
  203.         # Short format
  204.         if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtu\\.be/([a-zA-Z0-9_-]{11}))@ismU", $url, $m))) {
  205.                 $vid = $m[2];
  206.         }
  207.  
  208.         # YouTube "Shorts"
  209.         if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/shorts/([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
  210.                 $vid = $m[2];
  211.         }
  212.  
  213.         return $vid;
  214. }
  215.  
  216. function getPlaylistIDFromURL($url) {
  217.         $pid = false;
  218.  
  219.         # Usual format
  220.         $m = null;
  221.         if (($pid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(.*)(/|&|\\?)list=(.+)&@ismU", $url.'&', $m))) {
  222.                 $pid = $m[4];
  223.         }
  224.  
  225.         return $pid;
  226. }
  227.  
  228. function yt_check_apikey_syntax($apikey) {
  229.         return preg_match('@^[a-zA-Z0-9]{39}$@', $apikey);
  230. }
  231.  
  232. function yt_check_video_id($video_id) {
  233.         return preg_match('@^[a-zA-Z0-9\-_]{11}$@', $video_id);
  234. }
  235.  
  236. // Examples:
  237. //yt_set_apikey(trim(file_get_contents(__DIR__ . '/.yt_api_key')));
  238. //print_r(yt_playlist_items('PL9GbGAd-gY1pyxZJIX5MOdYdRbdweVAID'));
  239. //print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA'));
  240. //print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA', 'Knight'));
  241. //print_r(yt_search_items('Wesley Willis', 'date', 10));
  242.