using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CP.Model;
using HtmlAgilityPack;
using Newtonsoft.Json;
using Quartz;
using SCC.Common;
using SCC.Crawler.Tools;
using SCC.Interface;
using SCC.Models;
namespace SCC.Crawler.DT
{
///
/// 河北好运彩2
///
[DisallowConcurrentExecution]
[PersistJobDataAfterExecution]
public class HeBeiHYC2Job : IJob
{
///
/// 初始化函数
///
public HeBeiHYC2Job()
{
log = new LogHelper();
email = IOC.Resolve();
}
///
/// 作业执行入口
///
/// 作业执行上下文
public void Execute(IJobExecutionContext context)
{
Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
//预设节假日不开奖
if (Config.SkipDate.Contains(CommonHelper.SCCSysDateTime.ToString("yyyyMMdd"))) return;
LatestItem = context.JobDetail.JobDataMap["LatestItem"] as Fchebeihyc2LongInfo;
try
{
//服务启动时配置初始数据
if (LatestItem == null)
{
LatestItem = new Fchebeihyc2LongInfo
{
qi = CommonHelper.GenerateQiHaoYYYYQQQ(0),
date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
};
}
//程序时间第二天,程序根据配置检查是否昨天有开奖
isGetData = false;
if (CommonHelper.CheckDTIsNeedGetData(Config)) //
DoMainUrl();
if (!LatestItem.qi.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy")))
LatestItem = new Fchebeihyc2LongInfo
{
qi = CommonHelper.GenerateQiHaoYYYYQQQ(0),
date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
};
//当今日开奖并且当前时间是晚上8点过后开始抓取
if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12) DoMainUrl();
}
catch (Exception ex)
{
log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
}
//保存最新期号
context.JobDetail.JobDataMap["LatestItem"] = LatestItem;
}
///
/// 执行数据插入
///
private void DoMainUrl()
{
if (!string.IsNullOrEmpty(Config.MainUrl))
{
var openList = GetOpenListFromMainUrl(Config.MainUrl);
if (openList == null || openList.Count == 0) return; //无抓取数据
//抓取到的最新期数
var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
//数据库里面最新期数
LatestItem = Fchebeihyc2Data.GetLastOne();
var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
if (startQiNum > newestQiHao) return; //无最新数据
if (startQiNum == 0)
startQiNum = openList.OrderBy(m => m.qi).First().qi;
//处理最新开奖数据
Fchebeihyc2LongInfo matchItem = null;
for (var i = startQiNum; i <= newestQiHao; i++)
{
matchItem = openList.FirstOrDefault(r => r.qi.ToString() == i.ToString());
if (matchItem != null)
{
//add db
matchItem.addtime = DateTime.Now;
Fchebeihyc2Data.Add(matchItem);
//Do Success Log
log.Info(GetType(), CommonHelper.GetJobMainLogInfo(Config, i.ToString()));
LatestItem = matchItem;
isGetData = true;
}
}
}
}
///
/// 获取主站点开奖列表数据
///
///
private List GetOpenListFromMainUrl(string mainUrl)
{
var result = new List();
try
{
var url = new Uri(mainUrl);
var htmlResource = NetHelper.GetUrlResponse(mainUrl, Encoding.GetEncoding("UTF-8"));
if (htmlResource == null) return result;
var doc = new HtmlDocument();
doc.LoadHtml(htmlResource);
var div = doc.DocumentNode.SelectSingleNode("//div[@id='jq_difang_index_body']");
if (div == null) return result;
var divs = div.ChildNodes.Where(node => node.Name == "div").ToList();
if (divs == null) return result;
var div2 = divs[1].ChildNodes.Where(node => node.Name == "div").ToList();
if (div2 == null) return result;
var table = div2[1].ChildNodes.Where(node => node.Name == "table").ToList();
if (table == null) return result;
var tbody = table[0].ChildNodes.Where(node => node.Name == "tbody").ToList();
if (tbody == null) return result;
var trs = tbody[0].ChildNodes.Where(node => node.Name == "tr").ToList();
if (trs == null) return result;
Fchebeihyc2LongInfo model = null;
HtmlNode nodeA = null;
var optimizeUrl = string.Empty;
for (var i = 0; i < trs.Count; 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;
model = new Fchebeihyc2LongInfo();
nodeA = tds[2].ChildNodes.Where(n => n.Name == "a").FirstOrDefault();
if (nodeA == null) continue;
model.qi = Convert.ToInt32(tds[0].InnerText.Trim());
optimizeUrl = tds[0].InnerText.Trim();
model.date = Convert.ToDateTime(tds[3].InnerText.Substring(5, 10));
if (tds[1].ChildNodes.Count == 0) continue;
var opencode = tds[1].ChildNodes.Where(n => n.Name.ToLower() == "span").ToList();
var opencodeNode = opencode[0].ChildNodes.Where(n => n.Name.ToLower() == "strong").ToList();
var openCodeString = opencodeNode[0].InnerText.Trim();
model.n1 = Convert.ToInt32(openCodeString.Substring(0, 2));
model.n2 = Convert.ToInt32(openCodeString.Substring(3, 2));
GetKaijiangDetails(ref model,optimizeUrl);
result.Add(model);
}
//var checkDataHelper = new CheckDataHelper();
//var dbdata = services.GetListS(currentLottery);
//if (dbdata == null) return result;
//var data = dbdata.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
//checkDataHelper.CheckData(data, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
// Config.Area, currentLottery);
//result = result.OrderByDescending(S => S.Term).ToList();
}
catch (Exception ex)
{
log.Error(GetType(),
string.Format("【{0}】通过主站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
}
return result;
}
///
/// 完善主站点开奖实体信息
///
///
private void GetKaijiangDetails(ref Fchebeihyc2LongInfo model,string optimizeUrl)
{
var url = "http://kaijiang.500.com/shtml/hbhy2/" + optimizeUrl + ".shtml";
var htmlResource = NetHelper.GetUrlResponse(url, Encoding.GetEncoding("gb2312"));
if (string.IsNullOrWhiteSpace(htmlResource))
{
log.Warn(GetType(), string.Format("【{0}】通过主站点抓取开奖列表未获取到数据。", Config.Area + currentLottery));
return;
}
var doc = new HtmlDocument();
doc.LoadHtml(htmlResource);
//var div = doc.DocumentNode.SelectSingleNode("//div[@style='margin:20px auto; width:500px;']");
var div = doc.DocumentNode.SelectSingleNode("//div[@class='kjxq_box02']");
var content = div.ChildNodes.Where(node => node.Name == "div").ToList().ElementAt(1);
//爬去奖金
var table = content.ChildNodes.Where(node => node.Name == "table").ToList();
var tr = table[0].ChildNodes.Where(node => node.Name == "tr").ToList();
var td = tr[2].ChildNodes.Where(node => node.Name == "td").ToList();
var span = td[0].ChildNodes.Where(node => node.Name == "span").ToList();
//爬去开奖详情
var tr1 = table[1].ChildNodes.Where(node => node.Name == "tr").ToList();
var td1 = tr1[2].ChildNodes.Where(node => node.Name == "td").ToList();
// | 本期销量:2,154元 奖池滚存:0元 |
model.nextmoney = span[1].InnerHtml.Trim().Replace("元", "");
model.tzmoney = span[0].InnerHtml.Trim().Replace("元", "");
var list1 = new List();
//组装详情
//一等奖
model.zj1 = td1[1].InnerHtml.Trim().Replace(",", "");
model.jo1 = td1[2].InnerHtml.Trim().Replace(",", "");
var list = new List();
list.Add(new Winbonus()
{
item = "一等奖",
wincount = model.zj1,
winmoney = model.jo1
});
model.winbonus = JsonConvert.SerializeObject(list);
}
#region Attribute
///
/// 配置信息
///
private SCCConfig Config;
///
/// 当天抓取的最新一期开奖记录
///
private Fchebeihyc2LongInfo LatestItem;
#pragma warning disable CS0414 // 字段“HeBeiHYC2Job.FailedQiHaoList”已被赋值,但从未使用过它的值
///
/// 当天抓取失败列表
///
private List FailedQiHaoList = null;
#pragma warning restore CS0414 // 字段“HeBeiHYC2Job.FailedQiHaoList”已被赋值,但从未使用过它的值
///
/// 日志对象
///
private readonly LogHelper log;
///
/// 当前彩种
///
private SCCLottery currentLottery => SCCLottery.HeBeiHYC2;
///
/// 邮件接口
///
private IEmail email;
#pragma warning disable CS0414 // 字段“HeBeiHYC2Job.isGetData”已被赋值,但从未使用过它的值
///
/// 是否本次运行抓取到开奖数据
///
private bool isGetData;
#pragma warning restore CS0414 // 字段“HeBeiHYC2Job.isGetData”已被赋值,但从未使用过它的值
#endregion
}
}