QXCSkillJob.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using HtmlAgilityPack;
  6. using Quartz;
  7. using SCC.Common;
  8. using SCC.Interface;
  9. using SCC.Models;
  10. namespace SCC.Crawler.LotterySkill
  11. {
  12. public class QXCSkillJob : IJob
  13. { /// <summary>
  14. /// 构造函数
  15. /// </summary>
  16. public QXCSkillJob()
  17. {
  18. log = new LogHelper();
  19. services = IOC.Resolve<IDTOpenCode>();
  20. email = IOC.Resolve<IEmail>();
  21. }
  22. /// <summary>
  23. /// 执行入口
  24. /// </summary>
  25. /// <param name="context"></param>
  26. public void Execute(IJobExecutionContext context)
  27. {
  28. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  29. DoMainUrl();
  30. }
  31. /// <summary>
  32. /// 执行主站技巧
  33. /// </summary>
  34. private void DoMainUrl()
  35. {
  36. List<string> urls = GetMainUrl(Config);
  37. #pragma warning disable CS0219 // 变量“lotterySkill”已被赋值,但从未使用过它的值
  38. LotterySkillModel lotterySkill = null;
  39. #pragma warning restore CS0219 // 变量“lotterySkill”已被赋值,但从未使用过它的值
  40. foreach (string url in urls)
  41. {
  42. List<LotterySkillModel> res = GetOpenListFromMainUrl(url);
  43. foreach (var lotterySkillModel in res)
  44. {
  45. if (services.LotterySkillModel(currentLottery, lotterySkillModel))
  46. {
  47. //Do Success Log
  48. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, lotterySkillModel.Title));
  49. isGetData = true;
  50. }
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// 组装主站爬取地址
  56. /// </summary>
  57. /// <param name="config"></param>
  58. /// <returns></returns>
  59. private List<string> GetMainUrl(SCCConfig config)
  60. {
  61. List<string> urlList = new List<string>();
  62. string url = config.MainUrl;
  63. int pages = config.MainUrlPages > 0 ? config.MainUrlPages : 1;
  64. for (int i = 1; i <= pages; i++)
  65. {
  66. string res = string.Format(url, i);
  67. if (!urlList.Contains(res))
  68. {
  69. urlList.Add(res);
  70. }
  71. }
  72. return urlList;
  73. }
  74. /// <summary>
  75. /// 爬取主站技巧列表
  76. /// </summary>
  77. /// <param name="mainUrl"></param>
  78. /// <returns></returns>
  79. private List<LotterySkillModel> GetOpenListFromMainUrl(string mainUrl)
  80. {
  81. var result = new List<LotterySkillModel>();
  82. try
  83. {
  84. var url = new Uri(mainUrl);
  85. var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("utf-8"));
  86. if (htmlResource == null) return result;
  87. HtmlDocument doc = new HtmlDocument();
  88. doc.LoadHtml(htmlResource);
  89. //获取li下面所有a标签
  90. HtmlNodeCollection nodeList = doc.DocumentNode.SelectNodes("//*[@class='art-list']/ul/li/a");
  91. if (nodeList == null) return result;
  92. List<string> urls = new List<string>();
  93. //遍历a标签
  94. foreach (HtmlNode node in nodeList)
  95. {
  96. HtmlAttribute attr = node.Attributes.SingleOrDefault(a => a.Name.Equals("href"));
  97. if (attr != null)
  98. {
  99. string href = Host + attr.Value;
  100. //去重
  101. if (!urls.Contains(href))
  102. {
  103. urls.Add(href);
  104. }
  105. }
  106. }
  107. foreach (var url1 in urls)
  108. {
  109. var LotterySkill = GetSkillModel(url1);
  110. result.Add(LotterySkill);
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. log.Error(GetType(),
  116. string.Format("【{0}】通过主抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  117. }
  118. return result;
  119. }
  120. /// <summary>
  121. /// 根据主站url获取技巧详情
  122. /// </summary>
  123. /// <param name="url"></param>
  124. /// <returns></returns>
  125. private LotterySkillModel GetSkillModel(string url)
  126. {
  127. LotterySkillModel lotterySkill = new LotterySkillModel();
  128. try
  129. {
  130. var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("utf-8"));
  131. if (htmlResource == null) return lotterySkill;
  132. HtmlDocument doc = new HtmlDocument();
  133. doc.LoadHtml(htmlResource);
  134. //获取li下面所有a标签
  135. var div = doc.DocumentNode.SelectSingleNode("//*[@class='artile']");
  136. var Title = div.ChildNodes.Where(node => node.Name == "h1").ToList();
  137. var div1 = div.ChildNodes.Where(node => node.Name == "div").ToList();
  138. lotterySkill.Title = Title[0].InnerText.Trim();
  139. lotterySkill.Author = "cn55128";
  140. lotterySkill.Content = div1[1].InnerHtml.Trim();
  141. lotterySkill.IsDelete = false;
  142. lotterySkill.SourceUrl = url.ToString();
  143. lotterySkill.TypeId = lotterySkillType;
  144. lotterySkill.TypeName = lotterySkillType.GetEnumDescription();
  145. }
  146. catch (Exception ex)
  147. {
  148. log.Error(GetType(),
  149. string.Format("【{0}】通过主抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  150. }
  151. return lotterySkill;
  152. }
  153. #region Attribute
  154. /// <summary>
  155. /// 主机地址
  156. /// </summary>
  157. public string Host = "http://www.55125.cn/";
  158. /// <summary>
  159. /// 配置信息
  160. /// </summary>
  161. private SCCConfig Config;
  162. #pragma warning disable CS0414 // 字段“QXCSkillJob.LatestItem”已被赋值,但从未使用过它的值
  163. /// <summary>
  164. /// 当天抓取的最新一期开奖记录
  165. /// </summary>
  166. private LotterySkillModel LatestItem = null;
  167. #pragma warning restore CS0414 // 字段“QXCSkillJob.LatestItem”已被赋值,但从未使用过它的值
  168. #pragma warning disable CS0414 // 字段“QXCSkillJob.FailedQiHaoList”已被赋值,但从未使用过它的值
  169. /// <summary>
  170. /// 当天抓取失败列表
  171. /// </summary>
  172. private List<string> FailedQiHaoList = null;
  173. #pragma warning restore CS0414 // 字段“QXCSkillJob.FailedQiHaoList”已被赋值,但从未使用过它的值
  174. /// <summary>
  175. /// 日志对象
  176. /// </summary>
  177. private readonly LogHelper log;
  178. /// <summary>
  179. /// 数据服务
  180. /// </summary>
  181. private readonly IDTOpenCode services;
  182. /// <summary>
  183. /// 当前彩种
  184. /// </summary>
  185. private SCCLottery currentLottery => SCCLottery.LotterySkill;
  186. /// <summary>
  187. /// 福彩3D技巧
  188. /// </summary>
  189. private LotterySkillType lotterySkillType = LotterySkillType.QXC;
  190. /// <summary>
  191. /// 邮件接口
  192. /// </summary>
  193. private IEmail email;
  194. #pragma warning disable CS0414 // 字段“QXCSkillJob.isGetData”已被赋值,但从未使用过它的值
  195. /// <summary>
  196. /// 是否本次运行抓取到开奖数据
  197. /// </summary>
  198. private bool isGetData = false;
  199. #pragma warning restore CS0414 // 字段“QXCSkillJob.isGetData”已被赋值,但从未使用过它的值
  200. #endregion
  201. }
  202. }