167 lines
5.6 KiB
PHP
167 lines
5.6 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries;
|
|
|
|
use Symfony\Component\DomCrawler\Crawler;
|
|
|
|
//Yamap
|
|
// define('YAMAP', [
|
|
// 'host' => ['url' => getenv('yamap.host.url')],
|
|
// 'list' => [
|
|
// 'url' => getenv('yamap.list.url'),
|
|
// 'tag' => getenv('yamap.list.tag'),
|
|
// 'item' => [
|
|
// 'tag' => getenv('yamap.list.item.tag'),
|
|
// 'subject' => [
|
|
// 'tag' => getenv('yamap.list.item.subject.tag')
|
|
// ],
|
|
// 'nickname' => [
|
|
// 'tag' => getenv('yamap.list.item.nickname.tag'),
|
|
// 'except' => getenv('yamap.list.item.nickname.except'),
|
|
// ],
|
|
// ],
|
|
// ],
|
|
// 'view' => [
|
|
// 'tag' => getenv('yamap.view.tag'),
|
|
// 'content' => [
|
|
// 'tag' => getenv('yamap.view.content.tag'),
|
|
// ],
|
|
// 'test' => [
|
|
// 'url' => getenv('yamap.view.test.url'),
|
|
// ]
|
|
// ],
|
|
// ]);
|
|
|
|
class YamapLibrary extends CommonLibrary
|
|
{
|
|
private $_myWeb = null;
|
|
private $_myStorage = null;
|
|
private $_myCrawler = null;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
public function getMyWeb()
|
|
{
|
|
if ($this->_myWeb === null) {
|
|
throw new \Exception("MyWeb Library가 정의되지 않았습니다.");
|
|
}
|
|
return $this->_myWeb;
|
|
}
|
|
public function setMyWeb($myWeb)
|
|
{
|
|
$this->_myWeb = $myWeb;
|
|
}
|
|
|
|
public function getMyStorage()
|
|
{
|
|
if ($this->_myStorage === null) {
|
|
throw new \Exception("MyStorage Library가 정의되지 않았습니다.");
|
|
}
|
|
return $this->_myStorage;
|
|
}
|
|
public function setMyStorage($myStorage)
|
|
{
|
|
$this->_myStorage = $myStorage;
|
|
}
|
|
public function getMyCrawler()
|
|
{
|
|
if ($this->_myWeb === null) {
|
|
throw new \Exception("MyCrawler Library가 정의되지 않았습니다.");
|
|
}
|
|
return $this->_myCrawler;
|
|
}
|
|
public function setMyCrawler($myCrawler)
|
|
{
|
|
$this->_myCrawler = $myCrawler;
|
|
}
|
|
|
|
private function getCrawler(string $url, string $tag): Crawler
|
|
{
|
|
$response = $this->getMyWeb()->getContent($url);
|
|
if (!$response) {
|
|
throw new \Exception("getCrawler 실패:{$url}");
|
|
}
|
|
return $this->getMyCrawler()->create($response)->filter($tag);
|
|
}
|
|
|
|
private function download_process(Crawler $crawler, array $options): array
|
|
{
|
|
$datas = [];
|
|
log_message("debug", "download:{$options["tag"]},{$options["attr"]}");
|
|
$nodes = $this->getMyCrawler()->getNodes($crawler, $options);
|
|
foreach ($nodes as $node) {
|
|
list($fileName, $content) = $this->getMyWeb()->download($node->attr($options["attr"]));
|
|
$this->getMyStorage()->setFileName($fileName);
|
|
if (!$this->getMyStorage()->save($content)) {
|
|
continue;
|
|
}
|
|
$datas[] = [
|
|
"path" => $this->getMyStorage()->getDefaultPath() . DIRECTORY_SEPARATOR . $this->getMyStorage()->getPath(),
|
|
"fileName" => $fileName,
|
|
"content" => $content
|
|
];
|
|
}
|
|
return $datas;
|
|
}
|
|
|
|
public function mainPage(): array
|
|
{
|
|
$url = getenv("yamap.list.url");
|
|
$crawler = $this->getCrawler($url, getenv("yamap.list.tag"));
|
|
$item_tag = getenv("yamap.list.item.tag");
|
|
$item_subject_tag = getenv("yamap.list.item.subject.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_subject_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 객체를 찾아서
|
|
$url = $node->filter($item_subject_tag)->attr("href");
|
|
$lists[] = ['nickname' => $nickname, 'url' => $url];
|
|
}
|
|
}
|
|
);
|
|
if (!count($lists)) {
|
|
throw new \Exception("Target URL이 없습니다.");
|
|
}
|
|
return array($lists[0]["nickname"], $lists[0]["url"]);
|
|
}
|
|
|
|
public function detailPage($url): array
|
|
{
|
|
$crawler = $this->getCrawler($url, getenv("yamap.view.content.tag"));
|
|
//3. Image
|
|
$images = $this->download_process($crawler, ["tag" => "img", "attr" => "src"]);
|
|
//4. Video
|
|
$videos = $this->download_process($crawler, ["tag" => "video", "attr" => "src"]);
|
|
return array_merge($images, $videos);
|
|
}
|
|
|
|
public function build(): array
|
|
{
|
|
//1. 해당사이트 MainPage 처리
|
|
if ($this->getDebug()) {
|
|
$nickname = getenv("yamap.view.test.nickname");
|
|
$detail_url = getenv("yamap.view.test.url");
|
|
} else {
|
|
list($nickname, $detail_url) = $this->mainPage();
|
|
}
|
|
//2. DetailPage 처리 : bbs_view > div.contents 가진 객체를 찾아서 처리
|
|
$detailDatas = $this->detailPage($detail_url);
|
|
return array($nickname, $detailDatas);
|
|
}
|
|
}
|