From 75142b5390e90121e9b1294de9dc94a20dc2c755 Mon Sep 17 00:00:00 2001 From: Alexander Yakovlev Date: Fri, 3 Mar 2023 19:35:07 +0600 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=BF=D0=B0=D1=80=D1=81=D0=B5=D1=80=D0=B0=20=D0=BF=D1=80=D1=8F?= =?UTF-8?q?=D0=BC=D1=8B=D1=85=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/Collect.php | 2 +- app/Console/Commands/Url.php | 75 ++++++++++++++++++++++++++++++++ app/Sources/Ifiction.php | 7 ++- 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 app/Console/Commands/Url.php diff --git a/app/Console/Commands/Collect.php b/app/Console/Commands/Collect.php index 4246684..e03f0fe 100644 --- a/app/Console/Commands/Collect.php +++ b/app/Console/Commands/Collect.php @@ -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 { diff --git a/app/Console/Commands/Url.php b/app/Console/Commands/Url.php new file mode 100644 index 0000000..4975f8f --- /dev/null +++ b/app/Console/Commands/Url.php @@ -0,0 +1,75 @@ +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(); + } +} diff --git a/app/Sources/Ifiction.php b/app/Sources/Ifiction.php index 4a298d2..cfb448c 100644 --- a/app/Sources/Ifiction.php +++ b/app/Sources/Ifiction.php @@ -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; }