Skip to content

Commit

Permalink
adicionado cliente http webdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigoaramburu committed Dec 12, 2023
1 parent 01f9ad0 commit 9b0242a
Show file tree
Hide file tree
Showing 36 changed files with 1,173 additions and 490 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
exemplo
.vscode
.vscode
coverage
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@



# Executar Selenium

docker run --rm --net=host -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:latest
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"guzzlehttp/guzzle": "^7.8",
"symfony/dom-crawler": "^6.3",
"symfony/css-selector": "^6.3",
"monolog/monolog": "^3.5"
"monolog/monolog": "^3.5",
"php-webdriver/webdriver": "^1.15"
},
"require-dev": {
"pestphp/pest": "^2.24",
Expand Down
190 changes: 128 additions & 62 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions src/HttpClient/AssetFetcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace ScraPHP\HttpClient;

use Psr\Log\LoggerInterface;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use ScraPHP\Exceptions\HttpClientException;
use ScraPHP\Exceptions\AssetNotFoundException;

final class AssetFetcher
{

private \GuzzleHttp\Client $client;

public function __construct(
private LoggerInterface $logger,
){
$this->client = new \GuzzleHttp\Client();
}

/**
* Fetches an asset from the given URL.
*
* @param string $url The URL of the asset.
* @return string The contents of the asset.
*
* @throws AssetNotFoundException If the asset could not be found.
*/
public function fetchAsset(string $url): string
{
try {
$this->logger->info('Fetching asset '.$url);
$response = $this->client->request('GET', $url);
$this->logger->info('Status: '.$response->getStatusCode().' '.$url);
} catch (ClientException $e) {
if ($e->getCode() === 404) {
$this->logger->error('404 NOT FOUND '.$url);
throw new AssetNotFoundException($url.' not found');
}
} catch(ConnectException $e) {
throw new HttpClientException($e->getMessage(), $e->getCode(), $e);
}

return $response->getBody()->getContents();
}
}
16 changes: 16 additions & 0 deletions src/HttpClient/FilteredElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace ScraPHP\HttpClient;

interface FilteredElement
{
public function text(): string;

public function attr(string $attr): ?string;

public function filterCSS(string $cssSelector): ?FilteredElement;

public function filterCSSEach(string $cssSelector, callable $callback): array;

}
33 changes: 0 additions & 33 deletions src/HttpClient/Guzzle/FilterCss.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/HttpClient/Guzzle/FilterCssEach.php

This file was deleted.

Loading

0 comments on commit 9b0242a

Please sign in to comment.