Automation/app/Libraries/MyCrawler/MyCrawlerLibrary.php
2024-09-08 15:24:55 +09:00

92 lines
3.2 KiB
PHP

<?php
namespace App\Libraries\MyCrawler;
use Symfony\Component\DomCrawler\Crawler;
use App\Libraries\CommonLibrary;
abstract class MyCrawlerLibrary extends CommonLibrary
{
protected function __construct()
{
parent::__construct();
}
abstract protected function getMySocket();
abstract protected function getMyStorage();
abstract public function execute(): void;
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);
}
final protected function getNodes(Crawler $crawler, array $options, $nodes = []): array
{
$crawler->filter($options["tag"])->each(
function (Crawler $node) use (&$options, &$nodes): void {
log_message("debug", sprintf("getNode-> %s", $options["tag"]));
$nodes[] = $node;
}
);
return $nodes;
}
protected function getMediaTag(string $mediaType): string
{
$mediaTag = "";
switch ($mediaType) {
case "image":
$mediaTag = sprintf(
"<img src=\"/%s/%s/%s\" alt=\"%s\">",
$this->getMyStorage()->getUploadPath(),
$this->getMyStorage()->getEntity()->getPath(),
$this->getMyStorage()->getEntity()->getTitle(),
$this->getMyStorage()->getEntity()->getTitle()
);
break;
case "video":
$mediaTag = sprintf(
"<video alt=\"%s\" controls autoplay>
<source src=\"/%s/%s/%s\" type=\"%s\">
Your browser does not support the video tag.
</video>",
$this->getMyStorage()->getEntity()->getTitle(),
$this->getMyStorage()->getUploadPath(),
$this->getMyStorage()->getEntity()->getPath(),
$this->getMyStorage()->getEntity()->getTitle(),
$this->getMyStorage()->getEntity()->getMimeType(),
);
break;
}
return $mediaTag;
}
final protected function download(Crawler $crawler, array $options): array
{
$downloadInfos = [];
$nodes = $this->getNodes($crawler, $options);
foreach ($nodes as $node) {
$url = $node->attr($options["attr"]);
$downloadInfos[] = $this->getMySocket()->download($url);
}
return $downloadInfos;
}
protected function save(string $mediaType, array $downloadInfos, $fileEntitys = []): array
{
foreach ($downloadInfos as $downloadInfo) {
$entity = $this->getMyStorage()->getEntity();
$entity->setTitle($downloadInfo['fileName']);
if (!$this->getMyStorage()->save($downloadInfo['content'])) {
continue;
}
$entity->setMediaHTML($this->getMediaTag($mediaType));
$fileEntitys[] = $entity;
}
return $fileEntitys;
}
}