forked from jpscharf/laracast-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
laracast-download.php
executable file
·323 lines (307 loc) · 9.62 KB
/
laracast-download.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
//
// TODO
// Clean up the D.R.Y. all over (around curl_exec)
set_time_limit(0); // infinity
define('LD_DEBUG', false);
define('LD_CONFIG_FILE', dirname(__FILE__) . '/config.php');
define('LD_COOKIEJAR', dirname(__FILE__).'/cookie.jar');
define('LD_CLI', true);
define('LD_NL', LD_CLI ? PHP_EOL : '<br>');
if (!file_exists(LD_CONFIG_FILE)) die('No config file found.'.PHP_EOL);
// Kill the cookie jar
//if (file_exists(LD_CONFIG_FILE)) unlink(LD_COOKIEJAR);
require LD_CONFIG_FILE;
if (!file_exists($directory)) die('Configured directory does not exist, check config file'.PHP_EOL);
p('App Start');
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // no timeout for connection
curl_setopt($ch, CURLOPT_TIMEOUT, 0); // no timeout for curl
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, LD_COOKIEJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, LD_COOKIEJAR);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
p('Pruning casts list based on output directory');
$glob = glob($directory . '/*.mp4');
$existing = array();
if ($glob) {
foreach ($glob as $file) {
$matches = array();
if (preg_match('!' . $directory . '/[0-9]+_(.*).mp4!', $file, $matches)) {
$existing[] = $matches[1];
}
}
} else {
p('Not downloads found');
}
$authed = false;
if (file_exists(LD_COOKIEJAR)) {
p('Checking if our previous session is still active ... ');
$authed = check_session($email);
}
if (!$authed) {
p('Authenticating .. ');
$session = do_auth($email, $password);
if ($session===false) die('failed to auth, dying'.PHP_EOL);
p('We are authenticated!');
}
p('Grab list from /all');
$casts = grab_all();
if ($casts === false) die('Failed to grab all casts'.PHP_EOL);
// Find existing casts and unset them
// this is a bit ghetto, but who has the time
$casts_tmp = array();
$pre_count = count($casts);
foreach($casts as $cast) {
$cast_normalized = str_replace(array('series/', 'lessons/'), array('', ''), $cast);
$cast_normalized = str_replace('/', '_', $cast_normalized);
if (!in_array($cast_normalized, $existing)) {
$casts_tmp[] = $cast;
}
}
$casts = $casts_tmp;
$count = count($casts);
if ($pre_count != $count) {
p('Already downloaded ' . ($pre_count - $count) . ' casts');
}
if (!$count) {
p('Nothing new to download, dying');
die;
}
p('Found ' . $count . ' casts');
$padding = 4;
foreach ($casts as $cast) {
echo LD_NL . LD_NL;
$video = get_video_id($cast);
if (substr_count($video, '?')) {
$video_id = substr($video, 0, strpos($video, '?'));
} else {
$video_id = $video;
}
if ($video_id === false) {
p('Failed to get video id for cast=' . $cast);
continue;
}
$out_name = str_replace(array('series/', 'lessons/'), array('', ''), $cast);
$out_file = $directory . '/' . str_pad($video_id, $padding, '0', STR_PAD_LEFT) . '_' . str_replace('/', '_', $out_name) . '.mp4';
if (file_exists($out_file)) {
p('File exists for out_file=' . $out_file . ', skipping');
continue;
}
p('Checking first url before redirection : https://laracasts.com/downloads/' . $video);
$url = get_download_url('https://laracasts.com/downloads/' . $video);
if ($url === false) {
p('Could not get download url for video_id=' . $video_id);
continue;
}
p('Downloading ' . $url . ' to ' . $out_file);
$result = download_from_vimeo($out_file, $url);
if ($result===false) {
p('Failed to get url=' . $url);
continue;
}
p('Success, got video_id=' . $video_id . ', cast=' . $cast);
}
//functions
/*
* Authenticates by first call /login, we need to establish
* cookie jar for session
*/
function do_auth($email, $password) {
global $ch;
// init curl
// Call login to start session
p('Calling /login', 1);
curl_setopt($ch, CURLOPT_URL, 'https://laracasts.com/login');
$response=curl_exec($ch);
if ($response===false) {
p(LD_NL . 'curl_error is ' . curl_error($ch));
curl_close($ch);
p(' failed');
return false;
}
p(' success');
// Post login
p('Posting to /session', 1);
curl_setopt($ch, CURLOPT_URL, 'https://laracasts.com/sessions');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('email' => $email, 'password' => $password, 'remember' => 'on')));
$response=curl_exec($ch);
if ($response===false) {
p(LD_NL . 'curl_error is ' . curl_error($ch));
curl_close($ch);
p(' failed');
return false;
}
if (preg_match('!^HTTP/1.1 302 Found!', $response)) {
p(' success, got 302 from /sessions, looking for Location');
$matches = array();
if (preg_match('/^Location: (.*)/', $response, $matches)) {
$Location = $matches[1];
p('Success, got location=' . $location);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, null);
return true;
}
} else {
p('failed to get 302 response from /sessions');
dump_response_to_file($response);
}
}
/*
* We have a cookie jar file, so check if our session
* is still valid by loading /admin/profile
* we are looking for email because username may
* not be 100% accurate
*/
function check_session($email) {
global $ch;
p('Calling /admin/account ... ', 1);
curl_setopt($ch, CURLOPT_URL, 'https://laracasts.com/admin/account');
$response=curl_exec($ch);
if ($response===false) {
p(LD_NL . 'curl_error is ' . curl_error($ch));
curl_close($ch);
p(' failed');
return false;
}
p(' success');
dump_response_to_file($response);
if (preg_match('!^HTTP/1.1 200 OK!', $response)) {
p('Found 200 OK, checking for our email, in profile form');
if (preg_match('/value="' . $email . '"/', $response)) {
p('Found it, we are still active');
return true;
}
}
}
/*
* Call /all and get a list of the casts
*/
function grab_all() {
global $ch;
p('Calling /all ... ', 1);
curl_setopt($ch, CURLOPT_URL, 'https://laracasts.com/all');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response=curl_exec($ch);
if ($response===false) {
p(LD_NL . 'curl_error is ' . curl_error($ch));
curl_close($ch);
p(' failed');
return false;
}
p(' success');
dump_response_to_file($response);
if (preg_match('!^HTTP/1.1 200 OK!', $response)) {
p('Found 200 OK, checking cast links');
$matches=array();
$yup = preg_match_all('!https://laracasts.com/(lessons/[^"]+)!', $response, $matches);
$yup2 = preg_match_all('!https://laracasts.com/(series/[^"]+)!', $response, $matches2);
if ($yup || $yup2) {
$casts=array();
foreach($matches[1] as $i => $match) {
if ($match<>'lessons/complete' && substr_count($match, '/save')<1) $casts[]=$match;
}
foreach($matches2[1] as $i => $match) {
if (strpos($match, 'episodes')!==false) $casts[]=$match;
}
$count=count($casts);
if (!$count) {
p('Zero casts found');
return false;
}
p('Found ' . $count . ' casts');
return $casts;
}
}
return false;
}
/*
* Get download url from /lessons/ or /series/
*/
function get_video_id($cast) {
global $ch;
p('Calling ' . $cast . ' ... ', 1);
curl_setopt($ch, CURLOPT_URL, 'https://laracasts.com/' . $cast);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response=curl_exec($ch);
if ($response===false) {
p(LD_NL . 'curl_error is ' . curl_error($ch));
curl_close($ch);
p(' failed');
return false;
}
p(' success');
dump_response_to_file($response);
if (preg_match('!^HTTP/1.1 200 OK!', $response)) {
p('Found 200 OK, checking cast links');
$matches=array();
if (preg_match('!/downloads/([0-9a-z\?\=]+)!', $response, $matches)) {
$video_id = $matches[1];
p('Video ID for cast=' . $cast .' is ' . $video_id);
return $video_id;
}
}
return false;
}
/*
* Get the download URL
*/
function get_download_url($video) {
global $ch;
p('Calling ' . $video . ' ... ', 1);
curl_setopt($ch, CURLOPT_URL, $video);
$response=curl_exec($ch);
if ($response===false) {
p(LD_NL . 'curl_error is ' . curl_error($ch));
curl_close($ch);
p(' failed');
return false;
}
p(' success');
dump_response_to_file($response);
if (preg_match('!^HTTP/1.1 302 Found!', $response)) {
p('Found 302 Found, checking for Location again');
$matches=array();
if (preg_match('!Location: (.*)!', $response, $matches)) {
$download_url = trim($matches[1]);
if ($download_url === 'https://laracasts.com/admin/subscription/plan') {
p('Lession requires subscription');
echo LD_NL;
p(' exiting script, cannot download with a subscription');
die;
return false;
}
p('Download URL for video=' . $video .' url is ' . $download_url);
return trim($download_url);
}
}
return false;
}
/*
* Download from Vimeo after some redirects
* Using command line curl because following
* redirects in php is usually disabled by
* safe_mode/open_basedr
*/
function download_from_vimeo($out_file, $url) {
$cmd="/usr/bin/curl -L -o $out_file -A 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36' '$url'";
p('Executing command ' . $cmd);
passthru($cmd);
if (file_exists($out_file)) return true;
else return false;
}
/*
* Outputs message based on debug setting
*/
function p ($message, $nonewline = 0) {
print $message . ($nonewline ? '' : LD_NL);
}
function dump_response_to_file($response) {
if (!LD_DEBUG) return;
$out_file=microtime(true) . '-response.html';
file_put_contents($out_file, $response);
p('Response dumped to ' . $out_file);
}