Automation/app/Libraries/MyCrawler/YamapLibrary.php
2024-09-12 00:28:10 +09:00

138 lines
6.2 KiB
PHP

<?php
namespace App\Libraries\MyCrawler;
use App\Models\Mangboard\BoardModel;
use Symfony\Component\DomCrawler\Crawler;
class YamapLibrary extends MyCrawlerLibrary
{
private $_userModel = null;
private $_boardModel = null;
public function __construct($mySocket, $myStorage)
{
parent::__construct($mySocket, $myStorage);
//원래는 mb_board Table에서 해당Board정보를 읽어서 처리해아함
$this->getMyStorage()->setBoardTable($this->getBoardModel()->getTable());
}
private function getBoardModel(): BoardModel
{
if ($this->_boardModel === null) {
$this->_boardModel = new BoardModel(getenv("yamap.storage.board.table"));
}
return $this->_boardModel;
}
private function createBoard(array $itemInfo, array $myStorageLibrarys): void
{
//미디어관련정보 entity에 넣기
$formDatas = [];
$formDatas['title'] = $itemInfo["title"];
$formDatas['user_pid'] = $this->getMyStorage()->getUser()->getPK();
$formDatas['user_id'] = $this->getMyStorage()->getUser()->getID();
$formDatas['user_name'] = $itemInfo["nickname"] != "" ? $itemInfo["nickname"] : $this->getMyStorage()->getUser()->getTitle();
$formDatas['level'] = $this->getMyStorage()->getBoardLevel();
$formDatas['hit'] = $itemInfo['hit'];
$formDatas['reg_date'] = date("Y-m-d H:i:s", strtotime($itemInfo['date']));
$formDatas['data_type'] = "html";
$formDatas['editor_type'] = "S";
$formDatas['image_path'] = false;
$content = "";
foreach ($myStorageLibrarys as $myStorageLibrary) {
if ($formDatas['image_path'] === false) {
$formDatas['image_path'] = $myStorageLibrary->getPath();
}
$content .= $myStorageLibrary->getMediaTag();
}
$formDatas['content'] = $content;
//망보드 게시판에 등록
$entity = $this->getBoardModel()->create($formDatas);
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
foreach ($myStorageLibrarys as $myStorageLibrary) {
$myStorageLibrary->setBoardPID(intval($entity->getPK()));
}
log_message("notice", __FUNCTION__ . " 작업 완료");
}
private function mainPage(string $url): array
{
$crawler = $this->getContent($url, getenv("yamap.list.tag"));
$items = [];
//div.bbs_item를 가진 객체를 찾아서 같은 형식의 객체(sibling)를 배열로 넘김
$crawler->filter(getenv("yamap.list.item.tag"))->each(
function (Crawler $node) use (&$items): void {
//bbs_item에서 span.g_nickname 객체를 찾아서 작성자가 "관리자" 아닌지 확인 후 Return Bool
$nickname = $node->filter(getenv("yamap.list.item.nickname.tag"))->text();
$hit = $node->filter(getenv("yamap.list.item.hit.tag"))->text();
$date = $node->filter(getenv("yamap.list.item.date.tag"))->text();
if ($nickname != getenv("yamap.list.item.nickname.except")) {
//작성자가 "관리자"가 아니 게시물이면 해당 bbs_item에서 a.list_subject 객체를 찾아서
$link_node = $node->filter(getenv("yamap.list.item.link.tag"));
$detail_url = $link_node->attr("href");
$title = $link_node->children()->last()->text();
$items[] = ['title' => $title, 'nickname' => $nickname, 'detail_url' => $detail_url, 'date' => $date, 'hit' => $hit];
}
}
);
if (!count($items)) {
throw new \Exception("Target URL이 없습니다.");
}
log_message("notice", __FUNCTION__ . " 작업 완료");
return $items;
}
private function detailPage(array $itemInfo)
{
$crawler = $this->getContent($itemInfo['detail_url'], getenv("yamap.view.content.tag"));
//3. Image 처리
$myStorageLibrarys = $this->download("image", $crawler, ["tag" => "img", "attr" => "src"]);
//4. Video(mp4) 처리
$myStorageLibrarys = $this->download("video", $crawler, ["tag" => "video", "attr" => "src"], $myStorageLibrarys);
//5.망보드 일반게시판에 게시물 등록 처리
if (count($myStorageLibrarys)) {
$this->createBoard($itemInfo, $myStorageLibrarys);
}
log_message("notice", __FUNCTION__ . " 작업 완료");
}
public function execute(): void
{
//. 해당사이트 MainPage 처리
$itemInfos = [];
if ($this->getDebug()) {
$itemInfos[] = [
'title' => getenv("yamap.view.test.title"),
'nickname' => getenv("yamap.view.test.nickname"),
'detail_url' => getenv("yamap.view.test.url"),
'time' => date("Y-m-d H:i:s"),
'hit' => 1
];
} else {
$itemInfos = $this->mainPage(getenv("yamap.list.url"));
}
if (!count($itemInfos)) {
throw new \Exception("Yamap 사이트에서 게시물이 존재하지 않습니다.");
}
//Limit가 0이면 $itemInfos 갯수만큼 다하고, LIMIT 갯수 혹은 $items의 갯수중 작은수만큼 한다.
$max_limit = intval(getenv("yamap.list.max_limit"));
if ($max_limit) {
$max_limit = count($itemInfos) <= $max_limit ? count($itemInfos) : $max_limit;
} else {
$max_limit = count($itemInfos);
}
$i = 1;
foreach ($itemInfos as $itemInfo) {
if ($i < $max_limit) {
try {
log_message("notice", "게시물 {$i}번째 {$itemInfo["nickname"]} 작업시작");
$this->detailPage($itemInfo);
log_message("notice", "게시물 {$i}번째 {$itemInfo["nickname"]} 작업완료.");
$i++;
} catch (\Exception $e) {
log_message("debug", $e->getMessage());
}
}
}
log_message("notice", "Crawler->" . __FUNCTION__ . " 작업이 완료되었습니다.");
}
}