Automation/app/Libraries/MyCrawler/YamapLibrary.php
2024-09-08 18:45:08 +09:00

158 lines
6.4 KiB
PHP

<?php
namespace App\Libraries\MyCrawler;
use Symfony\Component\DomCrawler\Crawler;
use App\Models\Mangboard\UserModel;
use App\Models\Mangboard\BoardModel;
use App\Libraries\MySocket\WebLibrary;
use App\Libraries\Mangboard\FileLibrary;
use App\Libraries\Mangboard\BoardLibrary;
use App\Entities\Mangboard\UserEntity;
use App\Entities\Mangboard\BoardEntity;
class YamapLibrary extends MyCrawlerLibrary
{
private $_mySocket = null;
private $_myStorage = null;
private $_boardModel = null;
public function __construct()
{
parent::__construct();
}
protected function getMySocket(): WebLibrary
{
if ($this->_mySocket === null) {
$this->_mySocket = new WebLibrary(getenv('yamap.host.url'));
}
return $this->_mySocket;
}
protected function getMyStorage(): FileLibrary
{
if ($this->_myStorage === null) {
$this->_myStorage = new FileLibrary(getenv('yamap.storage.upload.path'));
//원래는 mb_board에서 해당Board정보를 읽어서 처리해아함
$this->_myStorage->setBoardName(getenv('yamap.storage.board.name'));
$this->_myStorage->setBoardTable($this->getBoardModel()->getTable());
$this->_myStorage->setBoardlevel(getenv('yamap.storage.board.level'));
}
return $this->_myStorage;
}
private function getBoardModel(): BoardModel
{
if ($this->_boardModel === null) {
$this->_boardModel = new BoardModel(getenv("yamap.storage.board.table"));
}
return $this->_boardModel;
}
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이 없습니다.");
}
return $items;
}
private function detailPage(string $url, array $fileEntitys = []): array
{
$crawler = $this->getContent($url, getenv("yamap.view.content.tag"));
//3. Image 처리
$fileEntitys = $this->download("image", $crawler, ["tag" => "img", "attr" => "src"]);
//4. Video(mp4) 처리
return $this->download("video", $crawler, ["tag" => "video", "attr" => "src"], $fileEntitys);
}
private function createBoard(array $item, array $fileEntitys): BoardEntity
{
$board = new BoardLibrary($this->getBoardModel());
$board->setDebug($this->getDebug());
//미디어관련정보 entity에 넣기
$entity = new BoardEntity();
$entity->title = $item["title"];
$entity->user_pid = $this->getMyStorage()->getUser()->getPK();
$entity->user_id = $this->getMyStorage()->getUser()->getID();
$entity->user_name = $item["nickname"] != "" ? $item["nickname"] : $this->getMyStorage()->getUser()->getTitle();
$entity->level = $this->getMyStorage()->getBoardLevel();
$entity->hit = $item['hit'];
$entity->reg_date = date("Y-m-d H:i:s", strtotime($item['date']));
$entity->data_type = "html";
$entity->editor_type = "S";
foreach ($fileEntitys as $fileEntity) {
$entity->content .= $fileEntity->getMediaHTML();
}
// echo $entity->title . "\n";
// echo $entity->user_name . "\n";
// echo $entity->hit . "\n";
// echo $entity->reg_date . "\n";
// exit;
//망보드 게시판에 등록
$entity = $board->create($entity);
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
foreach ($fileEntitys as $fileEntity) {
$this->getMyStorage()->updateFileEntityBoardPK($fileEntity, $entity);
}
return $entity;
}
public function execute(): void
{
//1. 사이트 로그인 처리
$userModel = new UserModel();
$user = $userModel->getEntityByID("idcjp");
// $daemonidc = new MyWebLibrary(getenv('daemonidc.host.url'));
// $daemonidc->setDebug($isDebug);
// $daemonidc->login(
// getenv('daemonidc.login.url'),
// getenv('daemonidc.login.user_id'),
// getenv('daemonidc.login.user_password')
// );
$this->getMyStorage()->setUser($user);
$items = [];
//2. 해당사이트 MainPage 처리
if ($this->getDebug()) {
$items[] = [
'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 {
$items = $this->mainPage(getenv("yamap.list.url"));
}
if (!count($items)) {
throw new \Exception("Yamap 사이트에서 게시물이 존재하지 않습니다.");
}
//최초 게시물만 등록하기 위함
$item = $items[0];
//3. DetailPage 처리 : bbs_view > div.contents 가진 객체를 찾아서 처리
$fileEntitys = $this->detailPage($item["detail_url"]);
//4.망보드 일반게시판에 게시물 등록 처리
$this->createBoard($item, $fileEntitys);
}
}