Subversion Repositories yt_downloader

Rev

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

Rev 14 Rev 16
Line 41... Line 41...
41
        $out = array();
41
        $out = array();
42
 
42
 
43
        $next_page_token = '';
43
        $next_page_token = '';
44
 
44
 
45
        do {
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()));
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;
47
                if (!$cont) return false; // e.g. if Playlist was deleted
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;
52
                if (!isset($obj['items'])) return false;
Line 65... Line 65...
65
 
65
 
66
        return $out;
66
        return $out;
67
}
67
}
68
 
68
 
69
function yt_get_channel_id($username) {
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');
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;
71
        if (!$cont) return false;
72
 
72
 
73
        $obj = json_decode($cont, true);
73
        $obj = json_decode($cont, true);
74
        if (!$obj) return false;
74
        if (!$obj) return false;
75
 
75
 
Line 81... Line 81...
81
                }
81
                }
82
        }
82
        }
83
}
83
}
84
 
84
 
85
function yt_get_channel_id_and_stats($username) {
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');
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;
87
        if (!$cont) return false;
88
 
88
 
89
        $obj = json_decode($cont, true);
89
        $obj = json_decode($cont, true);
90
        if (!$obj) return false;
90
        if (!$obj) return false;
91
 
91
 
Line 97... Line 97...
97
                }
97
                }
98
        }
98
        }
99
}
99
}
100
 
100
 
101
function yt_get_channel_stats($channel_id) {
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');
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;
103
        if (!$cont) return false;
104
 
104
 
105
        $obj = json_decode($cont, true);
105
        $obj = json_decode($cont, true);
106
        if (!$obj) return false;
106
        if (!$obj) return false;
107
 
107
 
Line 113... Line 113...
113
                }
113
                }
114
        }
114
        }
115
}
115
}
116
 
116
 
117
function yt_get_playlist_stats($playlist_id) {
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()));
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;
119
        if (!$cont) return false;
120
 
120
 
121
        $obj = json_decode($cont, true);
121
        $obj = json_decode($cont, true);
122
        if (!$obj) return false;
122
        if (!$obj) return false;
123
 
123
 
124
        if (!isset($obj['items'])) return false;
124
        if (!isset($obj['items'])) return false;
125
 
125
 
126
        foreach ($obj['items'] as $item) {
126
        foreach ($obj['items'] as $item) {
127
                if ($item['kind'] == 'youtube#playlist') {
127
                if ($item['kind'] == 'youtube#playlist') {
-
 
128
                        if (!isset($item['contentDetails']) || is_null($item['contentDetails'])) return false; // can happen to deleted playlists
128
                        return $item['contentDetails'];
129
                        return $item['contentDetails'];
129
                }
130
                }
130
        }
131
        }
131
}
132
}
132
 
133
 
Line 134... Line 135...
134
        $out = array();
135
        $out = array();
135
 
136
 
136
        $next_page_token = '';
137
        $next_page_token = '';
137
 
138
 
138
        do {
139
        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
                $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
                if (!$cont) return false;
141
 
142
 
142
                $obj = json_decode($cont, true);
143
                $obj = json_decode($cont, true);
143
                if (!$obj) return false;
144
                if (!$obj) return false;
144
 
145
 
Line 164... Line 165...
164
        $out = array();
165
        $out = array();
165
 
166
 
166
        $next_page_token = '';
167
        $next_page_token = '';
167
 
168
 
168
        do {
169
        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
                $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
                if (!$cont) return false;
171
 
172
 
172
                $obj = json_decode($cont, true);
173
                $obj = json_decode($cont, true);
173
                if (!$obj) return false;
174
                if (!$obj) return false;
174
 
175
 
Line 239... Line 240...
239
        // https://www.youtube.com/c/SASASMR
240
        // https://www.youtube.com/c/SASASMR
240
        // <link rel="canonical" href="https://www.youtube.com/channel/UCp4LfMtDfoa29kTlLnqQ5Mg">
241
        // <link rel="canonical" href="https://www.youtube.com/channel/UCp4LfMtDfoa29kTlLnqQ5Mg">
241
        // https://www.youtube.com/impaulsive
242
        // https://www.youtube.com/impaulsive
242
        // <link rel="canonical" href="https://www.youtube.com/channel/UCGeBogGDZ9W3dsGx-mWQGJA">
243
        // <link rel="canonical" href="https://www.youtube.com/channel/UCGeBogGDZ9W3dsGx-mWQGJA">
243
 
244
 
244
        $cont = file_get_contents($custom_url);
245
        $cont = @file_get_contents($custom_url);
245
        if ($cont === false) {
246
        if ($cont === false) {
246
                throw new Exception("Cannot open $custom_url using file_get_contents.");
247
                throw new Exception("Cannot open $custom_url using file_get_contents.");
247
        }
248
        }
248
        if (!preg_match('@<link rel="canonical" href="https://www.youtube.com/channel/([^"]+)">@ismU', $cont, $m)) {
249
        if (!preg_match('@<link rel="canonical" href="https://www.youtube.com/channel/([^"]+)">@ismU', $cont, $m)) {
249
                return false;
250
                return false;