diff --git a/app/Config/Routes.php b/app/Config/Routes.php index cb3f571..105ebcf 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -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) { diff --git a/app/Controllers/Admin/UserController.php b/app/Controllers/Admin/UserController.php index 7add4e9..900ac59 100644 --- a/app/Controllers/Admin/UserController.php +++ b/app/Controllers/Admin/UserController.php @@ -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(); } } diff --git a/app/Controllers/CLI/Crawl.php b/app/Controllers/CLI/Crawl.php deleted file mode 100644 index 72fcdb3..0000000 --- a/app/Controllers/CLI/Crawl.php +++ /dev/null @@ -1,27 +0,0 @@ - " . $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(); - } - } -} diff --git a/app/Controllers/CLI/Crawler.php b/app/Controllers/CLI/Crawler.php new file mode 100644 index 0000000..33f3423 --- /dev/null +++ b/app/Controllers/CLI/Crawler.php @@ -0,0 +1,31 @@ + " . $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; + } + } +} diff --git a/app/Controllers/CLI/Mangboard.php b/app/Controllers/CLI/Mangboard.php index 897ef25..f1d42d0 100644 --- a/app/Controllers/CLI/Mangboard.php +++ b/app/Controllers/CLI/Mangboard.php @@ -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; } } diff --git a/app/Libraries/CrawlerLibrary.php b/app/Libraries/CrawlerLibrary.php index 3e55623..543f540 100644 --- a/app/Libraries/CrawlerLibrary.php +++ b/app/Libraries/CrawlerLibrary.php @@ -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); diff --git a/app/Libraries/LoginLibrary.php b/app/Libraries/LoginLibrary.php deleted file mode 100644 index 64531e5..0000000 --- a/app/Libraries/LoginLibrary.php +++ /dev/null @@ -1,47 +0,0 @@ - '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); - } -} diff --git a/app/Libraries/WebBaseLibrary.php b/app/Libraries/WebBaseLibrary.php new file mode 100644 index 0000000..b3297c2 --- /dev/null +++ b/app/Libraries/WebBaseLibrary.php @@ -0,0 +1,93 @@ +_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; + } + } +}