Subversion Repositories yt_downloader

Rev

Rev 12 | Rev 14 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12 Rev 13
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
// ViaThinkSoft YouTube Downloader Functions 2.1
3
// ViaThinkSoft YouTube Downloader Functions 2.1
4
// Revision: 2021-07-15
4
// Revision: 2022-02-06
5
// Author: Daniel Marschall <www.daniel-marschall.de>
5
// Author: Daniel Marschall <www.daniel-marschall.de>
6
// Licensed under the terms of the Apache 2.0 License
6
// Licensed under the terms of the Apache 2.0 License
7
 
7
 
8
// Get API key:   https://console.developers.google.com/apis/credentials
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
9
// Test API here: https://developers.google.com/apis-explorer/?hl=de#p/youtube/v3/youtube.playlistItems.list
Line 231... Line 231...
231
 
231
 
232
function yt_check_video_id($video_id) {
232
function yt_check_video_id($video_id) {
233
        return preg_match('@^[a-zA-Z0-9\-_]{11}$@', $video_id);
233
        return preg_match('@^[a-zA-Z0-9\-_]{11}$@', $video_id);
234
}
234
}
235
 
235
 
-
 
236
function yt_get_channel_id_from_custom_url($custom_url) {
-
 
237
        // TODO: is there any API possibility??? API only accepts username and id ?!
-
 
238
 
-
 
239
        // https://www.youtube.com/c/SASASMR
-
 
240
        // <link rel="canonical" href="https://www.youtube.com/channel/UCp4LfMtDfoa29kTlLnqQ5Mg">
-
 
241
        // https://www.youtube.com/impaulsive
-
 
242
        // <link rel="canonical" href="https://www.youtube.com/channel/UCGeBogGDZ9W3dsGx-mWQGJA">
-
 
243
 
-
 
244
        $cont = file_get_contents($custom_url);
-
 
245
        if ($cont === false) {
-
 
246
                throw new Exception("Cannot open $custom_url using file_get_contents.");
-
 
247
        }
-
 
248
        if (!preg_match('@<link rel="canonical" href="https://www.youtube.com/channel/([^"]+)">@ismU', $cont, $m)) {
-
 
249
                return false;
-
 
250
        }
-
 
251
 
-
 
252
        return $m[1];
-
 
253
}
-
 
254
 
-
 
255
function yt_get_channel_id_from_url($channel_url) {
-
 
256
        $m = null;
-
 
257
        if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/user/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
-
 
258
                // Username (deprecated feature. Not every channel has a username associated with it)
-
 
259
                // https://www.youtube.com/user/USERNAME
-
 
260
                $username = $m[2];
-
 
261
                $channel_id = yt_get_channel_id($username);
-
 
262
                return $channel_id;
-
 
263
        } else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/channel/(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
-
 
264
                // Real channel ID
-
 
265
                // https://www.youtube.com/channel/ID
-
 
266
                $channel_id = $m[2];
-
 
267
                return $channel_id;
-
 
268
        } else if (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(c/){0,1}(.*)(/|&|\\?)@ismU", $channel_url.'&', $m)) {
-
 
269
                // Channel custom URL
-
 
270
                // https://www.youtube.com/NAME or https://www.youtube.com/c/NAME
-
 
271
                return yt_get_channel_id_from_custom_url($channel_url);
-
 
272
        } else {
-
 
273
                return false;
-
 
274
        }
-
 
275
}
-
 
276
 
236
// Examples:
277
// Examples:
237
//yt_set_apikey(trim(file_get_contents(__DIR__ . '/.yt_api_key')));
278
//yt_set_apikey(trim(file_get_contents(__DIR__ . '/.yt_api_key')));
238
//print_r(yt_playlist_items('PL9GbGAd-gY1pyxZJIX5MOdYdRbdweVAID'));
279
//print_r(yt_playlist_items('PL9GbGAd-gY1pyxZJIX5MOdYdRbdweVAID'));
239
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA'));
280
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA'));
240
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA', 'Knight'));
281
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA', 'Knight'));