public static void RequestValuationFromWebsite(CarDetails carDetails, string Filename) { ValuationParser_HonestJohn valuer = new ValuationParser_HonestJohn(); string baseURL = @"http://www.honestjohn.co.uk/used-prices/"; string args = "?q="; string detailsToFind = "Ford Mondeo 2004 Automatic Estate Ghia X TDCi"; detailsToFind = "renault Clio 2003 extreme"; detailsToFind = carDetails.FormatForValuation_HonestJohn(); string fullURL = baseURL + args + detailsToFind; var req = (HttpWebRequest)WebRequest.Create(fullURL); req.Method = "GET"; using (WebResponse odpoved = req.GetResponse()) { //Console.WriteLine(DateTime.Now.ToShortDateString()); var fileStream = File.Create(Filename); Stream str = odpoved.GetResponseStream(); str.CopyTo(fileStream); fileStream.Close(); } return; }
public static List <CarDetails> ParseCatalogue_StoredFile(string FileName) { AuctionCatalogueParser_Bawtry parser = new AuctionCatalogueParser_Bawtry(); List <CarDetails> returnDetails = new List <CarDetails>(); var req1 = (FileWebRequest)WebRequest.Create(FileName); req1.Method = "GET"; using (WebResponse odpoved = req1.GetResponse()) { var htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.Load(odpoved.GetResponseStream()); //string test = valuer.GetPathText(htmlDoc, "//*[@id=\"content\"]/table/tbody/tr[999]/td[2]"); for (int i = 2; i < 999; i += 2) { string carDetail = parser.GetPathText(htmlDoc, "//*[@id=\"content\"]/table/tbody/tr[" + i + "]/td[2]"); if (carDetail != null) { var result = Regex.Split(carDetail, "\r\n|\r|\n"); string longDesc1 = null; string longDesc2 = null; string registered_raw = null; for (int j = 0; j < result.Length; j++) { string currLine = result[j].Trim(); if (currLine != "") { //Console.WriteLine(currLine); if (longDesc1 == null) { longDesc1 = currLine; } else if (longDesc2 == null) { longDesc2 = currLine; } else if (registered_raw == null) { registered_raw = currLine; } else { throw new Exception("Unexpected detail line"); } // end if-then-else } // end if } // end for CarDetails newCar = new CarDetails(); newCar.FromCatalogue = "BAWTRY"; newCar.Long_Description = longDesc1 + " " + longDesc2; registered_raw = registered_raw.Replace("Registered", ""); registered_raw = registered_raw.Trim(); DateTime RegDate; if (DateTime.TryParse(registered_raw, out RegDate)) { newCar.RegDate = RegDate; } // end if string lotNo = parser.GetPathText(htmlDoc, "//*[@id=\"content\"]/table/tbody/tr[" + i + "]/td[4]"); newCar.Lot_Number = lotNo; //*[@id=\"content\"]/table/tbody/tr[4]/td/div //*[@id="content"]/table/tbody/tr[4]/td/div/img[1] //*[@id="content"]/table/tbody/tr[4]/td/div/img[2] string regNo = ""; for (int x = 1; x < 15; x++) { string xPath = "//*[@id=\"content\"]/table/tbody/tr[" + (i + 1) + "]/td/div/img[" + x + "]"; var nodes = htmlDoc.DocumentNode.SelectNodes(xPath); if (nodes != null) { for (int y = 0; y < nodes.Count; y++) { regNo = regNo + nodes[y].Attributes[1].Value; } // end for } // end if } // end for //Console.WriteLine(); newCar.RegNo = regNo.ToUpper(); //var nodes = htmlDoc.DocumentNode.SelectNodes(xPath); ////src = new List<string>(nodes.Count); //if (nodes != null) //{ // foreach (var node in nodes) // { // if (node.Id != null) // { // //src.Add(node.Id); // //Console.WriteLine("Most adverts are between " + node.InnerText); // string textFound = node.InnerText.Trim(); // textFound = textFound.Replace("Â", ""); // return textFound; // } // } //} //Console.WriteLine("***********************************"); //Console.WriteLine("Desc: " + newCar.Long_Description); //Console.WriteLine("Reg date: " + newCar.RegDate.ToShortDateString()); //Console.WriteLine("Reg year: " + newCar.RegDate.Year); //Console.WriteLine("Formatted: " + newCar.FormatForValuation_HonestJohn()); //Console.WriteLine("***********************************"); //Console.WriteLine(carDetail.Trim()); returnDetails.Add(newCar); } // end if } /* * foreach(HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//*[@id=\"content\"]/table/tbody/tr")) * { * Console.WriteLine("***********************************"); * Console.WriteLine(link.InnerText); * //Console.WriteLine(link.ToString()); * //HtmlAttribute att = link["href"]; * //att.Value = FixLink(att); * Console.WriteLine("***********************************"); * } */ } return(returnDetails); }
public static List <CarDetails> ParseCatalogue_StoredFile(string FileName) { Excel.Application ExcelObj = null; Excel.Workbook theWorkbook = null; List <CarDetails> returnDetails = new List <CarDetails>(); try { ExcelObj = new Excel.Application(); theWorkbook = ExcelObj.Workbooks.Open(FileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true); Excel.Sheets sheets = theWorkbook.Worksheets; Excel.Worksheet catalogueWorksheet = (Excel.Worksheet)sheets.get_Item(1); const int C_COL_NUM__LOT = 2; // B const int C_COL_NUM__MAKE = 4; // D const int C_COL_NUM__MODEL = 6; // F const int C_COL_NUM__TYPE = 7; // G const int C_COL_NUM__REGISTERED = 8; // H const int C_COL_NUM__FUEL = 10; // J const int C_COL_NUM__TRANS = 11; // K const int C_COL_NUM__DOORS = 13; // M const int C_COL_NUM__MILES = 14; // N Excel.Range range = catalogueWorksheet.get_Range("A1", "R50"); System.Array myvalues = (System.Array)range.Cells.Value; // loop through each depot row for (int rowIndex = 1; rowIndex <= range.Cells.Rows.Count; rowIndex++) { //Console.WriteLine("**********************************"); //Console.WriteLine("MAKE:" + (myvalues.GetValue(rowIndex, C_COL_NUM__MAKE) ?? "").ToString().Trim()); //Console.WriteLine("MODEL:" + (myvalues.GetValue(rowIndex, C_COL_NUM__MODEL) ?? "").ToString().Trim()); //Console.WriteLine("TYPE:" + (myvalues.GetValue(rowIndex, C_COL_NUM__TYPE) ?? "").ToString().Trim()); //Console.WriteLine("REGDATE:" + (myvalues.GetValue(rowIndex, C_COL_NUM__REGISTERED) ?? "").ToString().Trim()); //Console.WriteLine("FUEL:" + (myvalues.GetValue(rowIndex, C_COL_NUM__FUEL) ?? "").ToString().Trim()); //Console.WriteLine("TRANS:" + (myvalues.GetValue(rowIndex, C_COL_NUM__TRANS) ?? "").ToString().Trim()); //Console.WriteLine("DOORS:" + (myvalues.GetValue(rowIndex, C_COL_NUM__DOORS) ?? "").ToString().Trim()); //Console.WriteLine("MILES:" + (myvalues.GetValue(rowIndex, C_COL_NUM__MILES) ?? "").ToString().Trim()); //Console.WriteLine("**********************************"); CarDetails newCar = new CarDetails(); newCar.FromCatalogue = "NEWARK"; newCar.Lot_Number = (myvalues.GetValue(rowIndex, C_COL_NUM__LOT) ?? "").ToString().Trim(); newCar.Make = (myvalues.GetValue(rowIndex, C_COL_NUM__MAKE) ?? "").ToString().Trim(); newCar.Model = (myvalues.GetValue(rowIndex, C_COL_NUM__MODEL) ?? "").ToString().Trim(); newCar.Type = (myvalues.GetValue(rowIndex, C_COL_NUM__TYPE) ?? "").ToString().Trim(); DateTime regDate; if (DateTime.TryParse((myvalues.GetValue(rowIndex, C_COL_NUM__REGISTERED) ?? "").ToString().Trim(), out regDate)) { newCar.RegDate = regDate; } // end if newCar.Fuel = (myvalues.GetValue(rowIndex, C_COL_NUM__FUEL) ?? "").ToString().Trim(); newCar.Trans = (myvalues.GetValue(rowIndex, C_COL_NUM__TRANS) ?? "").ToString().Trim(); newCar.Doors = (myvalues.GetValue(rowIndex, C_COL_NUM__DOORS) ?? "").ToString().Trim(); newCar.Mileage = (myvalues.GetValue(rowIndex, C_COL_NUM__MILES) ?? "").ToString().Trim(); returnDetails.Add(newCar); } // end for if (ExcelObj != null) { ExcelObj.Visible = false; ExcelObj.UserControl = false; } // end if if (theWorkbook != null) { // Close the document and avoid user prompts to save if our method failed. theWorkbook.Close(false, null, null); ExcelObj.Workbooks.Close(); } // end if } finally { if (theWorkbook != null) { Marshal.ReleaseComObject(theWorkbook); } if (ExcelObj != null) { ExcelObj.Quit(); } if (ExcelObj != null) { Marshal.ReleaseComObject(ExcelObj); } theWorkbook = null; ExcelObj = null; GC.Collect(); } return(returnDetails); }