123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using HtmlAgilityPack;
- using Quartz;
- using SCC.Common;
- using SCC.Crawler.Tools;
- using SCC.Interface;
- using SCC.Models;
- using SCC.Services;
- namespace SCC.Crawler.GP
- {
-
-
-
- [DisallowConcurrentExecution]
- [PersistJobDataAfterExecution]
- public class AHK3Job : IJob
- {
-
-
-
- public AHK3Job()
- {
- log = new LogHelper();
- services = IOC.Resolve<IOpen3Code>();
- email = IOC.Resolve<IEmail>();
- }
-
-
-
-
- public void Execute(IJobExecutionContext context)
- {
- Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
-
- if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
- LatestQiHao = context.JobDetail.JobDataMap.GetString("LatestQiHao");
- try
- {
-
- if (string.IsNullOrEmpty(LatestQiHao))
- {
- var lastItem = services.GetLastItem(currentLottery);
- if (lastItem != null) LatestQiHao = lastItem.Term.ToString();
- }
-
- if (string.IsNullOrEmpty(LatestQiHao) ||
- !LatestQiHao.StartsWith(CommonHelper.SCCSysDateTime.ToString("yyMMdd")))
- {
- CheckingYesterdayTheLotteryData();
- LatestQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQQ(0);
- }
-
- if (Convert.ToInt32(LatestQiHao.Substring(6)) != Config.TimesPerDay)
- {
- DoTodayJobByMainUrl();
- DoTodayJobByBackUrl();
- }
- }
- catch (Exception ex)
- {
- log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
- }
-
- context.JobDetail.JobDataMap["LatestQiHao"] = LatestQiHao;
- }
-
-
-
- private void CheckingYesterdayTheLotteryData()
- {
- if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.AddDays(-1).ToString("yyyyMMdd")))
- return;
-
- FailedQiHaoList = services.GetYesterdayFailQQQList(currentLottery, Config.TimesPerDay);
- if (FailedQiHaoList.Count > 0)
- {
- DoYesterdayFailedListByMainUrl();
- DoYesterdayFailedListByBackUrl();
- }
- }
-
-
-
-
- private void DoTodayJobByMainUrl()
- {
- if (!string.IsNullOrEmpty(Config.MainUrl))
- {
- var OpenList = GetDocByMainUrl();
- if (OpenList.Count == 0) return;
- var newestQiHao = OpenList.Max(w => w.Key);
- var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
- var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
- if (startQiNum > newestQiNum) return;
-
- var total = OpenList.Count;
- var getQiHao = string.Empty;
- for (var i = startQiNum; i <= newestQiNum; i++)
- {
- getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQQ(i);
- var matchItem = OpenList.Where(R => R.Key == getQiHao).FirstOrDefault();
- var step = 0;
- var nowQiHao = Convert.ToInt32(getQiHao);
- while (matchItem.Key == null)
- if (step <= total)
- {
- nowQiHao++;
- step++;
- matchItem = OpenList.Where(R => R.Key.ToString() == nowQiHao.ToString()).FirstOrDefault();
- }
- else
- {
- break;
- }
- if (matchItem.Key != null && SaveRecord(getQiHao, matchItem.Value, false))
- {
-
- log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, getQiHao));
- LatestQiHao = getQiHao;
- }
- }
- }
- }
-
-
-
-
- private void DoYesterdayFailedListByMainUrl()
- {
- if (!string.IsNullOrEmpty(Config.MainUrl) && FailedQiHaoList.Count > 0)
- {
- var OpenList = GetDocByMainUrl(false);
- if (OpenList.Count == 0) return;
- var SuccessList = new List<string>();
- var total = OpenList.Count;
- foreach (var failedQiHao in FailedQiHaoList)
- {
- var matchItem = OpenList.FirstOrDefault(R => R.Key == failedQiHao);
- var step = 0;
- var nowQiHao = Convert.ToInt32(failedQiHao);
- while (matchItem.Key == null)
- if (step <= total)
- {
- nowQiHao++;
- step++;
- matchItem = OpenList.Where(R => R.Key.ToString() == nowQiHao.ToString()).FirstOrDefault();
- }
- else
- {
- break;
- }
- if (matchItem.Key != null && SaveRecord(failedQiHao, matchItem.Value, true))
- {
-
- log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, failedQiHao));
- SuccessList.Add(failedQiHao);
- }
- }
- foreach (var successQiHao in SuccessList) FailedQiHaoList.Remove(successQiHao);
- }
- }
-
-
-
-
- private Dictionary<string, string> GetTodayOpenListFromBackUrl()
- {
- return GetOpenListFromBackUrl(string.Format(Config.BackUrl, 1));
- }
-
-
-
-
- private Dictionary<string, string> GetYesterdayOpenListFromBackUrl()
- {
- return GetOpenListFromBackUrl(string.Format(Config.BackUrl, 2), false);
- }
-
-
-
-
-
- private Dictionary<string, string> GetOpenListFromBackUrl(string url, bool IsToday = true)
- {
- var result = new Dictionary<string, string>();
- try
- {
- var HtmlResource = NetHelper.GetUrlResponse(url);
- if (HtmlResource == null) return result;
- var doc = new HtmlDocument();
- doc.LoadHtml(HtmlResource);
- var tbody = doc.GetElementbyId("chartsTbody");
- if (tbody == null) return result;
- var trs = tbody.ChildNodes.Where(node => node.Name == "tr").ToList();
- var matchQiHao = string.Empty;
- var matchKJHaoMa = string.Empty;
- for (var i = 1; i < trs.Count - 1; i++)
- {
- var trstyle = trs[i].Attributes["style"];
- if (trstyle != null && trstyle.Value == "display:none") continue;
- var tds = trs[i].ChildNodes.Where(node => node.Name == "td").ToList();
- if (tds.Count < 4) continue;
- matchQiHao = tds[0].InnerText.Trim();
- matchKJHaoMa = string.Format("{0},{1},{2}", tds[1].InnerText.Trim(), tds[2].InnerText.Trim(),
- tds[3].InnerText.Trim());
- result.Add(matchQiHao, CommonHelper.TRHandleCode(matchKJHaoMa));
- }
- var checkDataHelper = new CheckDataHelper();
- var dbdata = services.GetListIn(currentLottery, IsToday)
- .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
- checkDataHelper.CheckData(dbdata, result, Config.Area, currentLottery);
- }
- catch (Exception ex)
- {
- log.Error(GetType(),
- string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
- }
- return result;
- }
-
-
-
-
-
- private Dictionary<string, string> GetDocByMainUrl(bool IsToday = true)
- {
- var result = new Dictionary<string, string>();
- try
- {
- var time = DateTime.Now;
- var arg = new OpenCaiApiArg
- {
- code = EnumHelper.GetLotteryCode(SCCLottery.AnHuiK3),
-
- date = time.ToString("yyyy-MM-dd")
- };
- if (!IsToday) arg.date = time.AddDays(-1).ToString("yyyy-MM-dd");
- var data = OpenCaiApiServices.GetOpenCaiApiData(arg);
- if (data == null) return result;
- if (data.data != null)
- {
- if (data.data.Count == 0) return result;
- for (var i = 0; i < data.data.Count; i++)
- result.Add(data.data[i].GetTermStr(), data.data[i].GetOpenCodeStr());
- var checkDataHelper = new CheckDataHelper();
- var dbdata = services.GetListIn(currentLottery, IsToday)
- .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
- checkDataHelper.CheckData(dbdata, result, Config.Area, currentLottery);
-
- }
- }
- catch (Exception ee)
- {
- log.Error(GetType(),
- string.Format("【{0}】通过站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ee.Message));
- }
- return result;
- }
-
-
-
-
- private void DoTodayJobByBackUrl()
- {
- if (!string.IsNullOrEmpty(Config.BackUrl))
- {
- var OpenList = GetTodayOpenListFromBackUrl();
- if (OpenList.Count == 0) return;
- var newestQiHao = OpenList.Max(w => w.Key);
- var startQiNum = Convert.ToInt32(LatestQiHao.Substring(6)) + 1;
- var newestQiNum = Convert.ToInt32(newestQiHao.Substring(6));
- var total = OpenList.Count;
- if (startQiNum > newestQiNum) return;
-
- var getQiHao = string.Empty;
- for (var i = startQiNum; i <= newestQiNum; i++)
- {
- getQiHao = CommonHelper.GenerateTodayQiHaoYYMMDDQQQ(i);
- var matchItem = OpenList.FirstOrDefault(R => R.Key == getQiHao);
- var step = 0;
- var nowQiHao = Convert.ToInt32(getQiHao);
- while (matchItem.Key == null)
- if (step <= total)
- {
- nowQiHao++;
- step++;
- matchItem = OpenList.Where(R => R.Key.ToString() == nowQiHao.ToString()).FirstOrDefault();
- }
- else
- {
- break;
- }
- if (matchItem.Key != null && SaveRecord(getQiHao, matchItem.Value, false))
- {
-
- log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, getQiHao));
- LatestQiHao = getQiHao;
- }
- }
- }
- }
-
-
-
-
- private void DoYesterdayFailedListByBackUrl()
- {
- if (!string.IsNullOrEmpty(Config.BackUrl) && FailedQiHaoList.Count > 0)
- {
- var OpenList = GetYesterdayOpenListFromBackUrl();
- if (OpenList.Count == 0) return;
- var SuccessList = new List<string>();
- var total = OpenList.Count;
- foreach (var failedQiHao in FailedQiHaoList)
- {
- var matchItem = OpenList.FirstOrDefault(R => R.Key == failedQiHao);
- var step = 0;
- var nowQiHao = Convert.ToInt32(failedQiHao);
- while (matchItem.Key == null)
- if (step <= total)
- {
- nowQiHao++;
- step++;
- matchItem = OpenList.Where(R => R.Key.ToString() == nowQiHao.ToString()).FirstOrDefault();
- }
- else
- {
- break;
- }
- if (matchItem.Key != null && SaveRecord(failedQiHao, matchItem.Value, true))
- {
-
- log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, failedQiHao));
- SuccessList.Add(failedQiHao);
- }
- }
- foreach (var successQiHao in SuccessList) FailedQiHaoList.Remove(successQiHao);
- }
- }
-
-
-
-
-
-
-
- private bool SaveRecord(string QiHao, string OpenCode, bool IsYesterdayRecord)
- {
- if (!string.IsNullOrWhiteSpace(QiHao) && !string.IsNullOrWhiteSpace(OpenCode))
- {
- var model = new OpenCode3Model();
- model.Term = Convert.ToInt64(QiHao);
- var haoMaArray = OpenCode.Split(',');
- model.OpenCode1 = Convert.ToInt32(haoMaArray[0]);
- model.OpenCode2 = Convert.ToInt32(haoMaArray[1]);
- model.OpenCode3 = Convert.ToInt32(haoMaArray[2]);
- if (IsYesterdayRecord)
- model.OpenTime = CommonHelper.GenerateYesterdayOpenTime(Config, QiHao);
- else
- model.OpenTime = CommonHelper.GenerateTodayOpenTime(Config, QiHao);
- return services.AddOpen3Code(currentLottery, model);
- }
- return false;
- }
- #region Attribute
-
-
-
- private SCCConfig Config;
-
-
-
- private string LatestQiHao;
-
-
-
- private List<string> FailedQiHaoList;
-
-
-
- private readonly LogHelper log;
-
-
-
- private readonly IOpen3Code services;
-
-
-
- private SCCLottery currentLottery => SCCLottery.AnHuiK3;
-
-
-
- private IEmail email;
- #endregion
- }
- }
|