Automation init...3

This commit is contained in:
최준흠 2024-09-11 19:41:35 +09:00
parent 0bb878409f
commit f72a46d180
12 changed files with 223 additions and 174 deletions

View File

@ -17,10 +17,10 @@ class CrawlerController extends CommonController
//1. 사이트 로그인 처리
$user = $crawler->login();
$crawler->getMyStorage()->setUser($user);
$items = [];
$itemInfos = [];
//2. 해당사이트 MainPage 처리
if ($crawler->getDebug()) {
$items[] = [
$itemInfos[] = [
'title' => getenv("yamap.view.test.title"),
'nickname' => getenv("yamap.view.test.nickname"),
'detail_url' => getenv("yamap.view.test.url"),
@ -28,30 +28,25 @@ class CrawlerController extends CommonController
'hit' => 1
];
} else {
$items = $crawler->mainPage(getenv("yamap.list.url"));
$itemInfos = $crawler->mainPage(getenv("yamap.list.url"));
}
if (!count($items)) {
if (!count($itemInfos)) {
throw new \Exception("Yamap 사이트에서 게시물이 존재하지 않습니다.");
}
//Limit가 0이면 $items 갯수만큼 다하고, LIMIT 갯수 혹은 $items의 갯수중 작은수만큼 한다.
//Limit가 0이면 $itemInfos 갯수만큼 다하고, LIMIT 갯수 혹은 $items의 갯수중 작은수만큼 한다.
$max_limit = intval(getenv("yamap.list.max_limit"));
if ($max_limit) {
$max_limit = count($items) <= $max_limit ? count($items) : $max_limit;
$max_limit = count($itemInfos) <= $max_limit ? count($itemInfos) : $max_limit;
} else {
$max_limit = count($items);
$max_limit = count($itemInfos);
}
$i = 1;
foreach ($items as $item) {
foreach ($itemInfos as $itemInfo) {
if ($i < $max_limit) {
try {
log_message("notice", "게시물 {$i}번째 {$item["nickname"]} 작업시작");
//3. DetailPage 처리 : bbs_view > div.contents 가진 객체를 찾아서 처리
$fileEntitys = $crawler->detailPage($item["detail_url"]);
//4.망보드 일반게시판에 게시물 등록 처리
if (count($fileEntitys)) {
$crawler->createBoard($item, $fileEntitys);
}
log_message("notice", "게시물 {$i}번째 {$item["nickname"]} 작업완료.");
log_message("notice", "게시물 {$i}번째 {$itemInfo["nickname"]} 작업시작");
$crawler->detailPage($itemInfo);
log_message("notice", "게시물 {$i}번째 {$itemInfo["nickname"]} 작업완료.");
$i++;
} catch (\Exception $e) {
log_message("debug", $e->getMessage());

View File

@ -37,25 +37,29 @@ abstract class MyCrawlerLibrary extends CommonLibrary
//Download한 파일 저장후 추가작업시 사용
final protected function download(string $mediaType, Crawler $crawler, array $options, array $entitys = []): array
final protected function download(string $mediaType, Crawler $crawler, array $options, array $results = []): array
{
$nodes = $this->getNodes($crawler, $options);
$file_sequence = 1;
foreach ($nodes as $node) {
try {
list($fileName, $content) = $this->getMySocket()->download($node->attr($options["attr"]));
$entitys[] = $this->getMyStorage()->save($fileName, $mediaType, $content, $file_sequence);
$this->getMyStorage()->setOriginName($fileName);
$this->getMyStorage()->setOriginContent($content);
$this->getMyStorage()->setOriginType($mediaType);
$this->getMyStorage()->setOriginSequence($file_sequence);
$results[] = $this->getMyStorage()->save();
$file_sequence++;
log_message("notice", __FUNCTION__ . " MediaType->{$mediaType} 작업 완료");
log_message("notice", __FUNCTION__ . " OriginType->{$mediaType} 작업 완료");
} catch (\Exception $e) {
log_message("warning", sprintf(
"\n---%s,MediaType->%s 오류---\n%s\n-----------------------------------------\n",
"\n---%s,OriginType->%s 오류---\n%s\n-----------------------------------------\n",
__FUNCTION__,
$mediaType,
$e->getMessage()
));
}
}
return $entitys;
return $results;
}
}

View File

@ -95,48 +95,46 @@ class YamapLibrary extends MyCrawlerLibrary
return $items;
}
public function detailPage(string $url, array $fileEntitys = []): array
{
$crawler = $this->getContent($url, getenv("yamap.view.content.tag"));
//3. Image 처리
$fileEntitys = $this->download("image", $crawler, ["tag" => "img", "attr" => "src"]);
//4. Video(mp4) 처리
$fileEntitys = $this->download("video", $crawler, ["tag" => "video", "attr" => "src"], $fileEntitys);
log_message("notice", __FUNCTION__ . " 작업 완료");
return $fileEntitys;
}
public function createBoard(array $item, array $fileEntitys): BoardEntity
private function createBoard(array $itemInfo, array $myStorageLibrarys): void
{
//미디어관련정보 entity에 넣기
$entity = new BoardEntity();
$entity->title = $item["title"];
$entity->user_pid = $this->getMyStorage()->getUser()->getPK();
$entity->user_id = $this->getMyStorage()->getUser()->getID();
$entity->user_name = $item["nickname"] != "" ? $item["nickname"] : $this->getMyStorage()->getUser()->getTitle();
$entity->level = $this->getMyStorage()->getBoardLevel();
$entity->hit = $item['hit'];
$entity->reg_date = date("Y-m-d H:i:s", strtotime($item['date']));
$entity->data_type = "html";
$entity->editor_type = "S";
$entity->image_path = false;
foreach ($fileEntitys as $fileEntity) {
if ($entity->image_path === false) {
$entity->image_path = $fileEntity->getPath();
$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();
}
$entity->content .= $fileEntity->getMediaHTML();
$formDatas['content'] .= $myStorageLibrary->getFileEntity()->getMediaHTML();
}
// echo $entity->image_path . "\n";
// echo $entity->title . "\n";
// echo $entity->user_name . "\n";
// echo $entity->hit . "\n";
// echo $entity->reg_date . "\n";
// exit;
//망보드 게시판에 등록
$entity = $this->getBoardModel()->create($entity);
$entity = $this->getBoardModel()->create($formDatas);
//망보드 파일관리툴에 등록된 파일게시물에 등록한 게시판번호 수정하기
$this->getMyStorage()->setBoardPID($fileEntitys, intval($entity->getPK()));
foreach ($myStorageLibrarys as $myStorageLibrary) {
$myStorageLibrary->setBoardPID(intval($entity->getPK()));
}
log_message("notice", __FUNCTION__ . " 작업 완료");
return $entity;
}
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);
}
}
}

View File

@ -9,23 +9,32 @@ class FileLibrary extends MyStorageLibrary
{
use FileTrait;
private $_path = "";
private $_mimeType = "";
private $_fileSize = 0;
private $_fileSequence = 0;
private $_imageLibrary = null;
public function __construct(string $path)
{
parent::__construct();
$this->_path = $path;
}
final public function getPath(): string
{
return $this->_path;
}
public function createFileEntity(): FileEntity
final public function getMimeType(): string
{
return new FileEntity();
return $this->_mimeType;
}
protected function getMediaTag(string $mediaType, FileEntity $entity): string
final public function getFileSize(): int
{
return $this->_fileSize;
}
final public function getFileSequence(): int
{
return $this->_fileSequence;
}
protected function getMediaTag(string $mediaType): string
{
$mediaTag = "";
switch ($mediaType) {
@ -33,9 +42,9 @@ class FileLibrary extends MyStorageLibrary
$mediaTag = sprintf(
"<img src=\"/%s/%s/%s\" alt=\"%s\">",
$this->getUploadPath(),
$entity->getPath(),
$entity->getTitle(),
$entity->getTitle()
$this->getPath(),
$this->getOriginName(),
$this->getOriginName()
);
break;
case "video":
@ -44,37 +53,31 @@ class FileLibrary extends MyStorageLibrary
<source src=\"/%s/%s/%s\" type=\"%s\">
Your browser does not support the video tag.
</video>",
$entity->getTitle(),
$this->getOriginName(),
$this->getUploadPath(),
$entity->getPath(),
$entity->getTitle(),
$entity->getMimeType(),
$this->getPath(),
$this->getOriginName(),
$this->getMimeType(),
);
break;
}
return $mediaTag;
}
public function save(string $fileName, string $mediaType, string $content, int $file_sequence): FileEntity
public function save(): static
{
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
$this->makeDirectory($fullPath);
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $fileName;
$saveFilePath = $fullPath . DIRECTORY_SEPARATOR . $this->getOriginName();
if (file_exists($saveFilePath)) {
throw new \Exception(__FUNCTION__ . " 이미 존재하는 파일:{$saveFilePath}");
}
//원본이미지 저장
if (!file_put_contents($saveFilePath, $content)) {
if (!file_put_contents($saveFilePath, $this->getOriginContent())) {
throw new \Exception(__FUNCTION__ . " 파일저장 실패:{$saveFilePath}");
}
$entity = $this->createFileEntity();
$entity->setPath($this->getPath());
$entity->setTitle($fileName);
$entity->setMimeType(mime_content_type($saveFilePath));
$entity->setSize(filesize($saveFilePath));
$entity->setSequence($file_sequence);
$entity->setMediaHTML($this->getMediaTag($mediaType, $entity));
log_message("notice", __FUNCTION__ . " 원본 {$file_sequence}번째 " . $entity->getTitle() . " 작업 완료");
return $entity;
$this->_mimeType = mime_content_type($saveFilePath);
$this->_fileSize = filesize($saveFilePath);
log_message("notice", __FUNCTION__ . " 원본파일 {$this->getOriginName()} 작업 완료");
return $this;
}
}

View File

@ -15,10 +15,19 @@ class FileLibrary extends MyStorageLibrary
private $_boardTable = "";
private $_boardLevel = 1;
private $_model = null;
private $_fileEntity = null;
public function __construct(string $path)
{
parent::__construct($path);
}
public function getFileEntity(): FileEntity
{
return $this->_fileEntity;
}
private function setFileEntity(FileEntity $fileEntity): void
{
$this->_fileEntity = $fileEntity;
}
public function getModel(): FileModel
{
if ($this->_model === null) {
@ -71,24 +80,16 @@ class FileLibrary extends MyStorageLibrary
$this->_boardLevel = $boardLevel;
}
//주의:Override함
public function createFileEntity(): FileEntity
{
return new FileEntity();
}
//망보드 Board Table생성 후 관련된 mb_files Table에 Board번호를 넣기 위함
public function setBoardPID(array $fileEntitys, int $board_pid): void
public function setBoardPID(int $board_pid): void
{
foreach ($fileEntitys as $fileEntity) {
$formDatas['board_pid'] = $board_pid;
$this->getModel()->modify($fileEntity, $formDatas);
}
$formDatas['board_pid'] = $board_pid;
$this->getModel()->modify($this->getFileEntity(), $formDatas);
log_message("notice", __FUNCTION__ . " 작업 완료");
}
//작은이미지 생성
private function save_small_image(FileEntity $entity, string $file, int $width = 480, int $height = 319): FileEntity
private function save_small_image(int $width = 480, int $height = 319): bool
{
$fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
$image = new ImageLibrary();
@ -96,71 +97,78 @@ class FileLibrary extends MyStorageLibrary
$image->setSourcePath($fullPath);
$image->setDestinationPath($fullPath);
//저장파일명
$fileInfos = pathinfo($image->getDestinationPath() . DIRECTORY_SEPARATOR . $file, PATHINFO_ALL);
$fileInfos = pathinfo($image->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getOriginName(), PATHINFO_ALL);
$dstFile = $fileInfos['filename'] . "_small." . $fileInfos['extension'];
$image->setDestinationFile($dstFile);
$result = $image->create($file, $width, $height);
//개인적인 생각방식
// if ($result) {
// //작은이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
// $entity->setPath(sprintf(
// "%s/%s",
// $entity->getPath(),
// $image->getDestinationFile()
// ));
// } else {
// //원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
// $entity->setPath(sprintf(
// "%s/%s",
// $entity->getPath(),
// $entity->getTitle()
// ));
// }
//개인적인 생각방식
//
//망보드 방식
//mb_files에서 file_path가 망보드 게시판 파일관리에서 image로 표시되어 file_path+file_name로 설정
//원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
$entity->setPath(sprintf(
"%s/%s",
$entity->getPath(),
$entity->getTitle()
));
//망보드 방식
$result = $image->create($this->getOriginName(), $width, $height);
log_message("notice", sprintf(
"%s %s번째:%s 작업 완료",
__FUNCTION__,
$entity->getSequence(),
$entity->getPath()
$this->getOriginSequence(),
$this->getPath()
));
return $entity;
return $result;
}
//망보드 파일관리 table에 등록
private function save_db(FileEntity $entity): FileEntity
private function save_db(array $formDatas): FileEntity
{
// log_message("notice", sprintf("%s %s번째 작업 시작",__FUNCTION__ ,$entity->getSequence()));
$entity->user_pid = $this->getUser()->getPK();
$entity->user_name = $this->getUser()->getTitle();
$entity->board_name = $this->getBoardName();
$entity->table_name = $this->getBoardTable();
$entity->reg_date = date("Y-m-d H:i:s");
$entity->file_caption = $entity->getTitle();
$entity->file_alt = $entity->getTitle();
$entity->file_description = "Filedata";
$formDatas['user_pid'] = $this->getUser()->getPK();
$formDatas['user_name'] = $this->getUser()->getTitle();
$formDatas['board_name'] = $this->getBoardName();
$formDatas['table_name'] = $this->getBoardTable();
$formDatas['file_name'] = $this->getOriginName();
$formDatas['file_path'] = $this->getOriginName();
$formDatas['file_type'] = $this->getMimeType();
$formDatas['file_caption'] = $this->getOriginName();
$formDatas['file_alt'] = $this->getOriginName();
$formDatas['file_description'] = "Filedata";
$formDatas['file_size'] = $this->getFileSize();
$formDatas['file_sequence'] = $this->getOriginSequence();
$formDatas['reg_date'] = date("Y-m-d H:i:s");
// log_message("debug", "\n-----Entity Value-----\n" . var_export($entity->toArray(), true) . "\n---------------------------\n");
$entity = $this->getModel()->create($entity);
$entity = $this->getModel()->create($formDatas);
log_message("notice", sprintf(
"%s %s번째 작업 완료",
__FUNCTION__,
$entity->getSequence()
$this->getOriginSequence()
));
return $entity;
}
public function save(string $fileName, string $mediaType, string $content, int $file_sequence): FileEntity
public function save(): static
{
$entity = parent::save($fileName, $mediaType, $content, $file_sequence);
$entity = $this->save_small_image($entity, $fileName);
return $this->save_db($entity);
parent::save();
$formDatas = [];;
if ($this->save_small_image()) {
//개인적인 생각방식
// if ($result) {
// //작은이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
// $entity->setPath(sprintf(
// "%s/%s",
// $entity->getPath(),
// $image->getDestinationFile()
// ));
// } else {
// //원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
// $entity->setPath(sprintf(
// "%s/%s",
// $entity->getPath(),
// $this->getOriginName()
// ));
// }
//개인적인 생각방식
//
//망보드 방식
//mb_files에서 file_path가 망보드 게시판 파일관리에서 image로 표시되어 file_path+file_name로 설정
//원본이미지 생성후 목적지 Path와 파일명을 다시 file_path에 넣는다. URL이 되기때문에 /로 넣어야함
$formDatas['file_path'] = sprintf(
"%s/%s",
$this->getPath(),
$this->getOriginName()
);
//망보드 방식
}
$this->setFileEntity($this->save_db($formDatas));
return $this;
}
}

View File

@ -7,14 +7,51 @@ use App\Libraries\CommonLibrary;
abstract class MyStorageLibrary extends CommonLibrary
{
private $_uploadPath = "uploads";
private $_originName = "";
private $_originContent = "";
private $_originType = "";
private $_originSequence = "";
protected function __construct()
{
parent::__construct();
}
abstract public function save(string $fileName, string $mediaType, string $content, int $file_sequence);
abstract public function save(): mixed;
final public function getUploadPath(): string
{
return $this->_uploadPath;
}
final public function getOriginName(): string
{
return $this->_originName;
}
final public function setOriginName(string $originName): void
{
$this->_originName = $originName;
}
final public function getOriginContent(): string
{
return $this->_originContent;
}
final public function setOriginContent(string $originContent): void
{
$this->_originContent = $originContent;
}
final public function getOrintginType(): string
{
return $this->_originType;
}
final public function setOriginType(string $originType): void
{
$this->_originType = $originType;
}
final public function getOriginSequence(): int
{
return $this->_originSequence;
}
final public function setOriginSequence(int $originSequence): void
{
$this->_originSequence = $originSequence;
}
}

View File

@ -125,28 +125,32 @@ class ImageLibrary extends MyUtilLibrary
public function create(string $file, int $width = 480, int $height = 319): bool
{
if (!$this->isFileType($this->getSourcePath(), $file)) {
log_message("warning", "{$file} Image 형식파일이 아닙니다.");
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 true;
} catch (\Exception $e) {
log_message("warning", $e->getMessage());
return false;
}
//저장할 디렉토리 생성
$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 true;
}
}

View File

@ -136,9 +136,9 @@ class BoardModel extends CommonModel
}
//create용
public function create(BoardEntity $entity, array $formDatas = []): BoardEntity
public function create(array $formDatas = []): BoardEntity
{
$entity = $this->create_process($entity, $formDatas);
$entity = $this->create_process(new BoardEntity(), $formDatas);
//입력후 PID값을 GID값에 넣어주기 위함
return $this->modify($entity, ['gid' => intval($entity->getPK())]);
}

View File

@ -112,9 +112,9 @@ class FileModel extends CommonModel
// }
//create용
public function create(FileEntity $entity, array $formDatas = []): FileEntity
public function create(array $formDatas = []): FileEntity
{
return $this->create_process($entity, $formDatas);
return $this->create_process(new FileEntity(), $formDatas);
}
//modify용
public function modify(FileEntity $entity, array $formDatas): FileEntity

View File

@ -215,9 +215,9 @@ class UserModel extends CommonModel
}
//create용
public function create(UserEntity $entity, array $formDatas = []): UserEntity
public function create(array $formDatas = []): UserEntity
{
return $this->create_process($entity, $formDatas);
return $this->create_process(new UserEntity(), $formDatas);
}
//modify용
public function modify(UserEntity $entity, array $formDatas): UserEntity

View File

@ -55,9 +55,9 @@ class SNSUserModel extends CommonModel
}
//create용
public function create(SNSUSerEntity $entity, array $formDatas = []): SNSUSerEntity
public function create(array $formDatas = []): SNSUSerEntity
{
return $this->create_process($entity, $formDatas);
return $this->create_process(new SNSUSerEntity(), $formDatas);
}
//modify용

View File

@ -70,9 +70,9 @@ class UserModel extends CommonModel
}
//create용
public function create(UserEntity $entity, array $formDatas = []): UserEntity
public function create(array $formDatas = []): UserEntity
{
return $this->create_process($entity, $formDatas);
return $this->create_process(new UserEntity(), $formDatas);
}
//modify용