using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Quartz; using SCC.Common; using SCC.Models; using SCC.Interface; using Newtonsoft.Json; using HtmlAgilityPack; using Newtonsoft.Json.Linq; namespace SCC.Crawler.DT { /// /// 数据爬取类 /// 广东好彩1 /// [DisallowConcurrentExecution] [PersistJobDataAfterExecution] public class GDHC1Job : IJob { /// /// 构造函数 /// public GDHC1Job() { log = new LogHelper(); services = IOC.Resolve(); 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 OpenCode1DTModel; try { //服务启动时配置初始数据 if (LatestItem == null) { LatestItem = services.GetOpenCode1DTLastItem(currentLottery); if (LatestItem == null) { //服务第一次启动,数据库一条数据都没有时 LatestItem = new OpenCode1DTModel() { Term = CommonHelper.GenerateQiHaoYYYYQQQ(0), OpenTime = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1) }; } } //程序时间第二天,程序根据配置检查是否昨天有开奖 isGetData = false; if (CommonHelper.CheckDTIsNeedGetData(Config)) { CheckingOpenDayTheLotteryData(); } if (!LatestItem.Term.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yyyy"))) { //翻年时 LatestItem = new OpenCode1DTModel() { Term = CommonHelper.GenerateQiHaoYYYYQQQ(0), OpenTime = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1) }; } //当今日开奖并且当前时间是晚上8点过后开始抓取 if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12) { DoTodayJobByMainUrl(); DoTodayJobByBackUrl(); } if (isGetData) TrendChartHelper.GenerateGDHC1TrendChart(log); } catch (Exception ex) { log.Error(typeof(GDHC1Job), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message)); } //保存最新期号 context.JobDetail.JobDataMap["LatestItem"] = LatestItem; } /// /// 自检爬取未爬取到的开奖数据,并对昨日开奖但未爬取到开奖数据的彩种添加邮件提醒 /// private void CheckingOpenDayTheLotteryData() { //从数据库中获取昨天数据抓取失败列表 FailedQiHaoList = services.GetFailedYYYYQQQList(currentLottery); if (FailedQiHaoList.Count > 0) { DoYesterdayFailedListByMainUrl(); DoYesterdayFailedListByBackUrl(); foreach (var fQiHao in FailedQiHaoList) { //将抓取失败数据推送至邮件列表,待邮件服务发送至配置管理员的邮箱中 if (email.AddEmail(Config.Area + Config.LotteryName, fQiHao, CommonHelper.GenerateDTOpenTime(Config))) log.Error(typeof(GDHC1Job), CommonHelper.GetJobLogError(Config, fQiHao)); } } if (LatestItem.OpenTime.ToString("yyyyMMdd") != CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd")) { //开奖时间(昨天)未抓取到最新开奖数据,则再抓取一次,若还不成功则写入待发送邮件列表 DoTodayJobByMainUrl(); DoTodayJobByBackUrl(); if (LatestItem.OpenTime.ToString("yyyyMMdd") != CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd")) { var openQiHao = (LatestItem.Term + 1).ToString(); if (email.AddEmail(Config.Area + Config.LotteryName, openQiHao, CommonHelper.GenerateDTOpenTime(Config))) log.Error(typeof(GDHC1Job), CommonHelper.GetJobLogError(Config, openQiHao)); } } } /// /// 通过主站点爬取开奖数据 /// (百度乐彩) /// private void DoTodayJobByMainUrl() { if (!string.IsNullOrEmpty(Config.MainUrl)) { var OpenList = GetOpenListFromMainUrl(Config.MainUrl); if (OpenList.Count == 0) return;//无抓取数据 var newestQiHao = OpenList.First().Term.ToString(); var startQiNum = Convert.ToInt32(LatestItem.Term.ToString().Substring(4)) + 1; var newestQiNum = Convert.ToInt32(newestQiHao.Substring(4)); if (startQiNum > newestQiNum) return;//无最新数据 //处理最新开奖数据 string getQiHao = string.Empty; OpenCode1DTModel matchItem = null; for (var i = startQiNum; i <= newestQiNum; i++) { getQiHao = LatestItem.Term.ToString().Substring(0, 4) + i.ToString().PadLeft(3, '0'); matchItem = OpenList.Where(R => R.Term.ToString() == getQiHao).FirstOrDefault(); if (matchItem != null && OptimizeMainModel(ref matchItem) && services.AddDTOpen1Code(currentLottery, matchItem)) { //Do Success Log log.Info(typeof(GDHC1Job), CommonHelper.GetJobMainLogInfo(Config, getQiHao)); LatestItem = matchItem; isGetData = true; } } } } /// /// 通过主站爬取错误期号列表中每一个期号 /// (百度乐彩) /// private void DoYesterdayFailedListByMainUrl() { if (!string.IsNullOrEmpty(Config.MainUrl) && FailedQiHaoList.Count > 0) { var OpenList = GetOpenListFromMainUrl(Config.MainUrl); if (OpenList.Count == 0) return;//无抓取数据 OpenCode1DTModel matchItem = null; var SuccessList = new List(); foreach (string failedQiHao in FailedQiHaoList) { matchItem = OpenList.Where(R => R.Term.ToString() == failedQiHao).FirstOrDefault(); if (matchItem != null && OptimizeMainModel(ref matchItem) && services.AddDTOpen1Code(currentLottery, matchItem)) { //Do Success Log log.Info(typeof(GDHC1Job), CommonHelper.GetJobMainLogInfo(Config, failedQiHao)); if (matchItem.Term > LatestItem.Term) { LatestItem = matchItem; } isGetData = true; SuccessList.Add(failedQiHao); } } foreach (var successQiHao in SuccessList) { FailedQiHaoList.Remove(successQiHao); } } } /// /// 获取主站开奖列表数据 /// /// 主站地址 /// private List GetOpenListFromMainUrl(string mainUrl) { List result = new List(); try { string requestUrl = string.Format("{0}?r={1}", mainUrl, new Random().Next(1000, 9999)); var htmlResource = NetHelper.GetBaiDuLeCaiResponse(requestUrl); if (string.IsNullOrWhiteSpace(htmlResource)) return result; HtmlDocument 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(); OpenCode1DTModel model = null; HtmlNode nodeA = null, nodeOpenCode = null; for (var i = 0; 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 < 4) continue; model = new OpenCode1DTModel(); model.OpenTime = Convert.ToDateTime(tds[0].InnerText); nodeA = tds[1].ChildNodes.Where(n => n.Name == "a").FirstOrDefault(); if (nodeA == null) continue; model.Term = Convert.ToInt64(nodeA.InnerText.Trim()); if (tds[2].ChildNodes.Count == 0) continue; nodeOpenCode = tds[2].ChildNodes[0].ChildNodes.Where(n => n.Name == "span").FirstOrDefault(); if (nodeOpenCode == null) continue; model.OpenCode1 = Convert.ToInt32(nodeOpenCode.InnerText.Trim()); result.Add(model); } result = result.OrderByDescending(S => S.Term).ToList(); } catch (Exception ex) { log.Error(typeof(GDHC1Job), string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message)); } return result; } /// /// 完善主站江苏体彩7位数开奖详情信息 /// /// private bool OptimizeMainModel(ref OpenCode1DTModel model) { var url = string.Format("http://baidu.lecai.com/lottery/draw/view/555/{0}?r={1}", model.Term, new Random().Next(1000, 9999)); try { var htmlResource = NetHelper.GetBaiDuLeCaiResponse(url); Regex reg = new Regex(@"var phaseData = ([\s\S]*?);"); Match m = reg.Match(htmlResource); if (m.Success) { var dataJson = m.Result("$1"); var obj = JsonConvert.DeserializeObject(dataJson); var data = obj[model.Term.ToString()]; if (data != null) { int Level1Num = 0, Level2Num = 0, Level3Num = 0, Level4Num = 0; decimal Level1Money = 0, Level2Money = 0, Level3Money = 0, Level4Money = 0, Sales = 0, Jackpot = 0; Jackpot = Convert.ToDecimal(data["formatPoolAmount"]); Sales = Convert.ToDecimal(data["formatSaleAmount"]); Level1Num = Convert.ToInt32(data["list"]["prize1"]["bet"].Value.Replace(",", string.Empty).Replace("注", string.Empty)); Level1Money = Convert.ToDecimal(data["list"]["prize1"]["prize"].Value.Replace(",", string.Empty).Replace("元", string.Empty)); Level2Num = Convert.ToInt32(data["list"]["prize2"]["bet"].Value.Replace(",", string.Empty).Replace("注", string.Empty)); Level2Money = Convert.ToDecimal(data["list"]["prize2"]["prize"].Value.Replace(",", string.Empty).Replace("元", string.Empty)); Level3Num = Convert.ToInt32(data["list"]["prize3"]["bet"].Value.Replace(",", string.Empty).Replace("注", string.Empty)); Level3Money = Convert.ToDecimal(data["list"]["prize3"]["prize"].Value.Replace(",", string.Empty).Replace("元", string.Empty)); Level4Num = Convert.ToInt32(data["list"]["prize4"]["bet"].Value.Replace(",", string.Empty).Replace("注", string.Empty)); Level4Money = Convert.ToDecimal(data["list"]["prize4"]["prize"].Value.Replace(",", string.Empty).Replace("元", string.Empty)); model.Detail = string.Format("{0},{1}^数字|{2}|{3},生肖|{4}|{5},季节|{6}|{7},方位|{8}|{9}", Sales, Jackpot, Level1Num, Level1Money, Level2Num, Level2Money, Level4Num, Level4Money, Level3Num, Level3Money);//百度是把方位放在季节前显示滴 } return true; } } catch (Exception ex) { log.Error(typeof(GDHC1Job), string.Format("【{0}】通过主站点优化开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message)); } return false; } /// /// 通过备用站点抓取开奖数据 /// (广东省福利彩票发行中心) /// private void DoTodayJobByBackUrl() { if (!string.IsNullOrEmpty(Config.BackUrl)) { var OpenList = GetOpenListFromBackUrl(Config.BackUrl); if (OpenList.Count == 0) return;//无抓取数据 var newestQiHao = OpenList.First().Term.ToString(); var startQiNum = Convert.ToInt32(LatestItem.Term.ToString().Substring(4)) + 1; var newestQiNum = Convert.ToInt32(newestQiHao.Substring(4)); if (startQiNum > newestQiNum) return;//无最新数据 //处理最新开奖数据 var getQiHao = string.Empty; OpenCode1DTModel matchItem = null; for (var i = startQiNum; i <= newestQiNum; i++) { getQiHao = LatestItem.Term.ToString().Substring(0, 4) + i.ToString().PadLeft(3, '0'); matchItem = OpenList.Where(R => R.Term.ToString() == getQiHao).FirstOrDefault(); if (matchItem != null && OptimizeBackModel(ref matchItem) && services.AddDTOpen1Code(currentLottery, matchItem)) { //Do Success Log log.Info(typeof(GDHC1Job), CommonHelper.GetJobBackLogInfo(Config, getQiHao)); LatestItem = matchItem; isGetData = true; } } } } /// /// 通过备用地址抓取错误期号列表中每一个期号 /// (广东省福利彩票发行中心) /// private void DoYesterdayFailedListByBackUrl() { if (!string.IsNullOrEmpty(Config.BackUrl) && FailedQiHaoList.Count > 0) { var OpenList = GetOpenListFromBackUrl(Config.BackUrl); if (OpenList.Count == 0) return;//无抓取数据 OpenCode1DTModel matchItem = null; var SuccessList = new List(); foreach (string failedQiHao in FailedQiHaoList) { matchItem = OpenList.Where(R => R.Term.ToString() == failedQiHao).FirstOrDefault(); if (matchItem != null && OptimizeBackModel(ref matchItem) && services.AddDTOpen1Code(currentLottery, matchItem)) { //Do Success Log log.Info(typeof(GDHC1Job), CommonHelper.GetJobBackLogInfo(Config, failedQiHao)); if (matchItem.Term > LatestItem.Term) { LatestItem = matchItem; } SuccessList.Add(failedQiHao); isGetData = true; } } foreach (var successQiHao in SuccessList) { FailedQiHaoList.Remove(successQiHao); } } } /// /// 获取备用站点开奖列表数据 /// /// 备用站点 /// private List GetOpenListFromBackUrl(string backUrl) { List result = new List(); try { Uri resourceUrl = new Uri(backUrl); var htmlResource = NetHelper.GetUrlResponse(resourceUrl.AbsoluteUri, Encoding.GetEncoding("gb2312")); if (string.IsNullOrWhiteSpace(htmlResource)) return result; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlResource); var table = doc.DocumentNode.SelectSingleNode("//table"); if (table == null) return result; var trs = table.ChildNodes.Where(C => C.Name == "tr").ToList(); var lastYear = (DateTime.Now.Year - 1).ToString(); List tds = null; HtmlNode tagA = null; OpenCode1DTModel model = null; string openCodes = null; string[] openList = null; for (var i = 1; i < trs.Count; i++)//第一行为表头 { tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList(); if (tds.Count < 3) continue; model = new OpenCode1DTModel(); if (tds[0].InnerText.Trim().StartsWith(lastYear)) { break; } model.Term = Convert.ToInt32(tds[0].InnerText); openCodes = tds[1].Attributes["luckyNo"].Value; if (string.IsNullOrWhiteSpace(openCodes)) continue; openList = openCodes.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (openList.Length < 4) continue; model.OpenCode1 = Convert.ToInt32(openList[0]); tagA = tds[2].ChildNodes.Where(N => N.Name.ToLower() == "a").FirstOrDefault(); if (tagA == null) continue; model.DetailUrl = new Uri(resourceUrl, tagA.Attributes["href"].Value).AbsoluteUri; result.Add(model); } result = result.OrderByDescending(S => S.Term).ToList(); } catch (Exception ex) { log.Error(typeof(GDHC1Job), string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message)); } return result; } /// /// 完善备用站点广东36选7开奖实体信息 /// /// private bool OptimizeBackModel(ref OpenCode1DTModel model) { try { var htmlResource = NetHelper.GetUrlResponse(model.DetailUrl, Encoding.GetEncoding("gb2312")); if (!string.IsNullOrEmpty(htmlResource)) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(htmlResource); int Level1Num = 0, Level2Num = 0, Level3Num = 0, Level4Num = 0; decimal Level1Money = 0, Level2Money = 0, Level3Money = 0, Level4Money = 0, Sales = 0, Jackpot = 0; var divOpenTime = doc.DocumentNode.SelectNodes("//div[@class='play_R_tbox']").FirstOrDefault(); if (divOpenTime == null || divOpenTime.InnerText.Trim().Length < 10) return false; model.OpenTime = Convert.ToDateTime(divOpenTime.InnerText.Trim().Substring(0, 10)); Sales = Convert.ToDecimal(doc.GetElementbyId("box-guangdong").ChildNodes[1].InnerText.Replace("¥", "").Trim()); Jackpot = Convert.ToDecimal(doc.GetElementbyId("box-rollNext").ChildNodes[1].InnerText.Replace("¥", "").Trim()); var tables = doc.DocumentNode.SelectNodes("//table"); if (tables.Count < 2) return false; var table2Trs = tables[1].ChildNodes.Where(S => S.Name.ToLower() == "tr").ToList(); List tds = null; string lotteryLevel = string.Empty; for (var i = 2; i < table2Trs.Count; i++)//第一二行为表头 { tds = table2Trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList(); if (tds.Count < 3) continue; lotteryLevel = tds[0].InnerText.Trim(); if (tds[0].InnerText == "数字") { Level1Num = Convert.ToInt32(tds[1].InnerText); Level1Money = Convert.ToDecimal(tds[2].InnerText.Replace("¥", "").Trim()); } else if (tds[0].InnerText == "生肖") { Level2Num = Convert.ToInt32(tds[1].InnerText); Level2Money = Convert.ToDecimal(tds[2].InnerText.Replace("¥", "").Trim()); } else if (tds[0].InnerText == "季节") { Level3Num = Convert.ToInt32(tds[1].InnerText); Level3Money = Convert.ToDecimal(tds[2].InnerText.Replace("¥", "").Trim()); } else if (tds[0].InnerText == "方位") { Level4Num = Convert.ToInt32(tds[1].InnerText); Level4Money = Convert.ToDecimal(tds[2].InnerText.Replace("¥", "").Trim()); } } model.Detail = string.Format("{0},{1}^数字|{2}|{3},生肖|{4}|{5},季节|{6}|{7},方位|{8}|{9}", Sales, Jackpot, Level1Num, Level1Money, Level2Num, Level2Money, Level3Num, Level3Money, Level4Num, Level4Money); return true; } } catch (Exception ex) { log.Error(typeof(GDHC1Job), string.Format("【{0}】通过备用站点完善抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message)); } return false; } #region Attribute /// /// 配置信息 /// private SCCConfig Config = null; /// /// 当天抓取的最新一期开奖记录 /// private OpenCode1DTModel LatestItem = null; /// /// 当天抓取失败列表 /// private List FailedQiHaoList = null; /// /// 日志对象 /// private LogHelper log = null; /// /// 数据服务 /// private IDTOpenCode services = null; /// /// 当前彩种 /// private SCCLottery currentLottery { get { return SCCLottery.GDHC1; } } /// /// 邮件接口 /// private IEmail email = null; /// /// 是否本次运行抓取到开奖数据 /// private bool isGetData = false; #endregion } }