HaiNan4J1Job.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. /// 海南4+1
  17. /// </summary>
  18. [DisallowConcurrentExecution]
  19. [PersistJobDataAfterExecution]
  20. public class HaiNan4J1Job : IJob
  21. {
  22. /// <summary>
  23. /// 河南四加一
  24. /// </summary>
  25. public HaiNan4J1Job()
  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 Tchn41longInfo;
  40. try
  41. {
  42. //服务启动时配置初始数据
  43. if (LatestItem == null)
  44. {
  45. LatestItem = new Tchn41longInfo
  46. {
  47. qi = CommonHelper.GenerateQiHaoYYQQQ(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 Tchn41longInfo
  60. {
  61. qi = CommonHelper.GenerateQiHaoYYQQQ(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 = Tchn41Data.GetLastOne();
  92. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  93. if (startQiNum == 0)
  94. startQiNum = openList.OrderBy(m => m.qi).First().qi;
  95. if (startQiNum > newestQiHao) return; //无最新数据
  96. //处理最新开奖数据
  97. Tchn41longInfo matchItem = null;
  98. for (var i = startQiNum; i <= newestQiHao; i++)
  99. {
  100. matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString());
  101. if (matchItem != null)
  102. {
  103. //add db
  104. matchItem.addtime = DateTime.Now;
  105. Tchn41Data.Add(matchItem);
  106. //Do Success Log
  107. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString()));
  108. LatestItem = matchItem;
  109. isGetData = true;
  110. }
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// 根据主站的url获取到数据列表
  116. /// </summary>
  117. /// <param name="mainUrl"></param>
  118. /// <returns></returns>
  119. private List<Tchn41longInfo> GetOpenListFromMainUrl(string mainUrl)
  120. {
  121. var result = new List<Tchn41longInfo>();
  122. try
  123. {
  124. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  125. if (htmlResource == null) return result;
  126. var doc = new HtmlDocument();
  127. doc.LoadHtml(htmlResource);
  128. var table = doc.DocumentNode.SelectSingleNode("//tbody");
  129. if (table == null) return result;
  130. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  131. List<HtmlNode> tds = null;
  132. string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
  133. Tchn41longInfo model = null;
  134. for (var i = 0; i < trs.Count; i++)
  135. {
  136. tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
  137. if (tds.Count < 8) continue;
  138. model = new Tchn41longInfo();
  139. term = tds[0].InnerText.Trim();
  140. if (term.StartsWith((CommonHelper.SCCSysDateTime.Year - 1).ToString())) break;
  141. term = term.Length < 7 ? $"20{term}" : term;
  142. model.qi = Convert.ToInt32(term);
  143. openCodeString = tds[1].InnerText.Trim();
  144. //3+9+7+4+2
  145. model.n1 = Convert.ToInt32(openCodeString.Substring(0, 1));
  146. model.n2 = Convert.ToInt32(openCodeString.Substring(2, 1));
  147. model.n3 = Convert.ToInt32(openCodeString.Substring(4, 1));
  148. model.n4 = Convert.ToInt32(openCodeString.Substring(6, 1));
  149. model.n5 = Convert.ToInt32(openCodeString.Substring(8, 1));
  150. model.date = Convert.ToDateTime(tds[12].InnerText.Trim());
  151. //model.DetailUrl = mainUrl;
  152. //组装开奖详情
  153. GetKaijiangDetails(ref model, tds);
  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. }
  162. catch (Exception ex)
  163. {
  164. log.Error(GetType(),
  165. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  166. }
  167. return result;
  168. }
  169. /// <summary>
  170. /// 获取主站下开奖详情
  171. /// </summary>
  172. /// <param name="nodes"></param>
  173. /// <returns></returns>
  174. private void GetKaijiangDetails(ref Tchn41longInfo model, List<HtmlNode> nodes)
  175. {
  176. model.nextmoney = nodes[11].InnerText.Replace(",", "");
  177. model.tzmoney = nodes[10].InnerText.Replace(",", "");
  178. //组装详情
  179. //4+1
  180. model.zj41 = nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Split("注".ToCharArray())[0];
  181. model.jo41 = nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "").Split("注".ToCharArray())[1];
  182. //定位4
  183. model.zjdw4 = nodes[3].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Split("注".ToCharArray())[0];
  184. model.jodw4 = nodes[3].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "").Split("注".ToCharArray())[1];
  185. //定位3
  186. model.zjdw3 = nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Split("注".ToCharArray())[0];
  187. model.jodw3 = nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "").Split("注".ToCharArray())[1];
  188. //定位2
  189. model.zjdw2 = nodes[5].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Split("注".ToCharArray())[0];
  190. model.jodw2 = nodes[5].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "").Split("注".ToCharArray())[1];
  191. //定位1
  192. model.zjdw1 = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Split("注".ToCharArray())[0];
  193. model.jodw1 = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Split("注".ToCharArray())[1];
  194. //任选4(3重)
  195. model.zjrx43 = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  196. .Split("注".ToCharArray())[0];
  197. model.jorx43 =
  198. nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  199. .Split("注".ToCharArray())[1];
  200. //任选4(2双重)
  201. model.zjrx42 = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  202. .Split("注".ToCharArray())[0];
  203. model.jorx42 =
  204. nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  205. .Split("注".ToCharArray())[1];
  206. //任选4(1双重)
  207. model.zjrx41 = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  208. .Split("注".ToCharArray())[0];
  209. model.jorx41 =
  210. nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  211. .Split("注".ToCharArray())[1];
  212. var list = new List<Winbonus>();
  213. list.Add(new Winbonus()
  214. {
  215. item = "4+1",
  216. wincount = model.zj41,
  217. winmoney = model.jo41
  218. });
  219. list.Add(new Winbonus()
  220. {
  221. item = "定位4",
  222. wincount = model.zjdw4,
  223. winmoney = model.jodw4
  224. });
  225. list.Add(new Winbonus()
  226. {
  227. item = "定位3",
  228. wincount = model.zjdw3,
  229. winmoney = model.jodw3
  230. });
  231. list.Add(new Winbonus()
  232. {
  233. item = "定位2",
  234. wincount = model.zjdw2,
  235. winmoney = model.jodw2
  236. });
  237. list.Add(new Winbonus()
  238. {
  239. item = "定位1",
  240. wincount = model.zjdw1,
  241. winmoney = model.jodw1
  242. });
  243. list.Add(new Winbonus()
  244. {
  245. item = "任选4(3重)",
  246. wincount = model.zjrx43,
  247. winmoney = model.jorx43
  248. });
  249. list.Add(new Winbonus()
  250. {
  251. item = "任选4(2双重)",
  252. wincount = model.zjrx42,
  253. winmoney = model.jorx42
  254. });
  255. list.Add(new Winbonus()
  256. {
  257. item = "任选4(1双重)",
  258. wincount = model.zjrx41,
  259. winmoney = model.jorx41
  260. });
  261. model.winbonus = JsonConvert.SerializeObject(list);
  262. }
  263. #endregion
  264. #region 通过副站爬取数据
  265. private List<Tchn41longInfo> GetOpenListFromBackUrl()
  266. {
  267. var result = new List<Tchn41longInfo>();
  268. try
  269. {
  270. var url = new Uri(Config.BackUrl);
  271. var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.GetEncoding("utf-8"));
  272. if (htmlResource == null) return result;
  273. var doc = new HtmlDocument();
  274. doc.LoadHtml(htmlResource);
  275. var table = doc.DocumentNode.SelectNodes("//table[@id ='tabTrend']");
  276. if (table == null) return result;
  277. var trs = table[0].ChildNodes.Where(node => node.Name == "tr").ToList();
  278. Tchn41longInfo model = null;
  279. var optimizeUrl = string.Empty;
  280. for (var i = 1; i < trs.Count; i++) //第一二行为表头
  281. {
  282. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  283. if (tds.Count < 10) continue;
  284. if (tds[1].ChildNodes.Count == 0) continue;
  285. var openCodeString = tds[1].InnerText.Trim();
  286. if (openCodeString.Length != 6) continue;
  287. model = new Tchn41longInfo();
  288. string qihao = tds[0].InnerText.Trim();
  289. qihao = qihao.Length < 7 ? $"20{qihao}" : qihao;
  290. model.qi = Convert.ToInt32(qihao);
  291. model.n1 = Convert.ToInt32(openCodeString.Substring(0, 1));
  292. model.n2 = Convert.ToInt32(openCodeString.Substring(1, 1));
  293. model.n3 = Convert.ToInt32(openCodeString.Substring(2, 1));
  294. model.n4 = Convert.ToInt32(openCodeString.Substring(3, 1));
  295. model.n5 = Convert.ToInt32(openCodeString.Substring(5));
  296. result.Add(model);
  297. }
  298. //var checkDataHelper = new CheckDataHelper();
  299. //var dbdata = services.GetListS<OpenCode5DTModel>(currentLottery)
  300. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  301. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  302. // Config.Area, currentLottery);
  303. //result = result.OrderByDescending(S => S.Term).ToList();
  304. }
  305. catch (Exception ex)
  306. {
  307. log.Error(GetType(),
  308. string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  309. }
  310. return result;
  311. }
  312. private void DoBackUrl()
  313. {
  314. if (!string.IsNullOrEmpty(Config.BackUrl))
  315. {
  316. var openList = GetOpenListFromBackUrl();
  317. if (openList == null || openList.Count == 0) return; //无抓取数据
  318. //抓取到的最新期数
  319. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  320. //数据库里面最新期数
  321. //LatestItem = Tchn41Data.GetLastOne();
  322. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  323. if (startQiNum > newestQiHao) return; //无最新数据
  324. //处理最新开奖数据
  325. Tchn41longInfo matchItem = null;
  326. for (var i = startQiNum; i <= newestQiHao; i++)
  327. {
  328. matchItem = openList.Where(R => R.qi.ToString() == i.ToString()).FirstOrDefault();
  329. if (matchItem != null)
  330. {
  331. //add db
  332. matchItem.addtime = DateTime.Now;
  333. Tchn41Data.Add(matchItem);
  334. //Do Success Log
  335. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, i.ToString()));
  336. LatestItem = matchItem;
  337. isGetData = true;
  338. }
  339. }
  340. }
  341. }
  342. #endregion
  343. #region Attribute
  344. /// <summary>
  345. /// 配置信息
  346. /// </summary>
  347. private SCCConfig Config;
  348. /// <summary>
  349. /// 当天抓取的最新一期开奖记录
  350. /// </summary>
  351. private Tchn41longInfo LatestItem;
  352. #pragma warning disable CS0414 // 字段“HaiNan4J1Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  353. /// <summary>
  354. /// 当天抓取失败列表
  355. /// </summary>
  356. private List<string> FailedQiHaoList = null;
  357. #pragma warning restore CS0414 // 字段“HaiNan4J1Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  358. /// <summary>
  359. /// 日志对象
  360. /// </summary>
  361. private readonly LogHelper log;
  362. /// <summary>
  363. /// 当前彩种
  364. /// </summary>
  365. private SCCLottery currentLottery => SCCLottery.HaiNan4J1;
  366. /// <summary>
  367. /// 邮件接口
  368. /// </summary>
  369. private IEmail email;
  370. #pragma warning disable CS0414 // 字段“HaiNan4J1Job.isGetData”已被赋值,但从未使用过它的值
  371. /// <summary>
  372. /// 是否本次运行抓取到开奖数据
  373. /// </summary>
  374. private bool isGetData;
  375. #pragma warning restore CS0414 // 字段“HaiNan4J1Job.isGetData”已被赋值,但从未使用过它的值
  376. #endregion
  377. }
  378. }