-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrawler.php
334 lines (289 loc) · 8.97 KB
/
crawler.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
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Web page crawler
*
* @author driverok <[email protected]>
*/
namespace driverok;
class Crawler
{
/** @var string $logFileName where log file located */
public $logFilename = '/tmp/crawler.log';
/** @var int $timeout timeout to sleep when got an error */
public $timeout = 10;
/** @var int $pageLimit limit of loaded pages */
public $pageLimit = 100;
/** @var int $resultsLimit limit of founded description */
public $resultsLimit = 10;
/** @var int $countPages count loaded pages */
private $countPages = 0;
/** @var int $countResults count of founded descriptions */
private $countResults = 0;
/** @var string $domain domain to proceed */
private $domain;
/** @var string $protocol protocol to proceed */
private $protocol = 'http://';
/** @var Database $database object of db class */
private $database;
/** @var string $userAgent userAgent to hello webserver */
private $userAgent = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)';
/** @var array $queue queue of urls to proceed */
private $queue = [];
/** @var array $page array of currently checked page information */
private $page = array();
/**
* Read the --domain cli param and set private var $domain
*/
public function __construct()
{
$params = getopt("d:p:s::");
$this->checkParams($params);
$this->setDomain($params['d']);
$this->setProtocol($params['p']);
$this->addQueue($this->protocol.$this->domain);
$this->database = new Database();
if (!empty($params['s'])) {
$this->setup();
}
}
/**
* Recursive function for checking urls from queue
*/
public function run()
{
if (empty($this->queue)) {
$this->log('Nothing to parse');
return;
}
foreach ($this->queue as $url) {
if ($this->needToStop())
{
$this->writeln('Time to stop our crawler');
return;
}
$this->getUrl($url);
$this->analyzeContent($this->page['content']);
$this->writeln(mb_substr($this->page['description'], 0, 50,'utf-8').'... '.$url
);
$this->saveUrl();
unset($this->queue[md5(trim($url, '/'))]);
}
if (!empty($this->queue)) {
$this->run();
}
}
/**
* Create table crawler if not exist
*/
private function setup()
{
$sql = ' CREATE TABLE IF NOT EXISTS `crawler` ('
.' `id` INT(11) AUTO_INCREMENT NOT NULL,'
.' `posted` DATETIME NOT NULL,'
.' `domain` VARCHAR(500) NOT NULL,'
.' `url` TEXT NOT NULL,'
.' `url_hash` VARCHAR (50) NOT NULL,'
.' `description` TEXT,'
.' `keywords` TEXT,'
.' `title` TEXT,'
.' PRIMARY KEY(`id`))';
if ($this->database->ExecQuery($sql)) {
$this->writeln('Setup Completed');
} else {
$this->writeln($this->database->error());
}
}
/**
* Check if all needed params exists
*/
private function checkParams($params)
{
if (empty($params['d']) or empty($params['p'])) {
$this->log('You loss some params');
die();
}
}
private function needToStop()
{
if ($this->countPages >= $this->pageLimit or ( $this->countResults >= $this->resultsLimit)) {
return true;
}
return false;
}
private function setProtocol($protocol)
{
$this->protocol = $protocol;
}
private function setDomain($domain)
{
$this->domain = str_replace(['http://', 'https://'], ['', ''], $domain);
}
private function analyzeContent()
{
$this->findLinks();
$this->findMetas();
$this->findTitle();
}
/**
* Find all meta tags from current page
*/
private function findMetas()
{
$this->page['description'] = '';
$this->page['keywords'] = '';
$pattern = '
~<\s*meta\s
# using lookahead to capture type to $1
(?=[^>]*?
\b(?:name|property|http-equiv)\s*=\s*
(?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'|
([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=))
)
# capture content to $2
[^>]*?\bcontent\s*=\s*
(?|"\s*([^"]*?)\s*"|\'\s*([^\']*?)\s*\'|
([^"\'>]*?)(?=\s*/?\s*>|\s\w+\s*=))
[^>]*>
~ix';
if (preg_match_all($pattern, $this->page['content'], $out)) {
$metas = array_combine($out[1], $out[2]);
if (!empty($metas['description'])) {
$this->countResults++;
$this->page['description'] = $metas['description'];
}
if (!empty($metas['keywords'])) {
$this->page['keywords'] = $metas['keywords'];
}
}
}
/**
* Find title tags from current page
*/
private function findTitle()
{
$this->page['title'] = '';
$pattern = '~<title[^>]*>([^<]+)<\/title>~si';
if (preg_match($pattern, $this->page['content'], $matches)) {
$this->page['title'] = $matches[1];
}
}
/**
* Find all links from current page
*/
private function findLinks()
{
$pattern = '#<(a|area)(\s+?[^>]*?\s+?|\s+?)href\s*=\s*(["\'`]*)\s*?([^>\s]+)\s*\3[^>]*?(/>|>(.*?)</\1>|>)#is';
preg_match_all($pattern, $this->page['content'], $matches, PREG_SET_ORDER);
if (empty($matches)) {
return;
}
foreach ($matches as $match) {
$href = trim(str_replace('"', '', $match[4]));
$preparedUrl = $this->prepareUrl($href);
$this->addQueue($preparedUrl);
}
}
/**
* Checking if url not foreign domain
*/
private function checkUrl($url)
{
if (strpos($url, $this->domain) === false or empty($url)) {
return false;
}
return true;
}
/**
* Making full url with domain and protocol parts
*/
private function prepareUrl($url)
{
if (preg_match('/^\/\//', $url)) {
$url = str_replace('//', $this->protocol, $url);
}
if (mb_strlen(trim($url), 'utf-8') == 0) {
return;
}
if (strpos($url, 'http://') === false and strpos($url, 'https://') === false) {//если относительная ссылка
if ($url{0} !== '/') {//link not from root
$url = $this->page['url'].'/'.$url;
} else {// link from root
$url = $this->protocol.$this->domain.'/'.ltrim($url, '/');
}
}
return $url;
}
/**
* Adding url to parse queue
*/
private function addQueue($url)
{
if ($this->checkUrl($url)
and ! isset($this->queue[md5(trim($url, '/'))])) {
$this->queue[md5($url)] = $url;
}
}
/**
* Load url using curl
*/
private function getUrl($url)
{
$curlId = curl_init();
curl_setopt($curlId, CURLOPT_URL, $url);
curl_setopt($curlId, CURLOPT_USERAGENT, $this->userAgent);
curl_setopt($curlId, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlId, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curlId, CURLOPT_REFERER, $this->domain);
curl_setopt($curlId, CURLOPT_VERBOSE, false);
curl_setopt($curlId, CURLOPT_HEADER, false);
$page = curl_exec($curlId);
$err = curl_error($curlId);
if (!empty($err)) {
sleep($this->timeout);
}
$page = preg_replace('/<!--.*-->/Uis', '', $page);
$this->page['content'] = $page;
$this->page['url'] = $url;
$this->page['info'] = curl_getinfo($curlId);
$this->page['error'] = $err;
if (empty($err)) {
$this->countPages++;
}
curl_close($curlId);
}
/**
* Save url to database
*/
private function saveUrl()
{
$sql = 'insert into crawler '
.'(domain,url,url_hash,description,title,keywords,posted)'
.' values ('
.'"'.$this->domain.'",'
.'"'.$this->page['url'].'",'
.'"'.md5($this->page['url']).'",'
.'"'.$this->page['description'].'",'
.'"'.$this->page['title'].'",'
.'"'.$this->page['keywords'].'",'
.'"'.date('Y-m-d H:i:s').'")';
if ($this->database->ExecQuery($sql)) {
$this->write(', saved');
} else {
$this->write(', error saving - '.$this->database->error());
}
}
private function write($str)
{
$this->log($str);
echo $str;
}
private function writeln($str)
{
$this->log($str.PHP_EOL);
echo PHP_EOL.$str;
}
private function log($str)
{
file_put_contents($this->logFilename, $str, FILE_APPEND);
}
}