ZheJiang20x5Job.cs 15 KB

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