Automation/app/Libraries/YamapLibrary.php
2024-09-05 00:09:04 +09:00

100 lines
3.5 KiB
PHP

<?php
namespace App\Libraries;
use App\Traits\MyCrawlerTrait;
use App\Traits\MyWebTrait;
use App\Traits\MyStorage\MyStorageFileTrait;
use Symfony\Component\DomCrawler\Crawler;
class YamapLibrary
{
use MyWebTrait, MyStorageFileTrait, MyCrawlerTrait;
private $_debug = false;
public function __construct(string $host)
{
$this->setHostByMyWeb($host);
$this->setPathByMyStorage(WRITEPATH . "uploads" . DIRECTORY_SEPARATOR . "Yamap");
}
final public function getDebug(): bool
{
return $this->_debug;
}
final public function setDebug(bool $debug): void
{
$this->_debug = $debug;
}
protected function getCrawler(string $url, string $tag): Crawler
{
$response = $this->getContentByMyWeb($url);
if (!$response) {
throw new \Exception("getCrawler 실패:{$url}");
}
return $this->createByMyCrawler($response)->filter($tag);
}
protected function getList(
Crawler $crawler,
string $item_tag,
string $item_subject_tag,
string $item_nickname_tag,
string $item_nickname_skip,
array $results = []
): array {
//div.bbs_item를 가진 객체를 찾아서 같은 형식의 객체(sibling)를 배열로 넘김
$crawler->filter($item_tag)->each(
function (Crawler $node) use (
$item_subject_tag,
&$item_nickname_tag,
&$item_nickname_skip,
&$results
): void {
//bbs_item에서 span.g_nickname 객체를 찾아서 작성자가 "관리자" 아닌지 확인 후 Return Bool
$nickname = $node->filter($item_nickname_tag)->text();
log_message("debug", $item_nickname_tag . ":" . $nickname);
if ($nickname != $item_nickname_skip) {
//작성자가 "관리자"가 아니 게시물이면 해당 bbs_item에서 a.list_subject 객체를 찾아서
$url = $node->filter($item_subject_tag)->attr("href");
$results[] = ['nickname' => $nickname, 'url' => $url];
}
}
);
return $results;
}
protected function download(Crawler $crawler, array $options): void
{
log_message("debug", "download:{$options["tag"]},{$options["attr"]}");
$nodes = $this->getNodesByMyCrawler($crawler, $options);
foreach ($nodes as $node) {
$this->downloadByMyWeb($node->attr($options["attr"]), $this->getPathByMyStorage(), $this->getDebug());
}
}
public function execute(): void
{
//1. MainPage
$url = getenv("yamap.list.url");
$crawler = $this->getCrawler($url, getenv("yamap.list.tag"));
$lists = $this->getList(
$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($lists)) {
throw new \Exception("Target URL이 없습니다.");
}
//2. TargetPage : div.contents 가진 객체를 찾아서 첫번쨰 요소에서만 참조
$url = $this->getDebug() ? getenv("yamap.view.test.url") : $lists[0]["url"];
$crawler = $this->getCrawler($url, getenv("yamap.view.content.tag"));
//3. Image
$this->download($crawler, ["tag" => "img", "attr" => "src"]);
//4. Video
$this->download($crawler, ["tag" => "video", "attr" => "src"]);
}
}