알라딘 API 응답 처리 구조화 및 URL 브라우저 열기 개선
- AladinBookData 클래스 추가하여 API 응답 데이터 구조화 - ToString() 메서드 오버라이드로 파이프 구분자 형식 출력 지원 - Aladin_struct() 메서드 추가: List<AladinBookData> 반환 - 기존 Aladin() 메서드는 호환성 유지를 위해 보존 - URL 링크 클릭 시 탐색기 대신 기본 웹 브라우저로 열리도록 수정 (UseShellExecute = true) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,41 @@ using System.Xml.Linq;
|
||||
|
||||
namespace ISBN_Check_test
|
||||
{
|
||||
/// <summary>
|
||||
/// 알라딘 API 응답 데이터 구조체
|
||||
/// </summary>
|
||||
public class AladinBookData
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public string Link { get; set; }
|
||||
public string Author { get; set; }
|
||||
public string PubDate { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Isbn { get; set; }
|
||||
public string Isbn13 { get; set; }
|
||||
public string PriceSales { get; set; }
|
||||
public string PriceStandard { get; set; }
|
||||
public string StockStatus { get; set; }
|
||||
public string Mileage { get; set; }
|
||||
public string Cover { get; set; }
|
||||
public string CategoryId { get; set; }
|
||||
public string CategoryName { get; set; }
|
||||
public string Publisher { get; set; }
|
||||
public string CustomerReviewRank { get; set; }
|
||||
public string FullDescription { get; set; }
|
||||
public string FullDescription2 { get; set; }
|
||||
public string ItemId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 지정된 필드들을 "|" 구분자로 출력
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Title}|{Author}|{Publisher}|{Isbn13}|{PriceStandard}|{PubDate}|{CategoryName}|{StockStatus}";
|
||||
}
|
||||
}
|
||||
|
||||
class API
|
||||
{
|
||||
/// <summary>
|
||||
@@ -85,6 +120,92 @@ namespace ISBN_Check_test
|
||||
}
|
||||
return new string[] { };
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://blog.aladin.co.kr/openapi 참고 (구조체 반환 버전)
|
||||
/// </summary>
|
||||
/// <param name="Query"></param>
|
||||
/// <param name="QueryType"></param>
|
||||
/// <returns>AladinBookData 리스트</returns>
|
||||
public List<AladinBookData> Aladin_struct(string Query, string QueryType,out string xml)
|
||||
{
|
||||
// 쿼리 생성
|
||||
string key = "ttbgloriabook1512001";
|
||||
string site = "http://www.aladin.co.kr/ttb/api/ItemSearch.aspx";
|
||||
string query = string.Format("{0}?query={1}&TTBKey={2}&output=xml&querytype={3}&MaxResults={4}",
|
||||
site, Query, key, QueryType, 30.ToString());
|
||||
xml = string.Empty;
|
||||
try
|
||||
{
|
||||
// 쿼리를 입력인자로 WebRequest 개채 생성
|
||||
WebRequest request = WebRequest.Create(query);
|
||||
|
||||
// WebResponse개체를 통해 서비스 요청.
|
||||
WebResponse response = request.GetResponse();
|
||||
|
||||
// 결과문자열 확인
|
||||
Stream stream = response.GetResponseStream();
|
||||
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
|
||||
xml = reader.ReadToEnd();
|
||||
stream.Close();
|
||||
|
||||
var xdoc = XDocument.Parse(xml);
|
||||
|
||||
// XML에서 직접 totalResults와 item 추출 (XDocument 사용)
|
||||
XNamespace ns = "http://www.aladin.co.kr/ttb/apiguide.aspx";
|
||||
|
||||
// totalResults 추출
|
||||
string totalResults = xdoc.Descendants(ns + "totalResults").FirstOrDefault()?.Value ?? "0";
|
||||
|
||||
// item들 추출
|
||||
var itemElements = xdoc.Descendants(ns + "item");
|
||||
|
||||
List<AladinBookData> resultList = new List<AladinBookData>();
|
||||
|
||||
if (!itemElements.Any())
|
||||
{
|
||||
Console.WriteLine(Query);
|
||||
return resultList;
|
||||
}
|
||||
|
||||
// 모든 item을 순회하며 리스트에 추가
|
||||
foreach (var itemElement in itemElements)
|
||||
{
|
||||
AladinBookData bookData = new AladinBookData
|
||||
{
|
||||
ItemId = itemElement.Attribute("itemId")?.Value ?? "",
|
||||
Title = itemElement.Element(ns + "title")?.Value ?? "",
|
||||
Link = itemElement.Element(ns + "link")?.Value ?? "",
|
||||
Author = itemElement.Element(ns + "author")?.Value ?? "",
|
||||
PubDate = itemElement.Element(ns + "pubDate")?.Value ?? "",
|
||||
Description = itemElement.Element(ns + "description")?.Value ?? "",
|
||||
Isbn = itemElement.Element(ns + "isbn")?.Value ?? "",
|
||||
Isbn13 = itemElement.Element(ns + "isbn13")?.Value ?? "",
|
||||
PriceSales = itemElement.Element(ns + "priceSales")?.Value ?? "",
|
||||
PriceStandard = itemElement.Element(ns + "priceStandard")?.Value ?? "",
|
||||
StockStatus = itemElement.Element(ns + "stockStatus")?.Value ?? "",
|
||||
Mileage = itemElement.Element(ns + "mileage")?.Value ?? "",
|
||||
Cover = itemElement.Element(ns + "cover")?.Value ?? "",
|
||||
CategoryId = itemElement.Element(ns + "categoryId")?.Value ?? "",
|
||||
CategoryName = itemElement.Element(ns + "categoryName")?.Value ?? "",
|
||||
Publisher = itemElement.Element(ns + "publisher")?.Value ?? "",
|
||||
CustomerReviewRank = itemElement.Element(ns + "customerReviewRank")?.Value ?? "",
|
||||
FullDescription = itemElement.Element(ns + "fulldescription")?.Value ?? "",
|
||||
FullDescription2 = itemElement.Element(ns + "fulldescription2")?.Value ?? ""
|
||||
};
|
||||
|
||||
resultList.Add(bookData);
|
||||
}
|
||||
|
||||
return resultList;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 에러 발생 시 빈 리스트 반환
|
||||
return new List<AladinBookData>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// https://blog.aladin.co.kr/openapi 참고
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user