AgainstJob.cs 13 KB

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