네이버 API 및 11번가 Open API를 이용해 키워드 검색프로그램을 만들면서 개발의 방법 및 스킬을 늘리는게 목적입니다.
기본 베이스가 되는 프로그램 언어는 c#(WPF)로 진행 할 생각 입니다.
1) IOpenApi.cs 인터페이스 생성
Compont/Ecommerce/EcommerceType.cs
Compont/Ecommerce/IOpenApi.cs
Compont/Ecommerce/OpenApi11st.cs
Compont/Ecommerce/OpenApiNaver.cs
Compont/Ecommerce/SearchInterpark.cs
네이버,11번가,인터파크 에서 각각의 제품수를 가지고와서 보여줄 생각 입니다.
같은 페턴을 데이터이고 추가로 확장을 생각하여 interfaceI OpenApi와 enum EcommerceType을
생성 하였습니다.
2) 오픈 API 셋팅
키워드의 네이버 및 11번가의 등록 제품수를 가지고 올 수 있는 API를 만들려면 각각의 api 키들을 알아야합니다.
네이버 오픈 API 호출을 위해서는 Client ID,Secret 키 11 번가는 Open Api key
(https://eehnuyh.tistory.com/1)
APIInfoSetting.settings 에 Client ID,Secret,Open Api Key 정보를 입력합니다.
OpenApiNaverClientID,OpenApiNaverSecret,OpenApi11StKey 값은 UI에서 입력 할 생각 입니다.
3) 네이버 Open Api (제품수)
using EEH.RestAPI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace EEH.Component.Ecommerce
{
public class OpenApiNaver : IOpenApi
{
RestAPIClient restApiClient = null;
public EcommerceType Type => EcommerceType.ETNAVER;
public OpenApiNaver()
{
restApiClient = new RestAPIClient("https://openapi.naver.com");
restApiClient.OnHeaderSettingDelegate = (header) =>
{
if (header != null)
{
header.Add("X-Naver-Client-Id", APIInfoSettings.Default.OpenApiNaverClientID);
header.Add("X-Naver-Client-Secret", APIInfoSettings.Default.OpenApiNaverSecret);
}
};
}
public async Task<int> GetProductTotalCnt(string keyword)
{
int returnValue = 0;
string strParams = string.Format("/v1/search/shop?query={0}&display=1", keyword);
JObject jObj = await restApiClient.GetAsync<JObject>(strParams);
if (jObj.ContainsKey("total"))
{
string strCnt = jObj["total"].ToString();
int.TryParse(strCnt, out returnValue);
}
return returnValue;
}
}
}
4) 11번가 Open Api (제품수)
using EEH.RestAPI;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
namespace EEH.Component.Ecommerce
{
public class OpenApi11st : IOpenApi
{
RestAPIClient restApiClient = null;
public OpenApi11st()
{
restApiClient = new RestAPIClient("https://openapi.11st.co.kr");
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);//.net core Encoding 문제로 등록 (등록 안할시 Encoding.GetEncoding("euc-kr") 에러발생)
}
public EcommerceType Type => EcommerceType.ET11ST;
public async Task<int> GetProductTotalCnt(string keyword)
{
int returnValue = 0;
string encKeyword = HttpUtility.UrlEncode(keyword, Encoding.GetEncoding("euc-kr"));
string strParams = string.Format("/openapi/OpenApiService.tmall?key={0}&apiCode=ProductSearch&keyword={1}&pageSize=0", APIInfoSettings.Default.OpenApi11StKey, encKeyword);
string strXml = await restApiClient.GetAsync(strParams);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(strXml);
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("TotalCount");
if(nodeList != null && nodeList.Count > 0)
{
string strCnt = nodeList[0].InnerText;
int.TryParse(strCnt, out returnValue);
}
return returnValue;
}
}
}
CodePagesEncodingProvider 부분
NuGet 으로 설치 합니다.
System.Text.Encoding.CodePages
5) 인터파크 크롤링(제품수)
인터파크는 따로 제공되는 API가 없어 웹사이트를 파싱하여 정보를 가지고 오게 작업하였습니다.
웹사이트 구조변경이되면 사용할수 없고,많은 데이터를 불러오기 때문에 API 비해 속도가 느린 단점이 있습니다.
using EEH.RestAPI;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace EEH.Component.Ecommerce
{
public class SearchInterpark : IOpenApi
{
RestAPIClient restApiClient = null;
public SearchInterpark()
{
restApiClient = new RestAPIClient("https://isearch.interpark.com");
}
public EcommerceType Type => EcommerceType.ETINTERPARK;
public async Task<int> GetProductTotalCnt(string keyword)
{
int returnValue = 0;
string textHtml = await restApiClient.GetAsync(string.Format("/isearch?q={0}", keyword));
if (!string.IsNullOrEmpty(textHtml))
{
string findString = "통합검색";
int startIndex = textHtml.IndexOf(findString)+findString.Length;
string subString = textHtml.Substring(startIndex, 50);
string[] split = subString.Split('(');
if(split != null && split.Length > 1)
{
string tmpStr = split[1];
if (!string.IsNullOrEmpty(tmpStr))
{
string[] split2 = tmpStr.Split(')');
if (split2 !=null && split2.Length > 0)
{
string strValue = split2[0];
if (!string.IsNullOrEmpty(strValue))
{
strValue = strValue.Replace(",","");
int.TryParse(strValue, out returnValue);
}
}
}
}
}
return returnValue;
}
}
}
'대충 만들면서 배우자 > 키워드검색' 카테고리의 다른 글
[C#/WPF]네이버 연관 검색어 프로그램 7(WPF UI : 결과) (0) | 2023.03.15 |
---|---|
[C#/WPF]네이버 연관 검색어 프로그램 6(WPF 공통) (0) | 2023.03.14 |
[C#/API]네이버 연관 검색어 프로그램 4(API 연동(1): 네이버 연관 키워드) (0) | 2023.03.12 |
[C#/API]네이버 연관 검색어 프로그램 3(공통프로젝트(2):Extentions,QueueAsync Class) (0) | 2023.03.12 |
[C#/API]네이버 연관 검색어 프로그램 2(공통프로젝트(1):Rest API Class) (0) | 2023.03.12 |
댓글