BJK3Job.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using Quartz;
  6. using SCC.Common;
  7. using SCC.Crawler.Tools;
  8. using SCC.Interface;
  9. using SCC.Models;
  10. using SCC.Services;
  11. namespace SCC.Crawler.GP
  12. {
  13. /// <summary>
  14. /// 北京快3
  15. /// </summary>
  16. [DisallowConcurrentExecution]
  17. [PersistJobDataAfterExecution]
  18. public class BJK3Job : IJob
  19. {
  20. /// <summary>
  21. /// 构造函数
  22. /// </summary>
  23. public BJK3Job()
  24. {
  25. log = new LogHelper();
  26. services = IOC.Resolve<IOpen3Code>();
  27. email = IOC.Resolve<IEmail>();
  28. }
  29. /// <summary>
  30. /// 作业执行入口
  31. /// </summary>
  32. /// <param name="context">作业执行上下文</param>
  33. public void Execute(IJobExecutionContext context)
  34. {
  35. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  36. //预设节假日不开奖
  37. if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
  38. LatestQiHao = context.JobDetail.JobDataMap.GetString("LatestQiHao");
  39. try
  40. {
  41. //服务启动时配置初始数据
  42. if (string.IsNullOrEmpty(LatestQiHao))
  43. {
  44. var lastItem = services.GetLastItem(currentLottery);
  45. if (lastItem != null) LatestQiHao = lastItem.Term.ToString();
  46. }
  47. //第一次启动服务或最新期号为昨天的开奖期号,则自检昨天开奖数据是否抓取完毕(否则插入邮件数据),并重置当天期号和失败列表
  48. if (string.IsNullOrEmpty(LatestQiHao) ||
  49. !LatestQiHao.StartsWith(CommonHelper.SCCSysDateTime.ToString("yyMMdd")))
  50. {
  51. CheckingYesterdayTheLotteryData();
  52. LatestQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQQ(0);
  53. }
  54. //当最新期号不符合当天总期数,执行当天作业
  55. if (Convert.ToInt32(LatestQiHao.Substring(6)) != Config.TimesPerDay)
  56. {
  57. DoTodayJobByMainUrl();
  58. DoTodayJobByBackUrl();
  59. }
  60. }
  61. catch (Exception ex)
  62. {
  63. log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  64. }
  65. //保存最新期号和失败期号列表
  66. context.JobDetail.JobDataMap["LatestQiHao"] = LatestQiHao;
  67. }
  68. /// <summary>
  69. /// 自检昨天开奖数据
  70. /// </summary>
  71. private void CheckingYesterdayTheLotteryData()
  72. {
  73. if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd")))
  74. return; //如果昨日设定不开奖则不自检昨日开奖数据
  75. //从数据库中获取昨天数据抓取失败列表
  76. FailedQiHaoList = services.GetYesterdayFailQQList(currentLottery, Config.TimesPerDay);
  77. if (FailedQiHaoList.Count > 0)
  78. {
  79. DoYesterdayFailedListByMainUrl();
  80. DoYesterdayFailedListByBackUrl();
  81. }
  82. }
  83. /// <summary>
  84. /// 从备用地址执行今天任务
  85. /// </summary>
  86. private void DoTodayJobByBackUrl()
  87. {
  88. if (!string.IsNullOrEmpty(Config.BackUrl))
  89. {
  90. var OpenList = GetDocByBackUrl();
  91. if (OpenList.Count == 0) return; //无抓取数据
  92. var newestQiHao = OpenList.Max(w => w.QiHao);
  93. var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
  94. var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
  95. var total = OpenList.Count;
  96. if (startQiNum > newestQiNum) return; //无最新数据
  97. //处理最新开奖数据
  98. var getQiHao = string.Empty;
  99. for (var i = startQiNum; i <= newestQiNum; i++)
  100. {
  101. getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(i);
  102. var matchItem = OpenList.Where(R => R.QiHao.ToString() == getQiHao).FirstOrDefault();
  103. var step = 0;
  104. var nowQiHao = Convert.ToInt32(getQiHao);
  105. while (matchItem == null)
  106. if (step <= total)
  107. {
  108. nowQiHao++;
  109. step++;
  110. matchItem = OpenList.Where(R => R.QiHao.ToString() == nowQiHao.ToString()).FirstOrDefault();
  111. }
  112. else
  113. {
  114. matchItem = null;
  115. break;
  116. }
  117. if (matchItem.QiHao != null && SaveRecord(matchItem))
  118. {
  119. //处理成功写入日志
  120. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, getQiHao));
  121. LatestQiHao = getQiHao;
  122. }
  123. }
  124. }
  125. }
  126. /// <summary>
  127. /// 通过主站点抓取开奖数据
  128. /// </summary>
  129. private void DoTodayJobByMainUrl()
  130. {
  131. var OpenList = GetDocByMainUrl();
  132. if (OpenList.Count == 0) return; //无抓取数据
  133. var newestQiHao = OpenList.Max(W => W.QiHao);
  134. var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
  135. var total = OpenList.Count;
  136. var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
  137. if (startQiNum > newestQiNum) return; //无最新数据
  138. //处理最新开奖数据
  139. var getQiHao = string.Empty;
  140. for (var i = startQiNum; i <= newestQiNum; i++)
  141. {
  142. getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQ(i);
  143. var matchItem = OpenList.Where(R => R.QiHao.ToString() == getQiHao).FirstOrDefault();
  144. var step = 0;
  145. var nowQiHao = Convert.ToInt32(getQiHao);
  146. while (matchItem == null)
  147. if (step <= total)
  148. {
  149. nowQiHao++;
  150. step++;
  151. matchItem = OpenList.Where(R => R.QiHao.ToString() == nowQiHao.ToString()).FirstOrDefault();
  152. }
  153. else
  154. {
  155. matchItem = null;
  156. break;
  157. }
  158. if (matchItem.QiHao != null && SaveRecord(matchItem))
  159. {
  160. //处理成功写入日志
  161. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
  162. LatestQiHao = getQiHao;
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// 从备用网址处理昨天失败的开彩
  168. /// </summary>
  169. public void DoYesterdayFailedListByBackUrl()
  170. {
  171. if (!string.IsNullOrEmpty(Config.BackUrl) && FailedQiHaoList.Count > 0)
  172. {
  173. var OpenList = GetDocByBackUrl(false);
  174. if (OpenList.Count == 0) return; //无抓取数据
  175. var total = OpenList.Count;
  176. var SuccessList = new List<string>();
  177. foreach (var failedQiHao in FailedQiHaoList)
  178. {
  179. var matchItem = OpenList.Where(R => R.QiHao.ToString() == failedQiHao).FirstOrDefault();
  180. var step = 0;
  181. var nowQiHao = Convert.ToInt32(failedQiHao);
  182. while (matchItem == null)
  183. if (step <= total)
  184. {
  185. nowQiHao++;
  186. step++;
  187. matchItem = OpenList.Where(R => R.QiHao.ToString() == nowQiHao.ToString()).FirstOrDefault();
  188. }
  189. else
  190. {
  191. matchItem = null;
  192. break;
  193. }
  194. if (matchItem != null && SaveRecord(matchItem))
  195. {
  196. //处理成功写入日志
  197. log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, failedQiHao));
  198. SuccessList.Add(failedQiHao);
  199. }
  200. }
  201. foreach (var successQiHao in SuccessList) FailedQiHaoList.Remove(successQiHao);
  202. }
  203. }
  204. /// <summary>
  205. /// 保存开奖信息到数据库中
  206. /// </summary>
  207. /// <param name="list"></param>
  208. /// <summary>
  209. /// 通过主站抓取错误期号列表中每一个期号
  210. /// </summary>
  211. private void DoYesterdayFailedListByMainUrl()
  212. {
  213. var OpenList = GetDocByMainUrl(false);
  214. if (OpenList.Count == 0) return; //无抓取数据
  215. var SuccessList = new List<string>();
  216. var total = OpenList.Count;
  217. foreach (var failedQiHao in FailedQiHaoList)
  218. {
  219. var matchItem = OpenList.Where(R => R.QiHao.ToString() == failedQiHao).FirstOrDefault();
  220. var step = 0;
  221. var nowQiHao = Convert.ToInt32(failedQiHao);
  222. while (matchItem == null)
  223. if (step <= total)
  224. {
  225. nowQiHao++;
  226. step++;
  227. matchItem = OpenList.Where(R => R.QiHao.ToString() == nowQiHao.ToString()).FirstOrDefault();
  228. }
  229. else
  230. {
  231. matchItem = null;
  232. break;
  233. }
  234. if (matchItem != null && SaveRecord(matchItem))
  235. {
  236. //处理成功写入日志
  237. log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, failedQiHao));
  238. SuccessList.Add(failedQiHao);
  239. }
  240. }
  241. foreach (var successQiHao in SuccessList) FailedQiHaoList.Remove(successQiHao);
  242. }
  243. /// <summary>
  244. /// 抓取站点开奖数据
  245. /// </summary>
  246. /// <param name="url">备用站点</param>
  247. /// <returns></returns>
  248. private List<BeiJingK3Enitity> GetDocByBackUrl(bool IsToday = true)
  249. {
  250. var list = new List<BeiJingK3Enitity>();
  251. try
  252. {
  253. var day = DateTime.Now.ToString("yyyyMMdd");
  254. if (!IsToday) day = DateTime.Now.AddDays(-1).ToString("yyyyMMdd");
  255. var url = string.Format(Config.BackUrl, day);
  256. //" http://kaijiang.500.com/static/info/kaijiang/xml/bjk3/20171124.xml?_A=YLQMBVJT1511764350898";
  257. //string url = @"http://www.bwlc.net/bulletin/prevqck3.html?page=3";
  258. var HtmlResource = NetHelper.GetUrlResponse(url);
  259. if (HtmlResource == null) return list;
  260. var doc = new XmlDocument();
  261. doc.LoadXml(HtmlResource);
  262. var records = doc.SelectNodes("//row");
  263. if (records == null) return list;
  264. foreach (XmlNode xmlnode in records)
  265. {
  266. if (xmlnode.Attributes["expect"] == null ||
  267. string.IsNullOrWhiteSpace(xmlnode.Attributes["expect"].Value) ||
  268. xmlnode.Attributes["opencode"] == null ||
  269. string.IsNullOrWhiteSpace(xmlnode.Attributes["opencode"].Value)) continue;
  270. var num = xmlnode.Attributes["expect"].Value;
  271. var opencode = xmlnode.Attributes["opencode"].Value;
  272. var opentime = xmlnode.Attributes["opentime"].Value;
  273. var tmp = new BeiJingK3Enitity
  274. {
  275. QiHao = num.Substring(2),
  276. KaiJiangHao = opencode,
  277. OpenTime = DateTime.Parse(opentime)
  278. };
  279. list.Add(tmp);
  280. }
  281. var checkDataHelper = new CheckDataHelper();
  282. var dbdata = services.GetListIn(currentLottery, IsToday)
  283. .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  284. checkDataHelper.CheckData(dbdata, list.ToDictionary(w => w.QiHao.ToString(), w => w.KaiJiangHao),
  285. Config.Area, currentLottery);
  286. }
  287. catch (Exception ex)
  288. {
  289. log.Error(GetType(),
  290. string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
  291. }
  292. return list;
  293. }
  294. /// 从主站抓取开奖数据 昨天/今天
  295. /// </summary>
  296. /// <param name="IsToday"></param>
  297. /// <returns></returns>
  298. private List<BeiJingK3Enitity> GetDocByMainUrl(bool IsToday = true)
  299. {
  300. var list = new List<BeiJingK3Enitity>();
  301. try
  302. {
  303. var time = DateTime.Now;
  304. var arg = new OpenCaiApiArg
  305. {
  306. code = EnumHelper.GetLotteryCode(SCCLottery.BeiJingK3),
  307. // rows = int.Parse(GetPeriodsNumberToDay(time, IsToday)),
  308. date = time.ToString("yyyy-MM-dd")
  309. };
  310. if (!IsToday) arg.date = time.AddDays(-1).ToString("yyyy-MM-dd");
  311. var data = OpenCaiApiServices.GetOpenCaiApiData(arg);
  312. if (data == null) return list;
  313. if (data.data != null)
  314. {
  315. if (data.data.Count == 0) return list;
  316. for (var i = 0; i < data.data.Count; i++)
  317. {
  318. var tmp = new BeiJingK3Enitity
  319. {
  320. QiHao = DateTime.Now.ToString("yyMMdd") +
  321. GetPeriodsNumberToDay(DateTime.Parse(data.data[i].opentime), true),
  322. KaiJiangHao = data.data[i].GetOpenCodeStr(),
  323. OpenTime = DateTime.Parse(data.data[i].opentime)
  324. };
  325. list.Add(tmp);
  326. }
  327. var checkDataHelper = new CheckDataHelper();
  328. var dbdata = services.GetListIn(currentLottery, IsToday)
  329. .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
  330. checkDataHelper.CheckData(dbdata, list.ToDictionary(w => w.QiHao.ToString(), w => w.KaiJiangHao),
  331. Config.Area, currentLottery);
  332. }
  333. }
  334. catch (Exception ee)
  335. {
  336. log.Error(GetType(),
  337. string.Format("【{0}】通过站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ee.Message));
  338. }
  339. return list;
  340. }
  341. /// <summary>
  342. /// 将此彩种指定期号和开奖号码保存至数据库
  343. /// </summary>
  344. /// <param name="QiHao">期号</param>
  345. /// <param name="OpenCode">开奖号码(形如01,02,03)</param>
  346. /// <param name="IsYesterdayRecord">是否是保存昨天的记录</param>
  347. /// <returns></returns>
  348. private bool SaveRecord(BeiJingK3Enitity data)
  349. {
  350. var model = new OpenCode3Model();
  351. model.Term = Convert.ToInt64(data.QiHao); //期号
  352. var haoMaArray = data.KaiJiangHao.Split(',');
  353. model.OpenCode1 = Convert.ToInt32(haoMaArray[0]);
  354. model.OpenCode2 = Convert.ToInt32(haoMaArray[1]);
  355. model.OpenCode3 = Convert.ToInt32(haoMaArray[2]);
  356. model.OpenTime = data.OpenTime;
  357. if (services.AddOpen3Code(currentLottery, model))
  358. {
  359. GetMaxPeriodNum((int) model.Term); //添加成功存放期数
  360. return true;
  361. }
  362. return false;
  363. }
  364. /// <summary>
  365. /// 比较期号大的存入LatestQiHao
  366. /// </summary>
  367. /// <param name="PeriodNum"></param>
  368. public void GetMaxPeriodNum(int PeriodNum)
  369. {
  370. var lastqihao = 0;
  371. int.TryParse(LatestQiHao, out lastqihao);
  372. if (lastqihao < PeriodNum) LatestQiHao = PeriodNum.ToString();
  373. }
  374. /// <summary>
  375. /// 获取今天期数
  376. /// </summary>
  377. /// <param name="time"></param>
  378. /// <param name="IsToday"></param>
  379. /// <returns></returns>
  380. public string GetPeriodsNumberToDay(DateTime time, bool IsToday = false)
  381. {
  382. if (!IsToday) return Config.TimesPerDay.ToString();
  383. int hc = 0, mc = 0;
  384. if (time.Hour >= Config.StartHour) //
  385. {
  386. hc = (time.Hour - Config.StartHour) * 6; //已开期数小时部分
  387. mc = time.Minute / 10; //已开期分钟部分
  388. if (Config.StartMinute == 0) mc += 1;
  389. }
  390. return hc + mc >= 10 ? (hc + mc).ToString() : "0" + (hc + mc); //;
  391. }
  392. #region Attribute
  393. /// <summary>
  394. /// 配置信息
  395. /// </summary>
  396. private SCCConfig Config;
  397. /// <summary>
  398. /// 当天抓取的最新一期期号
  399. /// </summary>
  400. private string LatestQiHao = "0";
  401. /// <summary>
  402. /// 当天抓取失败列表
  403. /// </summary>
  404. private List<string> FailedQiHaoList;
  405. /// <summary>
  406. /// 日志对象
  407. /// </summary>
  408. private readonly LogHelper log;
  409. /// <summary>
  410. /// 数据服务
  411. /// </summary>
  412. private readonly IOpen3Code services;
  413. /// <summary>
  414. /// 当前彩种
  415. /// </summary>
  416. private SCCLottery currentLottery => SCCLottery.BeiJingK3;
  417. /// <summary>
  418. /// 邮件接口
  419. /// </summary>
  420. private IEmail email;
  421. #endregion
  422. }
  423. public class BeiJingK3Enitity
  424. {
  425. public string QiHao { get; set; }
  426. public string KaiJiangHao { get; set; }
  427. public DateTime OpenTime { get; set; }
  428. }
  429. }