141 lines
6.0 KiB
PHP
141 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyCrawler;
|
|
|
|
use App\Libraries\MySocket\WebLibrary as MySocketLibrary;
|
|
use App\Libraries\MyStorage\Mangboard\FileLibrary as MyStorageLibrary;
|
|
use App\Models\Mangboard\UserModel;
|
|
use App\Models\Mangboard\BoardModel;
|
|
use App\Entities\Mangboard\BoardEntity;
|
|
use App\Entities\Mangboard\UserEntity;
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
class YamapLibrary extends MyCrawlerLibrary
|
|
{
|
|
private $_userModel = null;
|
|
private $_boardModel = null;
|
|
private $_mySocket = null;
|
|
private $_myStorage = null;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
private function getUserModel(): UserModel
|
|
{
|
|
if ($this->_userModel === null) {
|
|
$this->_userModel = new UserModel();
|
|
}
|
|
return $this->_userModel;
|
|
}
|
|
private function getBoardModel(): BoardModel
|
|
{
|
|
if ($this->_boardModel === null) {
|
|
$this->_boardModel = new BoardModel(getenv("yamap.storage.board.table"));
|
|
}
|
|
return $this->_boardModel;
|
|
}
|
|
|
|
public function getMySocket(): MySocketLibrary
|
|
{
|
|
if ($this->_mySocket === null) {
|
|
$this->_mySocket = new MySocketLibrary(getenv('yamap.host.url'));
|
|
}
|
|
return $this->_mySocket;
|
|
}
|
|
|
|
public function getMyStorage(): MyStorageLibrary
|
|
{
|
|
if ($this->_myStorage === null) {
|
|
$this->_myStorage = new MyStorageLibrary(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;
|
|
}
|
|
|
|
public function login(): UserEntity
|
|
{
|
|
$entity = $this->getUserModel()->getEntityByID("idcjp");
|
|
// $daemonidc = new MySocketLibrary(getenv('daemonidc.host.url'));
|
|
// $daemonidc->setDebug($isDebug);
|
|
// $daemonidc->login(
|
|
// getenv('daemonidc.login.url'),
|
|
// getenv('daemonidc.login.user_id'),
|
|
// getenv('daemonidc.login.user_password')
|
|
// );
|
|
return $entity;
|
|
}
|
|
|
|
public 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 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;
|
|
foreach ($myStorageLibrarys as $myStorageLibrary) {
|
|
if ($formDatas['image_path'] === false) {
|
|
$formDatas['image_path'] = $myStorageLibrary->getFileEntity()->getPath();
|
|
}
|
|
$formDatas['content'] .= $myStorageLibrary->getFileEntity()->getMediaHTML();
|
|
}
|
|
//망보드 게시판에 등록
|
|
$entity = $this->getBoardModel()->create($formDatas);
|
|
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
|
|
foreach ($myStorageLibrarys as $myStorageLibrary) {
|
|
$myStorageLibrary->setBoardPID(intval($entity->getPK()));
|
|
}
|
|
log_message("notice", __FUNCTION__ . " 작업 완료");
|
|
}
|
|
|
|
public 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);
|
|
log_message("notice", __FUNCTION__ . " 작업 완료");
|
|
//5.망보드 일반게시판에 게시물 등록 처리
|
|
if (count($myStorageLibrarys)) {
|
|
$this->createBoard($itemInfo, $myStorageLibrarys);
|
|
}
|
|
}
|
|
}
|