HeBei20X5Job.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using CP.Model;
  6. using HtmlAgilityPack;
  7. using Newtonsoft.Json;
  8. using Quartz;
  9. using SCC.Common;
  10. using SCC.Crawler.Tools;
  11. using SCC.Interface;
  12. using SCC.Models;
  13. namespace SCC.Crawler.DT
  14. {
  15. /// <summary>
  16. /// 河北20选5
  17. /// </summary>
  18. [DisallowConcurrentExecution]
  19. [PersistJobDataAfterExecution]
  20. public class HeBei20X5Job : IJob
  21. {
  22. /// <summary>
  23. /// 构造函数
  24. /// </summary>
  25. public HeBei20X5Job()
  26. {
  27. log = new LogHelper();
  28. email = IOC.Resolve<IEmail>();
  29. }
  30. /// <summary>
  31. /// 作业执行入口
  32. /// </summary>
  33. /// <param name="context">作业执行上下文</param>
  34. public void Execute(IJobExecutionContext context)
  35. {
  36. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  37. //预设节假日不开奖
  38. if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
  39. LatestItem = context.JobDetail.JobDataMap["LatestItem"] as Fchebei20x5LongInfo;
  40. try
  41. {
  42. //服务启动时配置初始数据
  43. if (LatestItem == null)
  44. {
  45. LatestItem = new Fchebei20x5LongInfo
  46. {
  47. qi = CommonHelper.GenerateQiHaoYYYYQQQ(0),
  48. date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
  49. };
  50. }
  51. //程序时间第二天,程序根据配置检查是否昨天有开奖
  52. isGetData = false;
  53. if (CommonHelper.CheckDTIsNeedGetData(Config)) //
  54. {
  55. DoMainUrl();
  56. //DoBackUrl();
  57. }
  58. if (!LatestItem.qi.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy")))
  59. LatestItem = new Fchebei20x5LongInfo
  60. {
  61. qi = CommonHelper.GenerateQiHaoYYYYQQQ(0),
  62. date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
  63. };
  64. //当今日开奖并且当前时间是晚上8点过后开始抓取
  65. if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12)
  66. {
  67. DoMainUrl();
  68. //DoBackUrl();
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  74. }
  75. //保存最新期号
  76. context.JobDetail.JobDataMap["LatestItem"] = LatestItem;
  77. }
  78. #region 通过主站爬取数据
  79. /// <summary>
  80. /// 执行主站的数据插入
  81. /// </summary>
  82. private void DoMainUrl()
  83. {
  84. if (!string.IsNullOrEmpty(Config.MainUrl))
  85. {
  86. var openList = GetOpenListFromMainUrl(Config.MainUrl);
  87. if (openList == null || openList.Count == 0) return; //无抓取数据
  88. //抓取到的最新期数
  89. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  90. //数据库里面最新期数
  91. LatestItem = Fchebei20x5Data.GetLastOne();
  92. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  93. if (startQiNum > newestQiHao) return; //无最新数据
  94. //处理最新开奖数据
  95. Fchebei20x5LongInfo matchItem = null;
  96. for (var i = startQiNum; i <= newestQiHao; i++)
  97. {
  98. matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString());
  99. if (matchItem != null)
  100. {
  101. //add db
  102. matchItem.addtime = DateTime.Now;
  103. Fchebei20x5Data.Add(matchItem);
  104. //Do Success Log
  105. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString()));
  106. LatestItem = matchItem;
  107. isGetData = true;
  108. }
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 获取主站的数据列表
  114. /// </summary>
  115. /// <param name="mainUrl"></param>
  116. /// <returns></returns>
  117. private List<Fchebei20x5LongInfo> GetOpenListFromMainUrl(string mainUrl)
  118. {
  119. var result = new List<Fchebei20x5LongInfo>();
  120. try
  121. {
  122. var url = new Uri(mainUrl);
  123. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("gb2312"));
  124. if (htmlResource == null) return result;
  125. var doc = new HtmlDocument();
  126. doc.LoadHtml(htmlResource);
  127. var table = doc.DocumentNode.SelectSingleNode("//table");
  128. if (table == null) return result;
  129. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  130. Fchebei20x5LongInfo model = null;
  131. #pragma warning disable CS0219 // 变量“nodeA”已被赋值,但从未使用过它的值
  132. HtmlNode nodeA = null;
  133. #pragma warning restore CS0219 // 变量“nodeA”已被赋值,但从未使用过它的值
  134. var optimizeUrl = string.Empty;
  135. for (var i = 2; i < trs.Count; i++) //第一二行为表头
  136. {
  137. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  138. if (tds.Count < 4) continue;
  139. model = new Fchebei20x5LongInfo();
  140. //nodeA = tds[6].ChildNodes.Where(n => n.Name == "a").FirstOrDefault();
  141. //if (nodeA == null) continue;
  142. model.qi = Convert.ToInt32(tds[0].InnerText.Trim());
  143. //optimizeUrl = nodeA.Attributes["href"].Value;
  144. //model.DetailUrl = new Uri(url, optimizeUrl).AbsoluteUri;
  145. model.date = Convert.ToDateTime(tds[1].InnerText.Remove(10));
  146. var opencodeNode = tds[2].ChildNodes.Where(n => n.Name.ToLower() == "span").ToList();
  147. if (opencodeNode.Count < 5) continue;
  148. model.n1 = Convert.ToInt32(opencodeNode[0].InnerText.Trim());
  149. model.n2 = Convert.ToInt32(opencodeNode[1].InnerText.Trim());
  150. model.n3 = Convert.ToInt32(opencodeNode[2].InnerText.Trim());
  151. model.n4 = Convert.ToInt32(opencodeNode[3].InnerText.Trim());
  152. model.n5 = Convert.ToInt32(opencodeNode[4].InnerText.Trim());
  153. GetKaijiangDetails(ref model, model.qi.ToString());
  154. result.Add(model);
  155. }
  156. //var checkDataHelper = new CheckDataHelper();
  157. //var dbdata = services.GetListS<OpenCode5DTModel>(currentLottery)
  158. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  159. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  160. // Config.Area, currentLottery);
  161. //result = result.OrderByDescending(S => S.Term).ToList();
  162. }
  163. catch (Exception ex)
  164. {
  165. log.Error(GetType(),
  166. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  167. }
  168. return result;
  169. }
  170. /// <summary>
  171. /// 获取主站下开奖详情
  172. /// </summary>
  173. /// <param name="nodes"></param>
  174. /// <returns></returns>
  175. private void GetKaijiangDetails(ref Fchebei20x5LongInfo model, string qi)
  176. {
  177. string url = $"http://kaijiang.500.com/shtml/hbesxw/{qi}.shtml";
  178. var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("gb2312"));
  179. if (htmlResource == null) return;
  180. var list = new List<Winbonus>();
  181. var doc = new HtmlDocument();
  182. doc.LoadHtml(htmlResource);
  183. var tables = doc.DocumentNode.SelectNodes("//table");
  184. if (tables == null) return;
  185. foreach (var table in tables)
  186. {
  187. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  188. for (var i = 1; i < trs.Count; i++) //第一二行为表头
  189. {
  190. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  191. if (tds.Count != 3) continue;
  192. if (tds[0].InnerText.Trim() == "一等奖")
  193. {
  194. model.zj1 = tds[1].InnerText.Trim();
  195. model.jo1 = tds[2].InnerText.Trim();
  196. list.Add(new Winbonus()
  197. {
  198. item = "一等奖",
  199. wincount = model.zj1,
  200. winmoney = model.jo1
  201. });
  202. }
  203. if (tds[0].InnerText.Trim() == "二等奖")
  204. {
  205. model.zj2 = tds[1].InnerText.Trim();
  206. model.jo2 = tds[2].InnerText.Trim();
  207. list.Add(new Winbonus()
  208. {
  209. item = "二等奖",
  210. wincount = model.zj2,
  211. winmoney = model.jo2
  212. });
  213. }
  214. if (tds[0].InnerText.Trim() == "三等奖")
  215. {
  216. model.zj3 = tds[1].InnerText.Trim();
  217. model.jo3 = tds[2].InnerText.Trim();
  218. list.Add(new Winbonus()
  219. {
  220. item = "三等奖",
  221. wincount = model.zj3,
  222. winmoney = model.jo3
  223. });
  224. }
  225. }
  226. }
  227. model.winbonus = JsonConvert.SerializeObject(list);
  228. }
  229. #endregion
  230. #region 通过副站爬取数据
  231. /// <summary>
  232. /// 副站数据爬取 地址:https://fx.cp2y.com/draw/history_10065_Y/
  233. /// </summary>
  234. /// <returns></returns>
  235. private List<Fchebei20x5LongInfo> GetOpenListFromBackUrl()
  236. {
  237. var result = new List<Fchebei20x5LongInfo>();
  238. try
  239. {
  240. var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.GetEncoding("gb2312"));
  241. if (htmlResource == null) return result;
  242. var doc = new HtmlDocument();
  243. doc.LoadHtml(htmlResource);
  244. var table = doc.DocumentNode.SelectNodes("//tbody[@id='pagedata']");
  245. if (table == null) return result;
  246. var trs = table[0].ChildNodes.Where(S => S.Name.ToLower() == "tr").ToList();
  247. List<HtmlNode> tds = null;
  248. string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
  249. Fchebei20x5LongInfo model = null;
  250. for (var i = 0; i < trs.Count; i++)
  251. {
  252. tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
  253. if (tds.Count < 4) continue;
  254. model = new Fchebei20x5LongInfo();
  255. term = tds[1].InnerText.Trim();
  256. if (term.StartsWith((CommonHelper.SCCSysDateTime.Year - 1).ToString())) break;
  257. model.qi = Convert.ToInt32(term);
  258. openCodeString = tds[2].InnerText.Replace("&nbsp;", "").Trim();
  259. model.n1 = Convert.ToInt32(openCodeString.Substring(0, 2));
  260. model.n2 = Convert.ToInt32(openCodeString.Substring(3, 2));
  261. model.n3 = Convert.ToInt32(openCodeString.Substring(6, 2));
  262. model.n4 = Convert.ToInt32(openCodeString.Substring(9, 2));
  263. model.n5 = Convert.ToInt32(openCodeString.Substring(12, 2));
  264. //组装开奖详情
  265. result.Add(model);
  266. }
  267. //var checkDataHelper = new CheckDataHelper();
  268. //var dbdata = services.GetListS<OpenCode5DTModel>(currentLottery)
  269. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  270. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  271. // Config.Area, currentLottery);
  272. }
  273. catch (Exception ex)
  274. {
  275. log.Error(GetType(),
  276. string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  277. }
  278. return result;
  279. }
  280. /// <summary>
  281. /// 执行备用站的数据插入
  282. /// </summary>
  283. private void DoBackUrl()
  284. {
  285. if (!string.IsNullOrEmpty(Config.BackUrl))
  286. {
  287. var openList = GetOpenListFromBackUrl();
  288. if (openList == null || openList.Count == 0) return; //无抓取数据
  289. //抓取到的最新期数
  290. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  291. //数据库里面最新期数
  292. //LatestItem = Fchebei20x5Data.GetLastOne();
  293. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  294. if (startQiNum > newestQiHao) return; //无最新数据
  295. //处理最新开奖数据
  296. Fchebei20x5LongInfo matchItem = null;
  297. for (var i = startQiNum; i <= newestQiHao; i++)
  298. {
  299. matchItem = openList.Where(R => R.qi.ToString() == i.ToString()).FirstOrDefault();
  300. if (matchItem != null)
  301. {
  302. //add db
  303. matchItem.addtime = DateTime.Now;
  304. Fchebei20x5Data.Add(matchItem);
  305. //Do Success Log
  306. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, i.ToString()));
  307. LatestItem = matchItem;
  308. isGetData = true;
  309. }
  310. }
  311. }
  312. }
  313. #endregion
  314. #region Attribute
  315. /// <summary>
  316. /// 配置信息
  317. /// </summary>
  318. private SCCConfig Config;
  319. /// <summary>
  320. /// 当天抓取的最新一期开奖记录
  321. /// </summary>
  322. private Fchebei20x5LongInfo LatestItem;
  323. #pragma warning disable CS0414 // 字段“HeBei20X5Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  324. /// <summary>
  325. /// 当天抓取失败列表
  326. /// </summary>
  327. private List<string> FailedQiHaoList = null;
  328. #pragma warning restore CS0414 // 字段“HeBei20X5Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  329. /// <summary>
  330. /// 日志对象
  331. /// </summary>
  332. private readonly LogHelper log;
  333. /// <summary>
  334. /// 当前彩种
  335. /// </summary>
  336. private SCCLottery currentLottery => SCCLottery.HeBei20X5;
  337. /// <summary>
  338. /// 邮件接口
  339. /// </summary>
  340. private IEmail email;
  341. #pragma warning disable CS0414 // 字段“HeBei20X5Job.isGetData”已被赋值,但从未使用过它的值
  342. /// <summary>
  343. /// 是否本次运行抓取到开奖数据
  344. /// </summary>
  345. private bool isGetData;
  346. #pragma warning restore CS0414 // 字段“HeBei20X5Job.isGetData”已被赋值,但从未使用过它的值
  347. #endregion
  348. }
  349. }