ShangHaiTTCX4Job.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.Linq;
  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. /// 上海天天彩
  17. /// </summary>
  18. [DisallowConcurrentExecution]
  19. [PersistJobDataAfterExecution]
  20. public class ShangHaiTTCX4Job : IJob
  21. {
  22. /// <summary>
  23. /// 初始化函数
  24. /// </summary>
  25. public ShangHaiTTCX4Job()
  26. {
  27. log = new LogHelper();
  28. email = IOC.Resolve<IEmail>();
  29. }
  30. public void Execute(IJobExecutionContext context)
  31. {
  32. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  33. //预设节假日不开奖
  34. if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
  35. LatestItem = context.JobDetail.JobDataMap["LatestItem"] as Fcshttc4;
  36. try
  37. {
  38. //服务启动时配置初始数据
  39. if (LatestItem == null)
  40. {
  41. LatestItem = new Fcshttc4
  42. {
  43. qi = CommonHelper.GenerateQiHaoYYYYQQQ(0),
  44. date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
  45. };
  46. }
  47. //程序时间第二天,程序根据配置检查是否昨天有开奖
  48. isGetData = false;
  49. if (CommonHelper.CheckDTIsNeedGetData(Config)) //
  50. {
  51. DoBackUrl();
  52. DoMainUrl();
  53. }
  54. if (!LatestItem.qi.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy")))
  55. LatestItem = new Fcshttc4
  56. {
  57. qi = CommonHelper.GenerateQiHaoYYYYQQQ(0),
  58. date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
  59. };
  60. //当今日开奖并且当前时间是晚上8点过后开始抓取
  61. DoBackUrl();
  62. DoMainUrl();
  63. }
  64. catch (Exception ex)
  65. {
  66. log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  67. }
  68. //保存最新期号
  69. context.JobDetail.JobDataMap["LatestItem"] = LatestItem;
  70. }
  71. private void DoMainUrl()
  72. {
  73. if (!string.IsNullOrEmpty(Config.MainUrl))
  74. {
  75. var openList = GetOpenListFromMainUrl(Config.MainUrl);
  76. if (openList == null || openList.Count == 0) return; //无抓取数据
  77. //抓取到的最新期数
  78. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  79. //数据库里面最新期数
  80. LatestItem = Fcshttc4Data.GetLastOne();
  81. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  82. if (startQiNum == 0)
  83. startQiNum = openList.OrderBy(m => m.qi).First().qi;
  84. if (startQiNum > newestQiHao) return; //无最新数据
  85. //处理最新开奖数据
  86. Fcshttc4 matchItem = null;
  87. for (var i = startQiNum; i <= newestQiHao; i++)
  88. {
  89. matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString());
  90. if (matchItem != null)
  91. {
  92. //add db
  93. matchItem.addtime = DateTime.Now;
  94. Fcshttc4Data.Add(matchItem);
  95. //Do Success Log
  96. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString()));
  97. LatestItem = matchItem;
  98. isGetData = true;
  99. }
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// 爬取主网站信息
  105. /// </summary>
  106. /// <returns></returns>
  107. private List<Fcshttc4> GetOpenListFromMainUrl(string mainUrl)
  108. {
  109. var result = new List<Fcshttc4>();
  110. try
  111. {
  112. var mainurl = mainUrl + "?type=4d&number=50";
  113. var htmlResource = NetHelper.GetUrlResponse(mainurl);
  114. if (htmlResource == null) return result;
  115. string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
  116. Fcshttc4 model = null;
  117. var data = htmlResource.JsonToEntity<JObject>();
  118. for (var i = 0; i < data["Models"].Count(); i++)
  119. {
  120. model = new Fcshttc4();
  121. term = data["Models"][i]["no"].ToString();
  122. model.qi = int.Parse(term);
  123. model.date = DateTime.Now;
  124. openCodeString = data["Models"][i]["Number"].ToString();
  125. var openCode = openCodeString.Split(',');
  126. model.n1 = Convert.ToInt32(openCode[0]);
  127. model.n2 = Convert.ToInt32(openCode[1]);
  128. model.n3 = Convert.ToInt32(openCode[2]);
  129. model.n4 = Convert.ToInt32(openCode[3]);
  130. //model.Spare = string.Empty;
  131. //model.DetailUrl = mainUrl;
  132. result.Add(model);
  133. }
  134. //var checkDataHelper = new CheckDataHelper();
  135. //var dbdata = services.GetListS<OpenCode4DTModel>(currentLottery)
  136. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  137. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  138. // Config.Area, currentLottery);
  139. }
  140. catch (Exception ex)
  141. {
  142. log.Error(GetType(),
  143. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  144. }
  145. return result;
  146. }
  147. /// <summary>
  148. /// 获取备用站点开奖列表数据
  149. /// </summary>
  150. /// <returns></returns>
  151. private List<Fcshttc4> GetOpenListFromBackUrl()
  152. {
  153. var result = new List<Fcshttc4>();
  154. try
  155. {
  156. var url = new Uri(Config.BackUrl);
  157. var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.GetEncoding("gb2312"));
  158. if (htmlResource == null) return result;
  159. var doc = new HtmlDocument();
  160. doc.LoadHtml(htmlResource);
  161. var table = doc.DocumentNode.SelectSingleNode("//table");
  162. if (table == null) return result;
  163. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  164. Fcshttc4 model = null;
  165. HtmlNode nodeA = null;
  166. var optimizeUrl = string.Empty;
  167. for (var i = 1; i < trs.Count; i++) //第一行为表头
  168. {
  169. var trstyle = trs[i].Attributes["style"];
  170. if (trstyle != null && trstyle.Value == "display:none") continue;
  171. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  172. if (tds.Count < 4) continue;
  173. model = new Fcshttc4();
  174. nodeA = tds[0].ChildNodes.Where(n => n.Name == "a").FirstOrDefault();
  175. if (nodeA == null) continue;
  176. model.qi = Convert.ToInt32(nodeA.InnerText.Trim());
  177. optimizeUrl = nodeA.Attributes["href"].Value;
  178. //model.DetailUrl = new Uri(url, optimizeUrl).AbsoluteUri;
  179. model.date = Convert.ToDateTime(tds[3].InnerText);
  180. if (tds[0].ChildNodes.Count == 0) continue;
  181. var opencodeNode = tds[1].ChildNodes.Where(n => n.Name.ToLower() == "i").ToList();
  182. if (opencodeNode.Count < 4) continue;
  183. model.n1 = Convert.ToInt32(opencodeNode[0].InnerText.Trim());
  184. model.n2 = Convert.ToInt32(opencodeNode[1].InnerText.Trim());
  185. model.n3 = Convert.ToInt32(opencodeNode[2].InnerText.Trim());
  186. model.n4 = Convert.ToInt32(opencodeNode[3].InnerText.Trim());
  187. result.Add(model);
  188. }
  189. //var checkDataHelper = new CheckDataHelper();
  190. //var dbdata = services.GetListS<OpenCode4DTModel>(currentLottery)
  191. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  192. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  193. // Config.Area, currentLottery);
  194. //result = result.OrderByDescending(S => S.Term).ToList();
  195. }
  196. catch (Exception ex)
  197. {
  198. log.Error(GetType(),
  199. string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  200. }
  201. return result;
  202. }
  203. /// <summary>
  204. /// 验证以前的七号数据
  205. /// </summary>
  206. private void DoBackUrl()
  207. {
  208. if (!string.IsNullOrEmpty(Config.BackUrl))
  209. {
  210. var openList = GetOpenListFromBackUrl();
  211. if (openList == null || openList.Count == 0) return; //无抓取数据
  212. //抓取到的最新期数
  213. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  214. //数据库里面最新期数
  215. //LatestItem = Fcshttc4Data.GetLastOne();
  216. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  217. if (startQiNum > newestQiHao) return; //无最新数据
  218. //处理最新开奖数据
  219. Fcshttc4 matchItem = null;
  220. for (var i = startQiNum; i <= newestQiHao; i++)
  221. {
  222. matchItem = openList.Where(R => R.qi.ToString() == i.ToString()).FirstOrDefault();
  223. if (matchItem != null)
  224. {
  225. //add db
  226. matchItem.addtime = DateTime.Now;
  227. Fcshttc4Data.Add(matchItem);
  228. //Do Success Log
  229. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, i.ToString()));
  230. LatestItem = matchItem;
  231. isGetData = true;
  232. }
  233. }
  234. }
  235. }
  236. /// <summary>
  237. /// 获取主站下开奖详情
  238. /// </summary>
  239. /// <param name="nodes"></param>
  240. /// <returns></returns>
  241. /// <summary>
  242. /// 配置信息
  243. /// </summary>
  244. private SCCConfig Config;
  245. /// <summary>
  246. /// 当天抓取的最新一期开奖记录
  247. /// </summary>
  248. private Fcshttc4 LatestItem;
  249. #pragma warning disable CS0414 // 字段“ShangHaiTTCX4Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  250. /// <summary>
  251. /// 当天抓取失败列表
  252. /// </summary>
  253. private List<string> FailedQiHaoList = null;
  254. #pragma warning restore CS0414 // 字段“ShangHaiTTCX4Job.FailedQiHaoList”已被赋值,但从未使用过它的值
  255. /// <summary>
  256. /// 日志对象
  257. /// </summary>
  258. private readonly LogHelper log;
  259. /// <summary>
  260. /// 当前彩种
  261. /// </summary>
  262. private SCCLottery currentLottery => SCCLottery.ShangHaiTTCX4;
  263. /// <summary>
  264. /// 邮件接口
  265. /// </summary>
  266. private IEmail email;
  267. #pragma warning disable CS0414 // 字段“ShangHaiTTCX4Job.isGetData”已被赋值,但从未使用过它的值
  268. /// <summary>
  269. /// 是否本次运行抓取到开奖数据
  270. /// </summary>
  271. private bool isGetData;
  272. #pragma warning restore CS0414 // 字段“ShangHaiTTCX4Job.isGetData”已被赋值,但从未使用过它的值
  273. }
  274. }