-
Notifications
You must be signed in to change notification settings - Fork 7
/
Spider.php
195 lines (167 loc) · 5.15 KB
/
Spider.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
<?php
namespace Simgroep\ConcurrentSpiderBundle;
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
use Guzzle\Plugin\Cookie\CookiePlugin;
use PhpAmqpLib\Message\AMQPMessage;
use VDB\Uri\Uri;
use VDB\Uri\Exception\UriSyntaxException;
use VDB\Spider\RequestHandler\GuzzleRequestHandler;
use VDB\Spider\Event\SpiderEvents;
use Simgroep\ConcurrentSpiderBundle\PersistenceHandler\PersistenceHandler;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class Spider
{
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var GuzzleRequestHandler
*/
private $requestHandler;
/**
* @var PersistenceHandler
*/
private $persistenceHandler;
/**
* @var CrawlJob
*/
private $currentCrawlJob;
/**
* @var CookiePlugin
*/
private $cookiePlugin;
/**
* @var resource
*/
private $curlClient;
/**
* Constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param GuzzleRequestHandler $requestHandler
* @param PersistenceHandler $persistenceHandler
* @param resource $curlClient
* @param CookiePlugin $cookiePlugin
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
GuzzleRequestHandler $requestHandler,
PersistenceHandler $persistenceHandler,
CurlClient $curlClient,
CookiePlugin $cookiePlugin
)
{
$this->eventDispatcher = $eventDispatcher;
$this->requestHandler = $requestHandler;
$this->persistenceHandler = $persistenceHandler;
$this->curlClient = $curlClient;
$this->cookiePlugin = $cookiePlugin;
}
/**
* Returns the request handler.
*
* @return GuzzleRequestHandler
*/
public function getRequestHandler()
{
return $this->requestHandler;
}
/**
* Returns the event dispatcher.
*
* @return EventDispatcherInterface
*/
public function getEventDispatcher()
{
return $this->eventDispatcher;
}
/**
* Returns the job that is currently being processed.
*
* @return CrawlJob
*/
public function getCurrentCrawlJob()
{
return $this->currentCrawlJob;
}
/**
* Returns the persistence handler.
*
* @return PersistenceHandler
*/
public function getPersistenceHandler()
{
return $this->persistenceHandler;
}
/**
* Function that crawls one webpage based on the give url.
*
* @param CrawlJob $crawlJob
* @param QueueFactory $queueFactory
* @param string $currentQueueType
*/
public function crawl(CrawlJob $crawlJob, QueueFactory $queueFactory, $currentQueueType)
{
$this->currentCrawlJob = $crawlJob;
$uri = new Uri($crawlJob->getUrl());
$this->curlClient->initClient();
if ($this->curlClient->isDocument($uri) && $currentQueueType != QueueFactory::QUEUE_DOCUMENTS) {
$message = new AMQPMessage(
json_encode($crawlJob->toArray()),
['delivery_mode' => 1]
);
$queueFactory->getQueue(QueueFactory::QUEUE_DOCUMENTS)->publish($message);
} else {
$resource = $this->requestHandler->request($uri);
$baseUrl = $resource->getUri()->toString();
if (!trim($resource->getResponse()->getBody(true))) {
$this->setClient($uri->toString());
$resource = $this->requestHandler->request($uri);
}
$uris = [];
$this->eventDispatcher->dispatch(SpiderEvents::SPIDER_CRAWL_PRE_DISCOVER);
$crawler = $resource->getCrawler()->filterXPath('//a');
foreach ($crawler as $node) {
try {
if ($node->getAttribute("rel") === "nofollow") {
continue;
}
$href = $node->getAttribute('href');
$uri = new Uri($href, $baseUrl);
$uris[] = $uri;
} catch (UriSyntaxException $e) {
//too bad
}
}
$crawler = $resource->getCrawler()->filterXPath('//loc');
foreach ($crawler as $node) {
try {
$href = $node->nodeValue;
$uri = new Uri($href, $baseUrl);
$uris[] = $uri;
} catch (UriSyntaxException $e) {
//too bad
}
}
$this->eventDispatcher->dispatch(
SpiderEvents::SPIDER_CRAWL_POST_DISCOVER,
new GenericEvent($this, ['uris' => $uris])
);
$this->persistenceHandler->persist($resource, $crawlJob);
}
}
/**
* Sets require cookie jar for request handler
*
* @param string $uri
*/
public function setClient($uri)
{
$client = new Client($uri);
$client->addSubscriber($this->cookiePlugin);
$this->requestHandler->setClient($client);
}
}