Automation/app/Libraries/MyCrawler/Mangboard/MangboardCrawler.php
2024-09-18 20:20:58 +09:00

119 lines
4.7 KiB
PHP

<?php
namespace App\Libraries\MyCrawler\Mangboard;
use App\Entities\Mangboard\BoardsEntity;
use App\Entities\Mangboard\UserEntity;
use App\Libraries\MyCrawler\MyCrawler;
use App\Libraries\MySocket\WebSocket;
use App\Libraries\MyStorage\MangboardStorage;
use App\Models\Mangboard\BoardModel;
use App\Models\Mangboard\BoardsModel;
abstract class MangboardCrawler extends MyCrawler
{
private $_mySocket = null;
private $_host = "";
private $_board_name = "";
private $_board_model = null;
private $_boards_entity = null;
private $_user_entity = null;
protected function __construct(string $host, string $board_name, UserEntity $user_entity)
{
parent::__construct();
$this->_host = $host;
$this->_board_name = $board_name;
$this->_user_entity = $user_entity;
}
abstract protected function detail_process(int $cnt, array $listInfo): array;
abstract public function execute(): void;
final protected function getMySocket()
{
if ($this->_mySocket === null) {
$this->_mySocket = new WebSocket($this->_host);
}
return $this->_mySocket;
}
final protected function createMyStorage()
{
return new MangboardStorage($this->getBoardName(), $this->getUserEntity());
}
final protected function getBoardModel(): BoardModel
{
if ($this->_board_model === null) {
$this->_board_model = new BoardModel("mb_" . $this->getBoardName());
}
return $this->_board_model;
}
final protected function getBoardName(): string
{
return $this->_board_name;
}
final protected function getBoardsEntity(): BoardsEntity
{
if ($this->_boards_entity === null) {
$boardsModel = new BoardsModel();
$this->_boards_entity = $boardsModel->getEntityByID($this->getBoardName());
if ($this->_boards_entity === null) {
throw new \Exception(__FUNCTION__ . "=> {$this->getBoardName()}에 해당 Board 정보가 존재하지 않습니다.");
}
}
return $this->_boards_entity;
}
final protected function getUserEntity(): UserEntity
{
return $this->_user_entity;
}
protected function backend_process(int $cnt, array $listInfo, array $storages)
{
//File DB 및 Board DB 등록작업등
$board_entity = $this->getBoardModel()->createByCrawler(
$this->getBoardsEntity(),
$this->getUserEntity(),
$cnt,
$listInfo,
$storages
);
foreach ($storages as $storage) {
try {
$storage->backend_process($this->getBoardsEntity(), $board_entity, $this->getBoardModel()->getTable());
} catch (\Exception $e) {
log_message("notice", sprintf(
"\n---%s -> %s 게시물의 %s번째:%s 파일 등록 오류---\n%s\n--------------------------------\n",
__FUNCTION__,
$board_entity->getTitle(),
$storage->getOriginSequence(),
$storage->getOriginName(),
$e->getMessage()
));
}
}
log_message("notice", __FUNCTION__ . " 작업이 완료되었습니다.");
}
protected function list_process(int $max_limit, array $listInfos): void
{
//Limit가 0이면 $listInfos 갯수만큼 다하고, LIMIT 갯수 혹은 item의 갯수중 작은수만큼 한다.
$max_limit = !$max_limit || count($listInfos) <= $max_limit ? count($listInfos) : $max_limit;
$total = count($listInfos);
$i = 1;
foreach ($listInfos as $listInfo) {
if ($i <= $max_limit) {
log_message("notice", __FUNCTION__ . " 게시물 {$i}번째/총:{$total} {$listInfo["nickname"]} 작업시작");
try {
//listInfo는 title,작성자,작성시간등등의 정보를 가지고 있어 detail_process 처리 안에서 바뀔 수 있으므로 다시 반환 받는다.
$listInfo = $this->detail_process($i, $listInfo);
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s {$i}번째/총:{$total} 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$e->getMessage()
));
}
log_message("notice", __FUNCTION__ . " 게시물 {$i}번째/총:{$total} {$listInfo["nickname"]} 작업완료.");
$i++;
}
}
log_message("notice", __FUNCTION__ . " 작업이 완료되었습니다.");
}
}