public async Task<List<Tariff>> Estimate(Parcel parcel, DeliveryOptions deliveryOptionsoptions, CancellationToken ct) { await _browser.ExecuteJs(JsCode, ct); var tariffs = new List<Tariff>(); var deliveryPath = await SetDeliveryPath(parcel, ct); if (string.IsNullOrWhiteSpace(deliveryPath)) return tariffs; await SetParcelProperties(parcel, ct); if (!await _browser.Select("#calc_calculate_btn", ct)) throw new InvalidOperationException("Can't find calculate button"); _browser.Click(); if (!await _browser.WaitForAjax(5000, ct)) throw new TimeoutException("Failed to wait for cost calculation"); await _browser.ExecuteJs(JsCode, ct); var itariffs = (await _browser.ExecuteJs("return fragma.modules.dpdTariffs.getTariffs()", ct) as IList); foreach (dynamic itariff in itariffs) { var dict = itariff as Dictionary<string, object>; var serviceName = dict["serviceName"].ToString(); var cost = dict["cost"].ToString(); var tariff = new Tariff { ProviderType = TariffProviderType.DPD, SubProviderName = serviceName, DeliveryMethod = DeliveryMethod.ToDoor, DeliveryPath = deliveryPath, Price = decimal.Parse(cost, CultureInfo.InvariantCulture) }; var days = dict["days"].ToString(); var times = days.Split('-'); var min = TimeSpan.Zero; min = new TimeSpan(int.Parse(times[0].Trim()), 0, 0, 0); var max = min; if (times.Count() > 1) max = new TimeSpan(int.Parse(times[1].Trim()), 0, 0, 0); tariff.MinDeliveryTime = min; tariff.MaxDeliveryTime = max; tariff.DeliveryTime = max; tariffs.Add(tariff); } return tariffs; }
private async Task<List<Tariff>> GetTariffs(IBrowser browser, CancellationToken ct) { await browser.Select("#spsr-calculator-form a.btn[onclick='calculate()']", ct); browser.Click(); if (!await browser.WaitForAjax(10000, ct)) throw new TimeoutException("Failed to wait for tariffs"); var tariffsResponse = (await browser.ExecuteJs("return fragma.modules.tariffs.getTariffs()", ct) as IList); var sTariffs = new List<string>(); foreach (var c in tariffsResponse) sTariffs.Add(c.ToString()); var tariffs = new List<Tariff>(); foreach (var tariffOptions in sTariffs.Chunk(3)) { var tariff = new Tariff(); var opts = tariffOptions.ToArray(); tariff.ProviderType = TariffProviderType.SPSR; tariff.SubProviderName = opts[0]; tariff.Price = decimal.Parse(opts[1].Split(' ').FirstOrDefault(), CultureInfo.InvariantCulture); tariff.DeliveryMethod = DeliveryMethod.ToTerminal; var times = opts[2].Split('-'); var min = TimeSpan.Zero; min = new TimeSpan(int.Parse(times[0].Trim()), 0, 0, 0); var max = min; if (times.Count() > 1) { max = new TimeSpan(int.Parse(times[1].Trim()), 0, 0, 0); } tariff.MinDeliveryTime = min; tariff.MaxDeliveryTime = max; tariff.DeliveryTime = max; tariffs.Add(tariff); } return tariffs; }