추가된 도서관 시스템: • 광주남구도서관 (5개관) • 광주시교육청통합도서관 (6개관) • 전남교육청통합도서관 (25개관) • 전남교육청행정자료실 (1개관) • 여수시립도서관 (34개관) • 고흥군립도서관 (7개관) • 광주북구통합도서관 (3개관) • 광주북구작은도서관 (23개관) • 광주북구공공도서관 (5개관) • 전북교육청도서관 (18개관) • 광주광산구통합도서관 (17개관) • 목포시립도서관 (23개관) • 순천시립도서관 (10개관) • 광주시립도서관 (4개관) • 완도군립도서관 (6개관) • 익산시통합도서관 (33개관) • 안산시중앙도서관 (27개관) • 광주서구구립도서관 (4개관) • 광주서구스마트도서관 (4개관) • 광주서구작은도서관 (5개관) • 광주동구도서관 (5개관) • 경남대표도서관 (1개관) • 무안군립도서관 (1개관) • 조선대학교중앙도서관 (1개관) • 조선이공대학교도서관 (1개관) • KCM통합도서관 (33개관) 총 400개 이상 도서관 복본조사 시스템 완성 HTTP API 방식 및 Selenium 크롤링 방식 혼용 브라우저 헤더 최적화 및 80% 화면배율 적용 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
229 lines
9.1 KiB
C#
229 lines
9.1 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web;
|
|
using UniMarc.SearchModel;
|
|
using System.Text;
|
|
|
|
namespace BokBonCheck
|
|
{
|
|
public class SuncheonLibSearcher : ILibrarySearcher
|
|
{
|
|
public string AreaCode { get; set; } = string.Empty;
|
|
public string SiteName { get; protected set; }
|
|
public string SiteUrl => "https://library.suncheon.go.kr/lib/book/search/searchIndex.do";
|
|
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" }
|
|
}
|
|
};
|
|
|
|
public SuncheonLibSearcher(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}?menuCd=L001001001&alpha=&vcindex=¤tPageNo=1&nPageSize=10&searchType=title&search={encodedSearchTerm}&mediaCode=";
|
|
|
|
// 도서관 코드가 있으면 추가
|
|
if (!string.IsNullOrEmpty(AreaCode))
|
|
{
|
|
if (AreaCode == "mini")
|
|
{
|
|
// 작은도서관의 경우 모든 코드를 포함
|
|
searchUrl += "&manageCd=AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU,AV,AW,AX,AY,AZ,BA,BB,BC,BD,BF,BG,BH,BI,BJ,BM,BQ,BS,BW,CA,CB,CC,CD,CE,CF,CG,CH,CI,CJ,CK,CL,CM,CN,CO,CP,CQ,DA,DB,DC,DD,DE,DF,DG,DH,DI,DJ,DK,DL,DS,GD";
|
|
}
|
|
else
|
|
{
|
|
searchUrl += $"&manageCd={AreaCode}";
|
|
}
|
|
}
|
|
|
|
Console.WriteLine($"순천시립도서관 검색 URL: {searchUrl}");
|
|
|
|
// HTTP GET 요청 실행 (추가 헤더 포함)
|
|
using (var request = new HttpRequestMessage(HttpMethod.Get, searchUrl))
|
|
{
|
|
// 브라우저와 유사한 헤더 추가
|
|
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}권)";
|
|
}
|
|
}
|
|
|
|
}
|
|
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
|
|
{
|
|
// HTML에서 "총 <strong class="cred">N</strong>건" 패턴 찾기
|
|
var patterns = new[]
|
|
{
|
|
@"총\s*<strong[^>]*class=""cred""[^>]*>(\d+)</strong>\s*건",
|
|
@"총\s*<strong[^>]*>(\d+)</strong>\s*건",
|
|
@"총\s*(\d+)\s*건",
|
|
@"<strong[^>]*class=""cred""[^>]*>(\d+)</strong>"
|
|
};
|
|
|
|
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}권)";
|
|
return count;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 검색 결과가 없다는 메시지 확인
|
|
if (htmlContent.Contains("검색결과가 없습니다") ||
|
|
htmlContent.Contains("검색된 자료가 없습니다") ||
|
|
htmlContent.Contains("자료가 없습니다") ||
|
|
htmlContent.Contains("총 0건"))
|
|
{
|
|
errorMessage = "검색결과없음";
|
|
return 0;
|
|
}
|
|
|
|
// resultCon 영역에서 더 자세히 검색
|
|
var resultConPattern = @"<div[^>]*class=""resultCon[^""]*""[^>]*>.*?총\s*<strong[^>]*>(\d+)</strong>\s*건.*?</div>";
|
|
var resultConMatch = Regex.Match(htmlContent, resultConPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
|
|
if (resultConMatch.Success)
|
|
{
|
|
if (int.TryParse(resultConMatch.Groups[1].Value, out int count))
|
|
{
|
|
if (count == 0)
|
|
{
|
|
errorMessage = "검색결과없음";
|
|
return 0;
|
|
}
|
|
errorMessage = $"검색성공({count}권)";
|
|
return count;
|
|
}
|
|
}
|
|
|
|
// 더 넓은 범위로 숫자 찾기 시도
|
|
var generalPatterns = new[]
|
|
{
|
|
@"검색한\s*결과\s*총\s*<strong[^>]*>(\d+)</strong>",
|
|
@"검색결과를\s*찾았습니다[^>]*>(\d+)</strong>",
|
|
@"<strong[^>]*class=""cred""[^>]*>(\d+)</strong>"
|
|
};
|
|
|
|
foreach (var pattern in generalPatterns)
|
|
{
|
|
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}권)";
|
|
return count;
|
|
}
|
|
}
|
|
}
|
|
|
|
errorMessage = "검색결과 패턴을 찾을 수 없음";
|
|
return -1;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
errorMessage = $"결과 분석 오류: {ex.Message}";
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
public Task WaitForPageChange(OpenQA.Selenium.Support.UI.WebDriverWait wait)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
} |