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; }