AnalysisEngine.
class AnalysisEngine { public enum Step { WEB, IN, CAFE, BLOG } const string NAVER_OPENAPI_KEY = "InputYourNaverAPIKey"; const string NAVER_OPENAPI_URL = "http://openapi.naver.com/search?"; const int NAVER_SEARCH_COUNT = 100; string keyword; string url; string blog; string cafe; public AnalysisEngine(string keyword, string url, string blog, string cafe) { this.keyword = keyword; this.url = url; this.blog = blog; this.cafe = cafe; } public void Analysis() { AnalysisResult result = new AnalysisResult(); result.webRank = AnalysisProccess(Step.WEB, url); result.blogRank = AnalysisProccess(Step.BLOG, blog); result.cafeRank = AnalysisProccess(Step.CAFE, cafe); Console.WriteLine("0 = 100위안에 없음."); Console.WriteLine("네이버 웹문서 분석 결과 : " + result.webRank); Console.WriteLine("네이버 블로그 분석 결과 : " + result.blogRank); Console.WriteLine("네이버 카페 분석 결과 : " + result.cafeRank); Console.ReadLine(); } int AnalysisProccess(Step step, string target) { string html = AnalysisTools.GetHTML(CreateURL(step)); XmlDocument doc = new XmlDocument(); doc.LoadXml(html); int rank = 0; foreach (XmlNode node in doc.SelectNodes("rss/channel/item/link")) { string realURL = GetRealURL(node.InnerText); if (realURL.Length > target.Length) realURL = realURL.Substring(0, target.Length); Console.WriteLine(realURL); ++rank; if (target == realURL) { return rank; } } return 0; } string CreateURL(Step step) { string target = ""; switch (step) { case Step.WEB: target = "webkr"; break; case Step.IN: target = "in"; break; case Step.CAFE: target = "cafearticle"; break; case Step.BLOG: target = "blog"; break; default: break; } return string.Format("{0}key={1}&query={2}&display={4}&start=1&target={3}", NAVER_OPENAPI_URL, NAVER_OPENAPI_KEY, keyword, target, NAVER_SEARCH_COUNT); } /* Naver API를 활용하여 url주소를 획득 할 시 실주소가 아닌 API 로 맵핑된 가상주소를 제공 해주기 때문에 직접 리다이렉트 주소를 획득하여 반영 하기위한 함수. */ string GetRealURL(string url) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.AllowAutoRedirect = false; webRequest.Timeout = 10000; webRequest.Method = "HEAD"; HttpWebResponse webResponse; string uriString = string.Empty; using (webResponse = (HttpWebResponse)webRequest.GetResponse()) { if ((int)webResponse.StatusCode >= 300 && (int)webResponse.StatusCode <= 399) { uriString = webResponse.Headers["Location"]; webResponse.Close(); } } return uriString; } }
AnalysisResult
class AnalysisResult { public int webRank; public int cafeRank; public int blogRank; }
AnalysisTool
class AnalysisTools { public static string GetHTML(string url) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream receiveStream = response.GetResponseStream(); StreamReader readStream = null; if (response.CharacterSet == null) { readStream = new StreamReader(receiveStream); } else { readStream = new StreamReader(receiveStream, Encoding.UTF8); } string data = readStream.ReadToEnd(); response.Close(); readStream.Close(); return data; } return string.Empty; } }
Main
class Program { static string keyword = ""; //키워드 static string homepage = ""; //홈페이지 주소 static string blog = ""; //블로그 주소 static string cafe = ""; //카페 주소 static void Main(string[] args) { AnalysisEngine engine = new AnalysisEngine( keyword , homepage, blog, cafe); engine.Analysis(); } }