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 _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(); } }