-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new methods `HttpLoader::useProxy()` and `HttpLoader::useRotatingProxies([...])` to define proxies that the loader shall use. They can be used with a guzzle HTTP client instance (default) and when the loader uses the headless chrome browser. Using them when providing some other PSR-18 implementation will throw an exception. (see #99) Also, fix the `HttpLoader::load()` implementation won't throw any exception, because it shouldn't kill a crawler run. When you want any loading error to end the whole crawler execution `HttpLoader::loadOrFail()` should be used. Also adapted the phpdoc in the `LoaderInterface`.
- Loading branch information
Showing
9 changed files
with
439 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Crwlr\Crawler\Loader\Http; | ||
|
||
class ProxyManager | ||
{ | ||
protected ?int $lastUsedProxy = null; | ||
|
||
/** | ||
* @param string[] $proxies | ||
*/ | ||
public function __construct(protected array $proxies) | ||
{ | ||
$this->proxies = array_values($this->proxies); | ||
} | ||
|
||
public function singleProxy(): bool | ||
{ | ||
return count($this->proxies) === 1; | ||
} | ||
|
||
public function hasOnlySingleProxy(): bool | ||
{ | ||
return count($this->proxies) === 1; | ||
} | ||
|
||
public function hasMultipleProxies(): bool | ||
{ | ||
return count($this->proxies) > 1; | ||
} | ||
|
||
public function getProxy(): string | ||
{ | ||
if ($this->hasOnlySingleProxy()) { | ||
return $this->proxies[0]; | ||
} | ||
|
||
if ($this->lastUsedProxy === null || !isset($this->proxies[$this->lastUsedProxy + 1])) { | ||
$this->lastUsedProxy = 0; | ||
} else { | ||
$this->lastUsedProxy += 1; | ||
} | ||
|
||
return $this->proxies[$this->lastUsedProxy]; | ||
} | ||
} |
Oops, something went wrong.