HaiNan4J1Job.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. /// 海南4+1
  15. /// </summary>
  16. [DisallowConcurrentExecution]
  17. [PersistJobDataAfterExecution]
  18. public class HaiNan4J1Job : IJob
  19. {
  20. /// <summary>
  21. /// 河南四加一
  22. /// </summary>
  23. public HaiNan4J1Job()
  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 OpenCode5DTModel;
  39. try
  40. {
  41. //服务启动时配置初始数据
  42. if (LatestItem == null)
  43. {
  44. LatestItem = services.GetOpenCode5DTLastItem(currentLottery);
  45. if (LatestItem == null)
  46. LatestItem = new OpenCode5DTModel
  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 OpenCode5DTModel
  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. #region 通过主站爬取数据
  80. /// <summary>
  81. /// 根据主站的数据列表插入数据库
  82. /// </summary>
  83. private void DoMainUrl()
  84. {
  85. if (!string.IsNullOrEmpty(Config.MainUrl))
  86. {
  87. var openList = GetOpenListFromMainUrl(Config.MainUrl);
  88. if (openList.Count == 0) return; //无抓取数据
  89. //抓取到的最新期数
  90. var newestQiHao = Convert.ToInt32(openList.Max(w => w.Term).ToString());
  91. //数据库里面最新期数
  92. var startQiNum = Convert.ToInt32(LatestItem.Term.ToString());
  93. if (startQiNum > newestQiHao) return; //无最新数据
  94. //处理最新开奖数据
  95. var getQiHao = string.Empty;
  96. OpenCode5DTModel matchItem = null;
  97. for (var i = startQiNum; i <= newestQiHao; i++)
  98. {
  99. getQiHao = i.ToString();
  100. matchItem = openList.FirstOrDefault(r => r.Term.ToString() == getQiHao);
  101. if (matchItem != null && services.AddDTOpen5Code(currentLottery, matchItem))
  102. {
  103. //Do Success Log
  104. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
  105. LatestItem = matchItem;
  106. isGetData = true;
  107. }
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// 根据主站的url获取到数据列表
  113. /// </summary>
  114. /// <param name="mainUrl"></param>
  115. /// <returns></returns>
  116. private List<OpenCode5DTModel> GetOpenListFromMainUrl(string mainUrl)
  117. {
  118. var result = new List<OpenCode5DTModel>();
  119. try
  120. {
  121. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  122. if (htmlResource == null) return result;
  123. var doc = new HtmlDocument();
  124. doc.LoadHtml(htmlResource);
  125. var table = doc.DocumentNode.SelectSingleNode("//tbody");
  126. if (table == null) return result;
  127. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  128. List<HtmlNode> tds = null;
  129. string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
  130. OpenCode5DTModel model = null;
  131. for (var i = 0; i < trs.Count; i++)
  132. {
  133. tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
  134. if (tds.Count < 8) continue;
  135. model = new OpenCode5DTModel();
  136. term = tds[0].InnerText.Trim();
  137. if (term.StartsWith((CommonHelper.SCCSysDateTime.Year - 1).ToString())) break;
  138. model.Term = Convert.ToInt64(term);
  139. openCodeString = tds[1].InnerText.Trim();
  140. //3+9+7+4+2
  141. model.OpenCode1 = Convert.ToInt32(openCodeString.Substring(0, 1));
  142. model.OpenCode2 = Convert.ToInt32(openCodeString.Substring(2, 1));
  143. model.OpenCode3 = Convert.ToInt32(openCodeString.Substring(4, 1));
  144. model.OpenCode4 = Convert.ToInt32(openCodeString.Substring(6, 1));
  145. model.OpenCode5 = Convert.ToInt32(openCodeString.Substring(8, 1));
  146. model.OpenTime = Convert.ToDateTime(tds[12].InnerText.Trim());
  147. //组装开奖详情
  148. var details = GetKaijiangDetails(tds);
  149. model.Spare = details;
  150. model.DetailUrl = mainUrl;
  151. result.Add(model);
  152. }
  153. var checkDataHelper = new CheckDataHelper();
  154. var dbdata = services.GetListS<OpenCode5DTModel>(currentLottery)
  155. .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  156. checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  157. Config.Area, currentLottery);
  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 string GetKaijiangDetails(List<HtmlNode> nodes)
  172. {
  173. var entity = new KaijiangDetailsEntity
  174. {
  175. Gdje = nodes[11].InnerText.Replace(",", ""),
  176. Trje = nodes[10].InnerText.Replace(",", "")
  177. };
  178. //TODO
  179. var list1 = new List<Kaijiangitem>();
  180. //组装详情
  181. var list = new List<Kaijiangitem>
  182. {
  183. new Kaijiangitem
  184. {
  185. Name = "4+1",
  186. Total = nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  187. .Split("注".ToCharArray())[0],
  188. TotalMoney =
  189. nodes[2].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  190. .Split("注".ToCharArray())[1]
  191. },
  192. new Kaijiangitem
  193. {
  194. Name = "定位4",
  195. Total = nodes[3].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  196. .Split("注".ToCharArray())[0],
  197. TotalMoney =
  198. nodes[3].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  199. .Split("注".ToCharArray())[1]
  200. },
  201. new Kaijiangitem
  202. {
  203. Name = "定位3",
  204. Total = nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  205. .Split("注".ToCharArray())[0],
  206. TotalMoney =
  207. nodes[4].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  208. .Split("注".ToCharArray())[1]
  209. },
  210. new Kaijiangitem
  211. {
  212. Name = "定位2",
  213. Total = nodes[5].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  214. .Split("注".ToCharArray())[0],
  215. TotalMoney =
  216. nodes[5].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  217. .Split("注".ToCharArray())[1]
  218. },
  219. new Kaijiangitem
  220. {
  221. Name = "定位1",
  222. Total = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  223. .Split("注".ToCharArray())[0],
  224. TotalMoney =
  225. nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  226. .Split("注".ToCharArray())[1]
  227. },
  228. new Kaijiangitem
  229. {
  230. Name = "任选4(3重)",
  231. Total = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  232. .Split("注".ToCharArray())[0],
  233. TotalMoney =
  234. nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  235. .Split("注".ToCharArray())[1]
  236. },
  237. new Kaijiangitem
  238. {
  239. Name = "任选4(2双重)",
  240. Total = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  241. .Split("注".ToCharArray())[0],
  242. TotalMoney =
  243. nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  244. .Split("注".ToCharArray())[1]
  245. },
  246. new Kaijiangitem
  247. {
  248. Name = "任选4(1双重)",
  249. Total = nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "")
  250. .Split("注".ToCharArray())[0],
  251. TotalMoney =
  252. nodes[6].InnerText.Replace("<br>", "").Replace("\t", "").Replace("元", "").Replace(",", "")
  253. .Split("注".ToCharArray())[1]
  254. }
  255. };
  256. entity.KaiJiangItems = list;
  257. return entity.TryToJson();
  258. }
  259. #endregion
  260. #region 通过副站爬取数据
  261. private List<OpenCode5DTModel> GetOpenListFromBackUrl()
  262. {
  263. var result = new List<OpenCode5DTModel>();
  264. try
  265. {
  266. var url = new Uri(Config.BackUrl);
  267. var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.GetEncoding("utf-8"));
  268. if (htmlResource == null) return result;
  269. var doc = new HtmlDocument();
  270. doc.LoadHtml(htmlResource);
  271. var table = doc.DocumentNode.SelectNodes("//table[@id ='tabTrend']");
  272. if (table == null) return result;
  273. var trs = table[0].ChildNodes.Where(node => node.Name == "tr").ToList();
  274. OpenCode5DTModel model = null;
  275. var optimizeUrl = string.Empty;
  276. for (var i = 1; i < trs.Count; i++) //第一二行为表头
  277. {
  278. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  279. if (tds.Count < 10) continue;
  280. if (tds[1].ChildNodes.Count == 0) continue;
  281. var openCodeString = tds[1].InnerText.Trim();
  282. if (openCodeString.Length != 6) continue;
  283. model = new OpenCode5DTModel();
  284. model.Term = Convert.ToInt64(tds[0].InnerText.Trim());
  285. model.OpenCode1 = Convert.ToInt32(openCodeString.Substring(0, 1));
  286. model.OpenCode2 = Convert.ToInt32(openCodeString.Substring(1, 1));
  287. model.OpenCode3 = Convert.ToInt32(openCodeString.Substring(2, 1));
  288. model.OpenCode4 = Convert.ToInt32(openCodeString.Substring(3, 1));
  289. model.OpenCode5 = Convert.ToInt32(openCodeString.Substring(5));
  290. result.Add(model);
  291. }
  292. var checkDataHelper = new CheckDataHelper();
  293. var dbdata = services.GetListS<OpenCode5DTModel>(currentLottery)
  294. .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  295. checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  296. Config.Area, currentLottery);
  297. result = result.OrderByDescending(S => S.Term).ToList();
  298. }
  299. catch (Exception ex)
  300. {
  301. log.Error(GetType(),
  302. string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  303. }
  304. return result;
  305. }
  306. private void DoBackUrl()
  307. {
  308. if (!string.IsNullOrEmpty(Config.BackUrl))
  309. {
  310. var OpenList = GetOpenListFromBackUrl();
  311. if (OpenList.Count == 0) return; //无抓取数据
  312. var newestQiHao = OpenList.First().Term.ToString();
  313. var startQiNum = Convert.ToInt32(LatestItem.Term.ToString().Substring(4)) + 1;
  314. var newestQiNum = Convert.ToInt32(newestQiHao.Substring(4));
  315. if (startQiNum > newestQiNum) return; //无最新数据
  316. //处理最新开奖数据
  317. var getQiHao = string.Empty;
  318. OpenCode5DTModel matchItem = null;
  319. for (var i = startQiNum; i <= newestQiNum; i++)
  320. {
  321. getQiHao = i.ToString();
  322. matchItem = OpenList.Where(R => R.Term.ToString() == getQiHao).FirstOrDefault();
  323. if (matchItem != null && services.AddDTOpen5Code(currentLottery, matchItem))
  324. {
  325. //Do Success Log
  326. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, getQiHao));
  327. LatestItem = matchItem;
  328. isGetData = true;
  329. }
  330. }
  331. }
  332. }
  333. #endregion
  334. #region Attribute
  335. /// <summary>
  336. /// 配置信息
  337. /// </summary>
  338. private SCCConfig Config;
  339. /// <summary>
  340. /// 当天抓取的最新一期开奖记录
  341. /// </summary>
  342. private OpenCode5DTModel LatestItem;
  343. /// <summary>
  344. /// 当天抓取失败列表
  345. /// </summary>
  346. private List<string> FailedQiHaoList = null;
  347. /// <summary>
  348. /// 日志对象
  349. /// </summary>
  350. private readonly LogHelper log;
  351. /// <summary>
  352. /// 数据服务
  353. /// </summary>
  354. private readonly IDTOpenCode services;
  355. /// <summary>
  356. /// 当前彩种
  357. /// </summary>
  358. private SCCLottery currentLottery => SCCLottery.HaiNan4J1;
  359. /// <summary>
  360. /// 邮件接口
  361. /// </summary>
  362. private IEmail email;
  363. /// <summary>
  364. /// 是否本次运行抓取到开奖数据
  365. /// </summary>
  366. private bool isGetData;
  367. #endregion
  368. }
  369. }