HeiLongJiangLJFC22x5Job.cs 15 KB

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