HeBeiHYC3Job.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /// 河北好运3
  17. /// </summary>
  18. [DisallowConcurrentExecution]
  19. [PersistJobDataAfterExecution]
  20. public class HeBeiHYC3Job : IJob
  21. {
  22. /// <summary>
  23. /// 初始化函数
  24. /// </summary>
  25. public HeBeiHYC3Job()
  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 Fchebeihyc3LongInfo;
  40. try
  41. {
  42. //服务启动时配置初始数据
  43. if (LatestItem == null)
  44. {
  45. LatestItem = new Fchebeihyc3LongInfo
  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. DoMainUrl();
  55. if (!LatestItem.qi.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy")))
  56. LatestItem = new Fchebeihyc3LongInfo
  57. {
  58. qi = CommonHelper.GenerateQiHaoYYYYQQQ(0),
  59. date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
  60. };
  61. //当今日开奖并且当前时间是晚上8点过后开始抓取
  62. if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12) DoMainUrl();
  63. }
  64. catch (Exception ex)
  65. {
  66. log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  67. }
  68. //保存最新期号
  69. context.JobDetail.JobDataMap["LatestItem"] = LatestItem;
  70. }
  71. #region 通过主站爬取数据
  72. /// <summary>
  73. /// 执行数据插入
  74. /// </summary>
  75. private void DoMainUrl()
  76. {
  77. if (!string.IsNullOrEmpty(Config.MainUrl))
  78. {
  79. var openList = GetOpenListFromMainUrl(Config.MainUrl);
  80. if (openList == null || openList.Count == 0) return; //无抓取数据
  81. //抓取到的最新期数
  82. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  83. //数据库里面最新期数
  84. LatestItem = Fchebeihyc3Data.GetLastOne();
  85. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  86. if (startQiNum > newestQiHao) return; //无最新数据
  87. if (startQiNum == 0)
  88. startQiNum = openList.OrderBy(m => m.qi).First().qi;
  89. //处理最新开奖数据
  90. Fchebeihyc3LongInfo matchItem = null;
  91. for (var i = startQiNum; i <= newestQiHao; i++)
  92. {
  93. matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString());
  94. if (matchItem != null)
  95. {
  96. //add db
  97. matchItem.addtime = DateTime.Now;
  98. Fchebeihyc3Data.Add(matchItem);
  99. //Do Success Log
  100. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString()));
  101. LatestItem = matchItem;
  102. isGetData = true;
  103. }
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// 获取主站点开奖列表数据
  109. /// </summary>
  110. /// <returns></returns>
  111. private List<Fchebeihyc3LongInfo> GetOpenListFromMainUrl(string mainUrl)
  112. {
  113. var result = new List<Fchebeihyc3LongInfo>();
  114. try
  115. {
  116. var url = new Uri(mainUrl);
  117. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("UTF-8"));
  118. if (htmlResource == null) return result;
  119. var doc = new HtmlDocument();
  120. doc.LoadHtml(htmlResource);
  121. var div = doc.DocumentNode.SelectSingleNode("//div[@id='jq_difang_index_body']");
  122. if (div == null) return result;
  123. var divs = div.ChildNodes.Where(node => node.Name == "div").ToList();
  124. if (divs == null) return result;
  125. var div2 = divs[2].ChildNodes.Where(node => node.Name == "div").ToList();
  126. if (div2 == null) return result;
  127. var table = div2[1].ChildNodes.Where(node => node.Name == "table").ToList();
  128. if (table == null) return result;
  129. var tbody = table[0].ChildNodes.Where(node => node.Name == "tbody").ToList();
  130. if (tbody == null) return result;
  131. var trs = tbody[0].ChildNodes.Where(node => node.Name == "tr").ToList();
  132. if (trs == null) return result;
  133. Fchebeihyc3LongInfo model = null;
  134. HtmlNode nodeA = null;
  135. var optimizeUrl = string.Empty;
  136. for (var i = 0; i < trs.Count; i++) //第一二行为表头
  137. {
  138. var trstyle = trs[i].Attributes["style"];
  139. if (trstyle != null && trstyle.Value == "display:none") continue;
  140. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  141. if (tds.Count < 4) continue;
  142. model = new Fchebeihyc3LongInfo();
  143. nodeA = tds[2].ChildNodes.Where(n => n.Name == "a").FirstOrDefault();
  144. if (nodeA == null) continue;
  145. model.qi = Convert.ToInt32(tds[0].InnerText.Trim());
  146. optimizeUrl = tds[0].InnerText.Trim();
  147. model.date = Convert.ToDateTime(tds[3].InnerText.Substring(5, 10));
  148. if (tds[1].ChildNodes.Count == 0) continue;
  149. var opencode = tds[1].ChildNodes.Where(n => n.Name.ToLower() == "span").ToList();
  150. var opencodeNode = opencode[0].ChildNodes.Where(n => n.Name.ToLower() == "strong").ToList();
  151. var openCodeString = opencodeNode[0].InnerText.Trim();
  152. model.n1 = Convert.ToInt32(openCodeString.Substring(0, 2));
  153. model.n2 = Convert.ToInt32(openCodeString.Substring(3, 2));
  154. model.n3 = Convert.ToInt32(openCodeString.Substring(6, 2));
  155. GetKaijiangDetails(ref model, optimizeUrl);
  156. result.Add(model);
  157. }
  158. //var checkDataHelper = new CheckDataHelper();
  159. //var dbdata = services.GetListS<OpenCode3DTModel>(currentLottery);
  160. //if (dbdata == null) return result;
  161. //var data = dbdata.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  162. //checkDataHelper.CheckData(data, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  163. // Config.Area, currentLottery);
  164. //result = result.OrderByDescending(S => S.Term).ToList();
  165. }
  166. catch (Exception ex)
  167. {
  168. log.Error(GetType(),
  169. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  170. }
  171. return result;
  172. }
  173. /// <summary>
  174. /// 完善主站点江苏体彩7位数开奖实体信息
  175. /// </summary>
  176. /// <param name="model"></param>
  177. private void GetKaijiangDetails(ref Fchebeihyc3LongInfo model, string optimizeUrl)
  178. {
  179. var url = "http://kaijiang.500.com/shtml/hbhy3/" + optimizeUrl + ".shtml";
  180. var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("gb2312"));
  181. if (string.IsNullOrWhiteSpace(htmlResource))
  182. {
  183. log.Warn(GetType(), string.Format("【{0}】通过主站点抓取开奖列表未获取到数据。", Config.Area + currentLottery));
  184. return;
  185. }
  186. var doc = new HtmlDocument();
  187. doc.LoadHtml(htmlResource);
  188. //var div = doc.DocumentNode.SelectSingleNode("//div[@style='margin:20px auto; width:500px;']");
  189. var div = doc.DocumentNode.SelectSingleNode("//div[@class='kjxq_box02']");
  190. var content = div.ChildNodes.Where(node => node.Name == "div").ToList().ElementAt(1);
  191. //爬去奖金
  192. var table = content.ChildNodes.Where(node => node.Name == "table").ToList();
  193. var tr = table[0].ChildNodes.Where(node => node.Name == "tr").ToList();
  194. var td = tr[2].ChildNodes.Where(node => node.Name == "td").ToList();
  195. var span = td[0].ChildNodes.Where(node => node.Name == "span").ToList();
  196. //爬去开奖详情
  197. var tr1 = table[1].ChildNodes.Where(node => node.Name == "tr").ToList();
  198. var td1 = tr1[2].ChildNodes.Where(node => node.Name == "td").ToList();
  199. // <td> 本期销量:<span class="cfont1 ">2,154元</span>&nbsp;&nbsp;&nbsp;&nbsp;奖池滚存:<span class="cfont1 ">0元</span></td>
  200. model.nextmoney = span[1].InnerHtml.Trim().Replace("元", "").Replace(",", "");
  201. model.tzmoney = span[0].InnerHtml.Trim().Replace("元", "").Replace(",", "");
  202. //组装详情
  203. //一等奖
  204. model.zj1 = td1[1].InnerHtml.Trim().Replace(",", "");
  205. model.jo1 = td1[2].InnerHtml.Trim().Replace(",", "");
  206. var list = new List<Winbonus>();
  207. list.Add(new Winbonus()
  208. {
  209. item = "一等奖",
  210. wincount = model.zj1,
  211. winmoney = model.jo1
  212. });
  213. model.winbonus = JsonConvert.SerializeObject(list);
  214. }
  215. #endregion
  216. #region Attribute
  217. /// <summary>
  218. /// 配置信息
  219. /// </summary>
  220. private SCCConfig Config;
  221. /// <summary>
  222. /// 当天抓取的最新一期开奖记录
  223. /// </summary>
  224. private Fchebeihyc3LongInfo LatestItem;
  225. #pragma warning disable CS0414 // 字段“HeBeiHYC3Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  226. /// <summary>
  227. /// 当天抓取失败列表
  228. /// </summary>
  229. private List<string> FailedQiHaoList = null;
  230. #pragma warning restore CS0414 // 字段“HeBeiHYC3Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  231. /// <summary>
  232. /// 日志对象
  233. /// </summary>
  234. private readonly LogHelper log;
  235. /// <summary>
  236. /// 当前彩种
  237. /// </summary>
  238. private SCCLottery currentLottery => SCCLottery.HeBeiHYC3;
  239. /// <summary>
  240. /// 邮件接口
  241. /// </summary>
  242. private IEmail email;
  243. #pragma warning disable CS0414 // 字段“HeBeiHYC3Job.isGetData”已被赋值,但从未使用过它的值
  244. /// <summary>
  245. /// 是否本次运行抓取到开奖数据
  246. /// </summary>
  247. private bool isGetData;
  248. #pragma warning restore CS0414 // 字段“HeBeiHYC3Job.isGetData”已被赋值,但从未使用过它的值
  249. #endregion
  250. }
  251. }