JS11X5Job.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Quartz;
  7. using SCC.Common;
  8. using SCC.Models;
  9. using SCC.Interface;
  10. using HtmlAgilityPack;
  11. using Newtonsoft.Json;
  12. using Newtonsoft.Json.Linq;
  13. namespace SCC.Crawler.GP
  14. {
  15. /// <summary>
  16. /// 数据爬取类
  17. /// 江苏11选5
  18. /// </summary>
  19. [DisallowConcurrentExecution]
  20. [PersistJobDataAfterExecution]
  21. public class JS11X5Job : IJob
  22. {
  23. /// <summary>
  24. /// 构造函数
  25. /// </summary>
  26. public JS11X5Job()
  27. {
  28. log = new LogHelper();
  29. services = IOC.Resolve<IOpen5Code>();
  30. email = IOC.Resolve<IEmail>();
  31. }
  32. /// <summary>
  33. /// 作业执行入口
  34. /// </summary>
  35. /// <param name="context">作业执行上下文</param>
  36. public void Execute(IJobExecutionContext context)
  37. {
  38. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  39. //预设节假日不开奖
  40. if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
  41. LatestQiHao = context.JobDetail.JobDataMap.GetString("LatestQiHao");
  42. try
  43. {
  44. //服务启动时配置初始数据
  45. if (string.IsNullOrEmpty(LatestQiHao))
  46. {
  47. var lastItem = services.GetLastItem(currentLottery);
  48. if (lastItem != null)
  49. {
  50. LatestQiHao = lastItem.Term.ToString();
  51. }
  52. }
  53. //第一次启动服务或最新期号为昨天的开奖期号,则自检昨天开奖数据是否抓取完毕(否则插入邮件数据),并重置当天期号和失败列表
  54. if (string.IsNullOrEmpty(LatestQiHao) || !LatestQiHao.StartsWith(CommonHelper.SCCSysDateTime.ToString("yyMMdd")))
  55. {
  56. CheckingYesterdayTheLotteryData();
  57. LatestQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(0);
  58. }
  59. //当最新期号不符合当天总期数,执行当天作业
  60. if (Convert.ToInt32(LatestQiHao.Substring(6)) != Config.TimesPerDay)
  61. {
  62. DoTodayJobByMainUrl();
  63. DoTodayJobByBackUrl();
  64. }
  65. }
  66. catch (Exception ex)
  67. {
  68. log.Error(typeof(JS11X5Job), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
  69. }
  70. //保存最新期号和失败期号列表
  71. context.JobDetail.JobDataMap["LatestQiHao"] = LatestQiHao;
  72. }
  73. /// <summary>
  74. /// 自检昨天开奖数据
  75. /// </summary>
  76. private void CheckingYesterdayTheLotteryData()
  77. {
  78. if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd"))) return;//如果昨日设定不开奖则不自检昨日开奖数据
  79. //从数据库中获取昨天数据抓取失败列表
  80. FailedQiHaoList = services.GetYesterdayFailQQList(currentLottery, Config.TimesPerDay);
  81. if (FailedQiHaoList.Count > 0)
  82. {
  83. DoYesterdayFailedListByMainUrl();
  84. DoYesterdayFailedListByBackUrl();
  85. foreach (var fQiHao in FailedQiHaoList)
  86. {
  87. //将抓取失败数据推送至邮件列表,待邮件服务发送至配置管理员的邮箱中
  88. if (email.AddEmail(Config.Area + Config.LotteryName, fQiHao, CommonHelper.GenerateYesterdayOpenTime(Config, fQiHao)))
  89. log.Error(typeof(JS11X5Job), CommonHelper.GetJobLogError(Config, fQiHao));
  90. }
  91. }
  92. }
  93. /// <summary>
  94. /// 通过主站点抓取开奖数据
  95. /// (江苏体彩网)
  96. /// </summary>
  97. private void DoTodayJobByMainUrl()
  98. {
  99. if (!string.IsNullOrEmpty(Config.MainUrl))
  100. {
  101. var OpenList = GetOpenListFromMainUrl();
  102. if (OpenList.Count == 0) return;//无抓取数据
  103. var newestQiHao = OpenList.Last().Key;
  104. var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
  105. var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
  106. if (startQiNum > newestQiNum) return;//无最新数据
  107. //处理最新开奖数据
  108. string getQiHao = string.Empty;
  109. for (var i = startQiNum; i <= newestQiNum; i++)
  110. {
  111. getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(i);
  112. var matchItem = OpenList.Where(R => R.Key == getQiHao).FirstOrDefault();
  113. if (matchItem.Key != null && SaveRecord(getQiHao, matchItem.Value, false))
  114. {
  115. //处理成功写入日志
  116. log.Info(typeof(JS11X5Job), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
  117. LatestQiHao = getQiHao;
  118. }
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// 通过主站抓取错误期号列表中每一个期号
  124. /// (江苏体彩网)
  125. /// </summary>
  126. private void DoYesterdayFailedListByMainUrl()
  127. {
  128. if (!string.IsNullOrEmpty(Config.MainUrl) && FailedQiHaoList.Count > 0)
  129. {
  130. var OpenList = GetOpenListFromMainUrl();
  131. if (OpenList.Count == 0) return;//无抓取数据
  132. var SuccessList = new List<string>();
  133. foreach (string failedQiHao in FailedQiHaoList)
  134. {
  135. var matchItem = OpenList.Where(R => R.Key == failedQiHao).FirstOrDefault();
  136. if (matchItem.Key != null && SaveRecord(failedQiHao, matchItem.Value, true))
  137. {
  138. //处理成功写入日志
  139. log.Info(typeof(JS11X5Job), CommonHelper.GetJobMainLogInfo(Config, failedQiHao));
  140. SuccessList.Add(failedQiHao);
  141. continue;
  142. }
  143. }
  144. foreach (var successQiHao in SuccessList)
  145. {
  146. FailedQiHaoList.Remove(successQiHao);
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 抓取主站点开奖数据
  152. /// </summary>
  153. /// <param name="url">主站点</param>
  154. /// <returns></returns>
  155. private Dictionary<string, string> GetOpenListFromMainUrl()
  156. {
  157. Dictionary<string, string> result = new Dictionary<string, string>();
  158. try
  159. {
  160. var HtmlResource = NetHelper.GetUrlResponse(Config.MainUrl);
  161. if (string.IsNullOrWhiteSpace(HtmlResource)) return result;
  162. Regex reg = new Regex(@"<table[\s\S]*?table>", RegexOptions.IgnoreCase);
  163. MatchCollection mc = reg.Matches(HtmlResource);
  164. string matchQiHao = string.Empty;
  165. string matchKJHaoMa = string.Empty;
  166. if (mc.Count == 2 && mc[1].Success)
  167. {
  168. var kjTable = mc[1].Value.Replace("\\", "");
  169. if (string.IsNullOrWhiteSpace(kjTable)) return result;
  170. HtmlDocument doc = new HtmlDocument();
  171. doc.LoadHtml(kjTable);
  172. var table = doc.DocumentNode.SelectSingleNode("//table");
  173. if (table == null) return result;
  174. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  175. for (var i = 2; i < trs.Count; i++)//第一行是表头
  176. {
  177. var trstyle = trs[i].Attributes["style"];
  178. if (trstyle != null && trstyle.Value == "display:none")
  179. {
  180. continue;
  181. }
  182. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  183. if (tds.Count < 3) continue;
  184. matchQiHao = tds[1].InnerText.Trim();
  185. matchKJHaoMa = tds[2].InnerText.Trim().Replace("&nbsp;", ",");
  186. if (matchQiHao == "期号" && matchKJHaoMa == "开奖号") break;
  187. result.Add(matchQiHao, matchKJHaoMa);
  188. }
  189. }
  190. }
  191. catch (Exception ex)
  192. {
  193. log.Error(typeof(JS11X5Job), string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
  194. }
  195. return result;
  196. }
  197. /// <summary>
  198. /// 通过备用站点抓取开奖数据
  199. /// (爱彩乐)
  200. /// </summary>
  201. private void DoTodayJobByBackUrl()
  202. {
  203. if (!string.IsNullOrEmpty(Config.BackUrl))
  204. {
  205. var OpenList = GetTodayOpenListFromBackUrl();
  206. if (OpenList.Count == 0) return;//无抓取数据
  207. var newestQiHao = OpenList.First().Key;
  208. var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
  209. var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
  210. if (startQiNum > newestQiNum) return;//无最新数据
  211. //处理最新开奖数据
  212. var getQiHao = string.Empty;
  213. for (var i = startQiNum; i <= newestQiNum; i++)
  214. {
  215. getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(i);
  216. var matchItem = OpenList.Where(R => R.Key == getQiHao).FirstOrDefault();
  217. if (matchItem.Key != null && SaveRecord(getQiHao, matchItem.Value, false))
  218. {
  219. //处理成功写入日志
  220. log.Info(typeof(JS11X5Job), CommonHelper.GetJobBackLogInfo(Config, getQiHao));
  221. LatestQiHao = getQiHao;
  222. }
  223. }
  224. }
  225. }
  226. /// <summary>
  227. /// 通过备用地址抓取错误期号列表中每一个期号
  228. /// (爱彩乐)
  229. /// </summary>
  230. private void DoYesterdayFailedListByBackUrl()
  231. {
  232. if (!string.IsNullOrEmpty(Config.BackUrl) && FailedQiHaoList.Count > 0)
  233. {
  234. var OpenList = GetYesterdayOpenListFromBackUrl();
  235. if (OpenList.Count == 0) return;//无抓取数据
  236. var SuccessList = new List<string>();
  237. foreach (var failedQiHao in FailedQiHaoList)
  238. {
  239. var matchItem = OpenList.Where(R => R.Key == failedQiHao).FirstOrDefault();
  240. if (matchItem.Key != null && SaveRecord(failedQiHao, matchItem.Value, true))
  241. {
  242. //处理成功写入日志
  243. log.Info(typeof(JS11X5Job), CommonHelper.GetJobBackLogInfo(Config, failedQiHao));
  244. SuccessList.Add(failedQiHao);
  245. continue;
  246. }
  247. }
  248. foreach (var successQiHao in SuccessList)
  249. {
  250. FailedQiHaoList.Remove(successQiHao);
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// 抓取备用站点开奖数据
  256. /// </summary>
  257. /// <returns></returns>
  258. private Dictionary<string, string> GetTodayOpenListFromBackUrl()
  259. {
  260. Dictionary<string, string> result = new Dictionary<string, string>();
  261. try
  262. {
  263. var HtmlResource = NetHelper.GetUrlResponse(Config.BackUrl);
  264. if (!string.IsNullOrWhiteSpace(HtmlResource))
  265. {
  266. HtmlDocument doc = new HtmlDocument();
  267. doc.LoadHtml(HtmlResource);
  268. var table = doc.DocumentNode.SelectSingleNode("//table");
  269. if (table == null) return result;
  270. var trs = table.ChildNodes.Where(R => R.Name.ToLower() == "tr").ToList();
  271. List<HtmlNode> tds = null, ems = null;
  272. var matchQiHao = string.Empty;
  273. var matchKJHaoMa = string.Empty;
  274. for (var i = 1; i < trs.Count; i++)//第一行为表头
  275. {
  276. tds = trs[i].ChildNodes.Where(R => R.Name.ToLower() == "td").ToList();
  277. if (tds.Count < 3) continue;
  278. matchQiHao = tds[0].InnerText.Trim();
  279. ems = tds[2].ChildNodes.Where(R => R.Name.ToLower() == "em").ToList();
  280. if (ems.Count < 5) continue;
  281. 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());
  282. if (!result.ContainsKey(matchQiHao))
  283. result.Add(matchQiHao, matchKJHaoMa);
  284. }
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. log.Error(typeof(JS11X5Job), string.Format("【{0}】通过备用站点抓取今日最新开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
  290. }
  291. return result;
  292. }
  293. /// <summary>
  294. /// 通过备用站点抓取昨日开奖列表
  295. /// </summary>
  296. /// <returns></returns>
  297. private Dictionary<string, string> GetYesterdayOpenListFromBackUrl()
  298. {
  299. Dictionary<string, string> result = new Dictionary<string, string>();
  300. try
  301. {
  302. var HtmlResource = NetHelper.GetUrlResponse(Config.BackUrl + "?action=chart&date=yesterday&id=506&async=true");
  303. if (!string.IsNullOrWhiteSpace(HtmlResource))
  304. {
  305. var obj = JsonConvert.DeserializeObject<JObject>(HtmlResource);
  306. if (obj != null && obj["data"] != null)
  307. {
  308. var matchQiHao = string.Empty;
  309. var matchKJHaoMa = string.Empty;
  310. JArray openCodeList = null;
  311. foreach (var item in obj["data"])
  312. {
  313. matchQiHao = item["dateNumber"].ToString();
  314. openCodeList = (JArray)item["list"];
  315. matchKJHaoMa = string.Format("{0},{1},{2},{3},{4}", openCodeList[0].ToString(), openCodeList[1].ToString(), openCodeList[2].ToString(), openCodeList[3].ToString(), openCodeList[4].ToString());
  316. if (!result.ContainsKey(matchQiHao))
  317. result.Add(matchQiHao, matchKJHaoMa);
  318. }
  319. }
  320. }
  321. }
  322. catch (Exception ex)
  323. {
  324. log.Error(typeof(JS11X5Job), string.Format("【{0}】通过备用站点抓取昨日开奖列表时发生错误,错误信息【{1}】", Config.Area + Config.LotteryName, ex.Message));
  325. }
  326. return result;
  327. }
  328. /// <summary>
  329. /// 将此彩种指定期号和开奖号码保存至数据库
  330. /// </summary>
  331. /// <param name="QiHao">期号</param>
  332. /// <param name="OpenCode">开奖号码(形如01,02,03,04,05)</param>
  333. /// <param name="IsYesterdayRecord">是否是保存昨天的记录</param>
  334. /// <returns></returns>
  335. private bool SaveRecord(string QiHao, string OpenCode, bool IsYesterdayRecord)
  336. {
  337. if (!string.IsNullOrWhiteSpace(QiHao) && !string.IsNullOrWhiteSpace(OpenCode))
  338. {
  339. OpenCode5Model model = new OpenCode5Model();
  340. model.Term = Convert.ToInt64(QiHao);
  341. var haoMaArray = OpenCode.Split(new char[] { ',' });
  342. model.OpenCode1 = Convert.ToInt32(haoMaArray[0]);
  343. model.OpenCode2 = Convert.ToInt32(haoMaArray[1]);
  344. model.OpenCode3 = Convert.ToInt32(haoMaArray[2]);
  345. model.OpenCode4 = Convert.ToInt32(haoMaArray[3]);
  346. model.OpenCode5 = Convert.ToInt32(haoMaArray[4]);
  347. if (IsYesterdayRecord)
  348. model.OpenTime = CommonHelper.GenerateYesterdayOpenTime(Config, QiHao);
  349. else
  350. model.OpenTime = CommonHelper.GenerateTodayOpenTime(Config, QiHao);
  351. return services.AddOpen5Code(currentLottery, model);
  352. }
  353. return false;
  354. }
  355. #region Attribute
  356. /// <summary>
  357. /// 配置信息
  358. /// </summary>
  359. private SCCConfig Config = null;
  360. /// <summary>
  361. /// 当天抓取的最新一期期号
  362. /// </summary>
  363. private string LatestQiHao = null;
  364. /// <summary>
  365. /// 当天抓取失败列表
  366. /// </summary>
  367. private List<string> FailedQiHaoList = null;
  368. /// <summary>
  369. /// 日志对象
  370. /// </summary>
  371. private LogHelper log = null;
  372. /// <summary>
  373. /// 数据服务
  374. /// </summary>
  375. private IOpen5Code services = null;
  376. /// <summary>
  377. /// 当前彩种
  378. /// </summary>
  379. private SCCLottery currentLottery
  380. {
  381. get
  382. {
  383. return SCCLottery.JiangSu11X5;
  384. }
  385. }
  386. /// <summary>
  387. /// 邮件接口
  388. /// </summary>
  389. private IEmail email = null;
  390. #endregion
  391. }
  392. }