feat: 도서관 검색기 최적화 및 브라우저 헤더 통일화
- 경남대표도서관 검색기를 직접 URL 네비게이션 방식으로 수정하여 JavaScript 변수 문제 해결 - HttpApiMode가 true인 모든 검색기에 표준 브라우저 헤더 적용 (목포, 여수, 광산, 동구, 익산, 완도) - Selenium 기반 검색기의 80% 브라우저 배율 설정 유지 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,8 @@ using OpenQA.Selenium.Interactions;
|
||||
using OpenQA.Selenium.Support.UI;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
@@ -57,6 +59,21 @@ namespace BokBonCheck
|
||||
{
|
||||
if (SeleniumHelper.IsReady == false) await SeleniumHelper.Download();
|
||||
_driver = await SeleniumHelper.CreateDriver(ShowBrowser: showdriver);
|
||||
|
||||
// 브라우저 배율을 80%로 설정 (브라우저가 표시되는 경우에만)
|
||||
if (showdriver)
|
||||
{
|
||||
try
|
||||
{
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("document.body.style.zoom = '0.8';");
|
||||
Console.WriteLine("브라우저 배율을 80%로 설정했습니다.");
|
||||
}
|
||||
catch (Exception zoomEx)
|
||||
{
|
||||
Console.WriteLine($"브라우저 배율 설정 실패: {zoomEx.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("AnsanLibSearcher Driver 초기화 완료");
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -73,102 +90,100 @@ namespace BokBonCheck
|
||||
{
|
||||
Console.WriteLine("도서관 선택 과정 시작...");
|
||||
|
||||
// 1. 모든 체크박스 찾기
|
||||
// 1. 열기버튼 찾기 (작은도서관 체크박스가 보이도록)
|
||||
var element = wait.Until(d => d.FindElement(By.CssSelector(".btIco.plus")));
|
||||
if (element != null)
|
||||
{
|
||||
SafeClick(element);
|
||||
}
|
||||
|
||||
// 2. 현재 체크된 상태를 확인하고, 목표 도서관이 아닌 것들만 해제
|
||||
int uncheckedCount = 0;
|
||||
int checkCount = 0;
|
||||
// 2. 현재 체크된 상태 확인
|
||||
var allCheckboxes = wait.Until(d => d.FindElements(By.CssSelector("div.library input[type='checkbox']")));
|
||||
foreach (var checkbox in allCheckboxes)
|
||||
{
|
||||
try
|
||||
{
|
||||
var checkboxValue = checkbox.GetAttribute("value");
|
||||
var isChecked = checkbox.Selected;
|
||||
var checkedCheckboxes = allCheckboxes.Where(cb => cb.Selected).ToList();
|
||||
|
||||
Console.WriteLine($"체크박스 {checkboxValue}: {(isChecked ? "체크됨" : "체크안됨")}");
|
||||
Console.WriteLine($"현재 체크된 체크박스 개수: {checkedCheckboxes.Count}");
|
||||
|
||||
// 체크되어 있고, 목표 도서관이 아닌 경우에만 해제
|
||||
if (isChecked && checkboxValue != AreaCode)
|
||||
{
|
||||
Console.WriteLine($"{checkboxValue} 도서관 체크 해제 중...");
|
||||
SafeClick(checkbox);
|
||||
Thread.Sleep(50);
|
||||
uncheckedCount++;
|
||||
}
|
||||
else if (isChecked == false && checkboxValue == AreaCode)
|
||||
{
|
||||
Console.WriteLine($"{checkboxValue} 도서관 체크 중...");
|
||||
SafeClick(checkbox);
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"체크박스 처리 중 오류: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"{uncheckedCount}개의 체크박스를 해제했습니다.");
|
||||
|
||||
// 3. 목표 도서관 선택 확인 및 설정
|
||||
// 3. 지정한 도서관만 정확히 1개 체크되어 있는지 확인
|
||||
bool isTargetOnlyChecked = false;
|
||||
if (!string.IsNullOrEmpty(AreaCode))
|
||||
{
|
||||
Console.WriteLine($"목표 도서관 '{AreaCode}' 선택 확인 중...");
|
||||
|
||||
try
|
||||
if (checkedCheckboxes.Count == 1)
|
||||
{
|
||||
var targetCheckbox = wait.Until(d => d.FindElement(By.CssSelector($"div.library input[type='checkbox'][value='{AreaCode}']")));
|
||||
|
||||
if (!targetCheckbox.Selected)
|
||||
var onlyChecked = checkedCheckboxes[0];
|
||||
var onlyCheckedValue = onlyChecked.GetAttribute("value");
|
||||
if (onlyCheckedValue == AreaCode)
|
||||
{
|
||||
Console.WriteLine($"{AreaCode} 도서관이 체크되지 않았으므로 선택합니다.");
|
||||
SafeClick(targetCheckbox);
|
||||
Thread.Sleep(300);
|
||||
Console.WriteLine($"✓ {AreaCode} 도서관만 정확히 선택되어 있음 - 완료");
|
||||
isTargetOnlyChecked = true;
|
||||
}
|
||||
|
||||
// 최종 선택 상태 확인
|
||||
if (targetCheckbox.Selected)
|
||||
{
|
||||
Console.WriteLine($"✓ {AreaCode} 도서관 선택 완료");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"✗ {AreaCode} 도서관 선택 실패");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"목표 도서관 선택 실패: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("AreaCode가 비어있음 - 모든 도서관 선택");
|
||||
|
||||
// 모든 체크박스를 체크
|
||||
foreach (var checkbox in allCheckboxes)
|
||||
// 4. 목표 상태가 아니면 전체선택 버튼으로 모두 해제
|
||||
if (!isTargetOnlyChecked)
|
||||
{
|
||||
Console.WriteLine("전체선택 버튼을 이용해서 모든 체크박스 해제 중...");
|
||||
|
||||
// 전체선택 버튼들 찾기 (총 2개)
|
||||
var allSelectButtons = wait.Until(d => d.FindElements(By.CssSelector("span.btnAllSelect")));
|
||||
Console.WriteLine($"전체선택 버튼 {allSelectButtons.Count}개 발견");
|
||||
|
||||
// 모든 전체선택 버튼 클릭해서 해제
|
||||
foreach (var button in allSelectButtons)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!checkbox.Selected)
|
||||
Console.WriteLine("전체선택 버튼 클릭 중...");
|
||||
SafeClick(button);
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"전체선택 버튼 클릭 오류: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 해제 후 체크된 체크박스 개수 확인
|
||||
var remainingChecked = allCheckboxes.Where(cb => cb.Selected).Count();
|
||||
Console.WriteLine($"전체선택 버튼 클릭 후 체크된 체크박스 개수: {remainingChecked}");
|
||||
|
||||
// 5. 지정 도서관만 체크
|
||||
if (!string.IsNullOrEmpty(AreaCode))
|
||||
{
|
||||
Console.WriteLine($"목표 도서관 '{AreaCode}' 선택 중...");
|
||||
|
||||
try
|
||||
{
|
||||
var targetCheckbox = wait.Until(d => d.FindElement(By.CssSelector($"div.library input[type='checkbox'][value='{AreaCode}']")));
|
||||
|
||||
if (!targetCheckbox.Selected)
|
||||
{
|
||||
SafeClick(checkbox);
|
||||
Thread.Sleep(100);
|
||||
Console.WriteLine($"{AreaCode} 도서관 체크 중...");
|
||||
SafeClick(targetCheckbox);
|
||||
Thread.Sleep(300);
|
||||
}
|
||||
|
||||
// 최종 선택 상태 확인
|
||||
if (targetCheckbox.Selected)
|
||||
{
|
||||
Console.WriteLine($"✓ {AreaCode} 도서관 선택 완료");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"✗ {AreaCode} 도서관 선택 실패");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"전체 선택 중 오류: {ex.Message}");
|
||||
Console.WriteLine($"목표 도서관 선택 실패: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("AreaCode가 비어있음 - 전체 도서관으로 검색");
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -250,6 +265,20 @@ namespace BokBonCheck
|
||||
|
||||
// 페이지 로딩 대기
|
||||
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(15));
|
||||
|
||||
// 페이지 로드 후 브라우저 배율을 80%로 설정 (브라우저가 표시되는 경우에만)
|
||||
try
|
||||
{
|
||||
if (_driver.Manage().Window.Size.Width > 0) // 브라우저가 표시되는 경우
|
||||
{
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("document.body.style.zoom = '0.8';");
|
||||
Console.WriteLine("페이지 로드 후 브라우저 배율을 80%로 설정했습니다.");
|
||||
}
|
||||
}
|
||||
catch (Exception zoomEx)
|
||||
{
|
||||
Console.WriteLine($"페이지 배율 설정 실패: {zoomEx.Message}");
|
||||
}
|
||||
|
||||
// 도서관 선택
|
||||
if (SelectLibrary(wait) == false)
|
||||
@@ -264,7 +293,7 @@ namespace BokBonCheck
|
||||
try
|
||||
{
|
||||
// 검색 입력창이 상호작용 가능할 때까지 대기
|
||||
var searchInput = wait.Until(d => d.FindElement(By.Id("advTitle")));
|
||||
var searchInput = wait.Until(d => d.FindElement(By.Id("advTitle")));
|
||||
var dispaly = searchInput.Displayed;
|
||||
var enabled = searchInput.Enabled;
|
||||
|
||||
@@ -274,7 +303,7 @@ namespace BokBonCheck
|
||||
|
||||
// readonly 속성이 있다면 제거
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].removeAttribute('readonly');", searchInput);
|
||||
|
||||
|
||||
// 클릭해서 포커스 맞추기
|
||||
try
|
||||
{
|
||||
@@ -303,13 +332,13 @@ namespace BokBonCheck
|
||||
{
|
||||
// SendKeys가 실패하면 JavaScript로 값 설정
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].value = arguments[1];", searchInput, searchTerm);
|
||||
|
||||
|
||||
// input 이벤트 발생시켜서 웹페이지에 변경사항 알림
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].dispatchEvent(new Event('input'));", searchInput);
|
||||
}
|
||||
|
||||
Console.WriteLine($"검색어 '{searchTerm}' 입력 완료");
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -354,7 +383,17 @@ namespace BokBonCheck
|
||||
|
||||
SearchComplete:
|
||||
Console.WriteLine("검색 실행 완료, 페이지 로딩 대기 중...");
|
||||
|
||||
|
||||
|
||||
if (this._driver.Url.EndsWith("DetailSearchResult") == false)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
await Task.Delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 페이지 변경을 감지하는 메서드
|
||||
await WaitForPageChange(new WebDriverWait(_driver, TimeSpan.FromSeconds(15)));
|
||||
|
||||
@@ -388,12 +427,13 @@ namespace BokBonCheck
|
||||
{
|
||||
errmessage = string.Empty;
|
||||
|
||||
if(driver.Url.EndsWith("DetailSearchResult")==false)
|
||||
if (driver.Url.EndsWith("DetailSearchResult") == false)
|
||||
{
|
||||
errmessage = "결과페이지가아님";
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
// 1. 검색결과가 없는 경우 확인
|
||||
@@ -411,17 +451,37 @@ namespace BokBonCheck
|
||||
// 검색결과가 있는 경우로 진행
|
||||
}
|
||||
|
||||
|
||||
RetryPoint:
|
||||
bool retry = false;
|
||||
// 2. 안산시 도서관 특화: span.count.gothic em 요소에서 직접 추출
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
var countElement = driver.FindElement(By.CssSelector("span.count.gothic em"));
|
||||
if (countElement != null)
|
||||
{
|
||||
// Text 대신 InnerHtml이나 GetAttribute 사용해보기
|
||||
var countText = countElement.Text.Trim();
|
||||
Console.WriteLine($"count 요소 텍스트: '{countText}'");
|
||||
|
||||
var innerHTML = countElement.GetAttribute("innerHTML");
|
||||
var outerHTML = countElement.GetAttribute("outerHTML");
|
||||
|
||||
Console.WriteLine($"count 요소 .Text: '{countText}'");
|
||||
Console.WriteLine($"count 요소 innerHTML: '{innerHTML}'");
|
||||
Console.WriteLine($"count 요소 outerHTML: '{outerHTML}'");
|
||||
|
||||
// innerHTML이나 outerHTML에서 텍스트 추출 시도
|
||||
string textToUse = !string.IsNullOrEmpty(countText) ? countText : innerHTML;
|
||||
if (string.IsNullOrEmpty(textToUse))
|
||||
{
|
||||
textToUse = outerHTML;
|
||||
}
|
||||
|
||||
Console.WriteLine($"추출할 텍스트: '{textToUse}'");
|
||||
|
||||
// "총 0 건 " 형태에서 숫자 추출
|
||||
var match = Regex.Match(countText, @"총\s*(\d+)\s*건", RegexOptions.IgnoreCase);
|
||||
var match = Regex.Match(textToUse, @"총\s*(\d+)\s*건", RegexOptions.IgnoreCase);
|
||||
if (match.Success && int.TryParse(match.Groups[1].Value, out int count))
|
||||
{
|
||||
if (count == 0)
|
||||
@@ -434,6 +494,10 @@ namespace BokBonCheck
|
||||
Console.WriteLine($"검색 결과: {count}건");
|
||||
return count;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"정규식 매칭 실패: '{textToUse}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex1)
|
||||
@@ -449,7 +513,7 @@ namespace BokBonCheck
|
||||
{
|
||||
var fullText = countSpan.Text.Trim();
|
||||
Console.WriteLine($"count span 전체 텍스트: '{fullText}'");
|
||||
|
||||
|
||||
var match = Regex.Match(fullText, @"총\s*(\d+)\s*건", RegexOptions.IgnoreCase);
|
||||
if (match.Success && int.TryParse(match.Groups[1].Value, out int count))
|
||||
{
|
||||
@@ -512,8 +576,19 @@ namespace BokBonCheck
|
||||
}
|
||||
}
|
||||
|
||||
errmessage = "결과수량을찾을수없음";
|
||||
return -1;
|
||||
if (retry == false)
|
||||
{
|
||||
Console.WriteLine( "결과를 찾을 수 없어 재시도");
|
||||
retry = true;
|
||||
Task.Delay(1000);
|
||||
goto RetryPoint;
|
||||
}
|
||||
else
|
||||
{
|
||||
errmessage = "결과수량을찾을수없음";
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -530,6 +605,8 @@ namespace BokBonCheck
|
||||
{
|
||||
await Task.Delay(500);
|
||||
|
||||
|
||||
|
||||
// 페이지 로딩 상태 확인
|
||||
wait.Until(d =>
|
||||
{
|
||||
|
||||
215
unimarc/unimarc/SearchModel/GwangjuDongguLibSearcher.cs
Normal file
215
unimarc/unimarc/SearchModel/GwangjuDongguLibSearcher.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using UniMarc.SearchModel;
|
||||
using System.Text;
|
||||
using OpenQA.Selenium.Support.UI;
|
||||
|
||||
namespace BokBonCheck
|
||||
{
|
||||
public class GwangjuDongguLibSearcher : ILibrarySearcher
|
||||
{
|
||||
protected string AreaCode { get; set; } = string.Empty;
|
||||
public string SiteName { get; protected set; }
|
||||
public string SiteUrl => "https://lib.donggu.kr/BookSearch/detail";
|
||||
public bool HttpApiMode { get; set; } = true;
|
||||
|
||||
public int No { get; set; }
|
||||
|
||||
private static readonly HttpClient _httpClient = new HttpClient()
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" },
|
||||
{ "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7" },
|
||||
{ "Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7" },
|
||||
{ "Accept-Encoding", "gzip, deflate, br, zstd" },
|
||||
{ "Sec-Fetch-Dest", "document" },
|
||||
{ "Sec-Fetch-Mode", "navigate" },
|
||||
{ "Sec-Fetch-Site", "none" },
|
||||
{ "Sec-Fetch-User", "?1" },
|
||||
{ "Cache-Control", "max-age=0" },
|
||||
{ "Upgrade-Insecure-Requests", "1" }
|
||||
}
|
||||
};
|
||||
|
||||
public GwangjuDongguLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
this.No = no;
|
||||
this.AreaCode = areaCode;
|
||||
this.SiteName = $"광주동구({areaName})";
|
||||
}
|
||||
|
||||
public async Task StartDriver(bool showdriver = false)
|
||||
{
|
||||
// HTTP 클라이언트 사용으로 별도 드라이버 불필요
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void StopDriver()
|
||||
{
|
||||
// HTTP 클라이언트 사용으로 별도 정리 불필요
|
||||
}
|
||||
|
||||
public async Task<BookSearchResult> SearchAsync(string searchTerm)
|
||||
{
|
||||
var result = new BookSearchResult
|
||||
{
|
||||
SiteName = SiteName,
|
||||
SearchTerm = searchTerm,
|
||||
SearchTime = DateTime.Now
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
// 검색어 URL 인코딩
|
||||
var encodedSearchTerm = HttpUtility.UrlEncode(searchTerm, Encoding.UTF8);
|
||||
|
||||
// 검색 URL 구성
|
||||
var searchUrl = $"{SiteUrl}?queryTitle={encodedSearchTerm}&queryPublisher=&queryAuthor=";
|
||||
|
||||
// 도서관 코드가 있으면 추가
|
||||
if (!string.IsNullOrEmpty(AreaCode))
|
||||
{
|
||||
searchUrl += $"&libCode%5B%5D={AreaCode}";
|
||||
}
|
||||
else
|
||||
{
|
||||
// 전체 도서관 선택 (모든 도서관 코드 추가)
|
||||
var allLibCodes = new[] { "129231", "129003", "729079", "729072", "729073" };
|
||||
foreach (var libCode in allLibCodes)
|
||||
{
|
||||
searchUrl += $"&libCode%5B%5D={libCode}";
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine($"광주동구 검색 URL: {searchUrl}");
|
||||
|
||||
// HTTP GET 요청 실행
|
||||
var response = await _httpClient.GetAsync(searchUrl);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
throw new HttpRequestException($"HTTP {(int)response.StatusCode} {response.StatusCode}: {errorContent}");
|
||||
}
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = $"검색 오류: {ex.Message}";
|
||||
result.BookCount = 0;
|
||||
Console.WriteLine($"광주동구 검색 오류: {ex.Message}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private int ExtractBookCount(string htmlContent, out string errorMessage)
|
||||
{
|
||||
errorMessage = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
// 1. 검색 결과가 없는 경우 확인
|
||||
if (htmlContent.Contains("검색결과가 없습니다") ||
|
||||
htmlContent.Contains("검색된 자료가 없습니다") ||
|
||||
htmlContent.Contains("자료가 없습니다"))
|
||||
{
|
||||
errorMessage = "검색결과없음";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 2. 검색 결과 수량 추출: <p class="totalCount">전체 <strong>2</strong> 건</p>
|
||||
var patterns = new[]
|
||||
{
|
||||
@"<p[^>]*class=""totalCount""[^>]*>전체\s*<strong>\s*(\d+)\s*</strong>\s*건</p>",
|
||||
@"전체\s*<strong>\s*(\d+)\s*</strong>\s*건",
|
||||
@"<strong>\s*(\d+)\s*</strong>\s*건",
|
||||
@"총\s*(\d+)\s*건"
|
||||
};
|
||||
|
||||
foreach (var pattern in patterns)
|
||||
{
|
||||
var match = Regex.Match(htmlContent, pattern, RegexOptions.IgnoreCase);
|
||||
if (match.Success)
|
||||
{
|
||||
if (int.TryParse(match.Groups[1].Value, out int count))
|
||||
{
|
||||
if (count == 0)
|
||||
{
|
||||
errorMessage = "검색결과없음";
|
||||
return 0;
|
||||
}
|
||||
errorMessage = $"검색성공({count}권)";
|
||||
Console.WriteLine($"광주동구 검색 결과: {count}건");
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 더 자세한 패턴으로 시도 (줄바꿈 포함)
|
||||
var multilinePatterns = new[]
|
||||
{
|
||||
@"전체\s*<strong>\s*\r?\n?\s*(\d+)\s*\r?\n?\s*</strong>\s*건",
|
||||
@"<strong>\s*\r?\n?\s*(\d+)\s*\r?\n?\s*</strong>\s*건"
|
||||
};
|
||||
|
||||
foreach (var pattern in multilinePatterns)
|
||||
{
|
||||
var match = Regex.Match(htmlContent, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
|
||||
if (match.Success)
|
||||
{
|
||||
if (int.TryParse(match.Groups[1].Value, out int count))
|
||||
{
|
||||
if (count == 0)
|
||||
{
|
||||
errorMessage = "검색결과없음";
|
||||
return 0;
|
||||
}
|
||||
errorMessage = $"검색성공({count}권)";
|
||||
Console.WriteLine($"광주동구 검색 결과: {count}건");
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
errorMessage = "검색결과 패턴을 찾을 수 없음";
|
||||
Console.WriteLine("광주동구 검색결과 패턴을 찾을 수 없음");
|
||||
return -1;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errorMessage = $"결과 분석 오류: {ex.Message}";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
public Task WaitForPageChange(WebDriverWait wait)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
496
unimarc/unimarc/SearchModel/GwangjuSeoguLibSearcher.cs
Normal file
496
unimarc/unimarc/SearchModel/GwangjuSeoguLibSearcher.cs
Normal file
@@ -0,0 +1,496 @@
|
||||
using AR;
|
||||
using OpenQA.Selenium;
|
||||
using OpenQA.Selenium.Chrome;
|
||||
using OpenQA.Selenium.Chromium;
|
||||
using OpenQA.Selenium.Interactions;
|
||||
using OpenQA.Selenium.Support.UI;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web.UI.WebControls;
|
||||
using System.Xml.Linq;
|
||||
using UniMarc.SearchModel;
|
||||
using UniMarc.마크;
|
||||
using WebDriverManager;
|
||||
using WebDriverManager.DriverConfigs.Impl;
|
||||
|
||||
namespace BokBonCheck
|
||||
{
|
||||
public class GwangjuSeoguLibSearcher : ILibrarySearcher
|
||||
{
|
||||
protected string AreaCode { get; set; } = string.Empty;
|
||||
public string SiteName { get; protected set; }
|
||||
public virtual string SiteUrl => "https://library.seogu.gwangju.kr/index.9is?contentUid=9be5df897834aa07017868116d3407de";
|
||||
public bool HttpApiMode { get; set; } = false;
|
||||
|
||||
public int No { get; set; }
|
||||
|
||||
private ChromiumDriver _driver;
|
||||
|
||||
public GwangjuSeoguLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
this.No = no;
|
||||
this.AreaCode = areaCode;
|
||||
this.SiteName = $"광주서구구립({areaName})";
|
||||
}
|
||||
|
||||
public void StopDriver()
|
||||
{
|
||||
if (_driver != null)
|
||||
{
|
||||
_driver.Quit();
|
||||
_driver.Dispose();
|
||||
_driver = null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StartDriver(bool showdriver = false)
|
||||
{
|
||||
if (_driver == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SeleniumHelper.IsReady == false) await SeleniumHelper.Download();
|
||||
_driver = await SeleniumHelper.CreateDriver(ShowBrowser: showdriver);
|
||||
|
||||
// 브라우저 배율을 80%로 설정 (브라우저가 표시되는 경우에만)
|
||||
if (showdriver)
|
||||
{
|
||||
try
|
||||
{
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("document.body.style.zoom = '0.8';");
|
||||
Console.WriteLine("브라우저 배율을 80%로 설정했습니다.");
|
||||
}
|
||||
catch (Exception zoomEx)
|
||||
{
|
||||
Console.WriteLine($"브라우저 배율 설정 실패: {zoomEx.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("GwangjuSeoguLibSearcher Driver 초기화 완료");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"GwangjuSeoguLibSearcher Driver 초기화 실패: {ex.Message}");
|
||||
throw new InvalidOperationException($"GwangjuSeoguLibSearcher Driver 초기화에 실패했습니다: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
virtual protected bool SelectLibrary(WebDriverWait wait)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("도서관 선택 과정 시작...");
|
||||
|
||||
// 1. 현재 체크된 상태 확인
|
||||
var allCheckboxes = wait.Until(d => d.FindElements(By.CssSelector("ul.lib_list2 input[type='checkbox']")));
|
||||
var checkedCheckboxes = allCheckboxes.Where(cb => cb.Selected).ToList();
|
||||
|
||||
Console.WriteLine($"현재 체크된 체크박스 개수: {checkedCheckboxes.Count}");
|
||||
|
||||
// 2. 지정한 도서관만 정확히 1개 체크되어 있는지 확인
|
||||
bool isTargetOnlyChecked = false;
|
||||
if (!string.IsNullOrEmpty(AreaCode))
|
||||
{
|
||||
if (checkedCheckboxes.Count == 1)
|
||||
{
|
||||
var onlyChecked = checkedCheckboxes[0];
|
||||
var onlyCheckedValue = onlyChecked.GetAttribute("value");
|
||||
if (onlyCheckedValue == AreaCode)
|
||||
{
|
||||
Console.WriteLine($"✓ {AreaCode} 도서관만 정확히 선택되어 있음 - 완료");
|
||||
isTargetOnlyChecked = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 목표 상태가 아니면 모든 체크박스 해제 후 지정 도서관만 선택
|
||||
if (!isTargetOnlyChecked)
|
||||
{
|
||||
Console.WriteLine("모든 체크박스 해제 후 지정 도서관만 선택 중...");
|
||||
|
||||
// 모든 체크박스를 해제
|
||||
foreach (var checkbox in allCheckboxes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (checkbox.Selected)
|
||||
{
|
||||
var checkboxValue = checkbox.GetAttribute("value");
|
||||
Console.WriteLine($"{checkboxValue} 도서관 체크 해제 중...");
|
||||
SafeClick(checkbox);
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"체크박스 해제 중 오류: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 지정 도서관만 체크
|
||||
if (!string.IsNullOrEmpty(AreaCode))
|
||||
{
|
||||
Console.WriteLine($"목표 도서관 '{AreaCode}' 선택 중...");
|
||||
|
||||
try
|
||||
{
|
||||
var targetCheckbox = wait.Until(d => d.FindElement(By.CssSelector($"ul.lib_list2 input[type='checkbox'][value='{AreaCode}']")));
|
||||
|
||||
if (!targetCheckbox.Selected)
|
||||
{
|
||||
Console.WriteLine($"{AreaCode} 도서관 체크 중...");
|
||||
SafeClick(targetCheckbox);
|
||||
Thread.Sleep(300);
|
||||
}
|
||||
|
||||
// 최종 선택 상태 확인
|
||||
if (targetCheckbox.Selected)
|
||||
{
|
||||
Console.WriteLine($"✓ {AreaCode} 도서관 선택 완료");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"✗ {AreaCode} 도서관 선택 실패");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"목표 도서관 선택 실패: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("AreaCode가 비어있음 - 전체 도서관으로 검색");
|
||||
|
||||
// 모든 체크박스를 체크
|
||||
foreach (var checkbox in allCheckboxes)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!checkbox.Selected)
|
||||
{
|
||||
SafeClick(checkbox);
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"전체 선택 중 오류: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"도서관 선택 실패: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected void SafeClick(IWebElement element)
|
||||
{
|
||||
// 안정적인 클릭을 위한 여러 방법 시도
|
||||
try
|
||||
{
|
||||
// 1. 요소가 보이도록 스크롤 후 일반 클릭
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);
|
||||
Thread.Sleep(300);
|
||||
element.Click();
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
// 2. JavaScript로 클릭 시도
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].click();", element);
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
// 3. Actions 클래스 사용
|
||||
var actions = new Actions(_driver);
|
||||
actions.MoveToElement(element).Click().Perform();
|
||||
}
|
||||
catch
|
||||
{
|
||||
try
|
||||
{
|
||||
// 4. 체크박스의 경우 직접 체크 상태 변경
|
||||
if (element.TagName.ToLower() == "input" && element.GetAttribute("type") == "checkbox")
|
||||
{
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].checked = !arguments[0].checked;", element);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 일반 요소는 강제 클릭
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].dispatchEvent(new Event('click'));", element);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"모든 클릭 방법 실패: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BookSearchResult> SearchAsync(string searchTerm)
|
||||
{
|
||||
var result = new BookSearchResult
|
||||
{
|
||||
SiteName = SiteName,
|
||||
SearchTerm = searchTerm,
|
||||
SearchTime = DateTime.Now
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
// 드라이버가 없으면 자동으로 시작
|
||||
if (_driver == null)
|
||||
{
|
||||
await StartDriver();
|
||||
}
|
||||
|
||||
var cururl = _driver.Url;
|
||||
if (cururl.Equals(SiteUrl) == false)
|
||||
_driver.Navigate().GoToUrl(SiteUrl);
|
||||
|
||||
// 페이지 로딩 대기
|
||||
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(15));
|
||||
|
||||
// 페이지 로드 후 브라우저 배율을 80%로 설정 (브라우저가 표시되는 경우에만)
|
||||
try
|
||||
{
|
||||
if (_driver.Manage().Window.Size.Width > 0) // 브라우저가 표시되는 경우
|
||||
{
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("document.body.style.zoom = '0.8';");
|
||||
Console.WriteLine("페이지 로드 후 브라우저 배율을 80%로 설정했습니다.");
|
||||
}
|
||||
}
|
||||
catch (Exception zoomEx)
|
||||
{
|
||||
Console.WriteLine($"페이지 배율 설정 실패: {zoomEx.Message}");
|
||||
}
|
||||
|
||||
// 도서관 선택
|
||||
if (SelectLibrary(wait) == false)
|
||||
{
|
||||
result.ErrorMessage = "도서관선택실패";
|
||||
result.BookCount = -1;
|
||||
result.IsSuccess = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 검색어 입력
|
||||
try
|
||||
{
|
||||
var searchInput = wait.Until(d => d.FindElement(By.Id("searchWord")));
|
||||
|
||||
// 요소가 보이도록 스크롤
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("arguments[0].scrollIntoView(true);", searchInput);
|
||||
Thread.Sleep(300);
|
||||
|
||||
// 기존 값 제거 후 검색어 입력
|
||||
searchInput.Clear();
|
||||
searchInput.SendKeys(searchTerm);
|
||||
|
||||
Console.WriteLine($"검색어 '{searchTerm}' 입력 완료");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.ErrorMessage = $"검색어입력실패({ex.Message})";
|
||||
result.BookCount = -1;
|
||||
result.IsSuccess = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 검색 버튼 클릭
|
||||
try
|
||||
{
|
||||
var searchButton = wait.Until(d => d.FindElement(By.CssSelector("button[name='seachBbsBt']")));
|
||||
SafeClick(searchButton);
|
||||
Console.WriteLine("검색 버튼 클릭 완료");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.ErrorMessage = $"검색버튼클릭실패({ex.Message})";
|
||||
result.BookCount = -1;
|
||||
result.IsSuccess = false;
|
||||
return result;
|
||||
}
|
||||
|
||||
// 페이지 변경을 감지하는 메서드
|
||||
await WaitForPageChange(new WebDriverWait(_driver, TimeSpan.FromSeconds(15)));
|
||||
|
||||
// 검색 결과 수 추출 (페이징 포함)
|
||||
var (resultCount, ermsg) = await ExtractBookCountWithPaging(_driver, searchTerm);
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = ermsg;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = ermsg;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = ex.Message;
|
||||
result.BookCount = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async Task<(int count, string message)> ExtractBookCountWithPaging(IWebDriver driver, string searchTerm)
|
||||
{
|
||||
string errmessage = string.Empty;
|
||||
int totalCount = 0;
|
||||
|
||||
try
|
||||
{
|
||||
// 첫 번째 페이지에서 테이블 row 수 확인
|
||||
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
|
||||
|
||||
// 검색 결과 테이블이 있는지 확인
|
||||
try
|
||||
{
|
||||
var bookTable = wait.Until(d => d.FindElement(By.CssSelector("table.board_list tbody#bookList")));
|
||||
var firstPageRows = bookTable.FindElements(By.TagName("tr"));
|
||||
|
||||
if (firstPageRows.Count == 0)
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
return (0, errmessage);
|
||||
}
|
||||
|
||||
totalCount += firstPageRows.Count;
|
||||
Console.WriteLine($"첫 번째 페이지 결과: {firstPageRows.Count}건");
|
||||
}
|
||||
catch
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
return (0, errmessage);
|
||||
}
|
||||
|
||||
// 페이징이 있는지 확인하고 각 페이지 방문
|
||||
try
|
||||
{
|
||||
var paginationDiv = driver.FindElement(By.CssSelector("div.pagination"));
|
||||
var pageLinks = paginationDiv.FindElements(By.TagName("a")).Where(a =>
|
||||
!a.GetAttribute("class").Contains("select") && // 현재 페이지가 아닌 것만
|
||||
!string.IsNullOrEmpty(a.Text.Trim()) &&
|
||||
int.TryParse(a.Text.Trim(), out int pageNum) // 숫자인 것만
|
||||
).ToList();
|
||||
|
||||
Console.WriteLine($"추가 페이지 {pageLinks.Count}개 발견");
|
||||
|
||||
foreach (var pageLink in pageLinks)
|
||||
{
|
||||
try
|
||||
{
|
||||
var pageNumber = pageLink.Text.Trim();
|
||||
Console.WriteLine($"{pageNumber} 페이지로 이동 중...");
|
||||
|
||||
SafeClick(pageLink);
|
||||
await Task.Delay(2000); // 페이지 로딩 대기
|
||||
|
||||
// 새 페이지에서 결과 수 확인
|
||||
var newBookTable = wait.Until(d => d.FindElement(By.CssSelector("table.board_list tbody#bookList")));
|
||||
var pageRows = newBookTable.FindElements(By.TagName("tr"));
|
||||
|
||||
totalCount += pageRows.Count;
|
||||
Console.WriteLine($"{pageNumber} 페이지 결과: {pageRows.Count}건");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"페이지 이동 중 오류: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("페이징이 없거나 페이징 처리 중 오류 발생");
|
||||
}
|
||||
|
||||
if (totalCount == 0)
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
return (0, errmessage);
|
||||
}
|
||||
|
||||
errmessage = $"검색성공({totalCount}권)";
|
||||
Console.WriteLine($"전체 검색 결과: {totalCount}건");
|
||||
return (totalCount, errmessage);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errmessage = ex.Message;
|
||||
return (-1, errmessage);
|
||||
}
|
||||
}
|
||||
|
||||
// 페이지 변경을 감지하는 메서드
|
||||
public async Task WaitForPageChange(WebDriverWait wait)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(500);
|
||||
|
||||
// 페이지 로딩 상태 확인
|
||||
wait.Until(d =>
|
||||
{
|
||||
var readyState = ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState");
|
||||
return readyState.Equals("complete");
|
||||
});
|
||||
|
||||
// 검색 결과 페이지가 로드될 때까지 대기
|
||||
wait.Until(d =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var pageSource = d.PageSource;
|
||||
// 검색 결과나 관련 요소가 나타나면 로드 완료
|
||||
return pageSource.Contains("board_list") ||
|
||||
pageSource.Contains("bookList") ||
|
||||
pageSource.Contains("검색결과가 없습니다");
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
await Task.Delay(500);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 모든 감지 방법이 실패하면 최소한의 대기 시간 적용
|
||||
await Task.Delay(3000);
|
||||
Console.WriteLine($"페이지 변경 감지 실패: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
15
unimarc/unimarc/SearchModel/GwangjuSeoguSmallLibSearcher.cs
Normal file
15
unimarc/unimarc/SearchModel/GwangjuSeoguSmallLibSearcher.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using BokBonCheck;
|
||||
|
||||
namespace BokBonCheck
|
||||
{
|
||||
public class GwangjuSeoguSmallLibSearcher : GwangjuSeoguLibSearcher
|
||||
{
|
||||
public override string SiteUrl => "https://library.seogu.gwangju.kr/library/index.9is?contentUid=9be5df8990e39d0e019491d3c5455c5c";
|
||||
|
||||
public GwangjuSeoguSmallLibSearcher(int no, string areaCode, string areaName)
|
||||
: base(no, areaCode, areaName)
|
||||
{
|
||||
this.SiteName = $"광주서구구립-작은도서관({areaName})";
|
||||
}
|
||||
}
|
||||
}
|
||||
15
unimarc/unimarc/SearchModel/GwangjuSeoguSmartLibSearcher.cs
Normal file
15
unimarc/unimarc/SearchModel/GwangjuSeoguSmartLibSearcher.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using BokBonCheck;
|
||||
|
||||
namespace BokBonCheck
|
||||
{
|
||||
public class GwangjuSeoguSmartLibSearcher : GwangjuSeoguLibSearcher
|
||||
{
|
||||
public override string SiteUrl => "https://library.seogu.gwangju.kr/library/index.9is?contentUid=9be5df897834aa08017868afdc3c1820";
|
||||
|
||||
public GwangjuSeoguSmartLibSearcher(int no, string areaCode, string areaName)
|
||||
: base(no, areaCode, areaName)
|
||||
{
|
||||
this.SiteName = $"광주서구구립-스마트도서관({areaName})";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,13 @@ namespace BokBonCheck
|
||||
|
||||
public int No { get; set; }
|
||||
|
||||
private static readonly HttpClient _httpClient = new HttpClient();
|
||||
private static readonly HttpClient _httpClient = new HttpClient()
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" }
|
||||
}
|
||||
};
|
||||
|
||||
public GwangsanLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
@@ -62,26 +68,36 @@ namespace BokBonCheck
|
||||
|
||||
Console.WriteLine($"광주광산구 검색 URL: {searchUrl}");
|
||||
|
||||
// HTTP GET 요청 실행
|
||||
var response = await _httpClient.GetAsync(searchUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
// HTTP GET 요청 실행 (추가 헤더 포함)
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Get, searchUrl))
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
// 브라우저와 유사한 헤더 추가
|
||||
request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
|
||||
request.Headers.Add("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3");
|
||||
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
|
||||
request.Headers.Add("Connection", "keep-alive");
|
||||
request.Headers.Add("Upgrade-Insecure-Requests", "1");
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
306
unimarc/unimarc/SearchModel/GyeongnamLibSearcher.cs
Normal file
306
unimarc/unimarc/SearchModel/GyeongnamLibSearcher.cs
Normal file
@@ -0,0 +1,306 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Web;
|
||||
using UniMarc.SearchModel;
|
||||
using System.Text;
|
||||
using OpenQA.Selenium.Support.UI;
|
||||
using OpenQA.Selenium;
|
||||
using OpenQA.Selenium.Chromium;
|
||||
using System.Threading;
|
||||
|
||||
namespace BokBonCheck
|
||||
{
|
||||
public class GyeongnamLibSearcher : ILibrarySearcher
|
||||
{
|
||||
protected string AreaCode { get; set; } = string.Empty;
|
||||
public string SiteName { get; protected set; }
|
||||
public string SiteUrl => "https://lib.gyeongnam.go.kr/index.lib?menuCd=DOM_000000201012000000";
|
||||
public bool HttpApiMode { get; set; } = false;
|
||||
|
||||
public int No { get; set; }
|
||||
|
||||
private ChromiumDriver _driver;
|
||||
|
||||
public GyeongnamLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
this.No = no;
|
||||
this.AreaCode = areaCode;
|
||||
this.SiteName = $"경남대표도서관({areaName})";
|
||||
}
|
||||
|
||||
public async Task StartDriver(bool showdriver = false)
|
||||
{
|
||||
if (_driver == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SeleniumHelper.IsReady == false) await SeleniumHelper.Download();
|
||||
_driver = await SeleniumHelper.CreateDriver(ShowBrowser: showdriver);
|
||||
Console.WriteLine("GyeongnamLibSearcher Driver 초기화 완료");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"GyeongnamLibSearcher Driver 초기화 실패: {ex.Message}");
|
||||
throw new InvalidOperationException($"GyeongnamLibSearcher Driver 초기화에 실패했습니다: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void StopDriver()
|
||||
{
|
||||
if (_driver != null)
|
||||
{
|
||||
_driver.Quit();
|
||||
_driver.Dispose();
|
||||
_driver = null;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<BookSearchResult> SearchAsync(string searchTerm)
|
||||
{
|
||||
var result = new BookSearchResult
|
||||
{
|
||||
SiteName = SiteName,
|
||||
SearchTerm = searchTerm,
|
||||
SearchTime = DateTime.Now
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
// 드라이버가 없으면 자동으로 시작
|
||||
if (_driver == null)
|
||||
{
|
||||
await StartDriver();
|
||||
}
|
||||
|
||||
// 검색어 URL 인코딩
|
||||
var encodedSearchTerm = HttpUtility.UrlEncode(searchTerm, Encoding.UTF8);
|
||||
|
||||
// 직접 검색 URL 구성해서 이동
|
||||
var directSearchUrl = $"https://lib.gyeongnam.go.kr/index.lib?menuCd=DOM_000000201001014000&search_select=search_title&search_text={encodedSearchTerm}";
|
||||
Console.WriteLine($"직접 검색 URL로 이동: {directSearchUrl}");
|
||||
|
||||
_driver.Navigate().GoToUrl(directSearchUrl);
|
||||
|
||||
// 페이지 로딩 대기
|
||||
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(15));
|
||||
|
||||
// 완전한 페이지 로딩 대기
|
||||
await WaitForCompletePageLoad(wait);
|
||||
|
||||
// 페이지 로드 후 브라우저 배율을 80%로 설정
|
||||
try
|
||||
{
|
||||
if (_driver.Manage().Window.Size.Width > 0)
|
||||
{
|
||||
((IJavaScriptExecutor)_driver).ExecuteScript("document.body.style.zoom = '0.8';");
|
||||
Console.WriteLine("페이지 로드 후 브라우저 배율을 80%로 설정했습니다.");
|
||||
}
|
||||
}
|
||||
catch (Exception zoomEx)
|
||||
{
|
||||
Console.WriteLine($"페이지 배율 설정 실패: {zoomEx.Message}");
|
||||
}
|
||||
|
||||
// JavaScript 렌더링 대기
|
||||
await Task.Delay(3000);
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(_driver, searchTerm, out string ermsg);
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = ermsg;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = ermsg;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = ex.Message;
|
||||
result.BookCount = 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private int ExtractBookCount(IWebDriver driver, string searchTerm, out string errmessage)
|
||||
{
|
||||
errmessage = string.Empty;
|
||||
|
||||
try
|
||||
{
|
||||
// JavaScript 실행 후 실제 렌더링된 DOM에서 결과 추출
|
||||
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
|
||||
|
||||
// 1. 검색결과가 없는 경우 확인
|
||||
try
|
||||
{
|
||||
var noResultElements = driver.FindElements(By.XPath("//*[contains(text(), '검색결과가 없습니다') or contains(text(), '검색된 자료가 없습니다')]"));
|
||||
if (noResultElements.Count > 0)
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 검색결과가 있는 경우로 진행
|
||||
}
|
||||
|
||||
// 2. total_area에서 결과 수량 추출 (JavaScript 렌더링 후)
|
||||
try
|
||||
{
|
||||
var totalAreaElement = wait.Until(d => d.FindElement(By.CssSelector("div.total_area p.total span")));
|
||||
if (totalAreaElement != null)
|
||||
{
|
||||
var countText = totalAreaElement.Text.Trim();
|
||||
Console.WriteLine($"total_area 텍스트: '{countText}'");
|
||||
|
||||
// "총 3건" 형태에서 숫자 추출
|
||||
var match = Regex.Match(countText, @"총\s*(\d+)\s*건", RegexOptions.IgnoreCase);
|
||||
if (match.Success && int.TryParse(match.Groups[1].Value, out int count))
|
||||
{
|
||||
if (count == 0)
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
Console.WriteLine("검색 결과: 0건");
|
||||
return 0;
|
||||
}
|
||||
errmessage = $"검색성공({count}권)";
|
||||
Console.WriteLine($"검색 결과: {count}건");
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex1)
|
||||
{
|
||||
Console.WriteLine($"total_area 요소 추출 실패: {ex1.Message}");
|
||||
}
|
||||
|
||||
// 3. 페이지 소스에서 렌더링된 결과 추출
|
||||
var pageSource = driver.PageSource;
|
||||
Console.WriteLine("페이지 소스에서 결과 추출 시도");
|
||||
|
||||
// 검색 결과가 없다는 메시지 확인
|
||||
if (pageSource.Contains("검색결과가 없습니다") ||
|
||||
pageSource.Contains("검색된 자료가 없습니다") ||
|
||||
pageSource.Contains("자료가 없습니다") ||
|
||||
pageSource.Contains("총 0건"))
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// HTML에서 다양한 패턴 찾기 (렌더링된 결과)
|
||||
var htmlPatterns = new[]
|
||||
{
|
||||
@"<span>총\s*(\d+)\s*건</span>",
|
||||
@"총\s*(\d+)\s*건이\s*검색되었습니다",
|
||||
@"총\s*(\d+)\s*건",
|
||||
@"검색결과\s*:\s*(\d+)\s*건"
|
||||
};
|
||||
|
||||
foreach (var pattern in htmlPatterns)
|
||||
{
|
||||
var match = Regex.Match(pageSource, pattern, RegexOptions.IgnoreCase);
|
||||
if (match.Success)
|
||||
{
|
||||
if (int.TryParse(match.Groups[1].Value, out int count))
|
||||
{
|
||||
if (count == 0)
|
||||
{
|
||||
errmessage = "검색결과없음";
|
||||
return 0;
|
||||
}
|
||||
errmessage = $"검색성공({count}권)";
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
errmessage = "결과수량을찾을수없음";
|
||||
return -1;
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
errmessage = ex.Message;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// 완전한 페이지 로딩 대기 메서드
|
||||
private async Task WaitForCompletePageLoad(WebDriverWait wait)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine("완전한 페이지 로딩 대기 시작...");
|
||||
|
||||
// 1. document.readyState가 'complete'가 될 때까지 대기
|
||||
wait.Until(d =>
|
||||
{
|
||||
var readyState = ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState");
|
||||
return readyState.Equals("complete");
|
||||
});
|
||||
|
||||
Console.WriteLine("document.readyState = complete");
|
||||
|
||||
// 2. jQuery가 로드되고 ready 상태까지 대기 (만약 사용한다면)
|
||||
try
|
||||
{
|
||||
wait.Until(d =>
|
||||
{
|
||||
var jqueryReady = ((IJavaScriptExecutor)d).ExecuteScript("return typeof jQuery !== 'undefined' && jQuery.active == 0");
|
||||
return jqueryReady.Equals(true);
|
||||
});
|
||||
Console.WriteLine("jQuery ready 완료");
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("jQuery 없음 또는 대기 생략");
|
||||
}
|
||||
|
||||
// 3. 추가 대기 시간
|
||||
await Task.Delay(2000);
|
||||
|
||||
// 4. 검색 입력창이 실제로 존재하고 상호작용 가능할 때까지 대기
|
||||
wait.Until(d =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var searchInput = d.FindElement(By.Id("search_text"));
|
||||
return searchInput != null && searchInput.Displayed && searchInput.Enabled;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
Console.WriteLine("검색 입력창 준비 완료");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"페이지 로딩 대기 중 오류: {ex.Message}");
|
||||
// 오류가 발생해도 최소한의 대기 시간 적용
|
||||
await Task.Delay(3000);
|
||||
}
|
||||
}
|
||||
|
||||
public Task WaitForPageChange(WebDriverWait wait)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,13 @@ namespace BokBonCheck
|
||||
|
||||
public int No { get; set; }
|
||||
|
||||
private static readonly HttpClient _httpClient = new HttpClient();
|
||||
private static readonly HttpClient _httpClient = new HttpClient()
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" }
|
||||
}
|
||||
};
|
||||
|
||||
public IksanLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
@@ -62,28 +68,37 @@ namespace BokBonCheck
|
||||
Console.WriteLine($"익산시통합도서관 검색 요청: {searchTerm}, 도서관코드: {AreaCode}");
|
||||
Console.WriteLine($"검색 URL: {searchUrl}");
|
||||
|
||||
// HTTP GET 요청 실행
|
||||
var response = await _httpClient.GetAsync(searchUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
// HTTP GET 요청 실행 (추가 헤더 포함)
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Get, searchUrl))
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
// 브라우저와 유사한 헤더 추가
|
||||
request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
|
||||
request.Headers.Add("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3");
|
||||
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
|
||||
request.Headers.Add("Connection", "keep-alive");
|
||||
request.Headers.Add("Upgrade-Insecure-Requests", "1");
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -17,7 +17,13 @@ namespace BokBonCheck
|
||||
|
||||
public int No { get; set; }
|
||||
|
||||
private static readonly HttpClient _httpClient = new HttpClient();
|
||||
private static readonly HttpClient _httpClient = new HttpClient()
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" }
|
||||
}
|
||||
};
|
||||
|
||||
public MokpoLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
@@ -65,26 +71,41 @@ namespace BokBonCheck
|
||||
|
||||
Console.WriteLine($"목포시립도서관 검색 URL: {searchUrl}");
|
||||
|
||||
// HTTP GET 요청 실행
|
||||
var response = await _httpClient.GetAsync(searchUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
// HTTP GET 요청 실행 (추가 헤더 포함)
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Get, searchUrl))
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
// 브라우저와 유사한 헤더 추가
|
||||
request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
|
||||
request.Headers.Add("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3");
|
||||
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
|
||||
request.Headers.Add("Connection", "keep-alive");
|
||||
request.Headers.Add("Upgrade-Insecure-Requests", "1");
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var errorContent = await response.Content.ReadAsStringAsync();
|
||||
throw new HttpRequestException($"HTTP {(int)response.StatusCode} {response.StatusCode}: {errorContent}");
|
||||
}
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -272,6 +272,21 @@ namespace UniMarc.SearchModel
|
||||
{
|
||||
Console.WriteLine($"웹드라이버 감지 방지 스크립트 실행 중 오류 (무시됨): {ex.Message}");
|
||||
}
|
||||
|
||||
// 브라우저 배율을 80%로 설정 (브라우저가 표시되는 경우에만)
|
||||
try
|
||||
{
|
||||
if (driver.Manage().Window.Size.Width > 0) // 브라우저가 표시되는 경우
|
||||
{
|
||||
((IJavaScriptExecutor)driver).ExecuteScript("document.body.style.zoom = '0.8';");
|
||||
Console.WriteLine("기본 브라우저 배율을 80%로 설정했습니다.");
|
||||
}
|
||||
}
|
||||
catch (Exception zoomEx)
|
||||
{
|
||||
Console.WriteLine($"기본 브라우저 배율 설정 실패: {zoomEx.Message}");
|
||||
}
|
||||
|
||||
await Task.Delay(1);
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,13 @@ namespace BokBonCheck
|
||||
public int No { get; set; }
|
||||
|
||||
private ChromiumDriver _driver;
|
||||
private static readonly HttpClient _httpClient = new HttpClient();
|
||||
private static readonly HttpClient _httpClient = new HttpClient()
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" }
|
||||
}
|
||||
};
|
||||
|
||||
public WandoLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
@@ -74,28 +80,37 @@ namespace BokBonCheck
|
||||
|
||||
Console.WriteLine($"완도군립도서관 검색 URL: {searchUrl}");
|
||||
|
||||
// HTTP GET 요청 실행
|
||||
var response = await _httpClient.GetAsync(searchUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
// HTTP GET 요청 실행 (추가 헤더 포함)
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Get, searchUrl))
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
// 브라우저와 유사한 헤더 추가
|
||||
request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
|
||||
request.Headers.Add("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3");
|
||||
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
|
||||
request.Headers.Add("Connection", "keep-alive");
|
||||
request.Headers.Add("Upgrade-Insecure-Requests", "1");
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -19,8 +19,13 @@ namespace BokBonCheck
|
||||
public int No { get; set; }
|
||||
public bool HttpApiMode { get; set; } = true;
|
||||
|
||||
private static readonly HttpClient _httpClient = new HttpClient();
|
||||
|
||||
private static readonly HttpClient _httpClient = new HttpClient()
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" }
|
||||
}
|
||||
};
|
||||
public YeosuLibSearcher(int no, string areaCode, string areaName)
|
||||
{
|
||||
this.No = no;
|
||||
@@ -71,26 +76,36 @@ namespace BokBonCheck
|
||||
|
||||
Console.WriteLine($"여수시립도서관 검색 URL: {searchUrl}");
|
||||
|
||||
// HTTP GET 요청 실행
|
||||
var response = await _httpClient.GetAsync(searchUrl);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
// HTTP GET 요청 실행 (추가 헤더 포함)
|
||||
using (var request = new HttpRequestMessage(HttpMethod.Get, searchUrl))
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
// 브라우저와 유사한 헤더 추가
|
||||
request.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
|
||||
request.Headers.Add("Accept-Language", "ko-KR,ko;q=0.8,en-US;q=0.5,en;q=0.3");
|
||||
request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
|
||||
request.Headers.Add("Connection", "keep-alive");
|
||||
request.Headers.Add("Upgrade-Insecure-Requests", "1");
|
||||
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var htmlContent = await response.Content.ReadAsStringAsync();
|
||||
|
||||
// 검색 결과 수 추출
|
||||
var resultCount = ExtractBookCount(htmlContent, out string errorMessage);
|
||||
|
||||
if (resultCount == -1)
|
||||
{
|
||||
result.BookCount = 0;
|
||||
result.IsSuccess = false;
|
||||
result.ErrorMessage = errorMessage;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.BookCount = resultCount;
|
||||
result.IsSuccess = true;
|
||||
result.ErrorMessage = $"검색성공({resultCount}권)";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -236,7 +236,12 @@
|
||||
</Compile>
|
||||
<Compile Include="SearchModel\GoheungLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\GwangjuCityLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\GwangjuDongguLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\GwangjuSeoguLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\GwangjuSeoguSmallLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\GwangjuSeoguSmartLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\GwangsanLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\GyeongnamLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\IksanLibSearcher.cs" />
|
||||
<Compile Include="SearchModel\ILibrarySearcher.cs" />
|
||||
<Compile Include="SearchModel\JeonbukEduLibSearcher.cs" />
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<ErrorReportUrlHistory />
|
||||
<FallbackCulture>ko-KR</FallbackCulture>
|
||||
<VerifyUploadedFiles>false</VerifyUploadedFiles>
|
||||
<ProjectView>ProjectFiles</ProjectView>
|
||||
<ProjectView>ShowAllFiles</ProjectView>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<EnableSecurityDebugging>false</EnableSecurityDebugging>
|
||||
|
||||
@@ -329,6 +329,51 @@ namespace WindowsFormsApp1.Mac
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MS", "수암도서관"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MR", "원고잔도서관"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "NJ", "월피예술도서관"));
|
||||
|
||||
// 안산시 작은도서관
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MK", "사이동꿈키"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MU", "해양동"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MO", "안산평생학습관"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MG", "안산다문화"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MJ", "신길샛별"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MM", "석수골"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "MW", "선녀마을"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "NA", "당곡"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "NB", "와동별빛누리"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "NG", "달미"));
|
||||
_searchService.AddSearcher(new AnsanLibSearcher(idx++, "NI", "신길"));
|
||||
|
||||
// 광주서구구립도서관
|
||||
idx = 1500;
|
||||
_searchService.AddSearcher(new GwangjuSeoguLibSearcher(idx++, "129222", "상록도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguLibSearcher(idx++, "129004", "어린이생태학습도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguLibSearcher(idx++, "129230", "서빛마루도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguLibSearcher(idx++, "124009", "문화의숲도서관"));
|
||||
|
||||
// 광주서구스마트도서관
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmartLibSearcher(idx++, "129222", "상록도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmartLibSearcher(idx++, "129004", "어린이생태학습도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmartLibSearcher(idx++, "129230", "서빛마루도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmartLibSearcher(idx++, "124009", "서구공공도서관"));
|
||||
|
||||
// 광주서구작은도서관
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmallLibSearcher(idx++, "MD", "화정4동작은도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmallLibSearcher(idx++, "ME", "동천작은도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmallLibSearcher(idx++, "MF", "금호2동작은도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmallLibSearcher(idx++, "MI", "서창한옥작은도서관"));
|
||||
_searchService.AddSearcher(new GwangjuSeoguSmallLibSearcher(idx++, "MZ", "새몰마루작은도서관"));
|
||||
|
||||
// 광주동구도서관
|
||||
idx = 1560;
|
||||
_searchService.AddSearcher(new GwangjuDongguLibSearcher(idx++, "129231", "책정원도서관"));
|
||||
_searchService.AddSearcher(new GwangjuDongguLibSearcher(idx++, "129003", "계림꿈나무도서관"));
|
||||
_searchService.AddSearcher(new GwangjuDongguLibSearcher(idx++, "729079", "다복마을도서관"));
|
||||
_searchService.AddSearcher(new GwangjuDongguLibSearcher(idx++, "729072", "학운동작은도서관"));
|
||||
_searchService.AddSearcher(new GwangjuDongguLibSearcher(idx++, "729073", "지원2동작은도서관"));
|
||||
|
||||
// 경남대표도서관
|
||||
idx = 1580;
|
||||
_searchService.AddSearcher(new GyeongnamLibSearcher(idx++, "", "경남대표도서관"));
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user