/// <summary> /// 进行异步采集操作 /// </summary>s /// <returns></returns> private bool TickectCollectAsync(CollectConfigModel model, Action <GiftBaseData, bool> CallBack) { if (DayCollectAsync(model, CallBack)) { return(true); } return(false); }
private void BuildCollectTask(CollectConfigModel model, Action <GiftBaseData, bool> CallBack) { TickectCollectAsync(model, CallBack); //var tasks = new List<Task>(); //if (model != null) //{ // tasks.Add(TickectCollectAsync(model)); //} //return tasks; }
private bool All_DayCollectAsync(CollectConfigModel model, Action <GiftBaseData, bool> CallBack) { bool isSucceed = false; var startTime = DateTime.Now; IList <GameDataModel> data = null; try { SendNotify_Ex("采集开始……"); data = CollectContext.HttpGetResult(model, CallBack); SendNotify_Ex("采集结束,开始同步数据……"); if (data != null && data.Count > 0) { int i = 0; foreach (var item in data) { i++; GiftBaseData basedata = new GiftBaseData { GiftName = item.Name, GiftStatus = item.GiftStatus.GetHashCode(), GiftStatusName = item.GiftStatusName, GiftType = item.GiftType.GetHashCode(), GiftTypeName = item.GiftTypeName, PlateForm = item.PlateForm, Remain = item.Remain, Url = item.Url, ValidateTime = item.ValidateTime }; isSucceed = _giftbll.AddandUpdateGift_All(basedata) > 0; if (isSucceed) { if (i == data.Count) { CallBack(basedata, true); } CallBack(basedata, false); } } } return(isSucceed); } catch (Exception) { throw; } }
/// <summary> /// 异步采集普通赛事 /// </summary> /// <param name="ticketData">彩票文件</param> public void StartCollectAsync(CollectConfigModel ticketData, Action <GiftBaseData, bool> CallBack) { BuildCollectTask(ticketData, CallBack); //及时请求最新一期的开奖结果,每15秒执行一次,低频每三分钟一次 //Task.Run(async () => //{ // //创建Task列表,添加延时任务 // while (true) // { // //间隔时间 // int delayTime = 180 * 1000; // var tasks = new List<Task>(); // tasks.AddRange(BuildCollectTask(ticketData)); // await Task.WhenAll(tasks); // await Task.Delay(delayTime); // } //}); }
public static IList <GameDataModel> HttpGetResult(CollectConfigModel item, Action <GiftBaseData, bool> CallBack) { var gameList = new List <GameDataModel>(); if (string.IsNullOrEmpty(item.Url)) { return(null); } var status = EnumHelper.GetDescription(item.GiftStatus); var giftType = EnumHelper.GetDescription(item.GiftType); var plateform = EnumHelper.GetDescription(item.GiftPlatForm); //处理分页问题 var totalRegex = new Regex(ConstantHelper.TotalGifts); //获取总条数采集地址 var requsetUrl = string.Format(item.Url, plateform, giftType, status); string totalResults = new HttpHleper().GetString(requsetUrl, "UTF-8"); var totalCounts = totalRegex.Matches(totalResults); if (totalCounts.Count > 0) { item.Count = int.Parse(totalCounts[0].Value); item.Page = (item.Count + item.PageSize - 1) / item.PageSize; } for (int i = 1; i <= item.Count; i++) { //检测Url为空返回 string result = ""; //result = await new HttpHleper().GetStringAsync(requsetUrl, "UTF-8"); result = new HttpHleper().GetString(string.Concat(requsetUrl, string.Format("&p={0}", i)), "UTF-8"); //result = new HttpHleper().GetHtml(requsetUrl, "", null, @"<table class=""newGifts"">", "</table>", false); //正则匹配数据 var ScopeRegex = new Regex(ConstantHelper.ContentScopeRegx); var nameRegx = new Regex(ConstantHelper.NameRegx); var validateTimeRegex = new Regex(ConstantHelper.ValidateTimeRegx); var remainRegex = new Regex(ConstantHelper.RemainRegx); var urlRegex = new Regex(ConstantHelper.UrlRegx); //获取内容 var searchMatchData = ScopeRegex.Matches(result); //int count = searchMatchData.Count; foreach (Match NextMatch in searchMatchData) { var str = NextMatch.Value; if (nameRegx.IsMatch(str)) { var model = new GameDataModel(); try { model.PlateForm = plateform; model.GiftType = item.GiftType; model.GiftTypeName = giftType; model.GiftStatus = item.GiftStatus; model.GiftStatusName = status; //1.匹配名称 var nameData = nameRegx.Matches(str); if (nameData.Count > 0) { model.Name = nameData[0].Value; } //2.提取有效时间 var validateTimeData = validateTimeRegex.Matches(str); if (validateTimeData.Count > 0) { model.ValidateTime = validateTimeData[0].Value; } //3.提取礼包剩余量 var remainData = remainRegex.Matches(str); if (remainData.Count > 0) { model.Remain = remainData[0].Value; } //4.提取游戏抢包详细页面 var urlData = urlRegex.Matches(str); if (urlData.Count > 0) { model.Url = urlData[0].Value; } if (!string.IsNullOrEmpty(model.Name)) { gameList.Add(model); GiftBaseData basedata = new GiftBaseData { GiftName = model.Name, GiftStatus = model.GiftStatus.GetHashCode(), GiftStatusName = model.GiftStatusName, GiftType = model.GiftType.GetHashCode(), GiftTypeName = model.GiftTypeName, PlateForm = model.PlateForm, Remain = model.Remain, Url = model.Url, ValidateTime = model.ValidateTime }; CallBack(basedata, false); } } catch (Exception) { throw; } } } } return(gameList); }