public async void Download() { if (!EnableRun) { return; } try { //获取自己和所有二级代理,今天采集的京东 var proxys = ProxyUsers; if (proxys.Count > 0) { var scrPage = await chrome.EvaluateScriptAsync("(function (){ return $('.page-num').text()})()"); var strPage = scrPage.Result.ToString().SplitToArray <int>('/'); Page.Number = strPage[0]; Page.Total = strPage[1]; var proxy = proxys.FirstOrDefault(); chrome.ExecuteScriptAsync("$('#bitch-checkbox').click()"); chrome.ExecuteScriptAsync("$('.btn-bitch').click()"); Thread.Sleep(100); chrome.ExecuteScriptAsync("$('#adtType_4').click()"); try { await chrome.Wait("#spaceName option:nth-child(2)", 10000, async() => { chrome.ExecuteScriptAsync("$('#protocol_1,#channel_2').click()"); var html = await chrome.GetSourceAsync(); var doc = CQ.CreateDocument(html); var spaceNames = doc.Select("#spaceName option") .Select(s => WebUtility.HtmlDecode(s.InnerText).Trim()) .ToList(); var index = spaceNames.IndexOf(proxy.PhoneNumber); if (index > 0) { chrome.ExecuteScriptAsync($"$('#spaceName option:nth-child({index + 1})').prop('selected',true);"); } else { chrome.ExecuteScriptAsync($"$('#kind_2').click();$('#positionName').val('{proxy.PhoneNumber}');"); } downloadHandler.Set(prefix: $"{proxy.PhoneNumber}_{NoLoadCids.FirstOrDefault().CidUrls.FirstOrDefault().Cid}_", suffix: $"_{Page.Number}_{Page.Total}"); chrome.ExecuteScriptAsync("$('#getcode-btn').click()"); }); } catch (Exception ex) { OnStateChange?.Invoke(Enums.StateLogType.JdCouponColumnError, "找不到广告位"); LoadAddress(chrome.Address); } } else { //OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponAddDbComplated, $"未检测到有新淘宝商品"); //Task task = new Task(() => //{ // System.Threading.Thread.Sleep(5 * 60 * 1000); // chrome.Load(COUPON_DOWNLOAD_URL); //}); //task.Start(); } } catch (Exception ex) { SystemBase.WriteLog($"加载错误{ex.Message}", "error"); } }
public async void Download() { if (!EnableRun) { return; } try { Action restart = () => { Task task = new Task(() => { System.Threading.Thread.Sleep(5 * 60 * 1000); chrome.Load(COUPON_DOWNLOAD_URL); }); task.Start(); }; //获取自己和所有二级代理,今天采集的淘宝券 var proxys = Bll.Buy.GetProxyCouponCount(UserInfo.ID, Enums.Platform.TaoBao) .Where(s => !string.IsNullOrWhiteSpace(s.PhoneNumber) && s.Count < 1000) .ToList(); if (proxys.Count > 0) { await chrome.Wait(".list-desc", 10000, async() => { chrome.ExecuteScriptAsync("$('.list-desc:last').click()"); await chrome.Wait(".dialog-overlay [mx-click]", 10000, async() => { var scrSite = await chrome.EvaluateScriptAsync("(function (){ return $('#J_sites_dropdown ul li').length;})()"); if (scrSite.Success) { if (Convert.ToInt32(scrSite.Result.ToString()) == 0) { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoNoAddSite, "未添加导购推广"); restart(); return; } } var html = await chrome.GetSourceAsync(); var doc = CQ.CreateDocument(html); var list = doc.Select("#J_zones_dropdown .dropdown-list li span"); var proxy = proxys[0]; //获取联盟广告位 var advs = list.Select(s => WebUtility.HtmlDecode(s.InnerText)).ToList(); var adv = advs.FirstOrDefault(s => s == proxy.PhoneNumber); dlProxy = new Models.BuyUserInfo { ID = proxy.UserID, PhoneNumber = proxy.PhoneNumber }; if (adv == null) //广告位里没有该手机号添加一个 { chrome.ExecuteScriptAsync($"$('#J_newzone_radio').click();$('#J_new_zone').val('{proxy.PhoneNumber}');"); } else //广告位里有选择一个 { var i = advs.IndexOf(adv); chrome.ExecuteScriptAsync($"$('#J_zones_dropdown .dropdown-hd').click();$('#J_zones_dropdown .dropdown-list li:nth-child({i})').click();"); } downloadHandler.Set(prefix: $"{proxy.PhoneNumber}_"); chrome.ExecuteScriptAsync("$('.dialog-overlay button[mx-click=submit]').click();"); }); }); } else { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponAddDbComplated, $"未检测到有新淘宝商品"); restart(); } } catch (Exception ex) { SystemBase.WriteLog($"加载错误{ex.Message}", "error"); } }
public void UploadTaobao(string path) { var fileInfo = new FileInfo(path); var fileName = fileInfo.Name; string fileUrl = null; try { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponUploadStart, $"代理{dlProxy.PhoneNumber}的{fileName}开始上传"); Dictionary <string, string> files = new Dictionary <string, string>(); files.Add("file", path); var uploadFileApi = new Api.BuyUploadApi(files); fileUrl = uploadFileApi.CreateRequestReturnUrls()[0]; OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponUploadComplated, $"代理{dlProxy.PhoneNumber}的{fileName}上传完成"); try { OnStateChange(Enums.StateLogType.TaoBaoCouponAddDbStart, $"代理{dlProxy.PhoneNumber}的{fileName}开始处理"); var api = new Api.BuyApi("Import", "Taobao", new { UserID = dlProxy.ID, Url = fileUrl }); var result = api.CreateRequestReturnBuyResult <object>(); if (result.State == "Success") { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponAddDbComplated, $"代理{dlProxy.PhoneNumber}的{fileName}处理完成"); } else { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponAddDbFail, $"代理{dlProxy.PhoneNumber}的{fileName}处理失败"); } } catch (Exception ex) { try { Bll.Buy.LoopCheckCouponUserTemps(dlProxy.ID, Enums.Platform.TaoBao); OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponAddDbComplated, $"代理{dlProxy.PhoneNumber}的{fileName}处理完成"); } catch (Exception) { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponDownloadFail, $"{fileName}处理失败"); SystemBase.WriteLog($"提交失败 {ex.Message}", "UploadTaobao"); } } } catch (Exception ex) { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponUploadFail, $"代理{dlProxy.PhoneNumber}的{fileName}上传失败"); SystemBase.WriteLog($"上传失败 {ex.Message}", "UploadTaobao"); } finally { try { fileInfo.Delete(); } catch (Exception ex) { OnStateChange?.Invoke(Enums.StateLogType.TaoBaoCouponDeleteTemp, $"代理{dlProxy.PhoneNumber}的{fileName}缓存文件删除失败"); } } Task task = new Task(() => { chrome.Load(COUPON_DOWNLOAD_URL); }); task.Start(); }
public void LoadCoupon() { OnStateChange?.Invoke(Enums.StateLogType.MoGuJieGetCouponStart, "开始加载蘑菇街优惠券"); var cids = MoGuJie.Method.AllCategory; var pageSize = 100; var models = new List <Coupon>(); foreach (var cid in cids) { if (!EnableRun) { return; } try { //获取第一页 var result = mgj.GetItemList(channels, cid, pageNo: 1, pageSize: pageSize); //已经初始化过了,就不往下加载了 if (result.Total > 0) { models.AddRange(result.Items); //获取 if (!onlyFirstPage) { for (int i = 2; i <= result.TotalPage; i++) { var result2 = mgj.GetItemList(channels, cid, pageNo: i, pageSize: pageSize); models.AddRange(result2.Items); } } } } catch (Exception ex) { SystemBase.WriteLog(ex.Message, "MoGuJie"); } } OnStateChange?.Invoke(Enums.StateLogType.MoGuJieGetCouponComplated, "加载蘑菇街优惠券加载完成"); var dirInfo = new System.IO.DirectoryInfo($"{Config.RuningPath}\\temp"); if (!dirInfo.Exists) { dirInfo.Create(); } foreach (var item in channels) { var pModel = models.Where(s => s.UserID == item.UserID).ToList(); var path = $"{Config.RuningPath}\\temp\\蘑菇街_{item.Name}_{DateTime.Now:yyyyMMddHHmm}.json"; var fileInfo = new System.IO.FileInfo(path); System.IO.File.WriteAllText(path, JsonConvert.SerializeObject(pModel)); try { OnStateChange?.Invoke(Enums.StateLogType.MoGuJieCouponAddDbStart, $"蘑菇街 代理{item.Name} 提交开始"); var files = new Dictionary <string, string>(); files.Add("file", path); var upload = new Api.BuyUploadApi(files).CreateRequestReturnUrls(); var update = new Api.BuyApi("ImportItems", "MoGuJie", new { Url = upload[0] }).CreateRequestReturnBuyResult <object>(); if (update.State == "Success") { OnStateChange?.Invoke(Enums.StateLogType.MoGuJieCouponAddDbComplated, $"蘑菇街 代理{item.Name} 提交完成"); try { System.IO.File.Delete(path); } catch (Exception) { } } else { OnStateChange?.Invoke(Enums.StateLogType.MoGuJieCouponAddDbError, update.Message); } } catch (Exception ex) { try { Bll.Buy.LoopCheckCouponUserTemps(item.UserID, Enums.Platform.MGJ); OnStateChange?.Invoke(Enums.StateLogType.MoGuJieCouponAddDbComplated, $"蘑菇街 代理{item.Name} 提交完成"); } catch (Exception) { OnStateChange?.Invoke(Enums.StateLogType.MoGuJieCouponAddDbError, $"蘑菇街 代理{item.Name} 提交超时"); } } finally { try { System.IO.File.Delete(path); } catch (Exception ex) { OnStateChange?.Invoke(Enums.StateLogType.MoGuJieCouponDeleteTemp, $"蘑菇街 代理{item.Name} 提交有误"); } } } onlyFirstPage = true; var task = new Task(() => { Thread.Sleep(5 * 60 * 1000); mgj.RefeashToken(); LoadCoupon(); }); task.Start(); }