mirror of
https://github.com/manualdousuario/marreta.git
synced 2026-05-05 23:36:42 +00:00
adiciona suporte a requisição via selenium gurizada
This commit is contained in:
parent
a6fc451f0a
commit
2b842a7cd6
10 changed files with 121 additions and 28 deletions
|
|
@ -5,7 +5,6 @@ piaui.folha.uol.com.br
|
|||
jota.info
|
||||
haaretz.com
|
||||
haaretz.co.il
|
||||
gauchazh.clicrbs.com.br
|
||||
economist.com
|
||||
liberation.fr
|
||||
lesoir.be
|
||||
|
|
@ -66,6 +65,7 @@ https://www.agazeta.com.br/concursos-e-empregos/concursos/prefeitura-de-cariacic
|
|||
https://natelinha.uol.com.br/televisao/2024/12/05/boninho-fecha-com-o-sbt-para-novo-reality-show-219855.php
|
||||
https://gamarevista.uol.com.br/semana/deu-vontade-de-ter-outra-vida/novas-formas-abandonar/
|
||||
https://tecnoblog.net/noticias/cor-do-ano-esta-em-celulares-da-motorola-que-serao-vendidos-no-brasil/
|
||||
https://gauchazh.clicrbs.com.br/pioneiro/policia/noticia/2024/11/caxias-do-sul-podera-fazer-emprestimo-de-ate-us-40-milhoes-para-melhorias-tecnologicas-em-educacao-seguranca-e-servicos-municipais-cm3q9yn870051014fzz77djqz.html
|
||||
|
||||
## Internacional
|
||||
https://www.nytimes.com/2024/11/20/us/politics/matt-gaetz-venmo-payments-sex.html
|
||||
|
|
|
|||
|
|
@ -27,4 +27,7 @@ S3_BUCKET=
|
|||
S3_REGION=us-east-1
|
||||
S3_FOLDER=cache/
|
||||
S3_ACL=private
|
||||
S3_ENDPOINT=
|
||||
S3_ENDPOINT=
|
||||
|
||||
# Configurações do Selenium
|
||||
SELENIUM_HOST=localhost:4444
|
||||
|
|
@ -2,7 +2,8 @@
|
|||
"require": {
|
||||
"vlucas/phpdotenv": "^5.6.1",
|
||||
"aws/aws-sdk-php": "^3.0",
|
||||
"php-curl-class/php-curl-class": "^11.0"
|
||||
"php-curl-class/php-curl-class": "^11.0",
|
||||
"php-webdriver/webdriver": "^1.15"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
|
|
|||
|
|
@ -25,10 +25,10 @@ $dotenv->load();
|
|||
define('SITE_NAME', isset($_ENV['SITE_NAME']) ? $_ENV['SITE_NAME'] : 'Marreta');
|
||||
define('SITE_DESCRIPTION', isset($_ENV['SITE_DESCRIPTION']) ? $_ENV['SITE_DESCRIPTION'] : 'Chapéu de paywall é marreta!');
|
||||
define('SITE_URL', isset($_ENV['SITE_URL']) ? $_ENV['SITE_URL'] : 'https://' . $_SERVER['HTTP_HOST']);
|
||||
define('MAX_ATTEMPTS', 3); // Número máximo de tentativas para acessar uma URL
|
||||
define('DNS_SERVERS', isset($_ENV['DNS_SERVERS']) ? $_ENV['DNS_SERVERS'] : '1.1.1.1, 8.8.8.8');
|
||||
define('CACHE_DIR', __DIR__ . '/cache');
|
||||
define('DISABLE_CACHE', isset($_ENV['DISABLE_CACHE']) ? filter_var($_ENV['DISABLE_CACHE'], FILTER_VALIDATE_BOOLEAN) : false);
|
||||
define('SELENIUM_HOST', isset($_ENV['SELENIUM_HOST']) ? $_ENV['SELENIUM_HOST'] : 'localhost:4444');
|
||||
|
||||
/**
|
||||
* Configurações de Cache S3
|
||||
|
|
|
|||
|
|
@ -23,7 +23,6 @@ return [
|
|||
'ole.com.ar',
|
||||
//-- Bloqueio tecnico de acesso ao conteudo
|
||||
'bloomberg.com',
|
||||
'gauchazh.clicrbs.com.br',
|
||||
'opopular.com.br',
|
||||
'npr.org',
|
||||
'sportskeeda.com',
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
* 'scriptTagRemove' => ['gtm.js', 'ga.js'], // Exclui scripts específicos das regras globais
|
||||
* 'classElementRemove' => ['subscription'] // Exclui classes específicas das regras globais
|
||||
* ]
|
||||
* - useSelenium: Boolean indicando se deve usar Selenium para extração
|
||||
*/
|
||||
return [
|
||||
'nsctotal.com.br' => [
|
||||
|
|
@ -38,6 +39,14 @@ return [
|
|||
],
|
||||
'classAttrRemove' => ['wall', 'protected-content', 'cropped-block']
|
||||
],
|
||||
'gauchazh.clicrbs.com.br' => [
|
||||
'classAttrRemove' => [' m-paid-content', 'paid-content-apply'],
|
||||
'scriptTagRemove' => ['vendors-', 'verdors-'],
|
||||
'excludeGlobalRules' => [
|
||||
'classElementRemove' => ['paid-content']
|
||||
],
|
||||
'useSelenium' => true
|
||||
],
|
||||
'foreignaffairs.com' => [
|
||||
'customCode' => 'document.addEventListener(\'DOMContentLoaded\', function() {
|
||||
const dropcapDiv = document.querySelector(\'.article-dropcap\');
|
||||
|
|
|
|||
|
|
@ -60,6 +60,12 @@ return [
|
|||
'lgpd',
|
||||
'push',
|
||||
'sw.js',
|
||||
'stats.js'
|
||||
'stats.js',
|
||||
'piano.io',
|
||||
'onesignal.com',
|
||||
'getsitecontrol.com',
|
||||
'navdmp.com',
|
||||
'getblue.io',
|
||||
'smartocto.com'
|
||||
]
|
||||
];
|
||||
|
|
|
|||
|
|
@ -34,7 +34,9 @@ class Rules
|
|||
'cookies',
|
||||
'classAttrRemove',
|
||||
'customCode',
|
||||
'excludeGlobalRules'
|
||||
'excludeGlobalRules',
|
||||
'customStyle',
|
||||
'useSelenium'
|
||||
];
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -10,15 +10,21 @@
|
|||
* - Requisições HTTP com múltiplas tentativas
|
||||
* - Processamento de conteúdo baseado em regras específicas por domínio
|
||||
* - Suporte a Wayback Machine como fallback
|
||||
* - Suporte a extração via Selenium quando habilitado por domínio
|
||||
*/
|
||||
|
||||
require_once 'Rules.php';
|
||||
require_once 'Cache.php';
|
||||
|
||||
use Curl\Curl;
|
||||
use Facebook\WebDriver\Remote\DesiredCapabilities;
|
||||
use Facebook\WebDriver\Remote\RemoteWebDriver;
|
||||
use Facebook\WebDriver\Firefox\FirefoxOptions;
|
||||
use Facebook\WebDriver\Firefox\FirefoxProfile;
|
||||
|
||||
class URLAnalyzer
|
||||
{
|
||||
// Rest of the file content remains exactly the same
|
||||
/**
|
||||
* @var array Lista de User Agents disponíveis para requisições
|
||||
*/
|
||||
|
|
@ -125,31 +131,93 @@ class URLAnalyzer
|
|||
throw new Exception($error);
|
||||
}
|
||||
|
||||
// 4. Tenta buscar conteúdo diretamente
|
||||
try {
|
||||
$content = $this->fetchContent($cleanUrl);
|
||||
if (!empty($content)) {
|
||||
$processedContent = $this->processContent($content, $host, $cleanUrl);
|
||||
$this->cache->set($cleanUrl, $processedContent);
|
||||
return $processedContent;
|
||||
// 4. Verifica se deve usar Selenium
|
||||
$domainRules = $this->getDomainRules($host);
|
||||
if (isset($domainRules['useSelenium']) && $domainRules['useSelenium'] === true) {
|
||||
try {
|
||||
$content = $this->fetchFromSelenium($cleanUrl);
|
||||
if (!empty($content)) {
|
||||
$processedContent = $this->processContent($content, $host, $cleanUrl);
|
||||
$this->cache->set($cleanUrl, $processedContent);
|
||||
return $processedContent;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->logError($cleanUrl, "Selenium fetch error: " . $e->getMessage());
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->logError($cleanUrl, "Direct fetch error: " . $e->getMessage());
|
||||
} else {
|
||||
// 5. Tenta buscar conteúdo diretamente
|
||||
try {
|
||||
$content = $this->fetchContent($cleanUrl);
|
||||
if (!empty($content)) {
|
||||
$processedContent = $this->processContent($content, $host, $cleanUrl);
|
||||
$this->cache->set($cleanUrl, $processedContent);
|
||||
return $processedContent;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->logError($cleanUrl, "Direct fetch error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
// 6. Tenta buscar do Wayback Machine como fallback
|
||||
try {
|
||||
$content = $this->fetchFromWaybackMachine($cleanUrl);
|
||||
if (!empty($content)) {
|
||||
$processedContent = $this->processContent($content, $host, $cleanUrl);
|
||||
$this->cache->set($cleanUrl, $processedContent);
|
||||
return $processedContent;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->logError($cleanUrl, "Wayback Machine error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
throw new Exception("Não foi possível obter o conteúdo da URL");
|
||||
}
|
||||
|
||||
// 5. Tenta buscar do Wayback Machine como fallback
|
||||
try {
|
||||
$content = $this->fetchFromWaybackMachine($cleanUrl);
|
||||
if (!empty($content)) {
|
||||
$processedContent = $this->processContent($content, $host, $cleanUrl);
|
||||
$this->cache->set($cleanUrl, $processedContent);
|
||||
return $processedContent;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$this->logError($cleanUrl, "Wayback Machine error: " . $e->getMessage());
|
||||
}
|
||||
|
||||
throw new Exception("Não foi possível obter o conteúdo da URL");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tenta obter o conteúdo da URL usando Selenium
|
||||
*
|
||||
* @param string $url URL para buscar
|
||||
* @return string|null Conteúdo HTML da página
|
||||
* @throws Exception Em caso de erro na requisição
|
||||
*/
|
||||
private function fetchFromSelenium($url)
|
||||
{
|
||||
$host = 'http://'.SELENIUM_HOST.'/wd/hub';
|
||||
|
||||
$profile = new FirefoxProfile();
|
||||
$profile->setPreference("permissions.default.image", 2);
|
||||
$profile->setPreference("javascript.enabled", true);
|
||||
|
||||
$options = new FirefoxOptions();
|
||||
$options->setProfile($profile);
|
||||
|
||||
$capabilities = DesiredCapabilities::firefox();
|
||||
$capabilities->setCapability(FirefoxOptions::CAPABILITY, $options);
|
||||
|
||||
try {
|
||||
$driver = RemoteWebDriver::create($host, $capabilities);
|
||||
$driver->manage()->timeouts()->pageLoadTimeout(10);
|
||||
$driver->manage()->timeouts()->setScriptTimeout(5);
|
||||
|
||||
$driver->get($url);
|
||||
|
||||
$htmlSource = $driver->executeScript("return document.documentElement.outerHTML;");
|
||||
|
||||
$driver->quit();
|
||||
|
||||
if (empty($htmlSource)) {
|
||||
throw new Exception("Selenium returned empty content");
|
||||
}
|
||||
|
||||
return $htmlSource;
|
||||
} catch (Exception $e) {
|
||||
if (isset($driver)) {
|
||||
$driver->quit();
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -85,6 +85,11 @@ if [ -n "${S3_ENDPOINT}" ]; then
|
|||
echo "S3_ENDPOINT=${S3_ENDPOINT}" >> /app/.env
|
||||
fi
|
||||
|
||||
# Configurações do Selenium
|
||||
if [ -n "${SELENIUM_HOST}" ]; then
|
||||
echo "SELENIUM_HOST=${SELENIUM_HOST}" >> /app/.env
|
||||
fi
|
||||
|
||||
log_success "Variáveis de ambiente configuradas"
|
||||
|
||||
# === Ajuste de Permissões ===
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue