Automation/app/Controllers/CLI/Crawler.php
2024-09-03 19:48:06 +09:00

38 lines
1.2 KiB
PHP

<?php
namespace App\Controllers\CLI;
use App\Controllers\BaseController;
use App\Libraries\YamapLibrary;
class Crawler extends BaseController
{
public function yamap(...$params): bool
{
try {
$isDebug = in_array("debug", $params);
$library = new YamapLibrary(getenv("yamap.host"));
$library->setDebug($isDebug);
//1. MainPage
$url = getenv("yamap.url.main");
$crawler = $library->getCrawlerByMainPage($url);
$links = $library->getLinks($crawler);
if (!count($links)) {
throw new \Exception("Target Links가 없습니다.");
}
//2. TargetPage : div.contents 가진 객체를 찾아서 첫번쨰 요소에서만 참조
$url = $isDebug ? getenv("yamap.url.test") : $links[0]["href"];
$crawler = $library->getCrawlerByDetailPage($url);
//3. Image
$library->getImages($crawler);
//4. Video
$library->getVideos($crawler);
log_message("info", "완료되었습니다.");
return true;
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return false;
}
}
}