95 lines
3.3 KiB
PHP
95 lines
3.3 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(): array;
|
|
|
|
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()->getPath(),
|
|
$this->getMyStorage()->getFieName(),
|
|
$this->getMyStorage()->getFieName()
|
|
);
|
|
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()->getFieName(),
|
|
$this->getMyStorage()->getUploadPath(),
|
|
$this->getMyStorage()->getPath(),
|
|
$this->getMyStorage()->getFieName(),
|
|
$this->getMyStorage()->getFieMimeType(),
|
|
);
|
|
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;
|
|
}
|
|
final protected function save(string $mediaType, array $downloadInfos, $fileInfos = []): array
|
|
{
|
|
foreach ($downloadInfos as $downloadInfo) {
|
|
$this->getMyStorage()->setFileName($downloadInfo['fileName']);
|
|
if (!$this->getMyStorage()->save($downloadInfo['content'])) {
|
|
continue;
|
|
}
|
|
$fileInfos[] = [
|
|
"mediatag" => $this->getMediaTag($mediaType),
|
|
"path" => $this->getMyStorage()->getPath(),
|
|
"mimeType" => $this->getMyStorage()->getMimeType(),
|
|
"fileName" => $this->getMyStorage()->getFieName(),
|
|
];
|
|
}
|
|
return $fileInfos;
|
|
}
|
|
}
|