XinJiangFC35X7Job.cs 16 KB

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