|
10 | 10 | * - Requisições HTTP com múltiplas tentativas |
11 | 11 | * - Processamento de conteúdo baseado em regras específicas por domínio |
12 | 12 | * - Suporte a Wayback Machine como fallback |
| 13 | + * - Suporte a extração via Selenium quando habilitado por domínio |
13 | 14 | */ |
14 | 15 |
|
15 | 16 | require_once 'Rules.php'; |
16 | 17 | require_once 'Cache.php'; |
17 | 18 |
|
18 | 19 | use Curl\Curl; |
| 20 | +use Facebook\WebDriver\Remote\DesiredCapabilities; |
| 21 | +use Facebook\WebDriver\Remote\RemoteWebDriver; |
| 22 | +use Facebook\WebDriver\Firefox\FirefoxOptions; |
| 23 | +use Facebook\WebDriver\Firefox\FirefoxProfile; |
19 | 24 |
|
20 | 25 | class URLAnalyzer |
21 | 26 | { |
| 27 | + // Rest of the file content remains exactly the same |
22 | 28 | /** |
23 | 29 | * @var array Lista de User Agents disponíveis para requisições |
24 | 30 | */ |
@@ -125,31 +131,93 @@ public function analyze($url) |
125 | 131 | throw new Exception($error); |
126 | 132 | } |
127 | 133 |
|
128 | | - // 4. Tenta buscar conteúdo diretamente |
129 | | - try { |
130 | | - $content = $this->fetchContent($cleanUrl); |
131 | | - if (!empty($content)) { |
132 | | - $processedContent = $this->processContent($content, $host, $cleanUrl); |
133 | | - $this->cache->set($cleanUrl, $processedContent); |
134 | | - return $processedContent; |
| 134 | + // 4. Verifica se deve usar Selenium |
| 135 | + $domainRules = $this->getDomainRules($host); |
| 136 | + if (isset($domainRules['useSelenium']) && $domainRules['useSelenium'] === true) { |
| 137 | + try { |
| 138 | + $content = $this->fetchFromSelenium($cleanUrl); |
| 139 | + if (!empty($content)) { |
| 140 | + $processedContent = $this->processContent($content, $host, $cleanUrl); |
| 141 | + $this->cache->set($cleanUrl, $processedContent); |
| 142 | + return $processedContent; |
| 143 | + } |
| 144 | + } catch (Exception $e) { |
| 145 | + $this->logError($cleanUrl, "Selenium fetch error: " . $e->getMessage()); |
135 | 146 | } |
136 | | - } catch (Exception $e) { |
137 | | - $this->logError($cleanUrl, "Direct fetch error: " . $e->getMessage()); |
| 147 | + } else { |
| 148 | + // 5. Tenta buscar conteúdo diretamente |
| 149 | + try { |
| 150 | + $content = $this->fetchContent($cleanUrl); |
| 151 | + if (!empty($content)) { |
| 152 | + $processedContent = $this->processContent($content, $host, $cleanUrl); |
| 153 | + $this->cache->set($cleanUrl, $processedContent); |
| 154 | + return $processedContent; |
| 155 | + } |
| 156 | + } catch (Exception $e) { |
| 157 | + $this->logError($cleanUrl, "Direct fetch error: " . $e->getMessage()); |
| 158 | + } |
| 159 | + |
| 160 | + // 6. Tenta buscar do Wayback Machine como fallback |
| 161 | + try { |
| 162 | + $content = $this->fetchFromWaybackMachine($cleanUrl); |
| 163 | + if (!empty($content)) { |
| 164 | + $processedContent = $this->processContent($content, $host, $cleanUrl); |
| 165 | + $this->cache->set($cleanUrl, $processedContent); |
| 166 | + return $processedContent; |
| 167 | + } |
| 168 | + } catch (Exception $e) { |
| 169 | + $this->logError($cleanUrl, "Wayback Machine error: " . $e->getMessage()); |
| 170 | + } |
| 171 | + |
| 172 | + throw new Exception("Não foi possível obter o conteúdo da URL"); |
138 | 173 | } |
139 | 174 |
|
140 | | - // 5. Tenta buscar do Wayback Machine como fallback |
| 175 | + |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Tenta obter o conteúdo da URL usando Selenium |
| 180 | + * |
| 181 | + * @param string $url URL para buscar |
| 182 | + * @return string|null Conteúdo HTML da página |
| 183 | + * @throws Exception Em caso de erro na requisição |
| 184 | + */ |
| 185 | + private function fetchFromSelenium($url) |
| 186 | + { |
| 187 | + $host = 'http://'.SELENIUM_HOST.'/wd/hub'; |
| 188 | + |
| 189 | + $profile = new FirefoxProfile(); |
| 190 | + $profile->setPreference("permissions.default.image", 2); |
| 191 | + $profile->setPreference("javascript.enabled", true); |
| 192 | + |
| 193 | + $options = new FirefoxOptions(); |
| 194 | + $options->setProfile($profile); |
| 195 | + |
| 196 | + $capabilities = DesiredCapabilities::firefox(); |
| 197 | + $capabilities->setCapability(FirefoxOptions::CAPABILITY, $options); |
| 198 | + |
141 | 199 | try { |
142 | | - $content = $this->fetchFromWaybackMachine($cleanUrl); |
143 | | - if (!empty($content)) { |
144 | | - $processedContent = $this->processContent($content, $host, $cleanUrl); |
145 | | - $this->cache->set($cleanUrl, $processedContent); |
146 | | - return $processedContent; |
| 200 | + $driver = RemoteWebDriver::create($host, $capabilities); |
| 201 | + $driver->manage()->timeouts()->pageLoadTimeout(10); |
| 202 | + $driver->manage()->timeouts()->setScriptTimeout(5); |
| 203 | + |
| 204 | + $driver->get($url); |
| 205 | + |
| 206 | + $htmlSource = $driver->executeScript("return document.documentElement.outerHTML;"); |
| 207 | + |
| 208 | + $driver->quit(); |
| 209 | + |
| 210 | + if (empty($htmlSource)) { |
| 211 | + throw new Exception("Selenium returned empty content"); |
147 | 212 | } |
| 213 | + |
| 214 | + return $htmlSource; |
148 | 215 | } catch (Exception $e) { |
149 | | - $this->logError($cleanUrl, "Wayback Machine error: " . $e->getMessage()); |
| 216 | + if (isset($driver)) { |
| 217 | + $driver->quit(); |
| 218 | + } |
| 219 | + throw $e; |
150 | 220 | } |
151 | | - |
152 | | - throw new Exception("Não foi possível obter o conteúdo da URL"); |
153 | 221 | } |
154 | 222 |
|
155 | 223 | /** |
|
0 commit comments