GamesDetailsJob.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using FCS.Common;
  11. using FCS.Crawler.ZCLotteryIP;
  12. using FCS.Interface;
  13. using FCS.Models;
  14. using HtmlAgilityPack;
  15. using Quartz;
  16. namespace FCS.Crawler.ZCLotteryGames
  17. {
  18. public class GamesDetailsJob : CommonJob, IJob
  19. {
  20. private Dictionary<string, int> detailDict;//比赛类型关系
  21. private List<DataItemDetail> detailType;//比赛类型
  22. private List<F_Players> playersList;//球员信息
  23. private static object locker = new object();
  24. private static List<F_GamesDetails> result = new List<F_GamesDetails>();
  25. private static string homeTeamName = "zhudui";
  26. public GamesDetailsJob()
  27. {
  28. //new IPJob().GetIP();
  29. playersList = services.Query<F_Players>().ToList();
  30. detailType = services.GetDataItem(DataItemDetailEnum.FootBallDetailType);
  31. // < !--1 : 入球, 2 : 红牌, 3 : 黄牌, 7 : 点球, 8 : 乌龙, 9 : 两黄变红1111111-- >
  32. detailDict = new Dictionary<string, int> {
  33. {"1",1 },{"3",5 },
  34. {"2",6 },{"7",2 },
  35. {"8",3 },{"9",6 },
  36. {"11",9 }
  37. };
  38. }
  39. public void Execute(IJobExecutionContext context)
  40. {
  41. Config = CommonHelper.GetConfigFromDataMap(context.JobDetail.JobDataMap);
  42. Click();
  43. }
  44. public void Click(string mainUrl = "http://fenxi.zgzcw.com")
  45. {
  46. CommonHelper.LogBD(typeof(GamesDetailsJob), () =>
  47. {
  48. var gamesList = services.Query<F_Games>(@" AND DateName(year,GetDate())=datename(year,StartDateTime)
  49. AND DateName(month, GetDate()) = datename(month, StartDateTime)
  50. AND DateName(day, GetDate()) = datename(day, StartDateTime)").ToList();
  51. var gamesDetailsList = services.Query<F_GamesDetails>(@"
  52. AND DateName(year,GetDate())=datename(year,b.StartDateTime)
  53. AND DateName(month, GetDate()) = datename(month, b.StartDateTime)
  54. AND DateName(day, GetDate()) = datename(day, b.StartDateTime)
  55. ", @"JOIN F_Games b ON a.GameId =b.Id") ?? new List<F_GamesDetails>();
  56. threadCount = gamesList.Count;
  57. gamesList.ForEach(p =>
  58. {
  59. Analysis(p);
  60. });
  61. lock (locker)
  62. {
  63. while (finishcount != threadCount)
  64. {
  65. Thread.Sleep(5000);
  66. Monitor.Wait(locker);//等待
  67. }
  68. }
  69. var data = new List<F_GamesDetails>();
  70. var addList = (from a in result
  71. where !(from b in gamesDetailsList orderby b.GameId select b.GameId).Contains(a.GameId)
  72. orderby a.GameId
  73. select a).ToList();
  74. data = data.Concat(addList).ToList();
  75. addList = (from a in result
  76. where !(from b in gamesDetailsList where a.GameId == b.GameId orderby b.GameId select b.Minutes).Contains(a.Minutes)
  77. && (from b in gamesDetailsList where a.GameId == b.GameId orderby b.GameId select b.GameId).Contains(a.GameId)
  78. orderby a.GameId
  79. select a).ToList();
  80. data = data.Concat(addList).ToList();
  81. services.SqlBulkCopyAdd(data);
  82. });
  83. }
  84. public void GetALL(string mainUrl = "http://fenxi.zgzcw.com")
  85. {
  86. var gamesList = services.Query<F_Games>("AND (Season LIKE '{0}%' or Season LIKE '%{1}%')".FormatMe(DateTime.Now.Year, DateTime.Now.AddYears(1).Year)).ToList();
  87. var result = new List<F_GamesDetails>();
  88. // var _gamesList = gamesList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  89. gamesList.ForEach(p =>
  90. {
  91. Analysis(p);
  92. });
  93. while (true)
  94. {
  95. if (CommonHelper.ThreadsFinsh())
  96. break;
  97. }
  98. services.SqlBulkCopyAdd(result);
  99. }
  100. private async Task Analysis(F_Games p, string mainUrl = "http://fenxi.zgzcw.com")
  101. {
  102. await Task.Run(() =>
  103. {
  104. try
  105. {
  106. var number = p.Remark.Split('/')[p.Remark.Split('/').ToList().Count - 2];
  107. var url = mainUrl + "/" + number + "/zrtj";
  108. Trace.WriteLine("URL:" + url);
  109. var doc = CommonHelper.GetHtml(url, "足彩");
  110. var jstj = doc.DocumentNode.SelectSingleNode(".//div[@class='jstj']");
  111. if (jstj != null)
  112. {
  113. var li = jstj.SelectNodes(".//li");
  114. if (li != null)
  115. foreach (HtmlNode item in li)
  116. {
  117. var liclassList = item.Attributes["data"].Value.Split(',').ToList();
  118. var detail = string.Empty;
  119. if (detailDict.ContainsKey(liclassList[1]))
  120. detail = detailType.Where(q => q.ItemValue == detailDict[liclassList[1]].ToString()).ToList()[0].Id;
  121. else
  122. continue;
  123. lock (locker)
  124. {
  125. result.Add(new F_GamesDetails
  126. {
  127. Id = CommonHelper.GetGuid().ToString(),
  128. CreateDateTime = DateTime.Now,
  129. GameId = p.Id,
  130. Minutes = int.Parse(liclassList[0]),
  131. // PlayerId = playerId,
  132. DetailType = detail,
  133. Describe = liclassList[2],
  134. IsHomeTeam = item.ParentNode.Attributes["class"].Value == homeTeamName ? true : false
  135. });
  136. }
  137. }
  138. }
  139. }
  140. finally
  141. {
  142. lock (locker)
  143. {
  144. finishcount++;
  145. Monitor.Pulse(locker); //完成,通知等待队列,告知已完,执行下一个。
  146. }
  147. }
  148. });
  149. }
  150. }
  151. }