AgainstJob.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using FCS.Common;
  9. using FCS.Interface;
  10. using FCS.Models;
  11. using FCS.Models.DTO;
  12. using HtmlAgilityPack;
  13. using Quartz;
  14. using Newtonsoft.Json;
  15. using System.Text.RegularExpressions;
  16. namespace FCS.Crawler.ZCLotteryAgainst
  17. {
  18. /// <summary>
  19. /// 对阵
  20. /// </summary>
  21. public class AgainstJob : CommonJob, IJob
  22. {
  23. private static List<AgainstTypeDTO> againstTypeList;//页面的参数
  24. private List<F_Team> teamList;//比赛信息
  25. private List<F_Events> eventsList;//赛事信息
  26. private int courteenCode = 14;//14
  27. private int sixHalfCode = 6;
  28. private int fourCode = 4;
  29. private List<string> vreifiList;//符号验证
  30. private List<AgainstEntity> result = new List<AgainstEntity>();
  31. public AgainstJob()
  32. {
  33. teamList = services.Query<F_Team>().ToList();
  34. eventsList = services.Query<F_Events>().ToList();
  35. vreifiList = new List<string>
  36. {
  37. "0",":","-"
  38. };
  39. againstTypeList = new List<AgainstTypeDTO> {
  40. new AgainstTypeDTO{
  41. Code=courteenCode,
  42. CaiZhong="13",
  43. Url="http://cp.zgzcw.com/lottery/zcplayvs.action",
  44. LotteryId="300",
  45. SessionUrl1="http://cp.zgzcw.com/lottery/getissue.action?issueLen=10&lotteryId=300",//"/lottery/getwqissuereturnall.action?lotteryId=300&issueLen=20&d=1538101462272"
  46. SessionUrl2="http://cp.zgzcw.com/lottery/getwqissuereturnall.action?lotteryId=300&issueLen=20&d="//"/lottery/getissue.action?lotteryId=300&issueLen=20&d=1538101378587"
  47. },
  48. new AgainstTypeDTO{
  49. Code=sixHalfCode,
  50. CaiZhong="15",
  51. Url="http://cp.zgzcw.com/lottery/zcplayvs.action",
  52. LotteryId="302",
  53. SessionUrl1="http://cp.zgzcw.com/lottery/getissue.action?issueLen=10&lotteryId=302",
  54. SessionUrl2="http://cp.zgzcw.com/lottery/getwqissuereturnall.action?lotteryId=302&issueLen=20&d="
  55. },
  56. new AgainstTypeDTO{
  57. Code=fourCode,
  58. CaiZhong="16",
  59. Url="http://cp.zgzcw.com/lottery/zcplayvs.action",
  60. LotteryId="303",
  61. SessionUrl1="http://cp.zgzcw.com/lottery/getissue.action?issueLen=10&lotteryId=303",
  62. SessionUrl2="http://cp.zgzcw.com/lottery/getwqissuereturnall.action?lotteryId=303&issueLen=20&d="
  63. },
  64. };
  65. }
  66. public void Execute(IJobExecutionContext context)
  67. {
  68. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  69. Click();
  70. }
  71. public void Click()
  72. {
  73. againstTypeList.ForEach(d =>
  74. {
  75. var sessionList = new List<string>();
  76. if (d.Code == courteenCode)
  77. sessionList = GetSession<F_Fourteen>(d, true).ToList();
  78. else if (d.Code == sixHalfCode)
  79. sessionList = GetSession<F_SixHalf>(d, true).ToList();
  80. else
  81. sessionList = GetSession<F_Four>(d, true).ToList();
  82. threadCount = sessionList.Count;
  83. finishcount = 0;
  84. sessionList.ForEach(p =>
  85. {
  86. Analysis(d, p);
  87. });
  88. lock (locker)
  89. {
  90. while (finishcount != threadCount)
  91. {
  92. Thread.Sleep(5000);
  93. Monitor.Wait(locker);//等待
  94. }
  95. }
  96. var deleteWhere = "";
  97. sessionList.ForEach(p =>
  98. {
  99. deleteWhere += p + ",";
  100. });
  101. deleteWhere = deleteWhere.ToString().Substring(0, deleteWhere.ToString().Length - 1);
  102. if (d.Code == courteenCode)
  103. {
  104. services.Delete<F_Fourteen>(" AND Session in ({0})".FormatMe(deleteWhere));
  105. services.SqlBulkCopyAdd(Mapper<List<F_Fourteen>>(result));
  106. }
  107. else if (d.Code == sixHalfCode)
  108. {
  109. services.Delete<F_SixHalf>(" AND Session in ({0})".FormatMe(deleteWhere));
  110. services.SqlBulkCopyAdd(Mapper<List<F_SixHalf>>(result));
  111. }
  112. else
  113. {
  114. services.Delete<F_Four>(" AND Session in ({0})".FormatMe(deleteWhere));
  115. services.SqlBulkCopyAdd(Mapper<List<F_Four>>(result));
  116. }
  117. });
  118. }
  119. public void GetALL()
  120. {
  121. againstTypeList.ForEach(d =>
  122. {
  123. var sessionList = GetSession<F_Fourteen>(d).ToList();
  124. var result = new List<AgainstEntity>();
  125. sessionList.ForEach(p =>
  126. {
  127. Analysis(d, p);
  128. });
  129. while (true)
  130. {
  131. if (CommonHelper.ThreadsFinsh())
  132. break;
  133. }
  134. if (d.Code == courteenCode)
  135. services.SqlBulkCopyAdd(Mapper<List<F_Fourteen>>(result));
  136. else if (d.Code == sixHalfCode)
  137. services.SqlBulkCopyAdd(Mapper<List<F_SixHalf>>(result));
  138. else
  139. services.SqlBulkCopyAdd(Mapper<List<F_Four>>(result));
  140. });
  141. }
  142. private async Task Analysis(AgainstTypeDTO d, string p)
  143. {
  144. await Task.Run(() =>
  145. {
  146. try
  147. {
  148. //http://cp.zgzcw.com/lottery/zcplayvs.action?lotteryId=13&issue=18094&v=1533868879113
  149. var url = d.Url + "?lotteryId=" + d.CaiZhong + "&issue=" + p + "&v=" + CommonHelper.ConvertDateTimeToInt(DateTime.Now);
  150. var doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = url });
  151. while (doc.DocumentNode.InnerText.ToLower().Contains("html"))//如果包含Html,不符合
  152. doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = url });
  153. var data = JsonConvert.DeserializeObject<AgainstGamesData>(doc.DocumentNode.InnerText);
  154. data.matchInfo.ToList().ForEach(q =>
  155. {
  156. var index = data.matchInfo.ToList().IndexOf(q);
  157. result.Add(new AgainstEntity
  158. {
  159. Id = CommonHelper.GetGuid().ToString(),
  160. Session = int.Parse(q.issue),
  161. StartDateTime = DateTime.Parse(q.gameStartDate),
  162. HomeTeamId = (from a in teamList
  163. where a.Name == Regex.Replace(q.hostName, @"\s", "")
  164. select a.Id).Take(1).ToJoin(),
  165. VisitingTeamId = (from a in teamList
  166. where a.Name == Regex.Replace(q.guestName, @"\s", "")
  167. select a.Id).Take(1).ToJoin(),
  168. HomeTeamName = q.hostName,
  169. VisitingTeamName = q.guestName,
  170. HalfResult = vreifiList.Contains(q.zuizhongbifen.Trim()) ?
  171. string.Empty : d.Code == sixHalfCode ? q.zuizhongbifen.Split(';')[index].Split(',')[0] : string.Empty,
  172. Result = vreifiList.Contains(q.zuizhongbifen.Trim()) ?
  173. string.Empty : d.Code == sixHalfCode ? q.zuizhongbifen.Split(';')[index].Split(',')[1] : q.zuizhongbifen.Split(';')[index],
  174. HalfColourFruit = d.Code == sixHalfCode || d.Code == fourCode ? q.code == "0" ? string.Empty : q.code.Split(',')[index * 2 + 1] : string.Empty,
  175. ColourFruit = d.Code == sixHalfCode || d.Code == fourCode ? q.code == "0" ? string.Empty : q.code.Split(',')[index * 2 + 2] : q.code == "0" ? string.Empty : q.code.Split(',')[index],
  176. Compensate_SOdd = q.europeSp.Trim().IsEmpty() ? 0 : float.Parse(q.europeSp.Split(' ')[0]),
  177. Compensate_POdd = q.europeSp.Trim().IsEmpty() ? 0 : float.Parse(q.europeSp.Split(' ')[1]),
  178. Compensate_FOdd = q.europeSp.Trim().IsEmpty() ? 0 : float.Parse(q.europeSp.Split(' ')[2]),
  179. AsianDish_SOdd = q.yapan.Trim().IsEmpty() ? 0 : float.Parse(q.europeSp.Split(' ')[0]),
  180. AsianDish_Disc = q.yapan.Trim().IsEmpty() ? "" : q.yapan.Split(' ')[1],
  181. AsianDish_FOdd = q.yapan.Trim().IsEmpty() ? 0 : float.Parse(q.yapan.Split(' ')[2]),
  182. EventId = eventsList.Where(s => s.Name.Contains(Regex.Replace(q.leageName, @"\s", ""))).ToList()[0].Id,
  183. CreateDateTime = DateTime.Now,
  184. Sort = index + 1
  185. });
  186. });
  187. }
  188. catch (Exception)
  189. {
  190. }
  191. finally
  192. {
  193. lock (locker)
  194. {
  195. finishcount++;
  196. Monitor.Pulse(locker); //完成,通知等待队列,告知已完,执行下一个。
  197. }
  198. }
  199. });
  200. }
  201. /// <summary>
  202. /// 得到期数
  203. /// </summary>
  204. /// <param name="url"></param>
  205. /// <returns></returns>
  206. private IEnumerable<string> GetSession<T>(AgainstTypeDTO model, bool isUpdate = false)
  207. {
  208. var sessionList = new List<string>();
  209. if (isUpdate)
  210. {
  211. var nosessionList = services.Query<T, AgainstEntity>(" AND Result IS NULL OR Result =''", "", "a.Session").ToList();
  212. if (nosessionList.Count <= 0)
  213. {
  214. nosessionList = services.Query<T, AgainstEntity>("", "", "max(a.Session) as Session").ToList();
  215. if (nosessionList.Count > 0)
  216. sessionList = (from a in nosessionList
  217. select a.Session.ToString()).Distinct().ToList();
  218. }
  219. else
  220. {
  221. sessionList = (from a in nosessionList
  222. select a.Session.ToString()).Distinct().ToList();
  223. }
  224. }
  225. //期数1
  226. var doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = model.SessionUrl1 });
  227. while (doc.DocumentNode.InnerText.ToLower().Contains("html"))
  228. doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = model.SessionUrl1 });
  229. var list = JsonConvert.DeserializeObject<List<AgainstSession>>(doc.DocumentNode.InnerText);
  230. //期数2
  231. doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = model.SessionUrl2 + CommonHelper.ConvertDateTimeToInt(DateTime.Now) });
  232. while (doc.DocumentNode.InnerText.ToLower().Contains("html"))
  233. doc = CommonHelper.GetHtmlHtmlDocument(new HtmlParameterDTO { Url = model.SessionUrl2 + CommonHelper.ConvertDateTimeToInt(DateTime.Now) });
  234. var list1 = JsonConvert.DeserializeObject<List<AgainstSession>>(doc.DocumentNode.InnerText);
  235. //判断依据来自:zc.min.js(对于页面的js分析得出)
  236. list1 = (from a in list1
  237. where a.status != "9" && !(from b in list select b.issue).Contains(a.issue)
  238. select a).ToList();
  239. list = list.Concat(list1).Distinct().ToList();
  240. if (sessionList != null)
  241. {
  242. if (sessionList.Count > 0)
  243. {
  244. var list_y = from a in list
  245. join b in sessionList on a.issue equals b
  246. select b;
  247. if (list_y.Count() <= 0)
  248. list_y = sessionList;
  249. var session = list_y.OrderBy(p => p).ToList()[0];
  250. return (from a in list
  251. where int.Parse(a.issue) >= int.Parse(session.IsEmpty() ? "0" : session)
  252. select a.issue
  253. ).Distinct();
  254. }
  255. }
  256. return (from a in list
  257. select a.issue
  258. ).Distinct();
  259. }
  260. }
  261. }