public void Run(Quartz.IJobExecutionContext context) { //获取可用蜘蛛 //待执行状态 //开启状态 //间隔时间符合条件 var SpiderCollection = DB.Database.SqlQuery <yy_Spider>("SELECT * FROM yy_Spider WITH(NOLOCK) WHERE Status = 0 AND IsShow = 1 AND DATEADD(second,ExecutionInterval,LastStartTime) < GETDATE()").ToList(); if (SpiderCollection.Count > 0) { foreach (var v in SpiderCollection) { try { //蜘蛛开始执行任务 Start(v); } catch (Exception ex) { Add(ex); } finally { } } } //状态重置 DB.Database.ExecuteSqlCommand("UPDATE yy_Spider SET Status=0"); MQueue.Send("NewsSync", null); }
private bool SendCommands(string _command, string cmd) { Dictionary <string, string> _parameters = new Dictionary <string, string>(); _parameters.Add("msgText", cmd); var parameters = string.Join("", _parameters.Select(p => string.Format("&{0}={1}", p.Key, p.Value)).ToArray()); var commandText = string.Format("/Device.{0}?devId={1}{2}", _command, DeviceId, parameters); try { var cola = new MQueue(_mobile.Queue); cola.Send(commandText); return(true); } catch { return(false); } }
public void ReceiveCompleted(String body) { var news = new List <yy_Spider_News>(); if (String.IsNullOrEmpty(body)) { var spiders = DB.yy_Spider.Where(x => x.SpiderMode == 1).Select(x => x.ID).ToList(); if (spiders.Count < 1) { return; } foreach (var v in spiders) { var _news = DB.yy_Spider_News.Where(x => x.SpiderID == v && x.IsSync == 0).ToList(); if (_news.Count > 0) { news.AddRange(_news); } } } else { var IDs = body.Split(new String[1] { "," }, StringSplitOptions.RemoveEmptyEntries); long ID = 0; foreach (var v in IDs) { if (!long.TryParse(v, out ID)) { ID = 0; } if (ID < 1) { continue; } var item = DB.yy_Spider_News.Find(v); if (item != null) { news.Add(item); } } } if (news.Count < 1) { return; } var crtDic = "/Images/" + DateTime.Now.Ticks.ToString() + "/"; var temp = ConfigurationManager.AppSettings["AdminImagesPath"] + crtDic; if (!Directory.Exists(temp)) { Directory.CreateDirectory(temp); } foreach (var v in news) { #region 新闻实体 var spiderItem = DB.yy_Spider.Where(x => x.ID == v.SpiderID).FirstOrDefault(); var newsItem = new yy_News() { Title = v.Title, DefaultImg = v.DefaultImage, KeyWords = v.KeyWords, Summary = v.Summary, Info = v.Info, TypeIDs = spiderItem.TypeIDs, UserID = spiderItem.UserID, TargetPlatforms = spiderItem.TargetPlatforms, CreateDate = DateTime.Now, IsShow = 1, CanReply = 1, ImgList = "", LookCount = 0, Recommend = 0, ShowIndex = 0, WechatMediaID = "", WechatNewsUrl = "" }; #endregion 新闻实体 #region 替换默认图片 if (String.IsNullOrEmpty(newsItem.DefaultImg)) { newsItem.DefaultImg = "/Images/users.jpg"; } else { var dftName = Guid.NewGuid() + System.IO.Path.GetExtension(newsItem.DefaultImg).Replace("!middle", ""); var dftIMG = temp + dftName; using (var wb = new WebClient()) { wb.DownloadFile(newsItem.DefaultImg, dftIMG); } newsItem.DefaultImg = crtDic + dftName; } #endregion 替换默认图片 #region 新闻内容里的图片上传到微信服务器并替换地址 var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(newsItem.Info); var imgs = doc.DocumentNode.SelectNodes("//img"); if (imgs != null && imgs.Count > 0) { for (var i = 0; i < imgs.Count; i++) { try { if (imgs[i].HasAttributes && imgs[i].Attributes["src"] != null) { var src = imgs[i].Attributes["src"].Value; if (src.IndexOf("http://") >= 0) { var savePath = temp + System.IO.Path.GetFileName(src).Replace("!middle", ""); using (var wc = new WebClient()) { if (!File.Exists(savePath)) { wc.DownloadFile(src, savePath); } } imgs[i].Attributes["src"].Value = crtDic + System.IO.Path.GetFileName(src); } } } catch { } Thread.Sleep(1); } newsItem.Info = doc.DocumentNode.OuterHtml; } #endregion 新闻内容里的图片上传到微信服务器并替换地址 DB.yy_News.Add(newsItem); v.IsSync = 1; DB.SaveChanges(); MQueue.Send("WechatSync", newsItem.ID.ToString()); } }