Automation/app/Controllers/CLI/Crawler.php
2024-09-04 18:59:45 +09:00

44 lines
1.6 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.list.url");
$crawler = $library->getCrawler($url, getenv("yamap.list.tag"));
$urls = $library->getListURLs(
$crawler,
getenv("yamap.list.item.tag"),
getenv("yamap.list.item.subject.tag"),
getenv("yamap.list.item.nickname.tag"),
getenv("yamap.list.item.nickname.skip")
);
if (!count($urls)) {
throw new \Exception("Target URL이 없습니다.");
}
//2. TargetPage : div.contents 가진 객체를 찾아서 첫번쨰 요소에서만 참조
$url = $isDebug ? getenv("yamap.view.test.url") : $urls[0];
$crawler = $library->getCrawler($url, getenv("yamap.view.content.tag"));
//3. Image
$library->download($crawler, ["tag" => "img", "attr" => "src"]);
//4. Video
$library->download($crawler, ["tag" => "video", "attr" => "src"]);
log_message("info", "완료되었습니다.");
return true;
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return false;
}
}
}