123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Quartz;
- using SCC.Common;
- using SCC.Models;
- using SCC.Interface;
- using HtmlAgilityPack;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- namespace SCC.Crawler.GP
- {
- /// <summary>
- /// 数据爬取类
- /// 湖北11选5
- /// </summary>
- [DisallowConcurrentExecution]
- [PersistJobDataAfterExecution]
- public class HUB11X5Job : IJob
- {
- /// <summary>
- /// 构造函数
- /// </summary>
- public HUB11X5Job()
- {
- log = new LogHelper();
- services = IOC.Resolve<IOpen5Code>();
- email = IOC.Resolve<IEmail>();
- }
- /// <summary>
- /// 作业执行入口
- /// </summary>
- /// <param name="context">作业执行上下文</param>
- public void Execute(IJobExecutionContext context)
- {
- Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
- //预设节假日不开奖
- if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
- LatestQiHao = context.JobDetail.JobDataMap.GetString("LatestQiHao");
- try
- {
- //服务启动时配置初始数据
- if (string.IsNullOrEmpty(LatestQiHao))
- {
- var lastItem = services.GetLastItem(currentLottery);
- if (lastItem != null)
- {
- LatestQiHao = lastItem.Term.ToString();
- }
- }
- //第一次启动服务或最新期号为昨天的开奖期号,则自检昨天开奖数据是否抓取完毕(否则插入邮件数据),并重置当天期号和失败列表
- if (string.IsNullOrEmpty(LatestQiHao) || !LatestQiHao.StartsWith(CommonHelper.SCCSysDateTime.ToString("yyMMdd")))
- {
- CheckingYesterdayTheLotteryData();
- LatestQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(0);
- }
- //当最新期号不符合当天总期数,执行当天作业
- if (Convert.ToInt32(LatestQiHao.Substring(6)) != Config.TimesPerDay)
- {
- DoTodayJobByDB();
- DoTodayJobByMainUrl();
- DoTodayJobByBackUrl();
- }
- }
- catch (Exception ex)
- {
- log.Error(typeof(HUB11X5Job), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
- }
- //保存最新期号和失败期号列表
- context.JobDetail.JobDataMap["LatestQiHao"] = LatestQiHao;
- }
- /// <summary>
- /// 通过数据库获取开奖数据
- /// </summary>
- private void DoTodayJobByDB()
- {
- var OpenList = services.GetYesterdayFailQQListDB5_toDay(currentLottery, Config.TimesPerDay);
- if (OpenList.Count == 0) return;//无抓取数据
- var newestQiHao = OpenList.OrderBy(a => a.Key).Last().Key;
- var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
- var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
- if (startQiNum > newestQiNum) return;//无最新数据
- //处理最新开奖数据
- string getQiHao = string.Empty;
- for (var i = startQiNum; i <= newestQiNum; i++)
- {
- getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(i);
- var matchItem = OpenList.Where(R => R.Key == getQiHao).FirstOrDefault();
- if (matchItem.Key != null && SaveRecord(getQiHao, matchItem.Value, false))
- {
- //处理成功写入日志
- log.Info(typeof(ZJ11X5Job), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
- LatestQiHao = getQiHao;
- }
- }
- }
- /// <summary>
- /// 自检昨天开奖数据
- /// </summary>
- private void CheckingYesterdayTheLotteryData()
- {
- if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd"))) return;//如果昨日设定不开奖则不自检昨日开奖数据
- //从数据库中获取昨天数据抓取失败列表
- FailedQiHaoList = services.GetYesterdayFailQQList(currentLottery, Config.TimesPerDay);
- if (FailedQiHaoList.Count > 0)
- {
- DoYesterdayFailedListByMainUrl();
- DoYesterdayFailedListByBackUrl();
- foreach (var fQiHao in FailedQiHaoList)
- {
- //将抓取失败数据推送至邮件列表,待邮件服务发送至配置管理员的邮箱中
- if (email.AddEmail(Config.Area + Config.LotteryName, fQiHao, CommonHelper.GenerateYesterdayOpenTime(Config, fQiHao)))
- log.Error(typeof(HUB11X5Job), CommonHelper.GetJobLogError(Config, fQiHao));
- }
- }
- }
- /// <summary>
- /// 通过主站点抓取开奖数据
- /// (湖北体彩官网)
- /// </summary>
- private void DoTodayJobByMainUrl()
- {
- if (!string.IsNullOrEmpty(Config.MainUrl))
- {
- var OpenList = GetOpenListFromMainUrl();
- if (OpenList.Count == 0) return;//无抓取数据
- var newestQiHao = OpenList.First().Key;
- var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
- var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
- if (startQiNum > newestQiNum) return;//无最新数据
- //处理最新开奖数据
- string getQiHao = string.Empty;
- for (var i = startQiNum; i <= newestQiNum; i++)
- {
- getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(i);
- var matchItem = OpenList.Where(R => R.Key == getQiHao).FirstOrDefault();
- if (matchItem.Key != null && SaveRecord(getQiHao, matchItem.Value, false))
- {
- //处理成功写入日志
- log.Info(typeof(HUB11X5Job), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
- LatestQiHao = getQiHao;
- }
- }
- //foreach (KeyValuePair<string, string> pair in OpenList)
- //{
- // if (pair.Key != null && SaveRecord(pair.Key, pair.Value, false))
- // {
- // 处理成功写入日志
- // log.Info(typeof(HUB11X5Job), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
- // LatestQiHao = pair.Key;
- // }
- //}
- }
- }
- /// <summary>
- /// 通过主站抓取错误期号列表中每一个期号
- /// (湖北体彩官网)
- /// </summary>
- private void DoYesterdayFailedListByMainUrl()
- {
- if (!string.IsNullOrEmpty(Config.MainUrl) && FailedQiHaoList.Count > 0)
- {
- var OpenList = GetOpenListFromMainUrl(false);
- if (OpenList.Count == 0) return;//无抓取数据
- var SuccessList = new List<string>();
- foreach (string failedQiHao in FailedQiHaoList)
- {
- var matchItem = OpenList.Where(R => R.Key == failedQiHao).FirstOrDefault();
- if (matchItem.Key != null && SaveRecord(failedQiHao, matchItem.Value, true))
- {
- //处理成功写入日志
- log.Info(typeof(HUB11X5Job), CommonHelper.GetJobMainLogInfo(Config, failedQiHao));
- SuccessList.Add(failedQiHao);
- continue;
- }
- }
- foreach (var successQiHao in SuccessList)
- {
- FailedQiHaoList.Remove(successQiHao);
- }
- }
- }
- /// <summary>
- /// 抓取主站点开奖数据
- /// </summary>
- /// <returns></returns>
- private Dictionary<string, string> GetOpenListFromMainUrl(bool isToday = true)
- {
- Dictionary<string, string> result = new Dictionary<string, string>();
- try
- {
- #region 废弃代码 BY 大师兄
- //string requestUrl = string.Empty;
- //for (var k = 1; k <= 2; k++)
- //{
- // requestUrl = string.Format("{0}&startIssue=&endIssue=&pageNo={1}&pageNum=81&format=jsonp&callback=?", Config.MainUrl, k);
- // var HtmlResource = NetHelper.GetHUB11X5UrlResponse(requestUrl);
- // if (!string.IsNullOrWhiteSpace(HtmlResource))
- // {
- // var obj = JsonConvert.DeserializeObject<JObject>(HtmlResource.Replace("?", string.Empty).Replace("(", string.Empty).Replace(")", string.Empty));
- // if (obj != null && obj["data"] != null && obj["data"]["numberList"] != null)
- // {
- // var matchQiHao = string.Empty;
- // var matchKJHaoMa = string.Empty;
- // foreach (JObject objchild in obj["data"]["numberList"])
- // {
- // if (objchild["issueNum"] == null || string.IsNullOrWhiteSpace(objchild["issueNum"].ToString()) || objchild["baseCode"] == null || string.IsNullOrWhiteSpace(objchild["baseCode"].ToString())) continue;
- // matchQiHao = objchild["issueNum"].ToString().Trim();
- // matchKJHaoMa = objchild["baseCode"].ToString().Trim();
- // if (!result.ContainsKey(matchQiHao))
- // result.Add(matchQiHao, matchKJHaoMa);
- // }
- // }
- // }
- //}
- #endregion
-
- var HtmlResource = NetHelper.GetUrlResponse(string.Format(Config.MainUrl, !isToday ? "/day/2" : "/num/50")); //Config.MainUrl
- if (HtmlResource == null) return null;
- var doc = new HtmlDocument();
- doc.LoadHtml(HtmlResource);
- var rootnode = doc.DocumentNode;
- var xpath = "//*[@id=\"ZstTable\"]/tbody/[@class=\"datainfo\"]";
- var xpath2 = "//*[@id=\"ZstTable\"]";
- var collection = rootnode.SelectNodes(xpath2);
- var data = collection[0].ChildNodes;
- for (var i = 0; i < data.Count; i++)
- {
- if (data[i].GetAttributeValue("class", " ") == "datainfo")
- {
- var periodnum = data[i].ChildNodes[0].InnerText.Trim();
- var code1 = data[i].ChildNodes[1].InnerText.Trim();
- var code2 = data[i].ChildNodes[2].InnerText.Trim();
- var code3 = data[i].ChildNodes[3].InnerText.Trim();
- var code4 = data[i].ChildNodes[4].InnerText.Trim();
- var code5 = data[i].ChildNodes[5].InnerText.Trim();
- result.Add(periodnum, string.Format("{0},{1},{2},{3},{4}", code1, code2,
- code3, code4, code5));
-
- }
- }
-
- }
- catch (Exception ex)
- {
- log.Error(typeof(HUB11X5Job), string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
- }
- return result;
- }
- /// <summary>
- /// 通过备用站点抓取开奖数据
- /// (爱彩乐)
- /// </summary>
- private void DoTodayJobByBackUrl()
- {
- if (!string.IsNullOrEmpty(Config.BackUrl))
- {
- var OpenList = GetTodayOpenListFromBackUrl();
- if (OpenList.Count == 0) return;//无抓取数据
- var newestQiHao = OpenList.Last().Key;
- var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
- var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
- if (startQiNum > newestQiNum) return;//无最新数据
- //处理最新开奖数据
- var getQiHao = string.Empty;
- for (var i = startQiNum; i <= newestQiNum; i++)
- {
- getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(i);
- var matchItem = OpenList.Where(R => R.Key == getQiHao).FirstOrDefault();
- if (matchItem.Key != null && SaveRecord(getQiHao, matchItem.Value, false))
- {
- //处理成功写入日志
- log.Info(typeof(HUB11X5Job), CommonHelper.GetJobBackLogInfo(Config, getQiHao));
- LatestQiHao = getQiHao;
- }
- }
- }
- }
- /// <summary>
- /// 通过备用地址抓取错误期号列表中每一个期号
- /// (爱彩乐)
- /// </summary>
- private void DoYesterdayFailedListByBackUrl()
- {
- if (!string.IsNullOrEmpty(Config.BackUrl) && FailedQiHaoList.Count > 0)
- {
- var OpenList = GetYesterdayOpenListFromBackUrl();
- if (OpenList.Count == 0) return;//无抓取数据
- var SuccessList = new List<string>();
- foreach (var failedQiHao in FailedQiHaoList)
- {
- var matchItem = OpenList.Where(R => R.Key == failedQiHao).FirstOrDefault();
- if (matchItem.Key != null && SaveRecord(failedQiHao, matchItem.Value, true))
- {
- //处理成功写入日志
- log.Info(typeof(HUB11X5Job), CommonHelper.GetJobBackLogInfo(Config, failedQiHao));
- SuccessList.Add(failedQiHao);
- continue;
- }
- }
- foreach (var successQiHao in SuccessList)
- {
- FailedQiHaoList.Remove(successQiHao);
- }
- }
- }
- /// <summary>
- /// 通过备用站点抓取今日最新开奖列表
- /// </summary>
- /// <returns></returns>
- private Dictionary<string, string> GetTodayOpenListFromBackUrl()
- {
- Dictionary<string, string> result = new Dictionary<string, string>();
- try
- {
- var HtmlResource = NetHelper.GetUrlResponse(Config.BackUrl);
- if (!string.IsNullOrWhiteSpace(HtmlResource))
- {
- HtmlDocument doc = new HtmlDocument();
- doc.LoadHtml(HtmlResource);
- var table = doc.DocumentNode.SelectSingleNode("//table");
- if (table == null) return result;
- var trs = table.ChildNodes.Where(R => R.Name.ToLower() == "tr").ToList();
- List<HtmlNode> tds = null, ems = null;
- var matchQiHao = string.Empty;
- var matchKJHaoMa = string.Empty;
- for (var i = 1; i < trs.Count; i++)//第一行为表头
- {
- tds = trs[i].ChildNodes.Where(R => R.Name.ToLower() == "td").ToList();
- if (tds.Count < 3) continue;
- matchQiHao = tds[0].InnerText.Trim();
- ems = tds[2].ChildNodes.Where(R => R.Name.ToLower() == "em").ToList();
- if (ems.Count < 5) continue;
- matchKJHaoMa = string.Format("{0},{1},{2},{3},{4}", ems[0].InnerText.Trim(), ems[1].InnerText.Trim(), ems[2].InnerText.Trim(), ems[3].InnerText.Trim(), ems[4].InnerText.Trim());
- if (!result.ContainsKey(matchQiHao))
- result.Add(matchQiHao, matchKJHaoMa);
- }
- }
- }
- catch (Exception ex)
- {
- log.Error(typeof(HUB11X5Job), string.Format("【{0}】通过备用站点抓取今日最新开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
- }
- return result;
- }
- /// <summary>
- /// 通过备用站点抓取昨日开奖列表
- /// </summary>
- /// <returns></returns>
- private Dictionary<string, string> GetYesterdayOpenListFromBackUrl()
- {
- Dictionary<string, string> result = new Dictionary<string, string>();
- try
- {
- var HtmlResource = NetHelper.GetUrlResponse(Config.BackUrl + "?action=chart&date=yesterday&id=520&async=true");
- if (!string.IsNullOrWhiteSpace(HtmlResource))
- {
- var obj = JsonConvert.DeserializeObject<JObject>(HtmlResource);
- if (obj != null && obj["data"] != null)
- {
- var matchQiHao = string.Empty;
- var matchKJHaoMa = string.Empty;
- JArray openCodeList = null;
- foreach (var item in obj["data"])
- {
- matchQiHao = item["dateNumber"].ToString();
- openCodeList = (JArray)item["list"];
- matchKJHaoMa = string.Format("{0},{1},{2},{3},{4}", openCodeList[0].ToString(), openCodeList[1].ToString(), openCodeList[2].ToString(), openCodeList[3].ToString(), openCodeList[4].ToString());
- if (!result.ContainsKey(matchQiHao))
- result.Add(matchQiHao, matchKJHaoMa);
- }
- }
- }
- }
- catch (Exception ex)
- {
- log.Error(typeof(HUB11X5Job), string.Format("【{0}】通过备用站点抓取昨日开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
- }
- return result;
- }
- /// <summary>
- /// 将此彩种指定期号和开奖号码保存至数据库
- /// </summary>
- /// <param name="QiHao">期号</param>
- /// <param name="OpenCode">开奖号码(形如01,02,03,04,05)</param>
- /// <param name="IsYesterdayRecord">是否是保存昨天的记录</param>
- /// <returns></returns>
- private bool SaveRecord(string QiHao, string OpenCode, bool IsYesterdayRecord)
- {
- if (!string.IsNullOrWhiteSpace(QiHao) && !string.IsNullOrWhiteSpace(OpenCode))
- {
- OpenCode5Model model = new OpenCode5Model();
- model.Term = Convert.ToInt64(QiHao);
- var haoMaArray = OpenCode.Split(new char[] { ',' });
- model.OpenCode1 = Convert.ToInt32(haoMaArray[0]);
- model.OpenCode2 = Convert.ToInt32(haoMaArray[1]);
- model.OpenCode3 = Convert.ToInt32(haoMaArray[2]);
- model.OpenCode4 = Convert.ToInt32(haoMaArray[3]);
- model.OpenCode5 = Convert.ToInt32(haoMaArray[4]);
- if (IsYesterdayRecord)
- model.OpenTime = CommonHelper.GenerateYesterdayOpenTime(Config, QiHao);
- else
- model.OpenTime = CommonHelper.GenerateTodayOpenTime(Config, QiHao);
- return services.AddOpen5Code(currentLottery, model);
- }
- return false;
- }
- #region Attribute
- /// <summary>
- /// 配置信息
- /// </summary>
- private SCCConfig Config = null;
- /// <summary>
- /// 当天抓取的最新一期期号
- /// </summary>
- private string LatestQiHao = null;
- /// <summary>
- /// 当天抓取失败列表
- /// </summary>
- private List<string> FailedQiHaoList = null;
- /// <summary>
- /// 日志对象
- /// </summary>
- private LogHelper log = null;
- /// <summary>
- /// 数据服务
- /// </summary>
- private IOpen5Code services = null;
- /// <summary>
- /// 当前彩种
- /// </summary>
- private SCCLottery currentLottery
- {
- get
- {
- return SCCLottery.HuBei11x5;
- }
- }
- /// <summary>
- /// 邮件接口
- /// </summary>
- private IEmail email = null;
- #endregion
- }
- }
|