public void LoadFile() { try { JObject json = ConfigManagement.GetObject("mysql"); Host = (string)json["host"]; Database = (string)json["database"]; ID = (string)json["id"]; Password = (string)json["password"]; Option = (string)json["option"]; Program.LogSystem.AddLog(3, "MysqlOption", "설정 파일 로드 성공"); } catch (FileNotFoundException e) { } catch (Exception e) { throw e; } }
public static Object Geocoding(string address) { HttpWebRequest hreq = (HttpWebRequest)WebRequest.Create("https://maps.googleapis.com/maps/api/geocode/json?sensor=false&language=ko&address=" + HttpUtility.HtmlEncode(address) + "&key=" + ConfigManagement.GetObject("google_api")["key"]); hreq.Method = "GET"; hreq.ContentType = "plain/text;charset=utf-8"; HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse(); if (hres.StatusCode == HttpStatusCode.OK) { Stream dataStream = hres.GetResponseStream(); StreamReader sr = new StreamReader(dataStream, Encoding.UTF8); string result = sr.ReadToEnd(); dataStream.Close(); sr.Close(); JObject result_json = (JObject)JObject.Parse(result)["result"]["geometry"]["location"]; return(result_json); } return(null); }
public static void main() { Program.LogSystem.AddLog(2, "AutoCrawling", "네이버를 통해 음식점 데이터를 갱신합니다"); foreach (string keyword in keywords) { Program.LogSystem.AddLog(1, "AutoCrawling", keyword + " 키워드로 검색 시작"); JObject search_data = NaverAPIModule.SearchPlace((string)ConfigManagement.GetObject("naver_api")["client_id"], (string)ConfigManagement.GetObject("naver_api")["client_secret"], keyword, 300); foreach (JObject restaurant in search_data["items"]) { MysqlNode update = new MysqlNode(Program.mysqlOption, "INSERT INTO restaurant (no, title, description, roadAddress, mapx, mapy, category, image, default_likes) VALUES (?no, ?title, ?description, ?roadAddress, ?mapx, ?mapy, ?category, ?image, ?default_likes)"); update["title"] = Regex.Replace((string)restaurant["title"], "(<[/a-zA-Z]+>)", ""); update["roadAddress"] = (string)restaurant["roadAddress"]; JObject map = NaverAPIModule.Geocoding((string)ConfigManagement.GetObject("naver_cloud_api")["client_id"], (string)ConfigManagement.GetObject("naver_cloud_api")["client_secret"], (string)restaurant["roadAddress"]); if (map == null) { update["mapx"] = null; update["mapy"] = null; } else { update["mapx"] = map["x"].ToString(); update["mapy"] = map["y"].ToString(); } update["category"] = (string)restaurant["category"]; update["no"] = NaverAPIModule.GetPlaceID((string)update["title"], (string)update["roadAddress"], (string)keyword); update["image"] = NaverAPIModule.GetPlaceImage((int)update["no"]); update["description"] = NaverAPIModule.GetPlaceDescription((int)update["no"]); update["default_likes"] = NaverAPIModule.GetPlaceReview((int)update["no"]); update.ExecuteNonQuery(); Program.LogSystem.AddLog(1, "AutoCrawling", update["title"] + " 를 리스트에 등록"); // 메뉴 갱신 JArray menus = NaverAPIModule.GetPlaceMenu((int)update["no"]); foreach (JObject json in menus) { MysqlNode menu_update = new MysqlNode(Program.mysqlOption, "INSERT INTO menu (restaurant_no, priority, name, price, description, image) VALUES (?restaurant_no, ?priority, ?name, ?price, ?description, ?image)"); menu_update["restaurant_no"] = update["no"]; menu_update["priority"] = (string)json["priority"]; menu_update["name"] = (string)json["name"]; menu_update["price"] = (string)json["price"]; if (String.IsNullOrEmpty((string)json["desc"])) { menu_update["description"] = null; } else { menu_update["description"] = (string)json["desc"]; } if (((JArray)json["images"]).Count > 0) { menu_update["image"] = (string)json["images"][0]; } else { menu_update["image"] = null; } menu_update.ExecuteNonQuery(); Program.LogSystem.AddLog(1, "AutoCrawling", json["name"] + " 를 메뉴 리스트에 등록"); } } } Program.LogSystem.AddLog(1, "AutoCrawling", "크롤링 완료"); }