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
{
///
/// 深圳风采 深圳35选7
///
[DisallowConcurrentExecution]
[PersistJobDataAfterExecution]
public class GuangDongSZFCJob : IJob
{
///
/// 构造函数
///
public GuangDongSZFCJob()
{
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 Fcszfc35x7LongInfo;
try
{
//服务启动时配置初始数据
if (LatestItem == null)
{
LatestItem = new Fcszfc35x7LongInfo
{
qi = CommonHelper.GenerateQiHaoYYQQQ(0),
date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
};
}
//程序时间第二天,程序根据配置检查是否昨天有开奖
isGetData = false;
if (CommonHelper.CheckDTIsNeedGetData(Config)) //
{
DoMainUrl();
DoBackUrl();
}
if (!LatestItem.qi.ToString().StartsWith(CommonHelper.SCCSysDateTime.ToString("yy")))
LatestItem = new Fcszfc35x7LongInfo
{
qi = CommonHelper.GenerateQiHaoYYQQQ(0),
date = new DateTime(CommonHelper.SCCSysDateTime.Year, 1, 1)
};
//当今日开奖并且当前时间是晚上8点过后开始抓取
if (CommonHelper.CheckTodayIsOpenDay(Config) && CommonHelper.SCCSysDateTime.Hour > 12)
{
DoMainUrl();
DoBackUrl();
}
}
catch (Exception ex)
{
log.Error(GetType(), string.Format("【{0}】抓取时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
}
//保存最新期号
context.JobDetail.JobDataMap["LatestItem"] = LatestItem;
}
#region 通过主站爬取数据
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 = Fcszfc35x7Data.GetLastOne();
var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
if (startQiNum == 0)
startQiNum = openList.OrderBy(m => m.qi).First().qi;
if (startQiNum > newestQiHao) return; //无最新数据
//处理最新开奖数据
Fcszfc35x7LongInfo 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;
Fcszfc35x7Data.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("gb2312"));
if (htmlResource == null) return result;
var doc = new HtmlDocument();
doc.LoadHtml(htmlResource);
var table = doc.DocumentNode.SelectSingleNode("//table");
if (table == null) return result;
var trs = table.ChildNodes.Where(node => node.Name == "tr").ToList();
Fcszfc35x7LongInfo model = null;
HtmlNode nodeA = null;
var optimizeUrl = string.Empty;
for (var i = 2; 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 < 8) continue;
model = new Fcszfc35x7LongInfo();
nodeA = tds[0].ChildNodes.Where(n => n.Name == "a").FirstOrDefault();
if (nodeA == null) continue;
string qihao = nodeA.InnerText.Trim().Substring(2, 5);
qihao = qihao.Length < 7 ? $"20{qihao}" : qihao;
model.qi = Convert.ToInt32(qihao);
optimizeUrl = nodeA.Attributes["href"].Value;
//model.DetailUrl = new Uri(url, optimizeUrl).AbsoluteUri;
model.date = Convert.ToDateTime(tds[9].InnerText);
if (tds[1].ChildNodes.Count == 0) continue;
var opencodeNode = tds[1].ChildNodes.Where(n => n.Name.ToLower() == "i").ToList();
if (opencodeNode.Count < 8) continue;
model.n1 = Convert.ToInt32(opencodeNode[0].InnerText.Trim());
model.n2 = Convert.ToInt32(opencodeNode[1].InnerText.Trim());
model.n3 = Convert.ToInt32(opencodeNode[2].InnerText.Trim());
model.n4 = Convert.ToInt32(opencodeNode[3].InnerText.Trim());
model.n5 = Convert.ToInt32(opencodeNode[4].InnerText.Trim());
model.n6 = Convert.ToInt32(opencodeNode[5].InnerText.Trim());
model.n7 = Convert.ToInt32(opencodeNode[6].InnerText.Trim());
model.n8 = Convert.ToInt32(opencodeNode[7].InnerText.Trim());
GetKaijiangDetails(ref model, tds);
result.Add(model);
}
//var checkDataHelper = new CheckDataHelper();
//var dbdata = services.GetListS(currentLottery)
// .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
//checkDataHelper.CheckData(dbdata, 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 Fcszfc35x7LongInfo model, List nodes)
{
model.nextmoney = "";
model.tzmoney = string.IsNullOrEmpty(nodes[2].InnerText.Replace(",", "").Replace("元", "")) ? "0"
: nodes[2].InnerText.Replace(",", "").Replace("元", "");
//组装详情
//一等奖
model.zj1 = string.IsNullOrEmpty(nodes[3].InnerText) ? "0" : nodes[3].InnerText;
model.jo1 = string.IsNullOrEmpty(nodes[4].InnerText) ? "0" : nodes[4].InnerText;
//二等奖
model.zj2 = string.IsNullOrEmpty(nodes[5].InnerText) ? "0" : nodes[5].InnerText;
model.jo2 = string.IsNullOrEmpty(nodes[6].InnerText) ? "0" : nodes[6].InnerText;
//三等奖
model.zj3 = string.IsNullOrEmpty(nodes[7].InnerText) ? "0" : nodes[7].InnerText;
model.jo3 = string.IsNullOrEmpty(nodes[8].InnerText) ? "0" : nodes[8].InnerText;
var list = new List();
list.Add(new Winbonus()
{
item = "一等奖",
wincount = model.zj1,
winmoney = model.jo1
});
list.Add(new Winbonus()
{
item = "二等奖",
wincount = model.zj2,
winmoney = model.jo2
});
list.Add(new Winbonus()
{
item = "三等奖",
wincount = model.zj3,
winmoney = model.jo3
});
model.winbonus = JsonConvert.SerializeObject(list);
}
#endregion
#region 通过副站爬取数据
///
/// 副站数据爬取 地址:https://fx.cp2y.com/draw/history_10065_Y/
///
///
private List GetOpenListFromBackUrl()
{
var result = new List();
try
{
var htmlResource = NetHelper.GetUrlResponse(Config.BackUrl, Encoding.GetEncoding("gb2312"));
if (htmlResource == null) return result;
var doc = new HtmlDocument();
doc.LoadHtml(htmlResource);
var table = doc.DocumentNode.SelectNodes("//table");
if (table == null) return result;
var trs = table[4].ChildNodes.Where(node => node.Name == "tr").ToList();
List tds = null;
string term = string.Empty, openCodeString = string.Empty, optimizeUrl = string.Empty;
for (var i = 2; i < trs.Count; i++)
{
tds = trs[i].ChildNodes.Where(S => S.Name.ToLower() == "td").ToList();
if (tds.Count < 4) continue;
var model = new Fcszfc35x7LongInfo();
if (string.IsNullOrEmpty(tds[1].InnerText)) continue;
term = tds[0].InnerText.Trim();
term = term.Length < 7 ? $"20{term}" : term;
model.qi = Convert.ToInt32(term);
model.n1 = Convert.ToInt32(tds[1].InnerText);
model.n2 = Convert.ToInt32(tds[2].InnerText);
model.n3 = Convert.ToInt32(tds[3].InnerText);
model.n4 = Convert.ToInt32(tds[4].InnerText);
model.n5 = Convert.ToInt32(tds[5].InnerText);
model.n6 = Convert.ToInt32(tds[6].InnerText);
model.n7 = Convert.ToInt32(tds[7].InnerText);
//组装开奖详情
model.n8 = Convert.ToInt32(tds[8].InnerText);
result.Add(model);
}
//var checkDataHelper = new CheckDataHelper();
//var dbdata = services.GetListS(currentLottery)
// .ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr());
//checkDataHelper.CheckData(dbdata, result.ToDictionary(w => w.Term.ToString(), w => w.GetCodeStr()),
// Config.Area, currentLottery);
}
catch (Exception ex)
{
log.Error(GetType(),
string.Format("【{0}】通过备用站点抓取开奖列表时发生错误,错误信息【{1}】", Config.Area + currentLottery, ex.Message));
}
return result;
}
private void DoBackUrl()
{
if (!string.IsNullOrEmpty(Config.BackUrl))
{
var openList = GetOpenListFromBackUrl();
if (openList == null || openList.Count == 0) return; //无抓取数据
//抓取到的最新期数
var newestQiHao = Convert.ToInt32(openList.OrderByDescending(m => m.qi).First().qi.ToString());
//数据库里面最新期数
//LatestItem = Fcszfc35x7Data.GetLastOne();
var startQiNum = Convert.ToInt32(LatestItem.qi.ToString());
if (startQiNum > newestQiHao) return; //无最新数据
//处理最新开奖数据
Fcszfc35x7LongInfo matchItem = null;
for (var i = startQiNum; i <= newestQiHao; i++)
{
matchItem = openList.Where(R => R.qi.ToString() == i.ToString()).FirstOrDefault();
if (matchItem != null)
{
//add db
matchItem.addtime = DateTime.Now;
Fcszfc35x7Data.Add(matchItem);
//Do Success Log
log.Info(GetType(), CommonHelper.GetJobBackLogInfo(Config, i.ToString()));
LatestItem = matchItem;
isGetData = true;
}
}
}
}
#endregion
#region Attribute
///
/// 配置信息
///
private SCCConfig Config;
///
/// 当天抓取的最新一期开奖记录
///
private Fcszfc35x7LongInfo LatestItem;
#pragma warning disable CS0414 // 字段“GuangDongSZFCJob.FailedQiHaoList”已被赋值,但从未使用过它的值
///
/// 当天抓取失败列表
///
private List FailedQiHaoList = null;
#pragma warning restore CS0414 // 字段“GuangDongSZFCJob.FailedQiHaoList”已被赋值,但从未使用过它的值
///
/// 日志对象
///
private readonly LogHelper log;
///
/// 当前彩种
///
private SCCLottery currentLottery => SCCLottery.GuangDongSZFC;
///
/// 邮件接口
///
private IEmail email;
#pragma warning disable CS0414 // 字段“GuangDongSZFCJob.isGetData”已被赋值,但从未使用过它的值
///
/// 是否本次运行抓取到开奖数据
///
private bool isGetData;
#pragma warning restore CS0414 // 字段“GuangDongSZFCJob.isGetData”已被赋值,但从未使用过它的值
#endregion
}
}