public RedirectToRouteResult EditProductParserAdmin(ParseInfo model) { var parser = context.ParseInfoes.FirstOrDefault(p => p.ID == model.ID); string message = ""; if (parser != null) { parser.Name = model.Name; parser.PriceXPath = model.PriceXPath; parser.ImageXpath = model.ImageXpath; parser.Parselink = model.Parselink; parser.CPUXPath = model.CPUXPath; parser.VGAXPath = model.VGAXPath; parser.HDDXPath = model.HDDXPath; parser.DisplayXPath = model.DisplayXPath; parser.RAMXPath = model.RAMXPath; parser.Parselink = model.Parselink; context.SaveChanges(); message = "success"; } else { message = "failed"; } TempData["edit"] = message; return RedirectToAction("Index"); }
public RedirectToRouteResult CreateProductParser(ProductParserCreator model) { var uri = new Uri(model.ParseProductLink); string host = uri.GetLeftPart(UriPartial.Authority); var parseInfo = context.ParseInfoes.FirstOrDefault(info => info.Parselink.Contains(host) && info.IsActive==true); if (parseInfo == null) { var productInfo = new ParseInfo { Name = model.ProductNameXpath, PriceXPath = model.PriceXpath, ImageXpath = model.ImageXpath, Parselink = model.ParseProductLink, CPUXPath = model.CPUXpath, DisplayXPath = model.DisplayXpath, HDDXPath = model.HDDXpath, RAMXPath = model.RAMXpath, VGAXPath = model.VGAXpath, IsActive = true, }; //Create parser if not exist context.ParseInfoes.Add(productInfo); context.SaveChanges(); } else { //If exist Update Parser parseInfo.Name = model.ProductNameXpath; parseInfo.PriceXPath = model.PriceXpath; parseInfo.ImageXpath = model.ImageXpath; parseInfo.Parselink = model.ParseProductLink; parseInfo.CPUXPath = model.CPUXpath; parseInfo.DisplayXPath = model.DisplayXpath; parseInfo.HDDXPath = model.HDDXpath; parseInfo.RAMXPath = model.RAMXpath; parseInfo.VGAXPath = model.VGAXpath; parseInfo.IsActive = true; context.SaveChanges(); } Task.Factory.StartNew(() => ParserHelper.ParseProductData(model)); int rcmId = Int32.Parse(model.RecommendProductId); var recommendProduct = context.RecommendProducts.Where(x => x.ID == rcmId).FirstOrDefault(); recommendProduct.IsApprove = true; context.SaveChanges(); TempData["createbyRecommendProduct"] = "success"; System.Threading.Thread.Sleep(5000); return RedirectToAction("Index"); }
public RedirectToRouteResult CreateProductParserAdmin(ProductParserCreator model) { var parser = new ParseInfo { Name = model.ProductNameXpath, PriceXPath = model.PriceXpath, ImageXpath = model.ImageXpath, Parselink = model.ParseProductLink, CPUXPath = model.CPUXpath, VGAXPath = model.VGAXpath, HDDXPath = model.HDDXpath, DisplayXPath = model.DisplayXpath, RAMXPath = model.RAMXpath }; context.ParseInfoes.Add(parser); context.SaveChanges(); TempData["create"] = "Success"; return RedirectToAction("Index"); }
public static ProductData GetProductData(HtmlWeb web, ParseInfo parseInfo) { var data = new ProductData(); try { //load page System.Net.ServicePointManager.Expect100Continue = false; var uri = new Uri(parseInfo.Parselink); string host = uri.GetLeftPart(UriPartial.Authority); HtmlNode.ElementsFlags.Remove("form"); var doc = web.Load(parseInfo.Parselink); data = MatchingProductData(host, doc, parseInfo.Name, parseInfo.PriceXPath, parseInfo.ImageXpath, parseInfo.CPUXPath, parseInfo.VGAXPath, parseInfo.HDDXPath, parseInfo.RAMXPath, parseInfo.DisplayXPath); return data; } catch (System.Net.WebException ex) { GetProductData(web, parseInfo); } catch (HtmlWebException ex) { GetProductData(web, parseInfo); } return data; }
public void DoTask(List<RecommendProduct> listOfRecommend, List<ParseInfo> listOfParseInfo) { string patter = "://|/"; Regex reg = new Regex(patter); List<ParseInfo> listInfo = new List<ParseInfo>(); ParseInfo infoItem = null; List<RecommendProduct> list = new List<RecommendProduct>(); if (listOfRecommend != null) { foreach (var info in listOfParseInfo) { infoItem = new ParseInfo { CPUXPath = info.CPUXPath, DisplayXPath = info.DisplayXPath, HDDXPath = info.HDDXPath, ImageXpath = info.ImageXpath, IsActive = true, Name = info.Name, Parselink = info.Parselink, RAMXPath = info.RAMXPath, VGAXPath = info.VGAXPath, }; listInfo.Add(infoItem); } var fitlerList = listInfo.Select(x => new { host = reg.Split(x.Parselink)[1], x.CPUXPath, x.DisplayXPath, x.HDDXPath, x.ImageXpath, x.Name, x.Parselink, x.RAMXPath, x.VGAXPath, x.IsActive }) .Distinct() .GroupBy(y => new { y.host }) .ToArray(); ProductParserCreator model = null; foreach (var item in fitlerList) { foreach (var recommend in listOfRecommend) { if (recommend.Parselink.Contains(item.Key.host)) { model = new ProductParserCreator { CPUXpath = item.First().CPUXPath, DisplayXpath = item.First().DisplayXPath, HDDXpath = item.First().HDDXPath, ImageXpath = item.First().ImageXpath, IsActive = true, ParseProductLink = item.First().Parselink, ProductNameXpath = item.First().Name, RAMXpath = item.First().RAMXPath, VGAXpath = item.First().VGAXPath, RecommendProductId = recommend.ID.ToString(), }; ParserHelper.ParseProductData(model); int rcmId = Int32.Parse(model.RecommendProductId); var recommendProduct = databaseContext.RecommendProducts.Where(x => x.ID == rcmId).FirstOrDefault(); recommendProduct.IsApprove = true; } } } databaseContext.SaveChanges(); } }
public ActionResult SetActive(int id) { var parser = context.ParseInfoes.FirstOrDefault(x => x.ID == id); bool statusFlag = false; if (ModelState.IsValid) { if (parser.IsActive) { parser.IsActive = false; statusFlag = false; } else { parser.IsActive = true; statusFlag = true; } context.SaveChanges(); } // Display the confirmation message var results = new ParseInfo { IsActive = statusFlag }; return Json(results); }