Automationn init...
This commit is contained in:
parent
749c691e11
commit
14e4fc5d47
@ -28,9 +28,11 @@ class YamapLibrary
|
||||
|
||||
public function getCrawler(string $url, string $tag): Crawler
|
||||
{
|
||||
log_message("debug", __FUNCTION__ . "-> " . $url . "\n");
|
||||
$html = $this->getContentByMyWeb($url);
|
||||
return $this->createByMyCrawler($html)->filter($tag);
|
||||
$response = $this->getContentByMyWeb($url);
|
||||
if (!$response) {
|
||||
throw new \Exception("getCrawler 실패:{$url}");
|
||||
}
|
||||
return $this->createByMyCrawler($response)->filter($tag);
|
||||
}
|
||||
|
||||
public function getListURLs(
|
||||
|
||||
@ -37,11 +37,11 @@ trait MyWebTrait
|
||||
}
|
||||
|
||||
//url에 http 나 https가 포함되어 있으면 true
|
||||
final protected function isContainsHttpOrHttpsByMyWeb($url)
|
||||
final protected function isContainsHttpOrHttpsByMyWeb($url): bool
|
||||
{
|
||||
return strpos($url, 'http://') !== false || strpos($url, 'https://') !== false;
|
||||
}
|
||||
final protected function getContentByMyWeb(string $url, array $options = [])
|
||||
final protected function getContentByMyWeb(string $url, array $options = []): string
|
||||
{
|
||||
//url에 http 나 https가 포함되어 있지않으면
|
||||
if (!($this->isContainsHttpOrHttpsByMyWeb($url))) {
|
||||
@ -49,70 +49,52 @@ trait MyWebTrait
|
||||
}
|
||||
$response = $this->getClientByMyWeb()->get($url, $options);
|
||||
if ($response->getStatusCode() != 200) {
|
||||
log_message("error", "{$url} 접속실패: " . $response->getStatusCode());
|
||||
return false;
|
||||
throw new \Exception("error", "{$url} 접속실패: " . $response->getStatusCode());
|
||||
}
|
||||
return $response->getBody()->getContents();
|
||||
}
|
||||
|
||||
// 로그인 메서드
|
||||
final protected function loginByMyWeb($url, $username, $password)
|
||||
final protected function loginByMyWeb($url, $username, $password): void
|
||||
{
|
||||
try {
|
||||
$response = $this->getClientByMyWeb()->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;
|
||||
$response = $this->getClientByMyWeb()->post($this->gethost() . $url, [
|
||||
'form_params' => [
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
],
|
||||
'cookies' => $this->getCookieJar(),
|
||||
]);
|
||||
if ($response->getStatusCode() != 200) {
|
||||
throw new \Exception("info", "로그인 실패: " . $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
||||
// 파일 다운로드 메서드
|
||||
final protected function downloadByMyWeb(string $url, string $fullPath, bool $debug = false): bool
|
||||
final protected function downloadByMyWeb(string $url, string $fullPath, bool $debug = false)
|
||||
{
|
||||
try {
|
||||
log_message("debug", "donwload:URL-> " . $url);
|
||||
$fileNames = explode('/', $url);
|
||||
if (!is_array($fileNames) || !count($fileNames)) {
|
||||
throw new \Exception("Download URL Error:" . $url);
|
||||
log_message("debug", "donwload:URL-> " . $url);
|
||||
$fileNames = explode('/', $url);
|
||||
if (!is_array($fileNames) || !count($fileNames)) {
|
||||
throw new \Exception("Download URL Error:" . $url);
|
||||
}
|
||||
if (!is_dir($fullPath)) {
|
||||
if (!mkdir($fullPath)) {
|
||||
throw new \Exception("Make Directory Error:" . $fullPath);
|
||||
}
|
||||
if (!is_dir($fullPath)) {
|
||||
if (!mkdir($fullPath)) {
|
||||
throw new \Exception("Make Directory Error:" . $fullPath);
|
||||
}
|
||||
}
|
||||
$fileName = array_pop($fileNames);
|
||||
$savePath = $fullPath . DIRECTORY_SEPARATOR . $fileName;
|
||||
log_message("debug", "download:SavePath-> " . $savePath);
|
||||
if (!$debug) {
|
||||
$response = $this->getContentByMyWeb($url, [
|
||||
'cookies' => $this->getCookieJarByMyWeb(),
|
||||
// 'sink' => $savePath,
|
||||
]);
|
||||
if (!$response) {
|
||||
throw new \Exception("info", "{$fileName} 파일 다운로드 실패");
|
||||
}
|
||||
$fileName = array_pop($fileNames);
|
||||
$savePath = $fullPath . DIRECTORY_SEPARATOR . $fileName;
|
||||
log_message("debug", "download:SavePath-> " . $savePath);
|
||||
if (!$debug) {
|
||||
$response = $this->getContentByMyWeb($url, [
|
||||
'cookies' => $this->getCookieJarByMyWeb(),
|
||||
// 'sink' => $savePath,
|
||||
]);
|
||||
if (!$response) {
|
||||
log_message("info", "{$fileName} 파일 다운로드 실패");
|
||||
return false;
|
||||
}
|
||||
$this->saveByMyStorage($savePath, $response);
|
||||
log_message("info", "{$fileName} 파일이 다운로드되었습니다!");
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
log_message("error", "다운로드 중 오류 발생: " . $e->getMessage());
|
||||
return false;
|
||||
$this->saveByMyStorage($savePath, $response);
|
||||
log_message("info", "{$fileName} 파일이 다운로드되었습니다!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user