private async Task <bool> EnsureHistoryLoaded(IStockModel stock) { if (!stock.MonthStatsExpired) { return(true); } try { await StocksManager.CheckMonthStatsAsync(stock).TimeoutAfter(TimeSpan.FromSeconds(5)); } catch (Exception ex) { var errorText = $"Не удалось загрузить историю {stock.Ticker}: {ex.Message}"; MainModel.AddErrorMessage(errorText); Logger?.LogError(ex, errorText); return(false); } if (stock.AvgDayVolumePerMonth < 0.01m) { var errorText = $"Не удалось загрузить историю {stock.Ticker}"; MainModel.AddErrorMessage(errorText); Logger?.LogError(errorText); return(false); } return(true); }
public async Task <bool> Authorize(string login, string password) { var startUrl = Settings.USAQuotesURL.Split(new [] { '/' }, StringSplitOptions.RemoveEmptyEntries)[1]; var response = await _httpClient.PostAsync($"https://{startUrl}/auth/g/authenticate/v0/501/{login}/{password}/", new StringContent(""), _taskCancellation.Token); dynamic result = JObject.Parse(await response.Content.ReadAsStringAsync()); if (result.code.name != "Ok") { MainModel.AddErrorMessage("L2DataService: " + result.message); return(false); } _sessionId = result.sid; return(true); }
public async Task GetDataAsync(CancellationToken token) { if (!await Authorize(Settings.USAQuotesLogin, Settings.USAQuotesPassword)) { Stop(); return; } var tickers = new List <string>(); while (!token.IsCancellationRequested) { tickers.Clear(); lock (_subscribedTickers) { tickers.AddRange(_subscribedTickers); } if (tickers.Count == 0) { await Task.Delay(100, token); continue; } bool hasError = false; try { int totalParts = (int)Math.Ceiling(tickers.Count / 100m); var runResult = Parallel.For(0, totalParts, new ParallelOptions() { MaxDegreeOfParallelism = 4 }, (partNum) => { var teekkaz = tickers.Skip(partNum * 100).Take(100).ToList(); var tstr = String.Join(",", teekkaz); try { var resp = _httpClient.PostAsync( $"{Settings.USAQuotesURL}?symbols={tstr}&webmasterId=501&sid={_sessionId}", new StringContent(""), token).Result; var text = resp.Content.ReadAsStringAsync().Result; ParseQuotes(text); } catch (Exception ex) { MainModel.AddErrorMessage("L2DataService: " + ex.Message); hasError = true; } }); while (!runResult.IsCompleted) { await Task.Delay(100); } } catch (Exception ex) { MainModel.AddErrorMessage("L2DataService: " + ex.Message); } if (hasError) { await Authorize(Settings.USAQuotesLogin, Settings.USAQuotesPassword); } await Task.Delay(1000, token); } }