126 lines
4.9 KiB
PHP
126 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MyCrawler;
|
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
use App\Libraries\MySocket\WebLibrary;
|
|
use App\Libraries\MyStorage\FileLibrary;
|
|
|
|
class YamapLibrary extends MyCrawlerLibrary
|
|
{
|
|
private $_mySocket = null;
|
|
private $_myStorage = null;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function getMySocket()
|
|
{
|
|
if ($this->_mySocket === null) {
|
|
$this->_mySocket = new WebLibrary(getenv('yamap.host.url'));
|
|
}
|
|
return $this->_mySocket;
|
|
}
|
|
|
|
public function getMyStorage()
|
|
{
|
|
if ($this->_myStorage === null) {
|
|
$this->_myStorage = new FileLibrary(getenv('yamap.storage.upload.path'));
|
|
}
|
|
return $this->_myStorage;
|
|
}
|
|
|
|
private function mainPage(string $url): array
|
|
{
|
|
$crawler = $this->getContent($url, getenv("yamap.list.tag"));
|
|
$item_tag = getenv("yamap.list.item.tag");
|
|
$item_link_tag = getenv("yamap.list.item.link.tag");
|
|
$item_nickname_tag = getenv("yamap.list.item.nickname.tag");
|
|
$item_nickname_except = getenv("yamap.list.item.nickname.except");
|
|
|
|
$lists = [];
|
|
//div.bbs_item를 가진 객체를 찾아서 같은 형식의 객체(sibling)를 배열로 넘김
|
|
$crawler->filter($item_tag)->each(
|
|
function (Crawler $node) use (
|
|
&$item_link_tag,
|
|
&$item_nickname_tag,
|
|
&$item_nickname_except,
|
|
&$lists
|
|
): void {
|
|
//bbs_item에서 span.g_nickname 객체를 찾아서 작성자가 "관리자" 아닌지 확인 후 Return Bool
|
|
$nickname = $node->filter($item_nickname_tag)->text();
|
|
log_message("debug", $item_nickname_tag . ":" . $nickname);
|
|
if ($nickname != $item_nickname_except) {
|
|
//작성자가 "관리자"가 아니 게시물이면 해당 bbs_item에서 a.list_subject 객체를 찾아서
|
|
$link_node = $node->filter($item_link_tag);
|
|
$url = $link_node->attr("href");
|
|
$title = $link_node->children()->last()->text();
|
|
$lists[] = ['title' => $title, 'nickname' => $nickname, 'url' => $url];
|
|
}
|
|
}
|
|
);
|
|
if (!count($lists)) {
|
|
throw new \Exception("Target URL이 없습니다.");
|
|
}
|
|
return array($lists[0]["title"], $lists[0]["nickname"], $lists[0]["url"]);
|
|
}
|
|
|
|
private function detailPage(string $url): array
|
|
{
|
|
$crawler = $this->getContent($url, getenv("yamap.view.content.tag"));
|
|
$mediaInfos = [];
|
|
$mediaTags = [];
|
|
//3. Image 처리
|
|
$downloadInfos = $this->download($crawler, ["tag" => "img", "attr" => "src"]);
|
|
foreach ($downloadInfos as $downloadInfo) {
|
|
if ($this->getMySocket()->isContainsHttpOrHttps($downloadInfo['orignal'])) {
|
|
$mediaTags[] = $downloadInfos['orignal'];
|
|
} else {
|
|
$mediaTags[] = sprintf(
|
|
"<img src=\"/%s/%s/%s\" alt=\"%s\">",
|
|
$this->getMyStorage()->getUploadPath(),
|
|
$downloadInfo["path"],
|
|
$downloadInfo["fileName"],
|
|
$downloadInfo["fileName"]
|
|
);
|
|
};
|
|
$mediaInfos[] = $downloadInfo;
|
|
}
|
|
//4. Video(mp4) 처리
|
|
$downloadInfos = $this->download($crawler, ["tag" => "video", "attr" => "src"]);
|
|
foreach ($downloadInfos as $downloadInfo) {
|
|
if ($this->getMySocket()->isContainsHttpOrHttps($downloadInfo['orignal'])) {
|
|
$mediaTags[] = $downloadInfos['orignal'];
|
|
} else {
|
|
$mediaTags[] = sprintf(
|
|
"<video src=\"/%s/%s/%s\" alt=\"%s\" controls autoplay>",
|
|
$this->getMyStorage()->getUploadPath(),
|
|
$downloadInfo["path"],
|
|
$downloadInfo["fileName"],
|
|
$downloadInfo["fileName"]
|
|
);
|
|
};
|
|
$mediaInfos[] = $downloadInfo;
|
|
}
|
|
log_message("debug", "-----mediaTags-----");
|
|
log_message("debug", var_export($mediaTags, true));
|
|
return array($mediaInfos, $mediaTags);
|
|
}
|
|
|
|
public function execute(): array
|
|
{
|
|
//1. 해당사이트 MainPage 처리
|
|
if ($this->getDebug()) {
|
|
$title = getenv("yamap.view.test.title");
|
|
$nickname = getenv("yamap.view.test.nickname");
|
|
$detail_url = getenv("yamap.view.test.url");
|
|
} else {
|
|
list($title, $nickname, $detail_url) = $this->mainPage(getenv("yamap.list.url"));
|
|
}
|
|
//2. DetailPage 처리 : bbs_view > div.contents 가진 객체를 찾아서 처리
|
|
list($mediaInfos, $mediaTags) = $this->detailPage($detail_url);
|
|
return array($title, $nickname, $mediaInfos, $mediaTags);
|
|
}
|
|
}
|