HeiLongJiangTC6J1Job.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using HtmlAgilityPack;
  6. using Quartz;
  7. using SCC.Common;
  8. using SCC.Crawler.Tools;
  9. using SCC.Interface;
  10. using SCC.Models;
  11. namespace SCC.Crawler.DT
  12. {
  13. /// <summary>
  14. /// 黑龙江体彩6+1
  15. /// </summary>
  16. [DisallowConcurrentExecution]
  17. [PersistJobDataAfterExecution]
  18. public class HeiLongJiangTC6J1Job : IJob
  19. {
  20. /// <summary>
  21. /// 初始化函数
  22. /// </summary>
  23. public HeiLongJiangTC6J1Job()
  24. {
  25. log = new LogHelper();
  26. services = IOC.Resolve<IDTOpenCode>();
  27. email = IOC.Resolve<IEmail>();
  28. }
  29. /// <summary>
  30. /// 作业执行入口
  31. /// </summary>
  32. /// <param name="context"></param>
  33. public void Execute(IJobExecutionContext context)
  34. {
  35. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  36. //预设节假日不开奖
  37. if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
  38. LatestItem = context.JobDetail.JobDataMap["LatestItem"] as OpenCode7DTModel;
  39. try
  40. {
  41. //服务启动时配置初始数据
  42. if (LatestItem == null)
  43. {
  44. LatestItem = services.GetOpenCode7DTLastItem(currentLottery);
  45. if (LatestItem == null)
  46. LatestItem = new OpenCode7DTModel
  47. {
  48. Term = CommonHelper.GenerateQiHaoYYQQQ(0),
  49. OpenTime = 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.Term.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy")))
  60. LatestItem = new OpenCode7DTModel
  61. {
  62. Term = CommonHelper.GenerateQiHaoYYQQQ(0),
  63. OpenTime = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
  64. };
  65. //当今日开奖并且当前时间是晚上8点过后开始抓取
  66. if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12)
  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. private void DoMainUrl()
  80. {
  81. if (!string.IsNullOrEmpty(Config.MainUrl))
  82. {
  83. var openList = GetOpenListFromMainUrl(Config.MainUrl);
  84. if (openList.Count == 0) return; //无抓取数据
  85. //抓取到的最新期数
  86. var newestQiHao = Convert.ToInt32(openList.First().Term.ToString());
  87. //数据库里面最新期数
  88. var startQiNum = Convert.ToInt32(LatestItem.Term.ToString());
  89. if (startQiNum > newestQiHao) return; //无最新数据
  90. //处理最新开奖数据
  91. var getQiHao = string.Empty;
  92. OpenCode7DTModel matchItem = null;
  93. for (var i = startQiNum; i <= newestQiHao; i++)
  94. {
  95. getQiHao = i.ToString();
  96. matchItem = openList.FirstOrDefault(r => r.Term.ToString() == getQiHao);
  97. if (matchItem != null && services.AddDTOpen7Code(currentLottery, matchItem))
  98. {
  99. //Do Success Log
  100. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
  101. LatestItem = matchItem;
  102. isGetData = true;
  103. }
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// 爬取主网站信息
  109. /// </summary>
  110. /// <returns></returns>
  111. private List<OpenCode7DTModel> GetOpenListFromMainUrl(string mainUrl)
  112. {
  113. var result = new List<OpenCode7DTModel>();
  114. try
  115. {
  116. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  117. if (htmlResource == null) return result;
  118. var doc = new HtmlDocument();
  119. doc.LoadHtml(htmlResource);
  120. var table = doc.DocumentNode.SelectNodes("//tbody");
  121. if (table == null) return result;
  122. var trs = table[0].ChildNodes.Where(S => S.Name.ToLower() == "tr").ToList();
  123. List<HtmlNode> tds = null;
  124. string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
  125. OpenCode7DTModel model = null;
  126. for (var i = 0; i < trs.Count; i++)
  127. {
  128. tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
  129. if (tds.Count < 10) continue;
  130. model = new OpenCode7DTModel();
  131. term = tds[0].InnerText.Trim();
  132. if (term.StartsWith((CommonHelper.SCCSysDateTime.Year - 1).ToString())) break;
  133. model.Term = Convert.ToInt64(term);
  134. openCodeString = tds[1].InnerText.Trim();
  135. model.OpenCode1 = Convert.ToInt32(openCodeString.Substring(0, 2));
  136. model.OpenCode2 = Convert.ToInt32(openCodeString.Substring(2, 2));
  137. model.OpenCode3 = Convert.ToInt32(openCodeString.Substring(4, 2));
  138. model.OpenCode4 = Convert.ToInt32(openCodeString.Substring(6, 2));
  139. model.OpenCode5 = Convert.ToInt32(openCodeString.Substring(8, 2));
  140. model.OpenCode6 = Convert.ToInt32(openCodeString.Substring(10, 2));
  141. model.OpenCode7 = Convert.ToInt32(openCodeString.Substring(13, 2));
  142. model.OpenTime = Convert.ToDateTime(tds[10].InnerText.Trim());
  143. //组装开奖详情
  144. var details = GetKaijiangDetails(tds);
  145. model.Spare = details;
  146. model.DetailUrl = mainUrl;
  147. result.Add(model);
  148. }
  149. var checkDataHelper = new CheckDataHelper();
  150. var dbdata = services.GetListS<OpenCode7DTModel>(currentLottery)
  151. .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  152. checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  153. Config.Area, currentLottery);
  154. }
  155. catch (Exception ex)
  156. {
  157. log.Error(GetType(),
  158. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  159. }
  160. return result;
  161. }
  162. /// <summary>
  163. /// 获取备用站点开奖列表数据
  164. /// </summary>
  165. /// <returns></returns>
  166. private List<OpenCode7DTModel> GetOpenListFromBackUrl()
  167. {
  168. var result = new List<OpenCode7DTModel>();
  169. try
  170. {
  171. var url = new Uri(Config.BackUrl);
  172. var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.GetEncoding("gb2312"));
  173. if (htmlResource == null) return result;
  174. var doc = new HtmlDocument();
  175. doc.LoadHtml(htmlResource);
  176. var table = doc.DocumentNode.SelectSingleNode("//table");
  177. if (table == null) return result;
  178. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  179. OpenCode7DTModel model = null;
  180. HtmlNode nodeA = null;
  181. var optimizeUrl = string.Empty;
  182. for (var i = 2; i < trs.Count; i++) //第一二行为表头
  183. {
  184. var trstyle = trs[i].Attributes["style"];
  185. if (trstyle != null && trstyle.Value == "display:none") continue;
  186. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  187. if (tds.Count < 9) continue;
  188. model = new OpenCode7DTModel();
  189. nodeA = tds[0].ChildNodes.Where(n => n.Name == "a").FirstOrDefault();
  190. if (nodeA == null) continue;
  191. model.Term = Convert.ToInt64(nodeA.InnerText.Trim().Substring(2, 5));
  192. optimizeUrl = nodeA.Attributes["href"].Value;
  193. model.DetailUrl = new Uri(url, optimizeUrl).AbsoluteUri;
  194. model.OpenTime = Convert.ToDateTime(tds[9].InnerText);
  195. if (tds[1].ChildNodes.Count == 0) continue;
  196. var opencodeNode = tds[1].ChildNodes.Where(n => n.Name.ToLower() == "i").ToList();
  197. if (opencodeNode.Count < 5) continue;
  198. model.OpenCode1 = Convert.ToInt32(opencodeNode[0].InnerText.Trim());
  199. model.OpenCode2 = Convert.ToInt32(opencodeNode[1].InnerText.Trim());
  200. model.OpenCode3 = Convert.ToInt32(opencodeNode[2].InnerText.Trim());
  201. model.OpenCode4 = Convert.ToInt32(opencodeNode[3].InnerText.Trim());
  202. model.OpenCode5 = Convert.ToInt32(opencodeNode[4].InnerText.Trim());
  203. model.OpenCode6 = Convert.ToInt32(opencodeNode[5].InnerText.Trim());
  204. model.OpenCode7 = Convert.ToInt32(opencodeNode[6].InnerText.Trim());
  205. result.Add(model);
  206. }
  207. var checkDataHelper = new CheckDataHelper();
  208. var dbdata = services.GetListS<OpenCode7DTModel>(currentLottery)
  209. .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  210. checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  211. Config.Area, currentLottery);
  212. result = result.OrderByDescending(S => S.Term).ToList();
  213. }
  214. catch (Exception ex)
  215. {
  216. log.Error(GetType(),
  217. string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  218. }
  219. return result;
  220. }
  221. /// <summary>
  222. /// 验证以前的七号数据
  223. /// </summary>
  224. private void DoBackUrl()
  225. {
  226. if (!string.IsNullOrEmpty(Config.BackUrl))
  227. {
  228. var OpenList = GetOpenListFromBackUrl();
  229. if (OpenList.Count == 0) return; //无抓取数据
  230. var newestQiHao = OpenList.First().Term.ToString();
  231. var startQiNum = Convert.ToInt32(LatestItem.Term.ToString().Substring(4)) + 1;
  232. var newestQiNum = Convert.ToInt32(newestQiHao.Substring(4));
  233. if (startQiNum > newestQiNum) return; //无最新数据
  234. //处理最新开奖数据
  235. var getQiHao = string.Empty;
  236. OpenCode7DTModel matchItem = null;
  237. for (var i = startQiNum; i <= newestQiNum; i++)
  238. {
  239. getQiHao = i.ToString();
  240. matchItem = OpenList.Where(R => R.Term.ToString() == getQiHao).FirstOrDefault();
  241. if (matchItem != null && services.AddDTOpen7Code(currentLottery, matchItem))
  242. {
  243. //Do Success Log
  244. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, getQiHao));
  245. LatestItem = matchItem;
  246. isGetData = true;
  247. }
  248. }
  249. }
  250. }
  251. /// <summary>
  252. /// 获取主站下开奖详情
  253. /// </summary>
  254. /// <param name="nodes"></param>
  255. /// <returns></returns>
  256. private string GetKaijiangDetails(List<HtmlNode> nodes)
  257. {
  258. var entity = new KaijiangDetailsEntity
  259. {
  260. Gdje = nodes[9].InnerText.Replace(",", ""),
  261. Trje = nodes[8].InnerText.Replace(",", "")
  262. };
  263. //TODO
  264. var list1 = new List<Kaijiangitem>();
  265. //组装详情
  266. var list = new List<Kaijiangitem>
  267. {
  268. new Kaijiangitem
  269. {
  270. Name = "特等奖",
  271. /// "0注<br>\t\t\t\t\t\t\t0.00元"
  272. Total = nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  273. .Split("注".ToCharArray())[0],
  274. TotalMoney =
  275. nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  276. .Split("注".ToCharArray())[1].Replace(",", "")
  277. },
  278. new Kaijiangitem
  279. {
  280. Name = "一等奖",
  281. /// "0注<br>\t\t\t\t\t\t\t0.00元"
  282. Total = nodes[3].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  283. .Split("注".ToCharArray())[0],
  284. TotalMoney =
  285. nodes[3].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  286. .Split("注".ToCharArray())[1].Replace(",", "")
  287. },
  288. new Kaijiangitem
  289. {
  290. Name = "二等奖",
  291. /// "0注<br>\t\t\t\t\t\t\t0.00元"
  292. Total = nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  293. .Split("注".ToCharArray())[0],
  294. TotalMoney =
  295. nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  296. .Split("注".ToCharArray())[1].Replace(",", "")
  297. },
  298. new Kaijiangitem
  299. {
  300. Name = "三等奖",
  301. /// "0注<br>\t\t\t\t\t\t\t0.00元"
  302. Total = nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  303. .Split("注".ToCharArray())[0],
  304. TotalMoney =
  305. nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  306. .Split("注".ToCharArray())[1]
  307. },
  308. new Kaijiangitem
  309. {
  310. Name = "四等奖",
  311. /// "0注<br>\t\t\t\t\t\t\t0.00元"
  312. Total = nodes[5].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  313. .Split("注".ToCharArray())[0],
  314. TotalMoney =
  315. nodes[5].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  316. .Split("注".ToCharArray())[1]
  317. },
  318. new Kaijiangitem
  319. {
  320. Name = "五等奖",
  321. /// "0注<br>\t\t\t\t\t\t\t0.00元"
  322. Total = nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  323. .Split("注".ToCharArray())[0],
  324. TotalMoney =
  325. nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  326. .Split("注".ToCharArray())[1]
  327. }
  328. };
  329. entity.KaiJiangItems = list;
  330. return entity.TryToJson();
  331. }
  332. #region Attribute
  333. /// <summary>
  334. /// 配置信息
  335. /// </summary>
  336. private SCCConfig Config;
  337. /// <summary>
  338. /// 当天抓取的最新一期开奖记录
  339. /// </summary>
  340. private OpenCode7DTModel LatestItem;
  341. /// <summary>
  342. /// 当天抓取失败列表
  343. /// </summary>
  344. private List<string> FailedQiHaoList = null;
  345. /// <summary>
  346. /// 日志对象
  347. /// </summary>
  348. private readonly LogHelper log;
  349. /// <summary>
  350. /// 数据服务
  351. /// </summary>
  352. private readonly IDTOpenCode services;
  353. /// <summary>
  354. /// 当前彩种
  355. /// </summary>
  356. private SCCLottery currentLottery => SCCLottery.HeiLongJiangTC6J1;
  357. /// <summary>
  358. /// 邮件接口
  359. /// </summary>
  360. private IEmail email;
  361. /// <summary>
  362. /// 是否本次运行抓取到开奖数据
  363. /// </summary>
  364. private bool isGetData;
  365. #endregion
  366. }
  367. }