-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrawler.php
More file actions
200 lines (182 loc) · 3.56 KB
/
Crawler.php
File metadata and controls
200 lines (182 loc) · 3.56 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
<?php
/**
* @author : Jakiboy
* @package : FloatPHP
* @subpackage : Helpers Http Component
* @version : 1.5.x
* @copyright : (c) 2018 - 2025 Jihad Sinnaour <me@jihadsinnaour.com>
* @link : https://floatphp.com
* @license : MIT
*
* This file is a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Helpers\Http;
use FloatPHP\Classes\Filesystem\{Arrayify, Stringify};
use FloatPHP\Classes\Http\Client;
use FloatPHP\Classes\Http\Curl;
use FloatPHP\Classes\Server\System;
use FloatPHP\Exceptions\Helpers\CrawlerException;
System::setTimeLimit(0);
System::setMemoryLimit('-1');
/**
* Built-in HTTP crawler.
*/
final class Crawler
{
/**
* @access private
*/
private $urls = [];
private $params = [];
private $bypass = false;
private $resources = 4;
private $save = false;
private $path = 'public/cache';
private $signature = true;
private $ua;
private static $limit = 1000;
/**
* Init crawler.
*
* @access public
* @param array $urls
* @param array $params
*/
public function __construct(array $urls = [], array $params = [])
{
// Limit URLs
$this->setUrls($urls);
// Set default User-Agent
$this->ua = Stringify::formatPath(self::class);
// Set Http client params
$this->params = Arrayify::merge([
'method' => Client::GET,
'timeout' => 3,
'redirect' => 3,
'follow' => true,
'return' => true,
'ua' => "{$this->ua}/1.0",
'header' => [
'Cache-Control' => 'no-cache'
]
], $params);
}
/**
* Bypass resources check.
*
* @access public
* @return object
*/
public function bypass() : self
{
$this->bypass = true;
return $this;
}
/**
* Set minimum CPU cores.
*
* @access public
* @param int $cores
* @return object
*/
public function setResources(int $cores) : self
{
$this->resources = $cores;
return $this;
}
/**
* Set URLs limit.
*
* @access public
* @param int $limit
* @return void
*/
public static function limit(int $limit) : void
{
self::$limit = $limit;
}
/**
* Disable file signature.
*
* @access public
* @return object
*/
public function noSignature() : self
{
$this->signature = false;
return $this;
}
/**
* Save crawled target response.
*
* @access public
* @param string $path
* @return object
*/
public function save(?string $path = null) : self
{
$path = $path ?: $this->path;
$this->path = ltrim($path, '/');
$this->save = true;
return $this;
}
/**
* Run crawler.
*
* @access public
* @return array
* @throws CrawlerException
*/
public function run() : array
{
if ( !$this->bypass && !$this->hasResources() ) {
throw new CrawlerException(
CrawlerException::insufficientResources(
$this->resources
)
);
}
// Set extra params
$extra = [];
if ( $this->save ) {
$extra['path'] = $this->path;
$extra['ext'] = '.html';
}
if ( $this->signature ) {
$extra['signature'] = "<!-- Cache: {$this->ua} -->";
}
// Send multiple request
return Curl::requestMultiple(
$this->urls,
$this->params,
$extra
);
}
/**
* Check server resources.
*
* @access private
* @return bool
*/
private function hasResources() : bool
{
$memory = System::isMemoryOut();
$cpu = System::getCpuCores() < $this->resources;
return !$memory && !$cpu;
}
/**
* Set crawler limited URLs.
*
* @access private
* @param array $urls
* @return void
*/
private function setUrls(array $urls) : void
{
if ( count($urls) > self::$limit ) {
$urls = Arrayify::slice($urls, 0, self::$limit);
}
$this->urls = $urls;
}
}