Automation init...

This commit is contained in:
최준흠 2024-09-02 00:04:12 +09:00
parent 4ee7090ffb
commit d3b9be4ab6
8 changed files with 130 additions and 97 deletions

View File

@ -22,7 +22,7 @@ $routes->group('/user', function ($routes) {
$routes->group('cli', ['namespace' => 'App\Controllers\CLI'], function ($routes) {
$routes->cli('mangboard/level', 'Mangboard::level');
$routes->cli('crawl/yamap', 'Crawl::yamap');
$routes->cli('crawler/yamap', 'Crawler::yamap');
});
$routes->group('admin', ['namespace' => 'App\Controllers\Admin', 'filter' => 'authFilter:manager'], function ($routes) {

View File

@ -41,7 +41,7 @@ class UserController extends AdminController
$entity = $this->setUserPointByMangboardTrait($entity, intval($point), $sign);
return "완료되었습니다.";
} catch (\Exception $e) {
log_message('error', $e->getMessage());
log_message("error", $e->getMessage());
return $e->getMessage();
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace App\Controllers\CLI;
use App\Libraries\CrawlerLibrary;
use App\Controllers\BaseController;
use Symfony\Component\DomCrawler\Crawler;
class Crawl extends BaseController
{
public function yamap()
{
try {
$library = new CrawlerLibrary("https://www.yamap16.com");
echo "Host-> " . $library->getHost() . "\n";
$html = $library->getInnerHTML("/Board/List.aspx?id=free&ca=1");
$links = $library->getLinks($html, "a.list_subject");
$html = $library->getInnerHTML($links[27], "div.contents p");
$images = $library->getImages($html);
var_dump($images);
// file_put_contents("test.jpg", $url);
} catch (\Exception $e) {
echo $e->getMessage();
}
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Controllers\CLI;
use App\Libraries\CrawlerLibrary;
use App\Controllers\BaseController;
class Crawler extends BaseController
{
public function yamap()
{
try {
$library = new CrawlerLibrary("https://www.yamap16.com");
echo "Host-> " . $library->getHost() . "\n";
// $html = $library->getInnerHTML("/Board/List.aspx?id=free&ca=1");
// $links = $library->getLinks($html, "a.list_subject");
$url = "/Board/View.aspx?id=free&ca=1&rno=192681&page=1";
$html = $library->getInnerHTML($url, "div.contents p");
$images = $library->getImages($html);
foreach ($images as $image) {
echo "Image-> " . $image . "\n";
$library->download($image);
}
log_message("info", "완료되었습니다.");
return true;
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return false;
}
}
}

View File

@ -19,10 +19,10 @@ class Mangboard extends BaseController
$entity = $this->setUserLevelByMangboardTrait($entity);
log_message("debug", __FUNCTION__ . "=>[{$entity}] 회원님의 Level은 {$entity->getLevel()} 입니다.");
}
log_message('info', "완료되었습니다.");
log_message("info", "완료되었습니다.");
return true;
} catch (\Exception $e) {
log_message('error', $e->getMessage());
log_message("error", $e->getMessage());
return false;
}
}

View File

@ -2,32 +2,15 @@
namespace App\Libraries;
use GuzzleHttp\Client;
use Symfony\Component\DomCrawler\Crawler;
class CrawlerLibrary
class CrawlerLibrary extends WebBaseLibrary
{
private $_client = null;
private $_host = "";
public function __construct(string $host)
{
$this->_host = $host;
parent::__construct($host);
}
final public function getHost(): string
{
return $this->_host;
}
private function getClient(): Client
{
if (is_null($this->_client)) {
$this->_client = new Client(['verify' => false]);
}
return $this->_client;
}
final public function getContent(string $url): string
{
$response = $this->getClient()->request('GET', $this->gethost() . $url);

View File

@ -1,47 +0,0 @@
<?php
namespace App\Libraries;
class LoginLibrary
{
private $url;
private $credentials;
private $cookieFile;
// $credentials = [
// 'username' => 'your_username',
// 'password' => 'your_password'
// ];
public function __construct(string $url, array $credentials)
{
$this->url = $url;
$this->credentials = $credentials;
$this->cookieFile = tempnam(sys_get_temp_dir(), 'cookie'); // 임시 쿠키 파일 생성
}
public function execute()
{
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->credentials));
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile); // 쿠키를 저장할 파일 지정
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function getCookie()
{
return $this->cookieFile;
}
public function clearCookie()
{
// 쿠키 파일 삭제
unlink($this->cookieFile);
}
}

View File

@ -0,0 +1,93 @@
<?php
namespace App\Libraries;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Client;
abstract class WebBaseLibrary
{
private $_host = "";
private $_client = null;
private $_cookieJar = null;
protected function __construct(string $host)
{
$this->_host = $host;
}
final public function getHost(): string
{
return $this->_host;
}
final protected function getClient(): Client
{
if ($this->_client === null) {
$this->_client = new Client(['verify' => false]);
}
return $this->_client;
}
final protected function getCookieJar()
{
if ($this->_cookieJar === null) {
$this->_cookieJar = new CookieJar();
}
return $this->_cookieJar;
}
// 로그인 메서드
public function login($url, $username, $password)
{
try {
$response = $this->getClient()->post($this->gethost() . $url, [
'form_params' => [
'username' => $username,
'password' => $password,
],
'cookies' => $this->getCookieJar(),
]);
if ($response->getStatusCode() == 200) {
log_message("info", "로그인 성공!");
return true;
} else {
log_message("info", "로그인 실패: " . $response->getStatusCode());
return false;
}
} catch (\Exception $e) {
log_message("error", "파일 다운로드 중 오류 발생: " . $e->getMessage());
return false;
}
}
// 파일 다운로드 메서드
public function download($url, $addPath = false)
{
try {
$fullPath = WRITEPATH . "uploads";
$fullPath .= !$addPath ? '' : DIRECTORY_SEPARATOR . $addPath;
if (!is_dir($fullPath)) {
mkdir($fullPath);
}
$temps = explode('/', $url);
if (!is_array($temps) || !count($temps)) {
throw new \Exception("URL error:" . var_dump($temps, true));
}
$file = $fullPath . DIRECTORY_SEPARATOR . array_pop($temps);
$response = $this->getClient()->get($this->gethost() . $url, [
'cookies' => $this->getCookieJar(),
'sink' => $file,
]);
if ($response->getStatusCode() == 200) {
log_message("info", "파일이 성공적으로 다운로드되었습니다!");
return true;
} else {
log_message("info", "파일 다운로드 실패: " . $response->getStatusCode());
return false;
}
} catch (\Exception $e) {
log_message("error", "파일 다운로드 중 오류 발생: " . $e->getMessage());
return false;
}
}
}