2020-07-23 12:26:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2020-07-27 12:52:46 +02:00
|
|
|
use GuzzleHttp\Client;
|
2020-07-23 14:55:00 +02:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2020-07-23 12:26:10 +02:00
|
|
|
|
|
|
|
abstract class Parser
|
|
|
|
{
|
|
|
|
protected $client;
|
|
|
|
protected $url;
|
|
|
|
|
|
|
|
protected function __construct($url)
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
2020-07-27 12:52:46 +02:00
|
|
|
$this->client = new Client([
|
2020-07-23 12:26:10 +02:00
|
|
|
'timeout' => 60,
|
|
|
|
'verify' => false,
|
|
|
|
'headers' => [
|
|
|
|
'user-agent' => 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; D5803 Build/MOB30M.Z1)',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
*
|
|
|
|
* @return \App\Parser|null
|
|
|
|
*/
|
2020-07-23 14:55:00 +02:00
|
|
|
public static function factory(string $url): ?Parser
|
2020-07-23 12:26:10 +02:00
|
|
|
{
|
2020-07-23 14:55:00 +02:00
|
|
|
$parsers = Config::get('parser.parsers', []);
|
|
|
|
foreach ($parsers as $domain => $parser) {
|
|
|
|
if (false !== strpos($url, $domain)) {
|
|
|
|
return new $parser($url);
|
|
|
|
}
|
2020-07-23 12:26:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \App\ParsedHome
|
|
|
|
*/
|
|
|
|
abstract public function parse(): ParsedHome;
|
|
|
|
}
|