LiaoNingFC35X7Job.cs 15 KB

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