Automation/app/Libraries/YamapLibrary.php
2024-09-04 19:29:52 +09:00

75 lines
2.4 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;
}
public function getCrawler(string $url, string $tag): Crawler
{
$response = $this->getContentByMyWeb($url);
if (!$response) {
throw new \Exception("getCrawler 실패:{$url}");
}
return $this->createByMyCrawler($response)->filter($tag);
}
public function getListURLs(
Crawler $crawler,
string $item_tag,
string $item_subject_tag,
string $item_nickname_tag,
string $item_nickname_skip,
array $urls = []
): array {
//div.bbs_item를 가진 객체를 찾아서 같은 형식의 객체(sibling)를 배열로 넘김
$crawler->filter($item_tag)->each(
function (Crawler $node) use (
&$urls,
$item_subject_tag,
&$item_nickname_tag,
&$item_nickname_skip
): 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) {
$options = ["tag" => $item_subject_tag, "attr" => "href"];
$urls = $this->getTagDatasByMyCrawler($node, $options);
}
}
);
return $urls;
}
public function download(Crawler $crawler, array $options): void
{
log_message("debug", "download:{$options["tag"]},{$options["attr"]}");
$urls = $this->getTagDatasByMyCrawler($crawler, $options);
foreach ($urls as $url) {
$this->downloadByMyWeb($url, $this->getPathByMyStorage(), $this->getDebug());
}
}
}