Repository navigation
Expand file tree
/
Copy pathRollingScraperAbstract.php
More file actions
467 lines (422 loc) · 14 KB
/
Copy pathRollingScraperAbstract.php
File metadata and controls
467 lines (422 loc) · 14 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<?php
/**
* multipurpose multi-curl scraping (crawling) framework
*/
require_once dirname(__FILE__). '/RollingCurlMini.php';
class RollingScraperAbstract
{
/**
* Config values
*/
protected $aConfig = array(
'state_time_storage' => '', // temporal section of state storage (file path)
'state_data_storage' => '', // data section of state storage (file path)
'scrape_life' => 0, // expiration time (secs) of scraped data
'run_timeout' => 30, // max. time (secs) to execute scraper script
'run_pages_loops' => 20, // max. number of loops through pages
'run_pages_buffer' => 500, // page requests buffer size
'curl_threads' => 10, // number of multi-curl threads
'curl_options' => array( // default CURL options used in multi-curl requests
CURLOPT_NOBODY => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => 'RollingScraper',
),
);
/**
* [state property:] timestamp of last scraping start
*/
protected $tStateStart = 0;
/**
* [state property:] timestamp of last scraping end
*/
protected $tStateEnd = 0;
/**
* [state property:] max. time (secs) to execute scraper script
*/
protected $nStateTimeout = 0;
/**
* [state property:] timestamp of last scraping run start
*/
protected $tStateRun = 0;
/**
* [state property:] timestamp of last scraping run end
*/
protected $tStateStop = 0;
/**
* [state property:] # of pages loop
*/
protected $iStateLoop = 0;
/**
* [state property:] array of all registered page request URLS
*/
protected $aStateUrls = array();
/**
* [state property:] array of page request data
*/
protected $aStateReqs = array();
protected $oCurl = 0;
protected $bBuffOver = false;
function __construct() {
$this->oCurl = new RollingCurlMini($this->aConfig['curl_threads']);
$this->oCurl->setOptions($this->aConfig['curl_options']);
}
/**
* Modify config values
* @param array $aCfg - new config values
*/
protected function modConfig($aCfg) {
if (isset($aCfg['curl_options'])) {
$this->aConfig['curl_options'] = $aCfg['curl_options'] +
$this->aConfig['curl_options'];
unset($aCfg['curl_options']);
}
$this->aConfig = array_merge($this->aConfig,
array_intersect_key($aCfg, $this->aConfig)
);
}
/**
* Reset state properties
* @param bool $bEnd - scraping end flag
*/
protected function resetState($bEnd = false) {
$this->nStateTimeout = $this->tStateRun = $this->tStateStop = 0;
if ($bEnd)
$this->tStateEnd = time();
else {
$this->tStateStart = time(); $this->tStateEnd = 0;
}
$this->aStateUrls = $this->aStateReqs = array();
}
/**
* Restore temporal state properties from appropriate storage section
* @return bool
*/
protected function restoreStateTime() {
$arr = $this->_restore($this->aConfig['state_time_storage']);
if (!$arr || count($arr) != 5) return false;
list($this->tStateStart, $this->tStateEnd,
$this->nStateTimeout, $this->tStateRun, $this->tStateStop) = $arr;
return true;
}
/**
* Save temporal state properties in appropriate storage section
*/
protected function saveStateTime() {
$this->_save($this->aConfig['state_time_storage'], array(
$this->tStateStart, $this->tStateEnd,
$this->nStateTimeout, $this->tStateRun, $this->tStateStop
));
}
/**
* Restore data state properties from appropriate storage section
* @return bool
*/
protected function restoreStateData() {
$arr = $this->_restore($this->aConfig['state_data_storage']);
if (!$arr || count($arr) != 3) return false;
list($this->iStateLoop, $this->aStateUrls, $this->aStateReqs) = $arr;
return true;
}
/**
* Save data state properties in appropriate storage section
*/
protected function saveStateData() {
$this->_save($this->aConfig['state_data_storage'], array(
$this->iStateLoop, $this->aStateUrls, $this->aStateReqs
));
}
/**
* Get state progress - data to be used in client-side output
* @return array
*/
function getStateProgress() {
return array(
$this->tStateStart, $this->tStateEnd,
$this->nStateTimeout, $this->tStateRun, $this->tStateStop,
$n = count($this->aStateUrls), $n? $n- count($this->aStateReqs) : 0
);
}
/**
* Save state progress into JSON file so it can be read by AJAX request on client [MAY be overridden]
*/
protected function _saveStateProgress() {
}
/**
* Main entry point
* @return bool
*/
function run() {
if (!$this->oCurl) return false;
$this->restoreStateTime();
$t0 = time();
if ($this->tStateRun && $t0-$this->tStateRun-$this->nStateTimeout <= 10)
return false;
$n_life = $this->aConfig['scrape_life'];
if ($this->tStateEnd) {
if ($n_life && $t0- $this->tStateEnd <= $n_life)
return false;
$this->resetState();
}
if ($this->tStateStop &&
$n_life && $t0 - $this->tStateStop > $n_life)
$this->resetState();
if ($this->tStateStop)
$this->restoreStateData();
$this->nStateTimeout = $this->aConfig['run_timeout'];
set_time_limit($this->nStateTimeout? $this->nStateTimeout + 30 : 0);
$this->tStateRun = $t0;
$this->tStateStop = $this->tStateEnd = 0;
$this->saveStateTime();
$this->_saveStateProgress();
$b = false;
try {
$b = $this->runPages();
}
catch (Exception $obj) {
}
if (!$b) {
$this->tStateRun = 0; $this->tStateStop = time();
}
$this->saveStateTime(); $this->saveStateData();
$this->_saveStateProgress();
return true;
}
/**
* Walk through web pages
* @return bool
*/
private function runPages() {
$this->_beforeRun();
$this->_readPauseFlag();
$this->oCurl->reset();
$this->oCurl->setCallback(array($this, 'callbackOnPage'));
$l = &$this->iStateLoop;
if (!count($this->aStateUrls)) {
$this->_initPages();
$l = 0;
if (!count($this->aStateUrls)) return false;
$this->oCurl->execute();
}
$n_loops = $this->aConfig['run_pages_loops'];
$n_buff = $this->aConfig['run_pages_buffer'];
for (; $l < $n_loops && count($this->aStateReqs); $l++) {
$this->oCurl->reset();
$j = 0; $b = false;
foreach ($this->aStateReqs as $arr) {
$this->addReq($arr);
if ($n_buff && $j++ < $n_buff) continue;
$this->oCurl->execute();
$b = true;
if (!count($this->aStateReqs)) break;
}
if (!$b)
$this->oCurl->execute();
$this->_afterRunLoop();
}
$this->_beforeEnd($this->aStateUrls);
$this->resetState(true);
return true;
}
/**
* Method to be called before each scraping run [MAY be overridden]
*/
protected function _beforeRun() {
}
/**
* Method to be called after each loop of scraping run [MAY be overridden]
*/
protected function _afterRunLoop() {
}
/**
* Method to be called before scraping end [MAY be overridden]
* @param array $aUrls - page request URLs which will be deleted on scraping end
*/
protected function _beforeEnd($aUrls) {
}
/**
* Initialize the starting list of page requests [MUST be overridden]
*/
protected function _initPages() {
}
/**
* Callback on response of a page request
* @param string $cont - response content
* @param string $url - URL of request
* @param array $aInfo - CURL info data
* @param array $aReq - request data (data associated with request)
*/
function callbackOnPage($cont, $url, $aInfo, $aReq) {
$this->detectStop();
list($i_req, ) = array_splice($aReq, 0, 2);
$b = $this->_handlePage($cont, $url, $aInfo, $i_req, $aReq);
if (!$b) return;
$this->removePageReq($i_req);
$this->_saveStateProgress();
}
/**
* Process response of a page request [MUST be overridden]
* @param string $cont - page content
* @param string $url - url of request
* @param array $aInfo - CURL info data
* @param int $index - # of page request
* @param array $aData - custom request data (part of request data)
* @return bool
*/
protected function _handlePage($cont, $url, $aInfo, $index, $aData) {
return true;
}
/**
* Add page request
* @param string $url - page URL
* @param array $aData - custom request data (part of request data)
* @param int $iParent - # of parent page request
* @return int|false # of added page request
*/
protected function addPage($url, $aData = 0, $iParent = false) {
$this->detectStop();
$n = count($this->aStateUrls);
if ($n && in_array($url, $this->aStateUrls)) return false;
$this->aStateUrls[$n] = $url;
$this->aStateReqs[$n] = $arr = array_merge(
array($n, $iParent), is_array($aData)? $aData : array()
);
if ($this->bBuffOver) return $n;
$this->addReq($arr, $url);
if ($this->aConfig['run_pages_buffer'] && $this->oCurl->count() >= $this->aConfig['run_pages_buffer'])
$this->bBuffOver = true;
return $n;
}
/**
* Get URL of page request by index
* @param int $index - index/# of page request
* @return string|false URL of page request
*/
protected function getPageByIndex($index) {
return $this->aStateUrls[$index];
}
/**
* Find page request by URL
* @param string $url - URL
* @return int|false # of found page request
*/
protected function getPageByUrl($url) {
return array_search($url, $this->aStateUrls);
}
/**
* Remove page request data
* @param int $index - # of page request
*/
protected function removePageReq($index) {
if (isset($this->aStateReqs[$index]))
unset($this->aStateReqs[$index]);
}
/**
* Add multi-CURL request
* @param array $aReq - request data (data associated with request)
* @param string $url - URL
*/
protected function addReq($aReq, $url = '') {
if (count($aReq) < 2) return;
$url = $url? $url : $this->aStateUrls[$aReq[0]];
$i = $aReq[1];
$a_data = array_slice($aReq, 2);
$a_opts = $this->_buildReqOptions($a_data);
if ($i !== false && isset($this->aStateUrls[$i])) {
$a_ref = array(CURLOPT_REFERER => $this->_fixReqUrl($this->aStateUrls[$i]));
$a_opts = $a_opts? array_merge($a_opts, $a_ref) : $a_ref;
}
$this->oCurl->add(
$this->_fixReqUrl($url), $this->_buildReqPost($a_data), 0, $aReq,
$a_opts, $this->_buildReqHeaders($a_data)
);
}
/**
* Fix (result to absolute) URL of request if needed [MAY be overridden]
* @param string $url - given URL
* @return string
*/
protected function _fixReqUrl($url) {
return $url;
}
/**
* Build POST data for request [MAY be overridden]
* @param array $aData - custom request data
* @return array|0
*/
protected function _buildReqPost($aData) {
return 0;
}
/**
* Build CURL options for request [MAY be overridden]
* @param array $aData - custom request data
* @return array|0
*/
protected function _buildReqOptions($aData) {
return 0;
}
/**
* Build HTTP headers for request [MAY be overridden]
* @param array $aData - custom request data
* @return array|0
*/
protected function _buildReqHeaders($aData) {
return 0;
}
protected function detectStop() {
if ($this->nStateTimeout && time() - $this->tStateRun - $this->nStateTimeout > 0)
throw new Exception('RollingScraperAbstract paused by Timeout', 1);
if ($this->_readPauseFlag())
throw new Exception('RollingScraperAbstract paused by PauseFlag', 1);
}
/**
* Check pause flag. If overridden, must also reset pause flag to false [MAY be overridden]
* @return bool
*/
protected function _readPauseFlag() {
return false;
}
/**
* Break (abort) scraping process
*/
protected function breakRun() {
$this->aStateReqs = array();
$this->oCurl->requestBreak();
}
/**
* Restore data from specified storage [MAY be overridden]
* @param mixed $storage - storage
* @return mixed
*/
protected function _restore($storage) {
if (!$storage || !is_readable($storage)) return false;
$s = file_get_contents($storage);
if (!$s) return false;
return $this->_decode($s);
}
/**
* Store data in specified storage [MAY be overridden]
* @param mixed $storage - storage
* @param mixed $data - data
*/
protected function _save($storage, $data) {
if (!$storage) return;
file_put_contents($storage, $this->_encode($data));
}
/**
* Encode (serialize) data to string to prepare it for storing [MAY be overridden]
* @param mixed $data - data
* @return string
*/
protected function _encode($data) {
return serialize($data);
}
/**
* Decode (unserialize) data encoded by _encode() method [MAY be overridden]
* @param string $s - encoded data
* @return mixed
*/
protected function _decode($s) {
return unserialize($s);
}
}