XinJiangFC25X7Job.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. /// 新疆25选7
  17. /// </summary>
  18. [DisallowConcurrentExecution]
  19. [PersistJobDataAfterExecution]
  20. public class XinJiangFC25X7Job : IJob
  21. {
  22. /// <summary>
  23. /// 初始化函数
  24. /// </summary>
  25. public XinJiangFC25X7Job()
  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 Fcxj25x7LongInfo;
  40. try
  41. {
  42. //服务启动时配置初始数据
  43. if (LatestItem == null)
  44. {
  45. LatestItem = new Fcxj25x7LongInfo
  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 Fcxj25x7LongInfo
  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 = Fcxj25x7Data.GetLastOne();
  92. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  93. if (startQiNum > newestQiHao) return; //无最新数据
  94. //处理最新开奖数据
  95. Fcxj25x7LongInfo 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. Fcxj25x7Data.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<Fcxj25x7LongInfo> GetOpenListFromMainUrl(string mainUrl)
  113. {
  114. var result = new List<Fcxj25x7LongInfo>();
  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. Fcxj25x7LongInfo 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 Fcxj25x7LongInfo();
  135. nodeA = tds[13].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. model.n8 = Convert.ToInt32(opencodeNode[7].InnerText.Trim());
  151. GetKaijiangDetails(ref model,tds);
  152. result.Add(model);
  153. }
  154. //var checkDataHelper = new CheckDataHelper();
  155. //var dbdata = services.GetListS<OpenCode8DTModel>(currentLottery)
  156. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  157. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  158. // Config.Area, currentLottery);
  159. //result = result.OrderByDescending(S => S.Term).ToList();
  160. }
  161. catch (Exception ex)
  162. {
  163. log.Error(GetType(),
  164. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  165. }
  166. return result;
  167. }
  168. /// <summary>
  169. /// 获取主站下开奖详情
  170. /// </summary>
  171. /// <param name="nodes"></param>
  172. /// <returns></returns>
  173. private void GetKaijiangDetails(ref Fcxj25x7LongInfo model, List<HtmlNode> nodes)
  174. {
  175. //组装详情
  176. //特等奖
  177. model.zj = nodes[3].InnerText;
  178. model.jo = nodes[4].InnerText;
  179. //一等奖
  180. model.zj1 = nodes[5].InnerText;
  181. model.jo1 = nodes[6].InnerText;
  182. //二等奖
  183. model.zj2 = nodes[7].InnerText;
  184. model.jo2 = "1000";
  185. //三等奖
  186. model.zj3 = nodes[8].InnerText;
  187. model.jo3 = "200";
  188. //四等奖
  189. model.zj4 = nodes[9].InnerText;
  190. model.jo4 = "100";
  191. //五等奖
  192. model.zj5 = nodes[10].InnerText;
  193. model.jo5 = "10";
  194. //六等奖
  195. model.zj6 = nodes[11].InnerText;
  196. model.jo6 = "3";
  197. //七等奖
  198. model.zj7 = nodes[12].InnerText;
  199. model.jo7 = "1";
  200. var list = new List<Winbonus>();
  201. list.Add(new Winbonus()
  202. {
  203. item = "特等奖",
  204. wincount = model.zj,
  205. winmoney = model.jo
  206. });
  207. list.Add(new Winbonus()
  208. {
  209. item = "一等奖",
  210. wincount = model.zj1,
  211. winmoney = model.jo1
  212. });
  213. list.Add(new Winbonus()
  214. {
  215. item = "二等奖",
  216. wincount = model.zj2,
  217. winmoney = model.jo2
  218. });
  219. list.Add(new Winbonus()
  220. {
  221. item = "三等奖",
  222. wincount = model.zj3,
  223. winmoney = model.jo3
  224. });
  225. list.Add(new Winbonus()
  226. {
  227. item = "四等奖",
  228. wincount = model.zj4,
  229. winmoney = model.jo4
  230. });
  231. list.Add(new Winbonus()
  232. {
  233. item = "五等奖",
  234. wincount = model.zj5,
  235. winmoney = model.jo5
  236. });
  237. list.Add(new Winbonus()
  238. {
  239. item = "六等奖",
  240. wincount = model.zj6,
  241. winmoney = model.jo6
  242. });
  243. list.Add(new Winbonus()
  244. {
  245. item = "七等奖",
  246. wincount = model.zj7,
  247. winmoney = model.jo7
  248. });
  249. model.winbonus = JsonConvert.SerializeObject(list);
  250. }
  251. #region 通过副站爬取数据
  252. /// <summary>
  253. /// 副站数据爬取 地址:https://fx.cp2y.com/draw/history_10065_Y/
  254. /// </summary>
  255. /// <returns></returns>
  256. private List<Fcxj25x7LongInfo> GetOpenListFromBackUrl()
  257. {
  258. var result = new List<Fcxj25x7LongInfo>();
  259. try
  260. {
  261. var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.Default);
  262. if (htmlResource == null) return result;
  263. var doc = new HtmlDocument();
  264. doc.LoadHtml(htmlResource);
  265. var table = doc.DocumentNode.SelectSingleNode("//table");
  266. if (table == null) return result;
  267. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  268. List<HtmlNode> tds = null;
  269. string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
  270. #pragma warning disable CS0219 // 变量“pool”已被赋值,但从未使用过它的值
  271. #pragma warning disable CS0219 // 变量“sales”已被赋值,但从未使用过它的值
  272. decimal sales = 0, pool = 0;
  273. #pragma warning restore CS0219 // 变量“sales”已被赋值,但从未使用过它的值
  274. #pragma warning restore CS0219 // 变量“pool”已被赋值,但从未使用过它的值
  275. Fcxj25x7LongInfo model = null;
  276. #pragma warning disable CS0219 // 变量“nodeA”已被赋值,但从未使用过它的值
  277. HtmlNode nodeA = null;
  278. #pragma warning restore CS0219 // 变量“nodeA”已被赋值,但从未使用过它的值
  279. for (var i = 0; i < trs.Count; i++)
  280. {
  281. tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
  282. if (tds.Count < 4) continue;
  283. model = new Fcxj25x7LongInfo();
  284. term = tds[0].InnerText.Trim();
  285. if (term.StartsWith((CommonHelper.SCCSysDateTime.Year - 1).ToString())) break;
  286. model.qi = Convert.ToInt32(term);
  287. openCodeString = tds[1].InnerText.Replace("&nbsp;", "").Trim();
  288. model.n1 = Convert.ToInt32(openCodeString.Substring(0, 2));
  289. model.n2 = Convert.ToInt32(openCodeString.Substring(2, 2));
  290. model.n3 = Convert.ToInt32(openCodeString.Substring(4, 2));
  291. model.n4 = Convert.ToInt32(openCodeString.Substring(6, 2));
  292. model.n5 = Convert.ToInt32(openCodeString.Substring(8, 2));
  293. model.n6 = Convert.ToInt32(openCodeString.Substring(10, 2));
  294. model.n7 = Convert.ToInt32(openCodeString.Substring(12, 2));
  295. model.n8 = Convert.ToInt32(openCodeString.Substring(14, 2));
  296. model.date = Convert.ToDateTime(tds[9].InnerText.Trim());
  297. //组装开奖详情
  298. result.Add(model);
  299. }
  300. //var checkDataHelper = new CheckDataHelper();
  301. //var dbdata = services.GetListS<OpenCode8DTModel>(currentLottery)
  302. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  303. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  304. // Config.Area, currentLottery);
  305. }
  306. catch (Exception ex)
  307. {
  308. log.Error(GetType(),
  309. string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  310. }
  311. return result;
  312. }
  313. private void DoBackUrl()
  314. {
  315. if (!string.IsNullOrEmpty(Config.BackUrl))
  316. {
  317. var openList = GetOpenListFromBackUrl();
  318. if (openList == null || openList.Count == 0) return; //无抓取数据
  319. //抓取到的最新期数
  320. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  321. //数据库里面最新期数
  322. //LatestItem = Fcxj25x7Data.GetLastOne();
  323. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  324. if (startQiNum > newestQiHao) return; //无最新数据
  325. //处理最新开奖数据
  326. Fcxj25x7LongInfo matchItem = null;
  327. for (var i = startQiNum; i <= newestQiHao; i++)
  328. {
  329. matchItem = openList.Where(R => R.qi.ToString() == i.ToString()).FirstOrDefault();
  330. if (matchItem != null)
  331. {
  332. //add db
  333. matchItem.addtime = DateTime.Now;
  334. Fcxj25x7Data.Add(matchItem);
  335. //Do Success Log
  336. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, i.ToString()));
  337. LatestItem = matchItem;
  338. isGetData = true;
  339. }
  340. }
  341. }
  342. }
  343. #endregion
  344. #region Attribute
  345. /// <summary>
  346. /// 配置信息
  347. /// </summary>
  348. private SCCConfig Config;
  349. /// <summary>
  350. /// 当天抓取的最新一期开奖记录
  351. /// </summary>
  352. private Fcxj25x7LongInfo LatestItem;
  353. #pragma warning disable CS0414 // 字段“XinJiangFC25X7Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  354. /// <summary>
  355. /// 当天抓取失败列表
  356. /// </summary>
  357. private List<string> FailedQiHaoList = null;
  358. #pragma warning restore CS0414 // 字段“XinJiangFC25X7Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  359. /// <summary>
  360. /// 日志对象
  361. /// </summary>
  362. private readonly LogHelper log;
  363. /// <summary>
  364. /// 当前彩种
  365. /// </summary>
  366. private SCCLottery currentLottery => SCCLottery.XinJiangFC25X7;
  367. /// <summary>
  368. /// 邮件接口
  369. /// </summary>
  370. private IEmail email;
  371. #pragma warning disable CS0414 // 字段“XinJiangFC25X7Job.isGetData”已被赋值,但从未使用过它的值
  372. /// <summary>
  373. /// 是否本次运行抓取到开奖数据
  374. /// </summary>
  375. private bool isGetData;
  376. #pragma warning restore CS0414 // 字段“XinJiangFC25X7Job.isGetData”已被赋值,但从未使用过它的值
  377. #endregion
  378. }
  379. }