Automation/app/Libraries/MyCrawler/Mangboard/MangboardCrawler.php
2024-09-18 17:19:48 +09:00

97 lines
4.0 KiB
PHP

<?php
namespace App\Libraries\MyCrawler\Mangboard;
use App\Libraries\MySocket\WebSocket;
use App\Entities\Mangboard\UserEntity;
use App\Libraries\MyCrawler\MyCrawler;
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 = "";
protected $_board_name = "";
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(int $max_limit): 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->_board_name, $this->_user_entity);
}
protected function backend_process(int $cnt, array $listInfo, array $storages)
{
//File DB 및 Board DB 등록작업등
$baord_name = $this->_board_name;
$boardsModel = new BoardsModel();
$boards_entity = $boardsModel->getEntityByID($this->_board_name);
if ($boards_entity === null) {
throw new \Exception(__FUNCTION__ . "=>{$this->_board_name}에 해당 Board 정보가 존재하지 않습니다.");
}
$boardModel = new BoardModel("mb_" . $baord_name);
$board_entity = $boardModel->createByCrawler(
$boards_entity,
$this->_user_entity,
$cnt,
$listInfo,
$storages
);
foreach ($storages as $storage) {
try {
$storage->backend_process($boards_entity, $board_entity, $boardModel->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__ . " 작업이 완료되었습니다.");
}
}