Команда парсера прямых URL
This commit is contained in:
parent
e3c3212f34
commit
75142b5390
|
@ -4,7 +4,7 @@ namespace App\Console\Commands;
|
|||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Console\Command;
|
||||
use Log;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Collect extends Command
|
||||
{
|
||||
|
|
75
app/Console/Commands/Url.php
Normal file
75
app/Console/Commands/Url.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class Url extends Command
|
||||
{
|
||||
/**
|
||||
* The signature of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'url {url}';
|
||||
|
||||
/**
|
||||
* The description of the command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Parse a specific URL';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$url = $this->argument('url');
|
||||
$files = scandir(__DIR__.'/../../Sources/');
|
||||
$parsers = array_map(function($file) {
|
||||
if (strpos($file, '.php') === false) {
|
||||
return '';
|
||||
}
|
||||
return 'App\\Sources\\' . str_replace('.php', '', $file);
|
||||
}, $files);
|
||||
|
||||
$found = false;
|
||||
foreach ($parsers as $parser) {
|
||||
if (empty($parser)) continue;
|
||||
try {
|
||||
$instance = new $parser();
|
||||
if (!method_exists($instance, 'checkPage')) {
|
||||
continue;
|
||||
}
|
||||
if (!$instance->checkPage($url)) {
|
||||
continue;
|
||||
}
|
||||
$found = true;
|
||||
Log::notice('Выбран парсер: '.$instance->keyword);
|
||||
$instance->page($url);
|
||||
} catch (\Exception $e) {
|
||||
Log::error($e->getMessage());
|
||||
Log::debug($e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
Log::notice('Нет подходящего парсера.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the command's schedule.
|
||||
*
|
||||
* @param \Illuminate\Console\Scheduling\Schedule $schedule
|
||||
* @return void
|
||||
*/
|
||||
public function schedule(Schedule $schedule): void
|
||||
{
|
||||
$schedule->command(static::class)->daily();
|
||||
}
|
||||
}
|
|
@ -32,8 +32,11 @@ class Ifiction extends Source {
|
|||
protected $platform;
|
||||
protected $language_model;
|
||||
|
||||
public function parse() {
|
||||
public function __construct() {
|
||||
$this->language_model = Language::findByCode('ru');
|
||||
}
|
||||
|
||||
public function parse() {
|
||||
$this->page('https://forum.ifiction.ru/viewtopic.php?id=2424&lid=2');
|
||||
$text = $this->get_text('https://forum.ifiction.ru/viewforum.php?id=36');
|
||||
$this->loadStr($text);
|
||||
|
@ -67,7 +70,7 @@ class Ifiction extends Source {
|
|||
});
|
||||
}
|
||||
public function checkPage($url) {
|
||||
$is_ifiction = (strpos($url,'https://forum.ifiction.ru/viewtopic.php?id=') !== FALSE);
|
||||
$is_ifiction = (strpos($url,'https://forum.ifiction.ru/viewtopic.php') !== FALSE);
|
||||
$is_list = (strpos($url, '&list=') !== FALSE);
|
||||
return $is_ifiction && !$is_list;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue