HeiLongJiangP62Job.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. /// 黑龙江P62
  17. /// </summary>
  18. [DisallowConcurrentExecution]
  19. [PersistJobDataAfterExecution]
  20. public class HeiLongJiangP62Job : IJob
  21. {
  22. /// <summary>
  23. /// 初始化函数
  24. /// </summary>
  25. public HeiLongJiangP62Job()
  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 Fchljp62LongInfo;
  40. try
  41. {
  42. //服务启动时配置初始数据
  43. if (LatestItem == null)
  44. {
  45. LatestItem = new Fchljp62LongInfo
  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. {
  55. DoMainUrl();
  56. DoBackUrl();
  57. }
  58. if (!LatestItem.qi.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy")))
  59. LatestItem = new Fchljp62LongInfo
  60. {
  61. qi = CommonHelper.GenerateQiHaoYYYYQQQ(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. /// <summary>
  79. /// 通过主站点爬取开奖数据
  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 = Fchljp62Data.GetLastOne();
  92. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  93. if (startQiNum > newestQiHao) return; //无最新数据
  94. //处理最新开奖数据
  95. Fchljp62LongInfo matchItem = null;
  96. for (var i = startQiNum; i <= newestQiHao; i++)
  97. {
  98. matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString());
  99. if (matchItem != null)
  100. {
  101. //add db
  102. matchItem.addtime = DateTime.Now;
  103. Fchljp62Data.Add(matchItem);
  104. //Do Success Log
  105. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString()));
  106. LatestItem = matchItem;
  107. isGetData = true;
  108. }
  109. }
  110. }
  111. }
  112. private List<Fchljp62LongInfo> GetOpenListFromMainUrl(string mainUrl)
  113. {
  114. var result = new List<Fchljp62LongInfo>();
  115. try
  116. {
  117. var url = new Uri(mainUrl);
  118. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  119. if (htmlResource == null) return result;
  120. var doc = new HtmlDocument();
  121. doc.LoadHtml(htmlResource);
  122. var table = doc.DocumentNode.SelectSingleNode("//table");
  123. if (table == null) return result;
  124. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  125. Fchljp62LongInfo model = null;
  126. HtmlNode nodeA = null;
  127. var optimizeUrl = string.Empty;
  128. for (var i = 2; i < trs.Count; i++) //第一二行为表头
  129. {
  130. var trstyle = trs[i].Attributes["style"];
  131. if (trstyle != null && trstyle.Value == "display:none") continue;
  132. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  133. if (tds.Count < 14) continue;
  134. model = new Fchljp62LongInfo();
  135. nodeA = tds[12].ChildNodes.Where(n => n.Name == "a").FirstOrDefault();
  136. if (nodeA == null) continue;
  137. model.qi = Convert.ToInt32(tds[0].InnerText.Trim());
  138. //model.DetailUrl = new Uri(url, optimizeUrl).AbsoluteUri;
  139. model.date = Convert.ToDateTime(tds[1].InnerText.Substring(0, 10));
  140. if (tds[2].ChildNodes.Count == 0) continue;
  141. var opencodeNode = tds[2].ChildNodes.Where(n => n.Name.ToLower() == "em").ToList();
  142. if (opencodeNode.Count < 7) continue;
  143. model.n1 = Convert.ToInt32(opencodeNode[0].InnerText.Trim());
  144. model.n2 = Convert.ToInt32(opencodeNode[1].InnerText.Trim());
  145. model.n3 = Convert.ToInt32(opencodeNode[2].InnerText.Trim());
  146. model.n4 = Convert.ToInt32(opencodeNode[3].InnerText.Trim());
  147. model.n5 = Convert.ToInt32(opencodeNode[4].InnerText.Trim());
  148. model.n6 = Convert.ToInt32(opencodeNode[5].InnerText.Trim());
  149. model.n7 = Convert.ToInt32(opencodeNode[6].InnerText.Trim());
  150. GetKaijiangDetails(ref model,tds);
  151. result.Add(model);
  152. }
  153. //var checkDataHelper = new CheckDataHelper();
  154. //var dbdata = services.GetListS<OpenCode7DTModel>(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. //result = result.OrderByDescending(S => S.Term).ToList();
  159. }
  160. catch (Exception ex)
  161. {
  162. log.Error(GetType(),
  163. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  164. }
  165. return result;
  166. }
  167. /// <summary>
  168. /// 获取主站下开奖详情
  169. /// </summary>
  170. /// <param name="nodes"></param>
  171. /// <returns></returns>
  172. private void GetKaijiangDetails(ref Fchljp62LongInfo model, List<HtmlNode> nodes)
  173. {
  174. var Trje = nodes[3].InnerText.Replace("万", "").Trim();
  175. var TotalMoney = nodes[5].InnerText.Replace("万", "").Trim();
  176. model.nextmoney = "";
  177. model.tzmoney = Trje == "" ? "0" : (double.Parse(Trje) * 10000).ToString();
  178. //组装详情
  179. //一等奖
  180. model.zj1 = nodes[4].InnerText;
  181. model.jo1 = TotalMoney == "" ? "0" : (double.Parse(TotalMoney) * 10000).ToString();
  182. //二等奖
  183. model.zj2 = nodes[6].InnerText;
  184. model.jo2 = nodes[7].InnerText.Replace(",", "");
  185. //三等奖
  186. model.zj3 = nodes[8].InnerText;
  187. model.jo3 = "2000";
  188. //四等奖
  189. model.zj4 = nodes[9].InnerText;
  190. model.jo4 = "200";
  191. //五等奖
  192. model.zj5 = nodes[10].InnerText;
  193. model.jo5 = "20";
  194. //六等奖
  195. model.zj6 = nodes[11].InnerText;
  196. model.jo6 = "5";
  197. var list = new List<Winbonus>();
  198. list.Add(new Winbonus()
  199. {
  200. item = "一等奖",
  201. wincount = model.zj1,
  202. winmoney = model.jo1
  203. });
  204. list.Add(new Winbonus()
  205. {
  206. item = "二等奖",
  207. wincount = model.zj2,
  208. winmoney = model.jo2
  209. });
  210. list.Add(new Winbonus()
  211. {
  212. item = "三等奖",
  213. wincount = model.zj3,
  214. winmoney = model.jo3
  215. });
  216. list.Add(new Winbonus()
  217. {
  218. item = "四等奖",
  219. wincount = model.zj4,
  220. winmoney = model.jo4
  221. });
  222. list.Add(new Winbonus()
  223. {
  224. item = "五等奖",
  225. wincount = model.zj5,
  226. winmoney = model.jo5
  227. });
  228. list.Add(new Winbonus()
  229. {
  230. item = "六等奖",
  231. wincount = model.zj6,
  232. winmoney = model.jo6
  233. });
  234. model.winbonus = JsonConvert.SerializeObject(list);
  235. }
  236. #region 通过副站爬取数据
  237. /// <summary>
  238. /// 副站数据爬取 地址:https://fx.cp2y.com/draw/history_10065_Y/
  239. /// </summary>
  240. /// <returns></returns>
  241. private List<Fchljp62LongInfo> GetOpenListFromBackUrl()
  242. {
  243. var result = new List<Fchljp62LongInfo>();
  244. try
  245. {
  246. var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.GetEncoding("utf-8"));
  247. if (htmlResource == null) return result;
  248. var doc = new HtmlDocument();
  249. doc.LoadHtml(htmlResource);
  250. var table = doc.DocumentNode.SelectSingleNode("//table");
  251. if (table == null) return result;
  252. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  253. List<HtmlNode> tds = null;
  254. string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
  255. Fchljp62LongInfo model = null;
  256. for (var i = 0; i < trs.Count; i++)
  257. {
  258. tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
  259. if (tds.Count < 4) continue;
  260. model = new Fchljp62LongInfo();
  261. term = tds[0].InnerText.Trim();
  262. model.date = Convert.ToDateTime(tds[1].InnerText.Trim());
  263. model.qi = Convert.ToInt32(term);
  264. openCodeString = tds[2].InnerText.Replace("&nbsp;", "").Replace("\t\t ", "").Trim();
  265. model.n1 = Convert.ToInt32(openCodeString.Substring(0, 1));
  266. model.n2 = Convert.ToInt32(openCodeString.Substring(1, 1));
  267. model.n3 = Convert.ToInt32(openCodeString.Substring(2, 1));
  268. model.n4 = Convert.ToInt32(openCodeString.Substring(3, 1));
  269. model.n5 = Convert.ToInt32(openCodeString.Substring(4, 1));
  270. model.n6 = Convert.ToInt32(openCodeString.Substring(5, 1));
  271. model.n7 = Convert.ToInt32(openCodeString.Substring(6, 1));
  272. //组装开奖详情
  273. result.Add(model);
  274. }
  275. //var checkDataHelper = new CheckDataHelper();
  276. //var dbdata = services.GetListS<OpenCode7DTModel>(currentLottery)
  277. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  278. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  279. // Config.Area, currentLottery);
  280. }
  281. catch (Exception ex)
  282. {
  283. log.Error(GetType(),
  284. string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  285. }
  286. return result;
  287. }
  288. private void DoBackUrl()
  289. {
  290. if (!string.IsNullOrEmpty(Config.BackUrl))
  291. {
  292. var openList = GetOpenListFromBackUrl();
  293. if (openList == null || openList.Count == 0) return; //无抓取数据
  294. //抓取到的最新期数
  295. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  296. //数据库里面最新期数
  297. //LatestItem = Fchljp62Data.GetLastOne();
  298. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  299. if (startQiNum > newestQiHao) return; //无最新数据
  300. //处理最新开奖数据
  301. Fchljp62LongInfo matchItem = null;
  302. for (var i = startQiNum; i <= newestQiHao; i++)
  303. {
  304. matchItem = openList.Where(R => R.qi.ToString() == i.ToString()).FirstOrDefault();
  305. if (matchItem != null)
  306. {
  307. //add db
  308. matchItem.addtime = DateTime.Now;
  309. Fchljp62Data.Add(matchItem);
  310. //Do Success Log
  311. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, i.ToString()));
  312. LatestItem = matchItem;
  313. isGetData = true;
  314. }
  315. }
  316. }
  317. }
  318. #endregion
  319. #region Attribute
  320. /// <summary>
  321. /// 配置信息
  322. /// </summary>
  323. private SCCConfig Config;
  324. /// <summary>
  325. /// 当天抓取的最新一期开奖记录
  326. /// </summary>
  327. private Fchljp62LongInfo LatestItem;
  328. #pragma warning disable CS0414 // 字段“HeiLongJiangP62Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  329. /// <summary>
  330. /// 当天抓取失败列表
  331. /// </summary>
  332. private List<string> FailedQiHaoList = null;
  333. #pragma warning restore CS0414 // 字段“HeiLongJiangP62Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  334. /// <summary>
  335. /// 日志对象
  336. /// </summary>
  337. private readonly LogHelper log;
  338. /// <summary>
  339. /// 当前彩种
  340. /// </summary>
  341. private SCCLottery currentLottery => SCCLottery.HeiLongJiangP62;
  342. /// <summary>
  343. /// 邮件接口
  344. /// </summary>
  345. private IEmail email;
  346. #pragma warning disable CS0414 // 字段“HeiLongJiangP62Job.isGetData”已被赋值,但从未使用过它的值
  347. /// <summary>
  348. /// 是否本次运行抓取到开奖数据
  349. /// </summary>
  350. private bool isGetData;
  351. #pragma warning restore CS0414 // 字段“HeiLongJiangP62Job.isGetData”已被赋值,但从未使用过它的值
  352. #endregion
  353. }
  354. }