Automation/app/Libraries/MyCrawler/MyCrawlerLibrary.php
2024-09-12 00:28:10 +09:00

78 lines
2.7 KiB
PHP

<?php
namespace App\Libraries\MyCrawler;
use Symfony\Component\DomCrawler\Crawler;
use App\Libraries\CommonLibrary;
abstract class MyCrawlerLibrary extends CommonLibrary
{
private $_mySocket = null;
private $_myStorage = null;
protected function __construct($mySocket, $myStorage)
{
parent::__construct();
$this->_mySocket = $mySocket;
$this->_myStorage = $myStorage;
}
abstract public function execute(): void;
final protected function getMySocket(): mixed
{
if ($this->_mySocket === null) {
throw new \Exception("MySocket이 지정되지 않았습니다.");
}
return $this->_mySocket;
}
final protected function getMyStorage(): mixed
{
if ($this->_myStorage === null) {
throw new \Exception("MySocket이 지정되지 않았습니다.");
}
return $this->_myStorage;
}
final protected function getContent(string $url, string $tag): Crawler
{
$response = $this->getMySocket()->getContent($url);
if (!$response) {
throw new \Exception("getCrawler 실패:{$url}");
}
$crawler = new Crawler($response);
return $crawler->filter($tag);
}
//Download한 파일 저장후 추가작업시 사용
final protected function download(string $mediaType, Crawler $crawler, array $options, array $myStorageLibrarys = []): array
{
$nodes = [];
$crawler->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 {
list($fileName, $content) = $this->getMySocket()->download($node->attr($options["attr"]));
$this->getMyStorage()->setOriginName($fileName);
$this->getMyStorage()->setOriginContent($content);
$this->getMyStorage()->setOriginType($mediaType);
$this->getMyStorage()->setOriginSequence($file_sequence);
$myStorageLibrarys[] = $this->getMyStorage()->save();
$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;
}
}