-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
829 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Spiders\GetPageSpider; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Arr; | ||
use RoachPHP\ItemPipeline\Item; | ||
use RoachPHP\Roach; | ||
use RoachPHP\Spider\Configuration\Overrides; | ||
|
||
class RoachSite extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'roach:example {url}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Pass url to see it work'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
$items = Roach::collectSpider( | ||
GetPageSpider::class, | ||
new Overrides(startUrls: [$this->argument('url')]) | ||
); | ||
|
||
$item = Arr::first($items); | ||
dd($item->get(0)); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace App\Spiders; | ||
|
||
use Generator; | ||
use RoachPHP\Downloader\Middleware\RequestDeduplicationMiddleware; | ||
use RoachPHP\Extensions\LoggerExtension; | ||
use RoachPHP\Extensions\StatsCollectorExtension; | ||
use RoachPHP\Http\Response; | ||
use RoachPHP\Spider\BasicSpider; | ||
use RoachPHP\Spider\ParseResult; | ||
|
||
class GetPageSpider extends BasicSpider | ||
{ | ||
public array $startUrls = [ | ||
// | ||
]; | ||
|
||
public array $downloaderMiddleware = [ | ||
RequestDeduplicationMiddleware::class, | ||
]; | ||
|
||
public array $spiderMiddleware = [ | ||
// | ||
]; | ||
|
||
public array $itemProcessors = [ | ||
// | ||
]; | ||
|
||
public array $extensions = [ | ||
LoggerExtension::class, | ||
StatsCollectorExtension::class, | ||
]; | ||
|
||
public int $concurrency = 2; | ||
|
||
public int $requestDelay = 1; | ||
|
||
/** | ||
* @return Generator<ParseResult> | ||
*/ | ||
public function parse(Response $response): Generator | ||
{ | ||
$body = $response->filter('body')->text(); | ||
yield $this->item([ | ||
$body, | ||
]); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace App\Spiders; | ||
|
||
class ResponseDto extends \Spatie\LaravelData\Data | ||
{ | ||
public function __construct( | ||
public string $content) | ||
{ | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace App\Tools; | ||
|
||
use App\Spiders\GetPageSpider; | ||
use Illuminate\Support\Arr; | ||
use RoachPHP\ItemPipeline\Item; | ||
use RoachPHP\Roach; | ||
use RoachPHP\Spider\Configuration\Overrides; | ||
|
||
class GetSiteWrapper | ||
{ | ||
public function handle(string $url): string|null | ||
{ | ||
|
||
/** @var Item[] $items */ | ||
$items = Roach::collectSpider( | ||
GetPageSpider::class, | ||
new Overrides(startUrls: [$url]) | ||
); | ||
|
||
$item = Arr::first($items); | ||
|
||
if ($item) { | ||
return $item->get(0); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
Oops, something went wrong.