QLCJob.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.QG
  14. {
  15. /// <summary>
  16. /// 七乐彩
  17. /// </summary>
  18. public class QLCJob : IJob
  19. {
  20. public QLCJob()
  21. {
  22. log = new LogHelper();
  23. email = IOC.Resolve<IEmail>();
  24. }
  25. #region QLC作业执行入口
  26. /// <summary>
  27. /// 作业执行入口
  28. /// </summary>
  29. /// <param name="context">作业执行上下文</param>
  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 FcqlcLongInfo;
  36. try
  37. {
  38. //服务启动时配置初始数据
  39. if (LatestItem == null)
  40. {
  41. LatestItem = new FcqlcLongInfo
  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)) DoMainUrl();
  50. //当今日开奖并且当前时间是晚上8点过后开始抓取
  51. if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 8) DoMainUrl();
  52. }
  53. catch (Exception ex)
  54. {
  55. log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  56. }
  57. //保存最新期号
  58. context.JobDetail.JobDataMap["LatestItem"] = LatestItem;
  59. }
  60. #endregion
  61. #region QLC数据爬取
  62. /// <summary>
  63. /// 把获取到的数据list保存到数据库
  64. /// </summary>
  65. private void DoMainUrl()
  66. {
  67. if (!string.IsNullOrEmpty(Config.MainUrl))
  68. {
  69. var openList = GetOpenListFromMainUrl(Config.MainUrl);
  70. if (openList ==null|| openList.Count == 0) return; //无抓取数据
  71. //抓取到的最新期数
  72. var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
  73. //数据库里面最新期数
  74. LatestItem = FcqlcData.GetLastOne();
  75. var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
  76. //startQiNum = 2018125;
  77. if (startQiNum > newestQiHao) return; //无最新数据
  78. //处理最新开奖数据
  79. FcqlcLongInfo matchItem = null;
  80. for (var i = startQiNum; i <= newestQiHao; i++)
  81. {
  82. matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString());
  83. if (matchItem != null)
  84. {
  85. //add db
  86. matchItem.addtime = DateTime.Now;
  87. FcqlcData.Add(matchItem);
  88. //Do Success Log
  89. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString()));
  90. LatestItem = matchItem;
  91. isGetData = true;
  92. }
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// 通过主站点爬取开奖数据
  98. /// </summary>
  99. private List<FcqlcLongInfo> GetOpenListFromMainUrl(string mainUrl)
  100. {
  101. var result = new List<FcqlcLongInfo>();
  102. try
  103. {
  104. var url = new Uri(mainUrl);
  105. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  106. if (htmlResource == null) return result;
  107. var doc = new HtmlDocument();
  108. doc.LoadHtml(htmlResource);
  109. var table = doc.DocumentNode.SelectSingleNode("//table");
  110. if (table == null) return result;
  111. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  112. FcqlcLongInfo model = null;
  113. var optimizeUrl = string.Empty;
  114. for (var i = 0; i < trs.Count; i++) //第一二行为表头
  115. {
  116. var trstyle = trs[i].Attributes["style"];
  117. if (trstyle != null && trstyle.Value == "display:none") continue;
  118. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  119. if (tds.Count < 9) continue;
  120. model = new FcqlcLongInfo();
  121. model.qi = Convert.ToInt32(tds[0].InnerText.Trim());
  122. optimizeUrl = model.qi.ToString();
  123. model.date = Convert.ToDateTime(tds[1].InnerText.Substring(0, 5));
  124. var openCodeString = tds[2].InnerText.Replace(" ", "").Trim();
  125. model.n1 = Convert.ToInt32(openCodeString.Substring(0, 2));
  126. model.n2 = Convert.ToInt32(openCodeString.Substring(3, 2));
  127. model.n3 = Convert.ToInt32(openCodeString.Substring(6, 2));
  128. model.n4 = Convert.ToInt32(openCodeString.Substring(9, 2));
  129. model.n5 = Convert.ToInt32(openCodeString.Substring(12, 2));
  130. model.n6 = Convert.ToInt32(openCodeString.Substring(15, 2));
  131. model.n7 = Convert.ToInt32(openCodeString.Substring(18, 2));
  132. model.n8 = Convert.ToInt32(openCodeString.Substring(22, 3));
  133. var details = GetKaijiangDetails(optimizeUrl);
  134. var list = new List<Winbonus>();
  135. model.zj1 = details.zj1;
  136. model.jo1 = details.jo1;
  137. list.Add(new Winbonus()
  138. {
  139. item = "一等奖",
  140. wincount = model.zj1,
  141. winmoney = model.jo1
  142. });
  143. model.zj2 = details.zj2;
  144. model.jo2 = details.jo2;
  145. list.Add(new Winbonus()
  146. {
  147. item = "二等奖",
  148. wincount = model.zj2,
  149. winmoney = model.jo2
  150. });
  151. model.zj3 = details.zj3;
  152. model.jo3 = details.jo3;
  153. list.Add(new Winbonus()
  154. {
  155. item = "三等奖",
  156. wincount = model.zj3,
  157. winmoney = model.jo3
  158. });
  159. model.zj4 = details.zj4;
  160. list.Add(new Winbonus()
  161. {
  162. item = "四等奖",
  163. wincount = model.zj4,
  164. winmoney = "200",
  165. });
  166. model.zj5 = details.zj5;
  167. list.Add(new Winbonus()
  168. {
  169. item = "五等奖",
  170. wincount = model.zj5,
  171. winmoney = "50",
  172. });
  173. model.zj6 = details.zj6;
  174. list.Add(new Winbonus()
  175. {
  176. item = "六等奖",
  177. wincount = model.zj6,
  178. winmoney = "10",
  179. });
  180. model.zj7 = details.zj7;
  181. list.Add(new Winbonus()
  182. {
  183. item = "七等奖",
  184. wincount = model.zj7,
  185. winmoney = "5",
  186. });
  187. model.winbonus = JsonConvert.SerializeObject(list);
  188. model.tzmoney = details.tzmoney;
  189. model.nextmoney = details.nextmoney;
  190. if (model.tzmoney !="0")
  191. model.isupdetails = true;
  192. result.Add(model);
  193. }
  194. //var checkDataHelper = new CheckDataHelper();
  195. //var dbdata = services.GetListS<OpenCode8DTModel>(currentLottery)
  196. // .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  197. //checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
  198. // Config.Area, currentLottery);
  199. //result = result.OrderByDescending(S => S.Term).ToList();
  200. }
  201. catch (Exception ex)
  202. {
  203. log.Error(GetType(),
  204. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  205. }
  206. return result;
  207. }
  208. /// <summary>
  209. /// 获取详情
  210. /// </summary>
  211. /// <param name="optimizeUrl"></param>
  212. /// <returns></returns>
  213. private FcqlcLongInfo GetKaijiangDetails(string optimizeUrl)
  214. {
  215. var url = "https://www.8200.cn/kjh/qlc/" + optimizeUrl + ".htm";
  216. var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("utf-8"));
  217. var doc = new HtmlDocument();
  218. doc.LoadHtml(htmlResource);
  219. var div = doc.DocumentNode.SelectSingleNode("//div[@class='text-16']");
  220. if (div == null) return null;
  221. //爬去奖金
  222. var jiangjin = div.ChildNodes.Where(node => node.Name == "p").ToList();
  223. //爬去奖项
  224. //var tbody = div.ChildNodes.Where(node => node.Name == "tbody").ToList();
  225. var table = doc.DocumentNode.SelectSingleNode("//table");
  226. var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
  227. var gdje = jiangjin[2].InnerText.Replace(" 万元", "").Replace("奖池滚存:", "").Replace("--", "0").Replace(",", "")
  228. .Trim();
  229. var trje = jiangjin[1].InnerText.Replace(" 万元", "").Replace("本期销量:", "").Replace("--", "0").Replace(",", "")
  230. .Trim();
  231. var entity = new FcqlcLongInfo
  232. {
  233. nextmoney = gdje == "0" ? "0" : (double.Parse(gdje) * 10000).ToString(),
  234. tzmoney = trje == "0" ? "0" : (double.Parse(trje) * 10000).ToString()
  235. };
  236. //TODO
  237. //组装详情
  238. for (var i = 0; i < trs.Count; i++)
  239. {
  240. var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
  241. var TotalMoney = tds[1].InnerText.Replace("元", "").Replace("--", "0").Replace(",", "").Trim();
  242. string name = tds[0].InnerText.Trim();
  243. string money = TotalMoney == "0" ? "0" : double.Parse(TotalMoney).ToString();
  244. string quantity = tds[2].InnerText.Trim().Replace(" 注", "").Replace("--", "0").Trim();
  245. if (name == "一等奖")
  246. {
  247. entity.zj1 = quantity;
  248. entity.jo1 = money;
  249. }
  250. else if (name == "二等奖")
  251. {
  252. entity.zj2 = quantity;
  253. entity.jo2 = money;
  254. }
  255. else if (name == "三等奖")
  256. {
  257. entity.zj3 = quantity;
  258. entity.jo3 = money;
  259. }
  260. else if (name == "四等奖")
  261. {
  262. entity.zj4 = quantity;
  263. }
  264. else if (name == "五等奖")
  265. {
  266. entity.zj5 = quantity;
  267. }
  268. else if (name == "六等奖")
  269. {
  270. entity.zj6 = quantity;
  271. }
  272. else if (name == "七等奖")
  273. {
  274. entity.zj7 = quantity;
  275. }
  276. }
  277. return entity;
  278. }
  279. #endregion
  280. #region QLC初始化
  281. /// <summary>
  282. /// 配置信息
  283. /// </summary>
  284. private SCCConfig Config;
  285. /// <summary>
  286. /// 当天抓取的最新一期开奖记录
  287. /// </summary>
  288. private FcqlcLongInfo LatestItem;
  289. #pragma warning disable CS0414 // 字段“QLCJob.FailedQiHaoList”已被赋值,但从未使用过它的值
  290. /// <summary>
  291. /// 当天抓取失败列表
  292. /// </summary>
  293. private List<string> FailedQiHaoList = null;
  294. #pragma warning restore CS0414 // 字段“QLCJob.FailedQiHaoList”已被赋值,但从未使用过它的值
  295. /// <summary>
  296. /// 日志对象
  297. /// </summary>
  298. private readonly LogHelper log;
  299. /// <summary>
  300. /// 当前彩种
  301. /// </summary>
  302. private SCCLottery currentLottery => SCCLottery.QLC;
  303. /// <summary>
  304. /// 邮件接口
  305. /// </summary>
  306. private IEmail email;
  307. #pragma warning disable CS0414 // 字段“QLCJob.isGetData”已被赋值,但从未使用过它的值
  308. /// <summary>
  309. /// 是否本次运行抓取到开奖数据
  310. /// </summary>
  311. private bool isGetData;
  312. #pragma warning restore CS0414 // 字段“QLCJob.isGetData”已被赋值,但从未使用过它的值
  313. #endregion
  314. }
  315. }