//Get data private void GetData() { try { var doc = new HtmlDocument(); this.URL = ConfigurationManager.AppSettings["FindClubState"] + this.State + ConfigurationManager.AppSettings["FindClubZip"] + this.Zip; doc.LoadHtml(ReadData(URL)); var allClubs = doc.DocumentNode.SelectNodes("//*[contains(@class,'TextDataColumn')]"); int ClubNumber = 1; foreach (var td in allClubs) { string ClubURI = string.Empty; string ClubName = string.Empty; string ClubAddress = string.Empty; string ClubDescription = string.Empty; string ClubID = string.Empty; string Zip = string.Empty; string State = string.Empty; if (td.InnerText.Trim() == "Club") { //Ignore } else { ClubURI = td.SelectSingleNode(".//a").GetAttributeValue("href", "Club Lunk not found"); ClubName = td.SelectSingleNode(string.Format(".//span[@id='ctl00_MainContent_repClubInfo_ctl0{0}_lblClubDisplayName']", ClubNumber)).InnerText.Trim(); ClubAddress = td.SelectSingleNode(string.Format("//span[@id='ctl00_MainContent_repClubInfo_ctl0{0}_lblAddress']", ClubNumber)).InnerHtml.Replace("<br>", ", ").Replace(" ", " ").Replace(" ", " "); ClubDescription = td.InnerText.Trim().Replace(" ", " "); ClubDescription = Regex.Replace(ClubDescription, @"\s+", " "); ClubID = ClubURI.Split('=')[1]; Zip = ClubAddress.Substring(ClubAddress.Length - 5, 5); State = ClubAddress.Substring(ClubAddress.Length - 8, 2); ClubNumber += 1; //Update values ClubsURIs.Add(ClubURI); ClubsNames.Add(ClubName); ClubsAddresses.Add(ClubAddress); ClubsDescriptions.Add(ClubDescription); ClubsIDs.Add(ClubID); if (!ZipCodes.Contains(Zip)) { ZipCodes.Add(Zip); } if (!States.Contains(State)) { States.Add(State); } //Add to the list Classes Classes = new Classes(ClubID); ClubsList.Add(new Club(State, Zip, ClubURI, ClubName, ClubAddress, ClubDescription, ClubID, Classes, Classes.GetClassesList())); } } } catch (Exception e) { throw new Exception(e.Message); } }
//private Location SaveCooridnate(string postalCode ) //{ // var coordinate = GetCoordinate(postalCode); // if (coordinate != null && coordinate.Latitude != null && coordinate.Longitude != null) // return coordinate; // var locationLatLong = this.GetAll()?.FirstOrDefault(x => x.Name.EqualsIgnoreCase(postalCode) && x.Latitude != null && x.Longitude != null); // if (locationLatLong != null) // { // //todo add as coordinate // return locationLatLong; // } // //todo Get lat and long // //then update or insert // var location = this.GetAll()?.FirstOrDefault(x => x.Name.EqualsIgnoreCase(postalCode)); // if (location == null) // { //add as coordinate // } // if (ConfigurationManager.AppSettings["google.maps.requestperday"] != null) // _apiRequestsPerDay = ConfigurationManager.AppSettings["google.maps.requestperday"].ToString().ConvertTo<int>(); // if (ConfigurationManager.AppSettings["google.maps.requestpersecond"] != null) // _apiRequestsPerSecond = ConfigurationManager.AppSettings["google.maps.requestpersecond"].ToString().ConvertTo<int>(); // // if (geos.Count > _apiRequestsPerDay) // // geos = geos.Take(_apiRequestsPerDay).ToList(); // long totalElapsedTime = 0; // int millisecondCap = 1000; //or 1 second. // //If we go below this time on all the requests then we'll go over the throttle limit. // int minRequesTimeThreshold = millisecondCap / _apiRequestsPerSecond; // Stopwatch stopwatch = new Stopwatch(); // int index = 1; // var address = this.GetFullAddress(location); //location.Address1 + " " + location.City + " " + location.State + " " + location.Postal; // //location.Name = address; // //var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?key=YOURGOOGLEAPIKEY&address={0}&sensor=false", Uri.EscapeDataString(address)); // var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?key=YOURGOOGLEAPIKEY&address={0}&sensor=false", Uri.EscapeDataString(address)); // stopwatch.Restart(); // Begin timing. // var request = WebRequest.Create(requestUri); // var response = request.GetResponse(); // var xdoc = XDocument.Load(response.GetResponseStream()); // // var xdoc = XDocument.Parse(_googleGeoXml); //test parse // var status = xdoc.Element("GeocodeResponse").Element("status"); // switch (status.Value) // { // case GoogleGeo.ResponseStatus.OK: // // location.DescriptionEx = xdoc.ToString(); // var result = xdoc.Element("GeocodeResponse").Element("result"); // var locationElement = result.Element("geometry").Element("location"); // location.Latitude = locationElement.Element("lat").Value.ConvertTo<float>(); // location.Longitude = locationElement.Element("lng").Value.ConvertTo<float>(); // //DataQuery dqFC = new DataQuery(); // // dqFC.SQL = string.Format("UPDATE FireDeptIncidents SET Latitude={0}, Longitude={1} WHERE inci_id='{2}'", location.Latitude, location.Longitude, location.inci_id); // // int res = fim.Update(dqFC); // // SetProgressBar(index, "updated:" + address); // break; // case GoogleGeo.ResponseStatus.OverLimit: // //SetProgressBar(-1, "Status: OverLimit"); // //todo log this // return null; // case GoogleGeo.ResponseStatus.Denied: // //todo log this // // SetProgressBar(-1, "Status: Denied"); // //SetProgressBar(0, xdoc.ToString()); // return null; // } // // Stop timing. // stopwatch.Stop(); // long elapsedTime = stopwatch.ElapsedMilliseconds;//How long it took to get and process the response. // if (elapsedTime < minRequesTimeThreshold) // { // //SetProgressBar(-1, "suspending for:" + (minRequesTimeThreshold - (int)elapsedTime).ToString()); // Thread.Sleep(minRequesTimeThreshold - (int)elapsedTime);//sleep is in milliseconds // totalElapsedTime += elapsedTime; // // millisecond = .001 or 10−3 or 1 / 1000 // //so 1 request every 100 milliseconds // } //} //public void ImportZipCodes(string pathToFile) { // ZipCodes codes = LoadZipCodeCoordinates(pathToFile); // foreach (int zipCode in codes.Keys) // { // ZipCode loc = codes[zipCode]; // SaveCooridnate(loc.Code.ToString(), loc.State, loc.Latitude, loc.Longitude); // } // return; //} // Columns 1-2: United States Postal Service State Abbreviation // Columns 3-66: Name (e.g. 35004 5-Digit ZCTA - there are no post office names) // Columns 67-75: Total Population (2000) // Columns 76-84: Total Housing Units (2000) // Columns 85-98: Land Area (square meters) - Created for statistical purposes only. // Columns 99-112: Water Area (square meters) - Created for statistical purposes only. // Columns 113-124: Land Area (square miles) - Created for statistical purposes only. // Columns 125-136: Water Area (square miles) - Created for statistical purposes only. // Columns 137-146: Latitude (decimal degrees) First character is blank or "-" denoting North or South latitude respectively // Columns 147-157: Longitude (decimal degrees) First character is blank or "-" denoting East or West longitude respectively private ZipCodes LoadZipCodeCoordinates(string pathToFile) { ZipCodes codes = new ZipCodes(); string[] fileLines = File.ReadAllLines(pathToFile); string sep = "\t"; foreach (string line in fileLines) { if (string.IsNullOrWhiteSpace(line)) { continue; } string code = ""; //int double lat = 0; double lon = 0; string[] tokens = line.Split(sep.ToCharArray()); //if (!Int32.TryParse(line.Substring(2, 5), out code) || // !double.TryParse(line.Substring(136, 10), out lat) || // !double.TryParse(line.Substring(146, 10), out lon)) // continue;// skip lines that aren't valid if ( //!Int32.TryParse(tokens[0], out code) || !double.TryParse(tokens[5], out lat) || !double.TryParse(tokens[6], out lon)) { continue;// skip lines that aren't valid } if (codes.ContainsKey(code)) { continue; // there are a few duplicates due to state boundaries, ignore them } codes.Add(code, new ZipCode() { // State = line.Substring(0, 2), Code = code, Latitude = ZipCode.ToRadians(lat), Longitude = ZipCode.ToRadians(lon), }); } return(codes); }