public override List<IStation> GetStations(string latitude, string longitude) { List<IStation> stations = new List<IStation>(); decimal lat = Convert.ToDecimal(latitude); decimal lng = Convert.ToDecimal(longitude); HttpWebRequest WebReq1 = (HttpWebRequest)WebRequest.Create(string.Format(_baseURL, latitude, longitude)); WebReq1.CookieContainer = new CookieContainer(); WebReq1.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"; HttpWebResponse WebResp1 = (HttpWebResponse)WebReq1.GetResponse(); CookieCollection cookies = WebResp1.Cookies; GasBuddyRequestPayload payload = new GasBuddyRequestPayload(); payload.dMinY = lat - _latFactor; payload.dMaxY = lat + _latFactor; payload.dMinX = lng + _lonFactor; payload.dMaxX = lng - _lonFactor; string payloadStr = JsonConvert.SerializeObject(payload); string s = string.Empty; HttpWebRequest WebReq2 = (HttpWebRequest)WebRequest.Create(_jsonURL); WebReq2.Method = "POST"; WebReq2.CookieContainer = new CookieContainer(); WebReq2.CookieContainer.Add(cookies); WebReq2.Headers.Add("X-AjaxPro-Method", "gus"); WebReq2.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"; byte[] byteArray = Encoding.UTF8.GetBytes(payloadStr); WebReq2.ContentType = "test/plain"; WebReq2.ContentLength = byteArray.Length; Stream dataStream = WebReq2.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = WebReq2.GetResponse(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); GasBuddy gb = new GasBuddy(); DataTable dt = gb.GetStationTable(responseFromServer); foreach (DataRow dr in dt.Rows) { GasBuddyStation gbst = new GasBuddyStation(); gbst.Location.Latitude = dr["lat"].ToString(); gbst.Location.Longitude = dr["lng"].ToString(); gbst.Name = dr["station_nm"].ToString(); GasPrice gp = new GasPrice(); gp.GasType = "Regular"; gp.LastUpdate = dr["tme"].ToString(); gp.Price = dr["price"].ToString(); gbst.Prices.Add(gp); stations.Add(gbst); } return stations; }
public override List<IStation> GetStations(string latitude, string longitude) { List<IStation> stations = new List<IStation>(); string s = string.Empty; decimal swLat = Convert.ToDecimal(latitude) - latFactor; decimal neLat = Convert.ToDecimal(latitude) + latFactor; decimal swLon = Convert.ToDecimal(longitude) - lonFactor; decimal neLon = Convert.ToDecimal(longitude) + lonFactor; HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create( string.Concat(baseURL1, string.Format(dataTemplate1, swLat, swLon, neLat, neLon))); WebReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"; HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); Stream answer = WebResp.GetResponseStream(); using (StreamReader rdr = new StreamReader(answer)) { s = rdr.ReadToEnd(); } string[] parts = s.Split(new char[] { '|' }); List<MSNAutosStationJson> lMSNJson = JsonConvert.DeserializeObject<List<MSNAutosStationJson>>(parts[1]); WebReq = (HttpWebRequest)WebRequest.Create(string.Concat(baseURL2, string.Format(dataTemplate2, parts[0]))); WebResp = (HttpWebResponse)WebReq.GetResponse(); answer = WebResp.GetResponseStream(); Bitmap prices = (Bitmap)Image.FromStream(answer); using (OCREngine engine = new OCREngine(_tessPath)) { for (int a = 0; a < lMSNJson.Count; a++) // Number of stations { MSNAutosStation tmpStation = new MSNAutosStation(); tmpStation.Location.Latitude = lMSNJson[a].Lt.ToString(); tmpStation.Location.Longitude = lMSNJson[a].Lg.ToString(); tmpStation.Name = lMSNJson[a].B; tmpStation.Address = lMSNJson[a].S; tmpStation.City = lMSNJson[a].C; tmpStation.Phone = lMSNJson[a].P; for (int b = 0; b < 4; b++) // Number of fuel types { int imgWidth = 0; if (b == 3) { imgWidth = priceImageWidth - 1; } else { imgWidth = priceImageWidth; } Bitmap tmpBitMap = new Bitmap(imgWidth, priceImageHeight); int wStart = (priceImageSeperatorWidth * b) + (b * priceImageWidth) + priceImageSeperatorWidth; wStart += priceImageLeftMargin; for (int c = wStart; c < wStart + imgWidth; c++) // Price Width { int hStart = (a * priceImageHeight); for (int d = hStart; d < hStart + priceImageHeight; d++) // Price Height { tmpBitMap.SetPixel(c - wStart, d - hStart, prices.GetPixel(c, d)); } } string text = Regex.Replace(engine.GetOCRForBitmap(tmpBitMap), @"[^0-9.]", string.Empty); if (text.Length > 0) { GasPrice gp = new GasPrice(); gp.Price = text; try { gp.LastUpdate = lMSNJson[a].D[b] == "UT" ? DateTime.Now.ToShortDateString() : lMSNJson[a].D[b]; } catch { } switch (b) { case 0: gp.GasType = "Regular"; break; case 1: gp.GasType = "Mid-grade"; break; case 2: gp.GasType = "Premium"; break; case 3: gp.GasType = "Diesel"; break; } tmpStation.Prices.Add(gp); } } stations.Add(tmpStation); } } return stations; }
public override List<IStation> GetStations(string latitude, string longitude) { List<IStation> stations = new List<IStation>(); string rawHTML = getGasWatchHtml(latitude, longitude); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(rawHTML); HtmlNodeCollection stationNodes = doc.DocumentNode.SelectNodes("/table/tr/td/div/table/tr/td[2]"); foreach (HtmlNode node in stationNodes) { if (!node.InnerHtml.Contains("Older Than")) { HtmlDocument docNode = new HtmlDocument(); docNode.LoadHtml(node.InnerHtml); HtmlNodeCollection stationNodeHiddenFields = docNode.DocumentNode.SelectNodes("//input"); IStation station = new GasPriceWatchStation(); if (stationNodeHiddenFields != null) { List<HtmlNode> nodesWithIds = (from s in stationNodeHiddenFields where s.Attributes["id"] != null select s).ToList(); PropertyInfo[] propsSt = typeof(GasPriceWatchStation).GetProperties(); foreach (PropertyInfo p in propsSt) { object[] objArr = p.GetCustomAttributes(typeof(XmlMappingAttribute), false); if (objArr != null) { if (objArr.Length > 0) { HtmlNode tmpNode = nodesWithIds.First( snode => snode.Attributes["id"].Value.StartsWith(((XmlMappingAttribute)objArr[0]).XmlFieldName)); p.SetValue(station, tmpNode.Attributes["value"].Value, null); } } } Regex re = new Regex(@"^hst[0-9]{1,3}$"); HtmlNode tmpNodeStateProvince = nodesWithIds.Find(snode => re.IsMatch(snode.Attributes["id"].Value)); if (tmpNodeStateProvince != null) { ((GasPriceWatchStation)station).StateProvince = tmpNodeStateProvince.Attributes["value"].Value; } re = new Regex(@"^Lat[0-9]{1,3}$"); HtmlNode tmpLocationLat = nodesWithIds.Find(snode => re.IsMatch(snode.Attributes["id"].Value)); if (tmpLocationLat != null) { ((GasPriceWatchStation)station).Location.Latitude = tmpLocationLat.Attributes["value"].Value; } re = new Regex(@"^Long[0-9]{1,3}$"); HtmlNode tmpLocationLon = nodesWithIds.Find(snode => re.IsMatch(snode.Attributes["id"].Value)); if (tmpLocationLon != null) { station.Location.Longitude = tmpLocationLon.Attributes["value"].Value; } int minutes = 0; HtmlNode pricesAge = docNode.DocumentNode.SelectSingleNode("/font/font/font"); try { Regex reDays = new Regex(@"[0-2] Day"); Match daysMatch = reDays.Match(pricesAge.InnerHtml); if (daysMatch != null) { minutes += Convert.ToInt32(daysMatch.Value.Replace(" Day", string.Empty)) * 1440; } Regex reHours = new Regex(@"[0-9]{1,2}h"); Match hoursMatch = reHours.Match(pricesAge.InnerHtml); if (hoursMatch != null) { minutes += Convert.ToInt32(hoursMatch.Value.Replace("h", string.Empty)) * 60; } Regex reMinutes = new Regex(@"[0-9]{1,2}h"); Match minutesMatch = reMinutes.Match(pricesAge.InnerHtml); if (minutesMatch != null) { minutes += Convert.ToInt32(minutesMatch.Value.Replace("h", string.Empty)); } } catch { } decimal hours = minutes / 60; hours = Math.Floor(hours); minutes = minutes % 60; DateTime updated = DateTime.Now.Subtract(new TimeSpan(Convert.ToInt32(hours), minutes, 0)); re = new Regex(@"^hR[0-9]{1,3}$"); HtmlNode tmpRegPrice = nodesWithIds.Find(snode => re.IsMatch(snode.Attributes["id"].Value)); if (tmpRegPrice != null) { string tmpPrice = tmpRegPrice.Attributes["value"].Value.Trim(); if (tmpPrice != "9.99") { GasPrice gp = new GasPrice(); gp.GasType = "Regular"; gp.Price = tmpPrice; gp.LastUpdate = updated.ToShortDateString(); station.Prices.Add(gp); } } re = new Regex(@"^hM[0-9]{1,3}$"); HtmlNode tmpMidPrice = nodesWithIds.Find(snode => re.IsMatch(snode.Attributes["id"].Value)); if (tmpMidPrice != null) { string tmpPrice = tmpMidPrice.Attributes["value"].Value.Trim(); if (tmpPrice != "9.99") { GasPrice gp = new GasPrice(); gp.GasType = "Mid-grade"; gp.Price = tmpPrice; gp.LastUpdate = updated.ToShortDateString(); station.Prices.Add(gp); } } re = new Regex(@"^hP[0-9]{1,3}$"); HtmlNode tmpPremPrice = nodesWithIds.Find(snode => re.IsMatch(snode.Attributes["id"].Value)); if (tmpPremPrice != null) { string tmpPrice = tmpPremPrice.Attributes["value"].Value.Trim(); if (tmpPrice != "9.99") { GasPrice gp = new GasPrice(); gp.GasType = "Premium"; gp.Price = tmpPrice; gp.LastUpdate = updated.ToShortDateString(); station.Prices.Add(gp); } } re = new Regex(@"^hP[0-9]{1,3}$"); HtmlNode tmpDieselPrice = nodesWithIds.Find(snode => re.IsMatch(snode.Attributes["id"].Value)); if (tmpDieselPrice != null) { string tmpPrice = tmpDieselPrice.Attributes["value"].Value.Trim(); if (tmpPrice != "9.99") { GasPrice gp = new GasPrice(); gp.GasType = "Diesel"; gp.Price = tmpPrice; gp.LastUpdate = updated.ToShortDateString(); station.Prices.Add(gp); } } } stations.Add(station); } } return (from s in stations where s.Prices.Count > 0 select s).ToList(); }
public override List<IStation> GetStations(string latitude, string longitude) { string s = string.Empty; HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(baseURL + string.Format(dataTemplate, latitude, longitude)); WebReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"; HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); Stream answer = WebResp.GetResponseStream(); using (StreamReader rdr = new StreamReader(answer)) { s = rdr.ReadToEnd(); } StringReader sr = new StringReader(s); XPathDocument doc = new XPathDocument(sr); XPathNavigator navDoc = doc.CreateNavigator(); XPathNodeIterator itStation = navDoc.Select("//station"); List<IStation> stations = new List<IStation>(); while (itStation.MoveNext()) { MapQuestStation st = new MapQuestStation(); XPathNavigator navStation = itStation.Current.Clone(); PropertyInfo[] propsSt = typeof(MapQuestStation).GetProperties(); foreach (PropertyInfo p in propsSt) { object[] objArr = p.GetCustomAttributes(typeof(XmlMappingAttribute), false); if (objArr != null) { if (objArr.Length > 0) { XPathNavigator navInner = navStation.SelectSingleNode(((XmlMappingAttribute)objArr[0]).XmlFieldName); p.SetValue(st, navInner.InnerXml, null); } } } XPathNavigator navLocation = navStation.SelectSingleNode("latlng"); XPathNavigator navLocationLat = navLocation.SelectSingleNode("latitude"); st.Location.Latitude = navLocationLat.InnerXml; XPathNavigator navLocationLng = navLocation.SelectSingleNode("longitude"); st.Location.Longitude = navLocationLng.InnerXml; XPathNodeIterator itGasPrice = navStation.Select("priceCollection/gasPrice"); while (itGasPrice.MoveNext()) { XPathNavigator navGasPrice = itGasPrice.Current.Clone(); GasPrice gp = new GasPrice(); PropertyInfo[] propsGp = typeof(GasPrice).GetProperties(); foreach (PropertyInfo p in propsGp) { object[] objArr = p.GetCustomAttributes(typeof(XmlMappingAttribute), false); if (objArr != null) { if (objArr.Length > 0) { XPathNavigator navInner = navGasPrice.SelectSingleNode(((XmlMappingAttribute)objArr[0]).XmlFieldName); p.SetValue(gp, navInner.InnerXml, null); } } } st.Prices.Add(gp); } stations.Add(st); } return stations; }