using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; 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 { /// /// 江苏体彩7位数 /// [DisallowConcurrentExecution] [PersistJobDataAfterExecution] public class JSTC7WSJob : IJob { /// /// 初始化函数 /// public JSTC7WSJob() { 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 Tcjs7wsLongInfo; try { //服务启动时配置初始数据 if (LatestItem == null) { LatestItem = new Tcjs7wsLongInfo { 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 Tcjs7wsLongInfo { qi = CommonHelper.GenerateQiHaoYYQQQ(0), date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1) }; //当今日开奖并且当前时间是晚上8点过后开始抓取 if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 0) { DoMainUrl(); DoBackUrl(); } } catch (Exception ex) { log.Error(typeof(JSTC7WSJob), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } //保存最新期号 context.JobDetail.JobDataMap["LatestItem"] = LatestItem; } /// /// 通过主站点爬取开奖数据 /// (江苏体彩网) /// 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 = Tcjs7wsData.GetLastOne(); var startQiNum = Convert.ToInt32(LatestItem.qi.ToString()); if (startQiNum > newestQiHao) return; //无最新数据 //处理最新开奖数据 Tcjs7wsLongInfo 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; Tcjs7wsData.Add(matchItem); //Do Success Log log.Info(typeof(JSTC7WSJob), CommonHelper.GetJobMainLogInfo(Config, i.ToString())); LatestItem = matchItem; isGetData = true; } } } } /// /// 获取主站开奖列表数据 /// /// 主站地址 /// private List GetOpenListFromMainUrl(string mainUrl) { var result = new List(); try { var pageIndex = 1; var htmlResource = string.Empty; var resourceUrl = new Uri(mainUrl); var isLoop = true; var lastYear = (DateTime.Now.Year - 1).ToString().Substring(2); var postData = "current_page={0}&all_count=0&num="; var OpenTime = string.Empty; while (isLoop) { htmlResource = NetHelper.GetUrlResponse(resourceUrl.AbsoluteUri, "POST", string.Format(postData, pageIndex), Encoding.UTF8); var jsonData = htmlResource.JsonToEntity(); var dataList = jsonData["items"]; foreach (var data in dataList) { if (data["num"].Value.StartsWith(lastYear)) { isLoop = false; break; } OpenTime = data["date_publish"].Value.Insert(6, "-").Insert(4, "-"); string detailUrl = string.Format( "http://www.js-lottery.com/Article/news/group_id/3/article_id/{0}.html", data["article_id"].Value); string qihao = data["num"].Value; qihao = qihao.Length < 7 ? $"20{qihao}" : qihao; var model = new Tcjs7wsLongInfo { qi = Convert.ToInt32(qihao), n1 = Convert.ToInt32(data["one"].Value), n2 = Convert.ToInt32(data["two"].Value), n3 = Convert.ToInt32(data["three"].Value), n4 = Convert.ToInt32(data["four"].Value), n5 = Convert.ToInt32(data["five"].Value), n6 = Convert.ToInt32(data["six"].Value), n7 = Convert.ToInt32(data["seven"].Value), date = Convert.ToDateTime(OpenTime), }; OptimizeMainModel(ref model, detailUrl); result.Add(model); } pageIndex++; } //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(typeof(JSTC7WSJob), string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } return result; } /// /// 完善主站江苏体彩7位数开奖详情信息 /// /// private void OptimizeBackModel(ref Tcjs7wsLongInfo model, HtmlNode tr) { try { var tds = tr.ChildNodes.Where(w => w.Name == "td").ToList(); var xiaoshoue = tds[2].InnerText.Trim().Replace(",", "").Replace("元", ""); var jiangchi = ""; //特等奖 model.zj = tds[3].InnerText.Trim(); model.jo = tds[4].InnerText; //一等奖 model.zj1 = tds[5].InnerText.Trim(); model.jo1 = tds[6].InnerText; //二等奖 model.zj2 = tds[7].InnerText.Trim(); model.jo2 = tds[8].InnerText; model.nextmoney = jiangchi; model.tzmoney = xiaoshoue; var list = new List(); list.Add(new Winbonus() { item = "特等奖", wincount = model.zj, winmoney = model.jo }); list.Add(new Winbonus() { item = "一等奖", wincount = model.zj1, winmoney = model.jo1 }); list.Add(new Winbonus() { item = "二等奖", wincount = model.zj2, winmoney = model.jo2 }); model.winbonus = JsonConvert.SerializeObject(list); } catch (Exception ex) { log.Error(typeof(JSTC7WSJob), string.Format("【{0}】通过主站点优化开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } } /// /// 完善主站江苏体彩7位数开奖详情信息 /// /// private void OptimizeMainModel(ref Tcjs7wsLongInfo model, string detailUrl) { try { var htmlResource = NetHelper.GetUrlResponse(detailUrl); if (htmlResource == null) return; if (!string.IsNullOrEmpty(htmlResource)) { var doc = new HtmlDocument(); doc.LoadHtml(htmlResource); var table = doc.DocumentNode.SelectNodes("//table"); if (table != null && table.Count > 1) { var trs = table[1].ChildNodes.Where(N => N.Name.ToLower() == "tbody").First().ChildNodes .Where(N => N.Name.ToLower() == "tr").ToList(); for (var i = 0; i < trs.Count; i++) { var tds = trs[i].ChildNodes.Where(N => N.Name.ToLower() == "td").ToList(); if (tds[0].InnerText == "特等奖") { model.zj = tds[1].InnerText.Replace(",", string.Empty).Replace("注", string.Empty); model.jo = tds[2].InnerText.Replace("元", string.Empty).Replace("--", "0").Replace(",", "").Trim(); } else if (tds[0].InnerText == "一等奖") { model.zj1 = tds[1].InnerText.Replace(",", string.Empty).Replace("注", string.Empty); model.jo1 = tds[2].InnerText.Replace("元", string.Empty).Replace("--", "0").Replace(",", "").Trim(); } else if (tds[0].InnerText == "二等奖") { model.zj2 = tds[1].InnerText.Replace(",", string.Empty).Replace("注", string.Empty); model.jo2 = tds[2].InnerText.Replace("元", string.Empty).Replace("--", "0").Replace(",", "").Trim(); } else if (tds[0].InnerText == "三等奖") { model.zj3 = tds[1].InnerText.Replace(",", string.Empty).Replace("注", string.Empty); model.jo3 = tds[2].InnerText.Replace("元", string.Empty).Replace("--", "0").Replace(",", "").Trim(); } else if (tds[0].InnerText == "四等奖") { model.zj4 = tds[1].InnerText.Replace(",", string.Empty).Replace("注", string.Empty); model.jo4 = tds[2].InnerText.Replace("元", string.Empty).Replace("--", "0").Replace(",", "").Trim(); } else if (tds[0].InnerText == "五等奖") { model.zj5 = tds[1].InnerText.Replace(",", string.Empty).Replace("注", string.Empty); model.jo5 = tds[2].InnerText.Replace("元", string.Empty).Replace("--", "0").Replace(",", "").Trim(); }; var list = new List(); list.Add(new Winbonus() { item = "特等奖", wincount = model.zj, winmoney = model.jo }); 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 }); list.Add(new Winbonus() { item = "四等奖", wincount = model.zj4, winmoney = model.jo4 }); list.Add(new Winbonus() { item = "五等奖", wincount = model.zj5, winmoney = model.jo5 }); model.winbonus = JsonConvert.SerializeObject(list); } } var reg1 = new Regex(@"本省(区、市)销售额:([\s\S]*?)元"); var match1 = reg1.Match(htmlResource); if (match1.Success) { //2016年182期及以前期数 //Sales = Convert.ToDecimal(match1.Result("$1")); model.tzmoney = match1.Result("$1"); } else { //2016年183期及以后期数 reg1 = new Regex(@"本期销售金额:([\s\S]*?)元"); match1 = reg1.Match(htmlResource); if (match1.Success) model.tzmoney = match1.Result("$1"); } var ps = table[1].ParentNode.ChildNodes.Where(N => N.Name.ToLower() == "p").ToList(); var potString = ps.Last().InnerHtml; reg1 = new Regex(@"
([\s\S]*?)元"); match1 = reg1.Match(potString); if (match1.Success) { var potValue = match1.Result("$1").Replace(" ", string.Empty); if (potValue.Contains("
")) { while (potValue.IndexOf("
") > 0) { potValue = potValue.Substring(potValue.IndexOf("
") + 4); } model.nextmoney = potValue; }// Jackpot = Convert.ToDecimal(potValue.Substring(potValue.IndexOf("
") + 4)); else // Jackpot = Convert.ToDecimal(potValue); model.tzmoney = potValue; } } } catch (Exception ex) { log.Error(typeof(JSTC7WSJob), string.Format("【{0}】通过主站点优化开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } } /// /// 通过备用站点抓取开奖数据 /// (百度乐彩) /// 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 = Tcjs7wsData.GetLastOne(); var startQiNum = Convert.ToInt32(LatestItem.qi.ToString()); if (startQiNum > newestQiHao) return; //无最新数据 //处理最新开奖数据 Tcjs7wsLongInfo 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; Tcjs7wsData.Add(matchItem); //Do Success Log log.Info(typeof(JSTC7WSJob), CommonHelper.GetJobBackLogInfo(Config, i.ToString())); LatestItem = matchItem; isGetData = true; } } } } private List GetOpenListFromBackUrl() { var result = new List(); try { var url = Config.BackUrl; try { var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("utf-8")); 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(); Tcjs7wsLongInfo 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 < 14) continue; model = new Tcjs7wsLongInfo(); nodeA = tds[13].ChildNodes.Where(n => n.Name == "a").FirstOrDefault(); if (nodeA == null) continue; model.qi = Convert.ToInt32(tds[0].InnerText.Trim()); //model.DetailUrl = new Uri(url, optimizeUrl).AbsoluteUri; model.date = Convert.ToDateTime(tds[1].InnerText.Substring(0, 10)); if (tds[2].ChildNodes.Count == 0) continue; var opencodeNode = tds[2].ChildNodes.Where(n => n.Name.ToLower() == "em").ToList(); if (opencodeNode.Count < 6) 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()); model.n6 = Convert.ToInt32(opencodeNode[5].InnerText.Trim()); model.n7 = Convert.ToInt32(opencodeNode[6].InnerText.Trim()); OptimizeBackModel(ref model, trs[i]); 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; } catch (Exception ex) { log.Error(typeof(JSTC7WSJob), string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message)); } return result; } #region Attribute /// /// 配置信息 /// private SCCConfig Config; /// /// 当天抓取的最新一期开奖记录 /// private Tcjs7wsLongInfo LatestItem; #pragma warning disable CS0414 // 字段“JSTC7WSJob.FailedQiHaoList”已被赋值,但从未使用过它的值 /// /// 当天抓取失败列表 /// private List FailedQiHaoList = null; #pragma warning restore CS0414 // 字段“JSTC7WSJob.FailedQiHaoList”已被赋值,但从未使用过它的值 /// /// 日志对象 /// private readonly LogHelper log; /// /// 当前彩种 /// private SCCLottery currentLottery => SCCLottery.JiangSuTC7WS; /// /// 邮件接口 /// private IEmail email; #pragma warning disable CS0414 // 字段“JSTC7WSJob.isGetData”已被赋值,但从未使用过它的值 /// /// 是否本次运行抓取到开奖数据 /// private bool isGetData; #pragma warning restore CS0414 // 字段“JSTC7WSJob.isGetData”已被赋值,但从未使用过它的值 #endregion } }