123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Threading.Tasks;
- using FCS.Common;
- using FCS.Interface;
- using FCS.Models;
- using FCS.Models.DTO;
- using HtmlAgilityPack;
- using Quartz;
- namespace FCS.Crawler.ZCLotteryScore
- {/// <summary>
- /// 创 建:czx
- /// 日 期:2018-09-27
- /// 描 述:比分数据更新、获取
- /// </summary>
- public class ScoreJob : CommonJob
- {
- #region 全局变量定义
- private List<DataItemDetail> scoreTypeList;//类别枚举类型
- private List<DataItemDetail> scoreStatusList;//状态枚举类型
- private Dictionary<string, string> scoreStatusDict;//状态枚举类型-字典
- private List<ScoreJobDTO> scoreJobList;//页面爬取参数
- private List<F_Events> eventsList;//赛事信息
- private List<F_Team> teamList;//球队信息
- private List<F_Games> gameList;//球队信息
- private string starting = "2";//开始中
- private string sfcCode = "300";//足彩
- private List<F_Score> result = new List<F_Score>();
- #endregion
- public ScoreJob()
- {
- scoreTypeList = services.GetDataItem(DataItemDetailEnum.FootScoreType);
- scoreStatusList = services.GetDataItem(DataItemDetailEnum.FootScoreStatus);
- eventsList = services.Query<F_Events>().ToList();
- teamList = services.Query<F_Team>().ToList();
- gameList = services.Query<F_Games>(" AND Datename(year,StartDateTime)={0}".FormatMe(DateTime.Now.Year)).ToList();
- scoreStatusDict = new Dictionary<string, string>
- {
- {"未","1" },
- {"完","3" },
- {"推迟 ","4" }
- };
- scoreJobList = new List<ScoreJobDTO>{
- new ScoreJobDTO{
- Type="2",
- Code="201",
- SessionName="date",
- Ajax="true",
- Url="http://live.zgzcw.com/jz/"
- },//竞彩
- new ScoreJobDTO{
- Type="3",
- Code="300",
- SessionName ="issue",
- Ajax="true",
- Url="http://live.zgzcw.com/sfc/"
- },//足彩
- new ScoreJobDTO{
- Type="4",
- Code="400",
- SessionName ="issue",
- Ajax="true",
- Url="http://live.zgzcw.com/bd/"
- },//北单
- };
- }
- /// <summary>
- /// 更新
- /// </summary>
- public void Click()
- {
- scoreJobList.ForEach(p =>
- {
- result = new List<F_Score>();
- var scoreType = scoreTypeList.Where(s => s.ItemValue == p.Type).ToList()[0].Id;
- //期数数据
- var sessionList = GetSession(p.Url, scoreType, true).ToList();
- threadCount = sessionList.Count;
- finishcount = 0;
- sessionList.ForEach(q =>
- {
- Analysis(p, q, scoreType);
- });
- lock (locker)
- {
- while (finishcount != threadCount)
- {
- Thread.Sleep(5000);
- Monitor.Wait(locker);//等待
- }
- }
- var deleteWhere = "";
- sessionList.ForEach(d =>
- {
- deleteWhere += d.Replace("-", "") + ",";
- });
- deleteWhere = deleteWhere.ToString().Substring(0, deleteWhere.ToString().Length - 1);
- services.Delete<F_Score>("AND ScoreType='{0}' AND Session in ({1})".FormatMe(scoreType, deleteWhere));
- services.SqlBulkCopyAdd(result);
- });
- }
- /// <summary>
- /// 获取全部
- /// </summary>
- /// <param name="url"></param>
- public void GetALL(string url = "http://live.zgzcw.com/ls/AllData.action")
- {
- var result = new List<F_Score>();
- scoreJobList.ForEach(p =>
- {
- Task.Run(() =>
- {
- //期数数据
- var sessionList = GetSession(p.Url).ToList();
- sessionList.ForEach(q =>
- {
- Analysis(p, q);
- });
- });
- });
- while (true)
- {
- if (CommonHelper.ThreadsFinsh())
- break;
- }
- services.SqlBulkCopyAdd<F_Score>(result);
- }
- /// <summary>
- /// 数据分析
- /// </summary>
- /// <param name="p">比分</param>
- /// <param name="q">期数</param>
- /// <param name="type">比分类型</param>
- /// <returns></returns>
- private async Task Analysis(ScoreJobDTO p, string q, string type = "")
- {
- string url = "http://live.zgzcw.com/ls/AllData.action";
- await Task.Run(() =>
- {
- try
- {
- var dict = new Dictionary<string, string> {{ "code",p.Code},{ p.SessionName,q},{ "ajax",p.Ajax}};//formData参数拼接
- var doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = url, FormData = dict, Title = "matchid", Method = "POST" });
- var tr = doc.DocumentNode.SelectNodes(".//tr");
- while (tr == null && doc.DocumentNode.InnerHtml != ConfigurationManager.AppSettings["Termination"])
- {
- doc = doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = url, FormData = dict, Title = "matchid", Method = "POST" });
- tr = doc.DocumentNode.SelectNodes(".//tr");
- }
- if (doc.DocumentNode.InnerHtml == ConfigurationManager.AppSettings["Termination"])//不存在退出
- return;
- foreach (HtmlNode item in tr)
- {
- var td = item.SelectNodes(".//td");
- if (td == null)
- continue;
- if (td.Count < 5)
- break;
- string _result = "", status = "", halfResultatus = "", colourFruit = "", gameId = "";
- if (scoreStatusDict.ContainsKey(td[4].InnerText.Trim()))
- {
- status = scoreStatusList.Where(a => a.ItemValue.Contains(scoreStatusDict[td[4].InnerText.Trim()])).ToList()[0].Id;
- _result = td[6].SelectNodes(".//span")[0].InnerHtml.Replace(" ", "");
- halfResultatus = td[8].SelectNodes(".//span")[0].InnerHtml.Replace(" ", "");
- }
- else
- status = scoreStatusList.Where(a => a.ItemValue.Contains(starting)).ToList()[0].Id;
- if (p.Code == sfcCode)
- colourFruit = td[9].SelectNodes(".//strong") != null ? td[9].SelectNodes(".//strong")[0].InnerText : string.Empty;
- var compensate = tr[tr.IndexOf(item)].SelectSingleNode(".//div[@class='oupei']").SelectNodes("span");
- var asianDish = tr[tr.IndexOf(item)].SelectSingleNode(".//div[@class='yapan']").SelectNodes("span");
- var model = new F_Score
- {
- Id = CommonHelper.GetGuid().ToString(),
- EventId = eventsList.Where(s => s.Name.Contains(td[1].InnerText)).ToList().Count > 0 ? eventsList.Where(s => s.Name.Contains(td[1].InnerText)).ToList()[0].Id : "",
- GroupName = td[2].InnerText,
- StartDateTime = DateTime.Parse(td[3].Attributes["date"].Value),
- HomeTeamId = (from a in teamList
- where a.Name == Regex.Replace(td[5].SelectNodes(".//a")[0].InnerText, @"\s", "")
- select a.Id).Take(1).ToJoin(),
- VisitingTeamId = (from a in teamList
- where a.Name == Regex.Replace(td[7].SelectNodes(".//a")[0].InnerText, @"\s", "")
- select a.Id).Take(1).ToJoin(),
- HomeTeamName = td[5].SelectNodes(".//a")[0].InnerText,
- VisitingTeamName = td[7].SelectNodes(".//a")[0].InnerText,
- Status = status,
- EventName = td[1].SelectNodes(".//span")[0].InnerText,
- Result = _result.Trim() == "-" ? string.Empty : _result.Trim(),
- HalfResult = halfResultatus.Trim() == "-" ? string.Empty : halfResultatus.Trim(),
- ColourFruit = colourFruit,
- Session = int.Parse(q.Replace("-", "")),
- Compensate_SOdd = float.Parse(compensate == null ? "0" : compensate[0].InnerText),
- Compensate_POdd = float.Parse(compensate == null ? "0" : compensate[1].InnerText),
- Compensate_FOdd = float.Parse(compensate == null ? "0" : compensate[2].InnerText),
- AsianDish_SOdd = float.Parse(asianDish == null ? "0" : asianDish[0].InnerText),
- AsianDish_Disc = asianDish != null ? asianDish[1].InnerText : string.Empty,
- AsianDish_FOdd = float.Parse(asianDish == null ? "0" : asianDish[2].InnerText),
- CreateDateTime = DateTime.Now,
- ScoreType = type.IsEmpty() ? scoreTypeList.Where(s => s.ItemValue == p.Type).ToList()[0].Id : type,
- Sort = tr.IndexOf(item) + 1
- };
- model.GameId = (from a in gameList
- where a.HomeTeamId == model.HomeTeamId && a.VisitingTeamId == model.VisitingTeamId && ((DateTime)a.StartDateTime).ToString("yyyy-MM-dd") == model.StartDateTime.ToString("yyyy-MM-dd")
- select a.Id).Take(1).ToJoin();
- result.Add(model);
- }
- }
- catch (Exception ex)
- {
- }
- finally
- {
- lock (locker)
- {
- finishcount++;
- Monitor.Pulse(locker); //完成,通知等待队列,告知已完,执行下一个。
- }
- }
- });
- }
- /// <summary>
- /// 获取期数
- /// </summary>
- /// <param name="url"></param>
- /// <param name="type"></param>
- /// <param name="isUpdate"></param>
- /// <returns></returns>
- private IEnumerable<string> GetSession(string url, string type = "", bool isUpdate = false)
- {
- var list = new List<string>();
- var sessionList = new List<string>();
- if (isUpdate)
- {
- var nosessionList = services.Query<F_Score>(" AND (Result IS NULL OR Result ='') AND ScoreType='{0}'".FormatMe(type), "", "a.Session").ToList();
- if (nosessionList.Count <= 0)
- {
- nosessionList = services.Query<F_Score>(" AND ScoreType='{0}'".FormatMe(type), "", "max(a.Session) as Session").ToList();
- if (nosessionList.Count > 0)
- sessionList = (from a in nosessionList
- select a.Session.ToString()).Distinct().ToList();
- }
- else
- {
- sessionList = (from a in nosessionList
- select a.Session.ToString()).Distinct().ToList();
- }
- }
- var doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = url, Title = "足彩" ,Timeout=10000});
- var opent = doc.DocumentNode.SelectSingleNode(".//select[@id='matchSel']").SelectNodes(".//option");
- foreach (var item in opent)
- {
- list.Add(item.Attributes["value"].Value);
- }
- if (sessionList.Count > 0)
- {
- var list_y = from a in list
- join b in sessionList on a.Replace("-", "") equals b
- select b;
- if (list_y.Count() <= 0)
- list_y = sessionList;
- var session = list_y.OrderBy(p => p).ToList()[0];
- return (from a in list
- where int.Parse(a.Replace("-", "")) >= int.Parse(session)
- select a
- ).Distinct().ToList();
- }
- return list;
- }
- }
- }
|