SHSSCSkillJob.cs 7.0 KB

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