Subversion Repositories yt_downloader

Rev

Rev 2 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
// ViaThinkSoft YouTube Downloader Functions 2.1
7 daniel-mar 4
// Revision: 2021-05-09
2 daniel-mar 5
// Author: Daniel Marschall <www.daniel-marschall.de>
7 daniel-mar 6
// Licensed under the terms of the Apache 2.0 License
2 daniel-mar 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
                foreach ($obj['items'] as $item) {
53
                        if ($item['snippet']['resourceId']['kind'] == 'youtube#video') {
54
                                $title    = $item['snippet']['title'];
55
                                $video_id = $item['snippet']['resourceId']['videoId'];
56
                                $out[] = array($video_id, $title);
57
                                if (($maxresults != -1) && ($maxresults == count($out))) return $out;
58
                        }
59
                }
60
 
61
                $next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
62
        } while ($next_page_token != '');
63
 
64
        return $out;
65
}
66
 
67
function yt_get_channel_id($username) {
68
        $cont = file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&forUsername='.urlencode($username).'&part=id');
69
        if (!$cont) return false;
70
 
71
        $obj = json_decode($cont, true);
72
        if (!$obj) return false;
73
 
74
        foreach ($obj['items'] as $item) {
75
                if ($item['kind'] == 'youtube#channel') {
76
                        return $item['id'];
77
                }
78
        }
79
}
80
 
81
function yt_get_channel_id_and_stats($username) {
82
        $cont = file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&forUsername='.urlencode($username).'&part=id,statistics');
83
        if (!$cont) return false;
84
 
85
        $obj = json_decode($cont, true);
86
        if (!$obj) return false;
87
 
88
        foreach ($obj['items'] as $item) {
89
                if ($item['kind'] == 'youtube#channel') {
90
                        return array($item['id'], $item['statistics']);
91
                }
92
        }
93
}
94
 
95
function yt_get_channel_stats($channel_id) {
96
        $cont = file_get_contents('https://www.googleapis.com/youtube/v3/channels?key='.urlencode(yt_get_apikey()).'&id='.urlencode($channel_id).'&part=statistics');
97
        if (!$cont) return false;
98
 
99
        $obj = json_decode($cont, true);
100
        if (!$obj) return false;
101
 
102
        foreach ($obj['items'] as $item) {
103
                if ($item['kind'] == 'youtube#channel') {
104
                        return $item['statistics'];
105
                }
106
        }
107
}
108
 
109
function yt_get_playlist_stats($playlist_id) {
110
        $cont = file_get_contents('https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&id='.urlencode($playlist_id).'&key='.urlencode(yt_get_apikey()));
111
        if (!$cont) return false;
112
 
113
        $obj = json_decode($cont, true);
114
        if (!$obj) return false;
115
 
116
        foreach ($obj['items'] as $item) {
117
                if ($item['kind'] == 'youtube#playlist') {
118
                        return $item['contentDetails'];
119
                }
120
        }
121
}
122
 
123
function yt_channel_items($channel_id, $searchterms='', $maxresults=-1) {
124
        $out = array();
125
 
126
        $next_page_token = '';
127
 
128
        do {
129
                $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()));
130
                if (!$cont) return false;
131
 
132
                $obj = json_decode($cont, true);
133
                if (!$obj) return false;
134
 
135
                foreach ($obj['items'] as $item) {
136
                        if ($item['id']['kind'] == 'youtube#video') {
137
                                $title    = $item['snippet']['title'];
138
                                $video_id = $item['id']['videoId'];
139
                                $out[] = array($video_id, $title);
140
                                if (($maxresults != -1) && ($maxresults == count($out))) return $out;
141
                        }
142
                }
143
 
144
                $next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
145
        } while ($next_page_token != '');
146
 
147
        return $out;
148
}
149
 
150
// Acceptable order values are: date, rating, relevance(default), title, videoCount, viewCount
151
function yt_search_items($searchterms, $order='', $maxresults=-1) {
152
        $out = array();
153
 
154
        $next_page_token = '';
155
 
156
        do {
157
                $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()));
158
                if (!$cont) return false;
159
 
160
                $obj = json_decode($cont, true);
161
                if (!$obj) return false;
162
 
163
                foreach ($obj['items'] as $item) {
164
                        if ($item['id']['kind'] == 'youtube#video') {
165
                                $title    = $item['snippet']['title'];
166
                                $video_id = $item['id']['videoId'];
167
                                $out[] = array($video_id, $title);
168
                                if (($maxresults != -1) && ($maxresults == count($out))) return $out;
169
                        }
170
                }
171
 
172
                $next_page_token = isset($obj['nextPageToken']) ? $obj['nextPageToken'] : '';
173
        } while ($next_page_token != '');
174
 
175
        return $out;
176
}
177
 
178
function getVideoIDFromURL($url) {
179
        // Extract video ID from the URL
180
 
181
        $vid = false;
7 daniel-mar 182
        $m = null;
2 daniel-mar 183
 
184
        # Usual format
7 daniel-mar 185
        if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/watch(.*)(&|\\?)v=([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
2 daniel-mar 186
                $vid = $m[4];
187
        }
188
 
189
        # Short format
7 daniel-mar 190
        if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtu\\.be/([a-zA-Z0-9_-]{11}))@ismU", $url, $m))) {
2 daniel-mar 191
                $vid = $m[2];
192
        }
193
 
7 daniel-mar 194
        # YouTube "Shorts"
195
        if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/shorts/([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
196
                $vid = $m[2];
197
        }
198
 
2 daniel-mar 199
        return $vid;
200
}
201
 
202
function getPlaylistIDFromURL($url) {
203
        $pid = false;
204
 
205
        # Usual format
7 daniel-mar 206
        $m = null;
2 daniel-mar 207
        if (($pid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(.*)(&|\\?)list=(.+)&@ismU", $url.'&', $m))) {
208
                $pid = $m[4];
209
        }
210
 
211
        return $pid;
212
}
213
 
214
function yt_check_apikey_syntax($apikey) {
215
        return preg_match('@^[a-zA-Z0-9]{39}$@', $apikey);
216
}
217
 
218
function yt_check_video_id($video_id) {
219
        return preg_match('@^[a-zA-Z0-9\-_]{11}$@', $video_id);
220
}
221
 
222
// Examples:
223
//yt_set_apikey(trim(file_get_contents(__DIR__ . '/.yt_api_key')));
224
//print_r(yt_playlist_items('PL9GbGAd-gY1pyxZJIX5MOdYdRbdweVAID'));
225
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA'));
226
//print_r(yt_channel_items('UCjPjcMEAN64opOoNQE9GykA', 'Knight'));
227
//print_r(yt_search_items('Wesley Willis', 'date', 10));