private async Task <StockUpdatePriceAndDatesDto> ScrapePriceAndDates(Stock stock) { this._consoleHubProxy.WriteLine(ConsoleWriteLineInput.Create($"ScrapePriceAndDates for : {stock.Symbol}")); StockUpdatePriceAndDatesDto dto = stock.MapTo <StockUpdatePriceAndDatesDto>(); using (WebClient webClient = new WebClient()) { String html = await webClient.DownloadStringTaskAsync(String.Format(YahooUrl, stock.Symbol)); var matches = _regexExDividendDate.Matches(html); dto.ExDividendDate = null; DateTime exDividendDate = DateTime.MinValue; if (matches.Count > 0 && DateTime.TryParse(matches[0].Groups["ExDividendDate"].Value, out exDividendDate)) { dto.ExDividendDate = exDividendDate; } matches = _regexTargetPrice.Matches(html); dto.TargetPrice = null; Decimal targetPrice = 0m; if (matches.Count > 0 && Decimal.TryParse(matches[0].Groups["TargetPrice"].Value, out targetPrice)) { dto.TargetPrice = targetPrice; } matches = _regexNextEarningsDate.Matches(html); dto.NextEarningsDate = null; DateTime nextEarningsDate = DateTime.MinValue; if (matches.Count > 0 && DateTime.TryParse(matches[0].Groups["NextEarningsDate"].Value, out nextEarningsDate)) { dto.NextEarningsDate = nextEarningsDate; } matches = _regexPrice.Matches(html); dto.Price = 0m; Decimal price = 0m; if (matches.Count > 0 && Decimal.TryParse(matches[0].Groups["Price"].Value, out price)) { dto.Price = price; } matches = _regexAvgVolume.Matches(html); dto.AvgVolume = 0; int avgVolume = 0; if (matches.Count > 0 && Int32.TryParse(matches[0].Groups["AvgVolume"].Value.Replace(",", String.Empty), out avgVolume)) { dto.AvgVolume = avgVolume; } if (dto.AvgVolume.HasValue) { dto.ADV = dto.AvgVolume.Value * dto.Price; } } return(dto); }
public void Save(StockUpdatePriceAndDatesDto dto) { if (dto.IsNew) { Stock stock = dto.MapTo <Stock>(); this._repository.Insert(stock); } else { Stock stock = this._repository.Get(dto.Id); dto.MapTo(stock); } }