GuangDongSZFCJob.cs 15 KB

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