using System; using System.Collections.Generic; using System.Linq; using System.Text; using CP.Model; using HtmlAgilityPack; using Newtonsoft.Json; using Quartz; using SCC.Common; using SCC.Crawler.Tools; using SCC.Interface; using SCC.Models; namespace SCC.Crawler.DT { /// /// 浙江20选5 /// [DisallowConcurrentExecution] [PersistJobDataAfterExecution] public class ZheJiang20x5Job : IJob { /// /// 初始化函数 /// public ZheJiang20x5Job() { log = new LogHelper(); email = IOC.Resolve(); } /// /// 作业执行入口 /// /// 作业执行上下文 public void Execute(IJobExecutionContext context) { Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap); //预设节假日不开奖 if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return; LatestItem = context.JobDetail.JobDataMap["LatestItem"] as Tczj20x5LongInfo; try { //服务启动时配置初始数据 if (LatestItem == null) { LatestItem = new Tczj20x5LongInfo { qi = CommonHelper.GenerateQiHaoYYQQQ(0), date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1) }; } //程序时间第二天,程序根据配置检查是否昨天有开奖 isGetData = false; if (CommonHelper.CheckDTIsNeedGetData(Config)) // { DoMainUrl(); DoBackUrl(); } if (!LatestItem.qi.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy"))) LatestItem = new Tczj20x5LongInfo { qi = CommonHelper.GenerateQiHaoYYQQQ(0), date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1) }; //当今日开奖并且当前时间是晚上8点过后开始抓取 if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12) { DoMainUrl(); DoBackUrl(); } } catch (Exception ex) { log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } //保存最新期号 context.JobDetail.JobDataMap["LatestItem"] = LatestItem; } #region 通过主站爬取数据 private void DoMainUrl() { if (!string.IsNullOrEmpty(Config.MainUrl)) { var openList = GetOpenListFromMainUrl(Config.MainUrl); if (openList == null || openList.Count == 0) return; //无抓取数据 //抓取到的最新期数 var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString()); //数据库里面最新期数 LatestItem = Tczj20x5Data.GetLastOne(); var startQiNum = Convert.ToInt32(LatestItem.qi.ToString()); if (startQiNum > newestQiHao) return; //无最新数据 //处理最新开奖数据 Tczj20x5LongInfo matchItem = null; for (var i = startQiNum; i <= newestQiHao; i++) { matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString()); if (matchItem != null) { //add db matchItem.addtime = DateTime.Now; Tczj20x5Data.Add(matchItem); //Do Success Log log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString())); LatestItem = matchItem; isGetData = true; } } } } private List GetOpenListFromMainUrl(string mainUrl) { var result = new List(); try { var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("gb2312")); if (htmlResource == null) return result; var doc = new HtmlDocument(); doc.LoadHtml(htmlResource); var table = doc.DocumentNode.SelectSingleNode("//tbody"); if (table == null) return result; var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList(); List tds = null; string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty; Tczj20x5LongInfo model = null; for (var i = 0; i < trs.Count; i++) { tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList(); if (tds.Count < 8) continue; model = new Tczj20x5LongInfo(); term = tds[0].InnerText.Trim(); term = term.Length < 7 ? $"20{term}" : term; if (term.StartsWith((CommonHelper.SCCSysDateTime.Year - 1).ToString())) break; model.qi = Convert.ToInt32(term); openCodeString = tds[1].InnerText.Trim(); model.n1 = Convert.ToInt32(openCodeString.Substring(0, 2)); model.n2 = Convert.ToInt32(openCodeString.Substring(3, 2)); model.n3 = Convert.ToInt32(openCodeString.Substring(6, 2)); model.n4 = Convert.ToInt32(openCodeString.Substring(9, 2)); model.n5 = Convert.ToInt32(openCodeString.Substring(12, 2)); model.date = Convert.ToDateTime(tds[9].InnerText.Trim()); //组装开奖详情 GetKaijiangDetails(ref model, tds); //model.DetailUrl = mainUrl; result.Add(model); } //var checkDataHelper = new CheckDataHelper(); //var dbdata = services.GetListS(currentLottery) // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()); //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()), // Config.Area, currentLottery); } catch (Exception ex) { log.Error(GetType(), string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } return result; } /// /// 获取主站下开奖详情 /// /// /// private void GetKaijiangDetails(ref Tczj20x5LongInfo model, List nodes) { model.nextmoney = ""; model.tzmoney = nodes[8].InnerText; //组装详情 //一等奖 model.zj1 = nodes[2].InnerText; model.jo1 = nodes[3].InnerText.Replace(",", ""); //二等奖 model.zj2 = nodes[4].InnerText; model.jo2 = nodes[5].InnerText.Replace(",", ""); //三等奖 model.zj3 = nodes[6].InnerText; model.jo3 = nodes[7].InnerText.Replace(",", ""); var list = new List(); list.Add(new Winbonus() { item = "一等奖", wincount = model.zj1, winmoney = model.jo1 }); list.Add(new Winbonus() { item = "二等奖", wincount = model.zj2, winmoney = model.jo2 }); list.Add(new Winbonus() { item = "三等奖", wincount = model.zj3, winmoney = model.jo3 }); model.winbonus = JsonConvert.SerializeObject(list); } #endregion #region 通过副站爬取数据 /// /// 副站数据爬取 地址:https://fx.cp2y.com/draw/history_10065_Y/ /// /// private List GetOpenListFromBackUrl() { var result = new List(); try { var url = new Uri(Config.BackUrl); var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.UTF8); if (htmlResource == null) return result; var doc = new HtmlDocument(); doc.LoadHtml(htmlResource); var table = doc.DocumentNode.SelectSingleNode("//table"); if (table == null) return result; var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList(); Tczj20x5LongInfo model = null; HtmlNode nodeA = null; var optimizeUrl = string.Empty; for (var i = 2; i < trs.Count; i++) //第一二行为表头 { var trstyle = trs[i].Attributes["style"]; if (trstyle != null && trstyle.Value == "display:none") continue; var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList(); if (tds.Count < 10) continue; model = new Tczj20x5LongInfo(); nodeA = tds[0].ChildNodes.Where(n => n.Name == "a").FirstOrDefault(); if (nodeA == null) continue; string term = nodeA.InnerText.Trim(); term = term.Length < 7 ? $"20{term}" : term; model.qi = Convert.ToInt32(term); optimizeUrl = nodeA.Attributes["href"].Value; //model.DetailUrl = new Uri(url, optimizeUrl).AbsoluteUri; model.date = Convert.ToDateTime(tds[9].InnerText); if (tds[1].ChildNodes.Count == 0) continue; var opencodeNode = tds[1].ChildNodes.Where(n => n.Name.ToLower() == "i").ToList(); if (opencodeNode.Count < 5) continue; model.n1 = Convert.ToInt32(opencodeNode[0].InnerText.Trim()); model.n2 = Convert.ToInt32(opencodeNode[1].InnerText.Trim()); model.n3 = Convert.ToInt32(opencodeNode[2].InnerText.Trim()); model.n4 = Convert.ToInt32(opencodeNode[3].InnerText.Trim()); model.n5 = Convert.ToInt32(opencodeNode[4].InnerText.Trim()); result.Add(model); } //var checkDataHelper = new CheckDataHelper(); //var dbdata = services.GetListS(currentLottery) // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()); //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()), // Config.Area, currentLottery); //result = result.OrderByDescending(S => S.Term).ToList(); } catch (Exception ex) { log.Error(GetType(), string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } return result; } private void DoBackUrl() { if (!string.IsNullOrEmpty(Config.BackUrl)) { var openList = GetOpenListFromBackUrl(); if (openList == null || openList.Count == 0) return; //无抓取数据 //抓取到的最新期数 var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString()); //数据库里面最新期数 //LatestItem = Tczj20x5Data.GetLastOne(); var startQiNum = Convert.ToInt32(LatestItem.qi.ToString()); if (startQiNum > newestQiHao) return; //无最新数据 //处理最新开奖数据 Tczj20x5LongInfo matchItem = null; for (var i = startQiNum; i <= newestQiHao; i++) { matchItem = openList.Where(R => R.qi.ToString() == i.ToString()).FirstOrDefault(); if (matchItem != null) { //add db matchItem.addtime = DateTime.Now; Tczj20x5Data.Add(matchItem); //Do Success Log log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, i.ToString())); LatestItem = matchItem; isGetData = true; } } } } #endregion #region Attribute /// /// 配置信息 /// private SCCConfig Config; /// /// 当天抓取的最新一期开奖记录 /// private Tczj20x5LongInfo LatestItem; #pragma warning disable CS0414 // 字段“ZheJiang20x5Job.FailedQiHaoList”已被赋值,但从未使用过它的值 /// /// 当天抓取失败列表 /// private List FailedQiHaoList = null; #pragma warning restore CS0414 // 字段“ZheJiang20x5Job.FailedQiHaoList”已被赋值,但从未使用过它的值 /// /// 日志对象 /// private readonly LogHelper log; /// /// 当前彩种 /// private SCCLottery currentLottery => SCCLottery.ZheJiang20x5; /// /// 邮件接口 /// private IEmail email; #pragma warning disable CS0414 // 字段“ZheJiang20x5Job.isGetData”已被赋值,但从未使用过它的值 /// /// 是否本次运行抓取到开奖数据 /// private bool isGetData; #pragma warning restore CS0414 // 字段“ZheJiang20x5Job.isGetData”已被赋值,但从未使用过它的值 #endregion } }