GuangDong26X5Job.cs 15 KB

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