Automation/app/Controllers/Mangboard/CrawlerController.php
2024-09-22 01:04:33 +09:00

137 lines
5.3 KiB
PHP

<?php
namespace App\Controllers\Mangboard;
use Psr\Log\LoggerInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\RequestInterface;
use App\Models\Mangboard\UserModel;
use App\Libraries\MyStorage\MangboardStorage;
use App\Libraries\MySocket\WebSocket;
use App\Libraries\MyCrawler\Yamap;
use App\Entities\Mangboard\UserEntity;
use App\Controllers\CommonController;
class CrawlerController extends CommonController
{
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
}
final protected function getUserModel(): UserModel
{
if ($this->_user_model === null) {
return $this->_user_model = new UserModel();
}
return $this->_user_model;
}
final protected function login_process(string $user_id = null): UserEntity
{
$user_id = $user_id ?? getenv("mangboard.login.default.id");
$password = getenv("mangboard.login.default.password");
// $response = $this->getWebLibrary($host)->getResponse(
// getenv("mangboard.host.url") . getenv("mangboard.login.url"),
// "post",
// [
// 'form_params' => [
// 'user_id' => $id,
// 'password' => $password,
// ],
// ]
// );
// if ($response->getStatusCode() == 200) {
// $entity = $this->getUserModel()->getEntityByLoginCheck($id, $password);
// if ($entity === null) {
// throw new \Exception("{$id}는 회원이 아니거나 암호가 맞지 않습니다.");
// }
// } else {
// throw new \Exception("연결실패:" . $response->getStatusCode());
// }
$entity = $this->getUserModel()->getEntityByID($user_id);
if ($entity === null) {
throw new \Exception("{$user_id}로 로그인 실패");
}
log_message("notice", "{$user_id}로 로그인 성공");
return $entity;
}
public function yamap(string $board_name, string $user_id = null, ...$params): void
{
try {
$user_entity = $this->login_process($user_id);
$mySocket = new WebSocket(getenv("yamap.host.url"));
$myStorage = new MangboardStorage($board_name, $user_entity);
$myCrawler = new Yamap($mySocket, $myStorage);
//추가옵션
$myCrawler->isDebug = in_array('debug', $params);
$myCrawler->isCopy = in_array('copy', $params);
$myCrawler->execute();
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$e->getMessage()
));
}
}
public function yamoon(string $board_name, string $user_id = null, ...$params): void
{
try {
$user_entity = $this->login_process($user_id);
$mySocket = new WebSocket(getenv("yamoon.host.url"));
$myStorage = new MangboardStorage($board_name, $user_entity);
$myCrawler = new Yamap($mySocket, $myStorage);
//추가옵션
$myCrawler->isDebug = in_array('debug', $params);
$myCrawler->isCopy = in_array('copy', $params);
$myCrawler->execute();
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$e->getMessage()
));
}
}
public function sir(string $board_name, string $user_id = null, ...$params): void
{
try {
$user_entity = $this->login_process($user_id);
$mySocket = new WebSocket(getenv("sir.host.url"));
$myStorage = new MangboardStorage($board_name, $user_entity);
$myCrawler = new Yamap($mySocket, $myStorage);
//추가옵션
$myCrawler->isDebug = in_array('debug', $params);
$myCrawler->isCopy = in_array('copy', $params);
$myCrawler->execute();
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$e->getMessage()
));
}
}
public function inven(string $board_name, string $user_id = null, ...$params): void
{
// echo "{$board_name}|{$user_id}|", implode("|", $params);
// exit;
try {
$user_entity = $this->login_process($user_id);
$mySocket = new WebSocket(getenv("inven.host.url"));
$myStorage = new MangboardStorage($board_name, $user_entity);
$myCrawler = new Yamap($mySocket, $myStorage);
//추가옵션
$myCrawler->isDebug = in_array('debug', $params);
$myCrawler->isCopy = in_array('copy', $params);
$myCrawler->execute();
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$e->getMessage()
));
}
}
}