private void GetImages() { WriteLog("获取商品图片"); List <string> xpaths = new List <string>() { ".//*[@id='altImages']/ul/li/span/span/span/span/img", ".//*[@id='altImages']/ul/li/span/span/span/span/span/img" }; Regex RegexImage = new Regex(@"L\..*?.jpg"); string imageSize = "L._SX450_SY450_CR,0,0,450,450_.jpg"; foreach (string xpath in xpaths) { HtmlNodeCollection htmlNodes = htmlDocument.DocumentNode.SelectNodes(xpath); if (htmlNodes != null && htmlNodes.Count > 0) { var count = 1; lock (insertData) { foreach (var htmlNode in htmlNodes) { if (count > 2) { break; } string imageSrc = htmlNode.GetAttributeValue("src", "").Trim(); imageSrc = RegexImage.Replace(imageSrc, imageSize); var pi = ProductImage.AddOrUpdate(new ProductImage() { Asin = product.Asin, Number = count, Status = 0, Url = imageSrc }); if (pi.Id > 0) { TaskSchedule.AddOrUpdate(new TaskSchedule() { PlayerAccountId = pi.Id, RunDateTime = DateTime.Now.GetTimestamp(), PlayerType = "Amazonspider.ProductImageDownload.Download", PlayerStep = "1" }); } count++; } } break; } } }
private void GetMoreProducts() { WriteLog("获取更多商品"); var descNodes = htmlDocument.GetElementbyId("sp_detail"); //GetHtmlNodesByCss("div[data-a-carousel-options*='ajax']"); if (descNodes != null) { //foreach (var item in descNodes) //{ string more = descNodes.GetAttributeValue("data-a-carousel-options", "").Replace(""", "\"").Trim() + "\r\n"; JObject jb = (JObject)JsonConvert.DeserializeObject(more); JArray jarr = null; if (more.IndexOf("initialSeenAsins") > -1) { jarr = jb["initialSeenAsins"].ToObject <JArray>(); } else if (more.IndexOf("id_list") > -1 && more.IndexOf("ajax") > -1) { jarr = jb["ajax"]["id_list"].ToObject <JArray>(); } if (jarr != null) { lock (insertData) { for (int i = 0; i < jarr.Count; i++) { string id = jarr[i].Value <string>().Trim(':'); var p = Product.AddOrUpdate(new Product() { Id = -2, Asin = id, Price = 0, Desc = "", Title = "", Time = DateTime.Now.GetTimestamp(), }); if (p.Id > -1) { TaskSchedule.AddOrUpdate(new TaskSchedule() { PlayerAccountId = p.Id, PlayerStep = "2", PlayerType = this.GetType().ToString(), RunDateTime = DateTime.Now.GetTimestamp() }); } } } //} } } }
private void btnAddAsin_Click(object sender, EventArgs e) { var product = Product.AddOrUpdate(new Product() { Asin = txtAsin.Text, Status = 0, Desc = "", Title = "", Price = 0, Time = DateTime.Now.GetTimestamp() }); if (product.Id > -1) { TaskSchedule.AddOrUpdate(new TaskSchedule() { PlayerAccountId = product.Id, PlayerStep = "download", RunDateTime = DateTime.Now.GetTimestamp(), PlayerType = new Amazonspider.ProductDownload.Download().GetType().ToString() }); } }
private void _timer_Elapsed(object sender, ElapsedEventArgs e) { try { _timer?.Stop(); var playerIsNullRunInfos = RunInfos.Where(q => q.player == null).ToList(); playerIsNullRunInfos.AsParallel().ForAll((runInfo) => { var result = TaskSchedule.Get(); if (result != null) { runInfo.TaskSchedule = result; runInfo.player = PlugInManager.PlugInSourceList.Select(q => q.GetNew <IPlayer>()).Where(p => p != null).ToList().Where(q => q.GetType().ToString() == result.PlayerType).FirstOrDefault(); if (runInfo.player != null) { runInfo.player.ConsoleLog = this; runInfo.player.Configuration = Configuration; runInfo.PlugInSource = PlugInManager.PlugInSourceList.Where(q => q.Assemblie == runInfo.player.GetType().Assembly).FirstOrDefault(); runInfo.player.Complete += (product, productImage, task, player) => { if (product != null) { product = Product.AddOrUpdate(product); if (task != null) { task.PlayerAccountId = product.Id; } } if (productImage != null) { productImage = ProductImage.AddOrUpdate(productImage); if (task != null) { task.PlayerAccountId = productImage.Id; } } if (task != null && task.PlayerAccountId > -1) { if (player != null) { task.PlayerType = player.GetType().ToString(); } TaskSchedule.AddOrUpdate(task); } runInfo.Dispose(); }; runInfo.Init(); WriteLog($"当前任务:{runInfo.pluginname}--{runInfo.TaskSchedule.PlayerStep}", ConsoleLogStatus.Nomral); } } else { WriteLog("未找到任务信息", ConsoleLogStatus.Nomral); } }); foreach (var runInfo in RunInfos) { //超时处理 if (runInfo.CheckTimeout()) { if (runInfo.State == 0) { WriteLog?.Invoke($"运行超时--{runInfo.simulatorname},释放资源", ConsoleLogStatus.Error); runInfo.player.TaskSchedule.RunDateTime = DateTime.Now.GetTimestamp(); runInfo.player.Complete?.Invoke(null, null, runInfo.player.TaskSchedule, runInfo.player); runInfo.Dispose(); } } } } catch (Exception ex) { log4net.ILog log = log4net.LogManager.GetLogger("testApp.Logging");//获取一个日志记录器 log.Error("定时器异常--", ex); } finally { _timer?.Start(); } }