Subversion Repositories yt_downloader

Rev

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

Rev 7 Rev 8
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-05-09
4
// Revision: 2021-07-15
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 47... Line 47...
47
                if (!$cont) return false;
47
                if (!$cont) return false;
48
 
48
 
49
                $obj = json_decode($cont, true);
49
                $obj = json_decode($cont, true);
50
                if (!$obj) return false;
50
                if (!$obj) return false;
51
 
51
 
-
 
52
                if (!isset($obj['items'])) return false;
-
 
53
 
52
                foreach ($obj['items'] as $item) {
54
                foreach ($obj['items'] as $item) {
53
                        if ($item['snippet']['resourceId']['kind'] == 'youtube#video') {
55
                        if ($item['snippet']['resourceId']['kind'] == 'youtube#video') {
54
                                $title    = $item['snippet']['title'];
56
                                $title    = $item['snippet']['title'];
55
                                $video_id = $item['snippet']['resourceId']['videoId'];
57
                                $video_id = $item['snippet']['resourceId']['videoId'];
56
                                $out[] = array($video_id, $title);
58
                                $out[] = array($video_id, $title);
Line 69... Line 71...
69
        if (!$cont) return false;
71
        if (!$cont) return false;
70
 
72
 
71
        $obj = json_decode($cont, true);
73
        $obj = json_decode($cont, true);
72
        if (!$obj) return false;
74
        if (!$obj) return false;
73
 
75
 
-
 
76
        if (!isset($obj['items'])) return false;
-
 
77
 
74
        foreach ($obj['items'] as $item) {
78
        foreach ($obj['items'] as $item) {
75
                if ($item['kind'] == 'youtube#channel') {
79
                if ($item['kind'] == 'youtube#channel') {
76
                        return $item['id'];
80
                        return $item['id'];
77
                }
81
                }
78
        }
82
        }
Line 83... Line 87...
83
        if (!$cont) return false;
87
        if (!$cont) return false;
84
 
88
 
85
        $obj = json_decode($cont, true);
89
        $obj = json_decode($cont, true);
86
        if (!$obj) return false;
90
        if (!$obj) return false;
87
 
91
 
-
 
92
        if (!isset($obj['items'])) return false;
-
 
93
 
88
        foreach ($obj['items'] as $item) {
94
        foreach ($obj['items'] as $item) {
89
                if ($item['kind'] == 'youtube#channel') {
95
                if ($item['kind'] == 'youtube#channel') {
90
                        return array($item['id'], $item['statistics']);
96
                        return array($item['id'], $item['statistics']);
91
                }
97
                }
92
        }
98
        }
Line 97... Line 103...
97
        if (!$cont) return false;
103
        if (!$cont) return false;
98
 
104
 
99
        $obj = json_decode($cont, true);
105
        $obj = json_decode($cont, true);
100
        if (!$obj) return false;
106
        if (!$obj) return false;
101
 
107
 
-
 
108
        if (!isset($obj['items'])) return false; //totalResults could be 0
-
 
109
 
102
        foreach ($obj['items'] as $item) {
110
        foreach ($obj['items'] as $item) {
103
                if ($item['kind'] == 'youtube#channel') {
111
                if ($item['kind'] == 'youtube#channel') {
104
                        return $item['statistics'];
112
                        return $item['statistics'];
105
                }
113
                }
106
        }
114
        }
Line 111... Line 119...
111
        if (!$cont) return false;
119
        if (!$cont) return false;
112
 
120
 
113
        $obj = json_decode($cont, true);
121
        $obj = json_decode($cont, true);
114
        if (!$obj) return false;
122
        if (!$obj) return false;
115
 
123
 
-
 
124
        if (!isset($obj['items'])) return false;
-
 
125
 
116
        foreach ($obj['items'] as $item) {
126
        foreach ($obj['items'] as $item) {
117
                if ($item['kind'] == 'youtube#playlist') {
127
                if ($item['kind'] == 'youtube#playlist') {
118
                        return $item['contentDetails'];
128
                        return $item['contentDetails'];
119
                }
129
                }
120
        }
130
        }
Line 130... Line 140...
130
                if (!$cont) return false;
140
                if (!$cont) return false;
131
 
141
 
132
                $obj = json_decode($cont, true);
142
                $obj = json_decode($cont, true);
133
                if (!$obj) return false;
143
                if (!$obj) return false;
134
 
144
 
-
 
145
                if (!isset($obj['items'])) return false;
-
 
146
 
135
                foreach ($obj['items'] as $item) {
147
                foreach ($obj['items'] as $item) {
136
                        if ($item['id']['kind'] == 'youtube#video') {
148
                        if ($item['id']['kind'] == 'youtube#video') {
137
                                $title    = $item['snippet']['title'];
149
                                $title    = $item['snippet']['title'];
138
                                $video_id = $item['id']['videoId'];
150
                                $video_id = $item['id']['videoId'];
139
                                $out[] = array($video_id, $title);
151
                                $out[] = array($video_id, $title);
Line 158... Line 170...
158
                if (!$cont) return false;
170
                if (!$cont) return false;
159
 
171
 
160
                $obj = json_decode($cont, true);
172
                $obj = json_decode($cont, true);
161
                if (!$obj) return false;
173
                if (!$obj) return false;
162
 
174
 
-
 
175
                if (!isset($obj['items'])) return false;
-
 
176
 
163
                foreach ($obj['items'] as $item) {
177
                foreach ($obj['items'] as $item) {
164
                        if ($item['id']['kind'] == 'youtube#video') {
178
                        if ($item['id']['kind'] == 'youtube#video') {
165
                                $title    = $item['snippet']['title'];
179
                                $title    = $item['snippet']['title'];
166
                                $video_id = $item['id']['videoId'];
180
                                $video_id = $item['id']['videoId'];
167
                                $out[] = array($video_id, $title);
181
                                $out[] = array($video_id, $title);
Line 180... Line 194...
180
 
194
 
181
        $vid = false;
195
        $vid = false;
182
        $m = null;
196
        $m = null;
183
 
197
 
184
        # Usual format
198
        # Usual format
185
        if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/watch(.*)(&|\\?)v=([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
199
        if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/watch(.*)(/|&|\\?)v=([a-zA-Z0-9_-]{11})@ismU", $url, $m))) {
186
                $vid = $m[4];
200
                $vid = $m[4];
187
        }
201
        }
188
 
202
 
189
        # Short format
203
        # Short format
190
        if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtu\\.be/([a-zA-Z0-9_-]{11}))@ismU", $url, $m))) {
204
        if (($vid === false) && (preg_match("@https{0,1}://(www\\.|)youtu\\.be/([a-zA-Z0-9_-]{11}))@ismU", $url, $m))) {
Line 202... Line 216...
202
function getPlaylistIDFromURL($url) {
216
function getPlaylistIDFromURL($url) {
203
        $pid = false;
217
        $pid = false;
204
 
218
 
205
        # Usual format
219
        # Usual format
206
        $m = null;
220
        $m = null;
207
        if (($pid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(.*)(&|\\?)list=(.+)&@ismU", $url.'&', $m))) {
221
        if (($pid === false) && (preg_match("@https{0,1}://(www\\.|)youtube\\.com/(.*)(/|&|\\?)list=(.+)&@ismU", $url.'&', $m))) {
208
                $pid = $m[4];
222
                $pid = $m[4];
209
        }
223
        }
210
 
224
 
211
        return $pid;
225
        return $pid;
212
}
226
}