Automation init...3

This commit is contained in:
최준흠 2024-09-14 18:39:00 +09:00
parent ff3ed49cdd
commit 19ef055cf4
10 changed files with 216 additions and 265 deletions

View File

@ -30,45 +30,4 @@ abstract class MyCrawlerLibrary extends CommonLibrary
}
return $crawler->filter($tag);
}
protected function save(string $url, string $mediaType, int $file_sequence): mixed
{
list($file_name, $content) = $this->getMySocket()->download($url);
$this->getMyStorage()->setOriginName($file_name);
$this->getMyStorage()->setOriginContent($content);
$this->getMyStorage()->setOriginType($mediaType);
$this->getMyStorage()->setOriginSequence($file_sequence);
return $this->getMyStorage()->save();
}
final protected function download(string $mediaType, Crawler $selector, array $options, array $myStorageLibrarys = []): array
{
$nodes = [];
$selector->filter($options["tag"])->each(
function (Crawler $node) use (&$options, &$nodes): void {
log_message("debug", sprintf(
"getNode->%s[%s]",
$options["tag"],
$node->attr($options['attr'])
));
$nodes[] = $node;
}
);
$file_sequence = 1;
foreach ($nodes as $node) {
try {
$myStorageLibrarys[] = $this->save($node->attr($options["attr"]), $mediaType, $file_sequence);
$file_sequence++;
log_message("notice", __FUNCTION__ . " OriginType->{$mediaType} 작업 완료");
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s,OriginType->%s 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$mediaType,
$e->getMessage()
));
}
}
return $myStorageLibrarys;
}
}

View File

@ -8,12 +8,15 @@ use App\Libraries\MyStorage\Mangboard\BoardsLibrary;
use App\Libraries\MyStorage\Mangboard\BoardLibrary;
use App\Libraries\MySocket\WebLibrary as MySocketLibrary;
use App\Entities\Mangboard\UserEntity;
use App\Traits\FileTrait;
class YamapLibrary extends MyCrawlerLibrary
{
use FileTrait;
private $_user_entity = null;
private $_boards_library = null;
private $_board_library = null;
private $_file_librarys = [];
public function __construct()
{
parent::__construct();
@ -65,6 +68,79 @@ class YamapLibrary extends MyCrawlerLibrary
{
$this->_user_entity = $_user_entity;
}
private function save(int $file_sequence, string $mediaType, string $file_name, string $content): void
{
$this->getMyStorage()->setOriginName($file_name);
$this->getMyStorage()->setOriginContent($content);
$this->getMyStorage()->setOriginType($mediaType);
$this->getMyStorage()->setOriginSequence($file_sequence);
$this->_file_librarys[] = $this->getMyStorage()->save();
}
//Yamap ViewPage의 이미지나영상데이터가 있으면 Dodownload 한다.
private function download(string $mediaType, string $url): array
{
$file_names = explode('/', $url);
if (!is_array($file_names) || !count($file_names)) {
throw new \Exception("URL이 파일명 형식이 아닙니다 : " . $this->getMySocket()->getHost() . $url);
}
$file_name = array_pop($file_names);
$temps = explode(".", $file_name);
$file_ext = array_pop($temps);
if (!$this->isFileType($file_ext, $mediaType)) {
throw new \Exception("파일명 형식이 {$mediaType}가 아닙니다");
}
$content = $this->getMySocket()->getContent($url);
log_message("notice", "{$file_name} 파일이 다운로드되었습니다!");
return array($file_name, $content);
}
private function mediaContent(array $urls): void
{
$file_sequence = 1;
$this->_file_librarys = []; //CreateBoard에서 사용을 위해 DetailPage마다 초기화
foreach ($urls as $mediaType => $url) {
try {
list($file_name, $content) = $this->download($mediaType, $url);
$this->save($file_sequence, $mediaType, $file_name, $content);
$file_sequence++;
log_message("notice", __FUNCTION__ . " OriginType->{$mediaType} 작업 완료");
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s,OriginType->%s 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$mediaType,
$e->getMessage()
));
}
}
if (!count($this->_file_librarys)) {
throw new \Exception("Download된 Content가 없습니다.");
}
}
//Yamap ViewPage의 이미지나영상데이터가 있으면 URL과MediaType을 가져온다
private function getUrlsByDetailPageMediaType(string $mediaType, Crawler $selector, array $options, array $urls = []): array
{
$selector->filter($options["tag"])->each(
function (Crawler $node) use (&$mediaType, &$options, &$urls): void {
log_message("debug", sprintf(
"getNode->%s[%s]",
$options["tag"],
$node->attr($options['attr'])
));
$urls[$mediaType] = $node->attr($options["attr"]);
}
);
return $urls;
}
private function detailPage(array $listInfo): array
{
$response = $this->getMySocket()->getContent($listInfo['detail_url']);
$selector = $this->getSelector($response, getenv("yamap.view.content.tag"));
$urls = $this->getUrlsByDetailPageMediaType("image", $selector, ["tag" => "img", "attr" => "src"]);
$urls = $this->getUrlsByDetailPageMediaType("video", $selector, ["tag" => "video", "attr" => "src"], $urls);
log_message("notice", sprintf("\n-----------%s 작업완료--------\n%s\n-----------------------\n", __FUNCTION__, var_export($urls, true)));
return $urls;
}
private function mainPage(string $url): array
{
$listInfos = [];
@ -93,38 +169,6 @@ class YamapLibrary extends MyCrawlerLibrary
log_message("notice", __FUNCTION__ . " 작업 완료");
return $listInfos;
}
protected function save(string $url, string $mediaType, int $file_sequence): mixed
{
$myStorageLibrary = parent::save($url, $mediaType, $file_sequence);
$content = $this->getMyStorage()->getHTMLTag();
if ($content === "") {
throw new \Exception(__FUNCTION__ . " Content의 내용없음");
}
//망보드 Content 추가
$this->getBoardLibrary()->addContent($content);
return $myStorageLibrary;
}
private function detailPage(array $listInfo): void
{
//1. Yamap ViewPage의 이미지나영상데이터가 있으면
$response = $this->getMySocket()->getContent($listInfo['detail_url']);
//1.망보드 게시판에 게시물 생성 처리
$this->getMyStorage()->setBoardEntity($this->getBoardLibrary()->create($listInfo));
//망보드 image_path설정
$this->getBoardLibrary()->setImagePath(sprintf("%s/%s", $this->getMyStorage()->getPath(), $this->getMyStorage()->getOriginName()));
$selector = $this->getSelector($response, getenv("yamap.view.content.tag"));
//3. Image 처리
log_message("debug", sprintf("\n-------------DetailPage------------\n%s\n--------------------------\n", $selector->html()));
$myStorageLibrarys = $this->download("image", $selector, ["tag" => "img", "attr" => "src"]);
//4. Video(mp4) 처리
$myStorageLibrarys = $this->download("video", $selector, ["tag" => "video", "attr" => "src"], $myStorageLibrarys);
//5.망보드 게시판에 게시물 수정 처리
$this->getBoardLibrary()->modify();
log_message("notice", __FUNCTION__ . " 작업 완료");
}
public function execute(): void
{
if ($this->getDebug()) {
@ -150,7 +194,9 @@ class YamapLibrary extends MyCrawlerLibrary
if ($i <= $max_limit) {
try {
log_message("notice", "게시물 {$i}번째 {$listInfo["nickname"]} 작업시작");
$this->detailPage($listInfo);
$this->mediaContent($this->detailPage($listInfo));
//File DB 및 Board DB 등록작업
$this->getBoardLibrary()->create($i, $listInfo, $this->_file_librarys);
log_message("notice", "게시물 {$i}번째 {$listInfo["nickname"]} 작업완료.");
$i++;
} catch (\Exception $e) {

View File

@ -74,21 +74,9 @@ class WebLibrary extends MySocketLibrary
{
$response = $this->getResponse($url, $method, $options);
if ($response->getStatusCode() == 200) {
return $response->getBody()->getContents();
// return $response->getBody();
// return $response->getBody()->getContents();
return $response->getBody();
}
throw new \Exception("error", "{$url} 접속실패: " . $response->getStatusCode());
}
final public function download(string $url): array
{
$file_names = explode('/', $url);
if (!is_array($file_names) || !count($file_names)) {
throw new \Exception("Socket URL Error:" . $this->getHost() . $url);
}
$file_name = array_pop($file_names);
$response = $this->getContent($url);
log_message("notice", "{$file_name} 파일이 다운로드되었습니다!");
return array($file_name, $response);
}
}

View File

@ -45,7 +45,11 @@ class FileLibrary extends MyStorageLibrary
$this->makeDirectory($fullPath);
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $this->getOriginName();
if (file_exists($saveFilePath)) {
throw new \Exception(__FUNCTION__ . " 이미 존재하는 파일:{$saveFilePath}");
$fileInfos = pathinfo($saveFilePath, PATHINFO_ALL);
$saveFile = $fileInfos['filename'] . "_" . uniqid() . $fileInfos['extension'];
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $this->getOriginName();
log_message("notice", __FUNCTION__ . "파일명 변경 : 원본파일 {$this->getOriginName()}->저장파일 {$saveFile}");
$this->setOriginName($saveFile);
}
//원본이미지 저장
if (!file_put_contents($saveFilePath, $this->getOriginContent())) {

View File

@ -10,10 +10,8 @@ use App\Entities\Mangboard\BoardEntity;
class BoardLibrary
{
private $_model = null;
private $_entity = null;
private $_boards_entity = null;
private $_user_entity = null;
private $_mediaTags = ["image_path" => "", "content" => ""];
public function __construct(BoardsEntity $boards_entity, UserEntity $user_entity)
{
$this->_boards_entity = $boards_entity;
@ -27,10 +25,6 @@ class BoardLibrary
}
return $this->_model;
}
public function getEntity(): BoardEntity
{
return $this->_entity;
}
public function getBoardsEntity(): BoardsEntity
{
return $this->_boards_entity;
@ -39,20 +33,8 @@ class BoardLibrary
{
return $this->_user_entity;
}
public function setImagePath(string $image_path): void
public function create(int $cnt, array $listInfo, array $file_librarys): BoardEntity
{
$this->_mediaTags['image_path'] = $image_path;
}
public function addContent(string $content)
{
if ($content != "") {
$this->_mediaTags['content'] .= $content;
}
}
public function create(array $listInfo): BoardEntity
{
//초기화
$this->_mediaTags = ["image_path" => "", "content" => ""];
$formDatas = [];
//미디어관련정보 entity에 넣기
$formDatas['title'] = $listInfo["title"];
@ -66,19 +48,34 @@ class BoardLibrary
$formDatas['editor_type'] = "S";
$formDatas['image_path'] = "";
$formDatas['content'] = "";
foreach ($file_librarys as $file_library) {
if ($formDatas['image_path'] == "") {
$formDatas['image_path'] = sprintf("%s/%s", $file_library->getPath(), $file_library->getOriginName());
}
$formDatas['content'] .= $file_library->getHTMLTag();
}
//망보드 게시판에 등록
$this->_entity = $this->getModel()->create($formDatas);
log_message("notice", message: __FUNCTION__ . "=>{$this->_entity->getPK()}:{$this->_entity->getTitle()} 생성 작업 완료");
return $this->_entity;
}
public function modify(): void
{
if ($this->_mediaTags['content'] != "") {
$this->getModel()->modify($this->_entity, $this->_mediaTags);
log_message("notice", __FUNCTION__ . "=>{$this->_entity->getPK()}:{$this->_entity->getTitle()} 수정 작업 완료");
} else {
$this->getModel()->delete([$this->getModel()->getPKField() => $this->_entity->getPK()]);
log_message("warning", __FUNCTION__ . "=>{$this->_entity->getPK()}:{$this->_entity->getTitle()} 내용이 없어 삭제처리");
if ($formDatas['content'] == "") {
throw new \Exception(sprintf(
"%s=>%s번째 %s 내용이 없어 => %s 등록 취소",
__FUNCTION__,
$cnt,
$listInfo["title"],
$this->getModel()->getTable()
));
}
$entity = $this->getModel()->create($formDatas);
//망보드 파일관리툴 등록
foreach ($file_librarys as $file_library) {
$file_library->createByBoardLibrary($entity, $cnt, $listInfo);
}
log_message("notice", sprintf(
"%s=>%s번째 %s => %s 등록 완료",
__FUNCTION__,
$cnt,
$listInfo["title"],
$this->getModel()->getTable()
));
return $entity;
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace App\Libraries\MyStorage\Mangboard\File;
use App\Libraries\MyUtil\ImageLibrary as MyUtilLibrary;
class ImageLibrary extends MyUtilLibrary
{
public function __construct() {}
public function create(string $file_name, int $width = 480, int $height = 319): bool|string
{
try {
$file_ext = pathinfo($this->getSourcePath() . DIRECTORY_SEPARATOR . $file_name, PATHINFO_EXTENSION);
if (!$this->isFileType($file_ext)) {
throw new \Exception("{$file_name} Image 형식파일이 아닙니다.");
}
//저장할 디렉토리 생성
$this->makeDirectory($this->getDestinationPath());
// 이미지 파일 로드
$this->load($this->getSourcePath() . DIRECTORY_SEPARATOR . $file_name);
// 200x200으로 이미지 크기 조정
$this->resize($width, $height);
// 파일 저장
$this->save($this->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getDestinationFile());
// 메모리 해제
$this->destroy();
log_message("debug", sprintf(
"%s %s->%s(W:%s,H:%s) 작업완료)",
__FUNCTION__,
$file_name,
$this->getDestinationFile(),
$width,
$height
));
return $file_name;
} catch (\Exception $e) {
log_message("warning", $e->getMessage());
return false;
}
}
}

View File

@ -3,7 +3,7 @@
namespace App\Libraries\MyStorage\Mangboard;
use App\Models\Mangboard\FileModel;
use App\Libraries\MyStorage\Mangboard\SmallImageLibrary;
use App\Libraries\MyStorage\Mangboard\File\ImageLibrary;
use App\Libraries\MyStorage\FileLibrary as MyStorageLibrary;
use App\Entities\Mangboard\UserEntity;
use App\Entities\Mangboard\FileEntity;
@ -13,19 +13,11 @@ use App\Entities\Mangboard\BoardEntity;
class FileLibrary extends MyStorageLibrary
{
private $_model = null;
private $_entity = null;
private $_boards_entity = null;
private $_board_entity = null;
private $_user_entity = null;
private $_imageLibrary = null;
public function __construct(string $path)
{
parent::__construct($path);
$this->_imageLibrary = new SmallImageLibrary();
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
$this->_imageLibrary->setDebug($this->getDebug());
$this->_imageLibrary->setSourcePath($fullPath);
$this->_imageLibrary->setDestinationPath($fullPath);
}
public function getModel(): FileModel
{
@ -34,10 +26,6 @@ class FileLibrary extends MyStorageLibrary
}
return $this->_model;
}
public function getEntity(): FileEntity
{
return $this->_entity;
}
public function getBoardsEntity(): BoardsEntity
{
if ($this->_boards_entity === null) {
@ -49,13 +37,6 @@ class FileLibrary extends MyStorageLibrary
{
$this->_boards_entity = $boards_entity;
}
public function getBoardEntity(): BoardEntity
{
if ($this->_board_entity === null) {
throw new \Exception("Board Entity가 정의되지 않았습니다.");
}
return $this->_board_entity;
}
public function setUserEntity(UserEntity $user_entity): void
{
$this->_user_entity = $user_entity;
@ -67,10 +48,6 @@ class FileLibrary extends MyStorageLibrary
}
return $this->_user_entity;
}
public function setBoardEntity(BoardEntity $board_entity): void
{
$this->_board_entity = $board_entity;
}
final public function getHTMLTag(string $content = ""): string
{
//Board 게시판 image_path , content용 데이터 배열에 추가 후 modifyBoard에서 처리
@ -105,27 +82,18 @@ class FileLibrary extends MyStorageLibrary
));
return $content;
}
public function save(): static
public function createByBoardLibrary(BoardEntity $board_entity, int $cnt, array $listInfos): FileEntity
{
parent::save();
try {
//파일관리 table에 등록
$formDatas = [];
//Board PID 넣기
$formDatas['board_pid'] = $this->getBoardEntity()->getPk();
//작은이미지생성후 Path/파일명 넣기
$fileInfos = pathinfo($this->_imageLibrary->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getOriginName(), PATHINFO_ALL);
$dstFile = $fileInfos['filename'] . "_small." . $fileInfos['extension'];
$this->_imageLibrary->setDestinationFile($dstFile);
$formDatas['file_path'] = sprintf(
"%s/%s",
$this->getPath(),
$this->_imageLibrary->create($this->getOriginName())
);
$formDatas['board_pid'] = $board_entity->getPk();
$formDatas['user_pid'] = $this->getUserEntity()->getPK();
$formDatas['user_name'] = $this->getUserEntity()->getTitle();
$formDatas['board_name'] = $this->getBoardsEntity()->getTitle();
$formDatas['table_name'] = $this->getModel()->getTable();
$formDatas['file_path'] = sprintf("%s/%s", $this->getPath(), $this->getOriginName());
$formDatas['file_name'] = $this->getOriginName();
$formDatas['file_type'] = $this->getMimeType();
$formDatas['file_caption'] = $this->getOriginName();
@ -136,10 +104,41 @@ class FileLibrary extends MyStorageLibrary
$formDatas['reg_date'] = date("Y-m-d H:i:s");
$entity = $this->getModel()->create($formDatas);
log_message("notice", sprintf(
"%s %s번째 작업 완료",
"게시물 %s번째[%s]의 %s번째용 파일 등록 완료",
$cnt,
$listInfos['title'],
__FUNCTION__,
$this->getOriginSequence()
));
return $entity;
} catch (\Exception $e) {
log_message("notice", sprintf(
"\n---%s 게시물 %s번째[%s]의 %s번째용 파일 등록 오류---\n%s\n--------------------------------\n",
__FUNCTION__,
$cnt,
$listInfos['title'],
__FUNCTION__,
$this->getOriginSequence(),
$e->getMessage()
));
}
}
private function save_smallImage(): string
{
$imageLibrary = new ImageLibrary();
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
$imageLibrary->setDebug($this->getDebug());
$imageLibrary->setSourcePath($fullPath);
$imageLibrary->setDestinationPath($fullPath);
//작은이미지생성후 Path/파일명 넣기
$fileInfos = pathinfo($imageLibrary->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getOriginName(), PATHINFO_ALL);
$imageLibrary->setDestinationFile($fileInfos['filename'] . "_small." . $fileInfos['extension']);
return $imageLibrary->create($this->getOriginName());
}
public function save(): static
{
parent::save();
$this->save_smallImage();
return $this;
}
}

View File

@ -1,82 +0,0 @@
<?php
namespace App\Libraries\MyStorage\Mangboard;
use App\Libraries\MyUtil\ImageLibrary;
class SmallImageLibrary extends ImageLibrary
{
public function __construct() {}
// public function save(string $fullPath): static
// {
// // $fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
// $image = new ImageLibrary();
// $image->setDebug($this->getDebug());
// $image->setSourcePath($fullPath);
// $image->setDestinationPath($fullPath);
// //저장파일명
// $fileInfos = pathinfo($image->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getOriginName(), PATHINFO_ALL);
// $dstFile = $fileInfos['filename'] . "_small." . $fileInfos['extension'];
// $image->setDestinationFile($dstFile);
// $result = $image->create($this->getOriginName(), $width, $height);
// log_message("notice", sprintf(
// "%s %s번째:%s 작업 완료",
// __FUNCTION__,
// $this->getOriginSequence(),
// $this->getPath()
// ));
// //정석방식
// // if ($result) {
// // if ($result) {
// // //작은이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
// // return = sprintf(
// // "%s/%s",
// // $entity->getPath(),
// // $image->getDestinationFile()
// // ));
// // } else {
// // //원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
// // return sprintf(
// // "%s/%s",
// // $entity->getPath(),
// // $this->getOriginName()
// // ));
// // }
// // }
// //망보드 방식
// //mb_files에서 file_path가 망보드 게시판 파일관리에서 image로 표시되어 file_path+file_name로 설정
// //원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
// $this->_imagePath = sprintf("%s/%s", $this->getPath(), $this->getOriginName());
// return $this;
// }
public function create(string $file, int $width = 480, int $height = 319): bool|string
{
try {
if (!$this->isFileType($this->getSourcePath(), $file)) {
throw new \Exception("{$file} Image 형식파일이 아닙니다.");
}
//저장할 디렉토리 생성
$this->makeDirectory($this->getDestinationPath());
// 이미지 파일 로드
$this->load($this->getSourcePath() . DIRECTORY_SEPARATOR . $file);
// 200x200으로 이미지 크기 조정
$this->resize($width, $height);
// 파일 저장
$this->save($this->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getDestinationFile());
// 메모리 해제
$this->destroy();
log_message("debug", sprintf(
"%s %s->%s(W:%s,H:%s) 작업완료)",
__FUNCTION__,
$file,
$this->getDestinationFile(),
$width,
$height
));
return $file;
} catch (\Exception $e) {
log_message("warning", $e->getMessage());
return false;
}
}
}

View File

@ -4,7 +4,7 @@ namespace App\Traits;
trait AuthTrait
{
public function login_check(): array
final public function login_check(): array
{
//사용자 기본 Role 지정
$session[SESSION_NAMES['ISLOGIN']] = false;

View File

@ -4,7 +4,7 @@ namespace App\Traits;
trait FileTrait
{
public function makeDirectory(string $path)
final public function makeDirectory(string $path)
{
if (!is_dir($path)) {
if (!mkdir($path)) {
@ -13,7 +13,7 @@ trait FileTrait
}
}
public function isFileType(string $path, string $file, $type = "image"): bool
final public function isFileType(string $file_ext, $type = "image"): bool
{
switch ($type) {
case "audio":
@ -27,12 +27,11 @@ trait FileTrait
$exts = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
break;
}
$ext = pathinfo($path . DIRECTORY_SEPARATOR . $file, PATHINFO_EXTENSION);
return in_array($ext, $exts);
return in_array($file_ext, $exts);
}
//디렉토리에 속한 파일 List
public function getFilesByExtentionType(string $path, string $type = "image"): array
final public function getFilesByExtentionType(string $path, string $type = "image"): array
{
// 디렉토리에서 파일 목록 가져오기
$files = [];