private void InsertOrUpdateItem(SxResultsContext dbContext, DailyResult dailyItem) { var item = dbContext.DailyResults.Where(r => r.Date == dailyItem.Date).FirstOrDefault(); if (item == null) { //dont insert if 4 first value is all 0! if (dailyItem.Special != 0 || dailyItem.First != 0 || dailyItem.Second1 != 0 || dailyItem.Second2 != 0) dbContext.DailyResults.Add(dailyItem); else _logger.Error("All 4 first values are 0 on day:" + dailyItem.Date.ToShortDateString()); } else { //update item.First = dailyItem.First; item.Second1 = dailyItem.Second1; item.Second2 = dailyItem.Second2; item.Third1 = dailyItem.Third1; item.Third2 = dailyItem.Third2; item.Third3 = dailyItem.Third3; item.Third4 = dailyItem.Third4; item.Third5 = dailyItem.Third5; item.Third6 = dailyItem.Third6; item.Fourth1 = dailyItem.Fourth1; item.Fourth2 = dailyItem.Fourth2; item.Fourth3 = dailyItem.Fourth3; item.Fourth4 = dailyItem.Fourth4; item.Fifth1 = dailyItem.Fifth1; item.Fifth2 = dailyItem.Fifth2; item.Fifth3 = dailyItem.Fifth3; item.Fifth4 = dailyItem.Fifth4; item.Fifth5 = dailyItem.Fifth5; item.Fifth6 = dailyItem.Fifth6; item.Sixth1 = dailyItem.Sixth1; item.Sixth2 = dailyItem.Sixth2; item.Sixth3 = dailyItem.Sixth3; item.Seventh1 = dailyItem.Seventh1; item.Seventh2 = dailyItem.Seventh2; item.Seventh3 = dailyItem.Seventh3; item.Seventh4 = dailyItem.Seventh4; } }
public void FetchData(DateTime fromDate, DateTime toDate) { DateTime now = toDate; var count = 0; while (now >= fromDate) { count++; try { var d = now.ToString(DateFormat); WebRequest req = WebRequest.CreateHttp(string.Format(URL, d)); //WebRequest.CreateHttp("http://www.minhngoc.net.vn/ket-qua-xo-so/mien-bac/" + d + ".html"); req.Method = "GET"; var res = req.GetResponse(); var dailyItem = new DailyResult(); dailyItem.Date = now; bool found = false; using (var s = new StreamReader(res.GetResponseStream())) { found = ExtractAllValues(dailyItem, found, s); } if (found) { InsertOrUpdateItem(dbContext, dailyItem); } else { _logger.Error("Do not found (special) data on day:" + dailyItem.Date.ToShortDateString()); } now = now.AddDays(-1); if (count % 100 == 0) dbContext.SaveChanges(); } catch (Exception ex) { _logger.Error(ex); } } dbContext.SaveChanges(); }
private bool ExtractAllValues(DailyResult dailyItem, bool found, StreamReader s) { var result = s.ReadToEnd(); var match = Special.Match(result); if (match.Success) { found = true; dailyItem.Special = int.Parse(match.Groups[1].Value); } match = First.Match(result); if (match.Success) { dailyItem.First = int.Parse(match.Groups[1].Value); } else _logger.Error("Do not found first data on day:" + dailyItem.Date.ToShortDateString()); match = Second.Match(result); if (match.Success) { dailyItem.Second1 = int.Parse(match.Groups[1].Value); dailyItem.Second2 = int.Parse(match.Groups[2].Value); } else _logger.Error("Do not found second data on day:" + dailyItem.Date.ToShortDateString()); match = Third.Match(result); if (match.Success) { dailyItem.Third1 = int.Parse(match.Groups[1].Value); dailyItem.Third2 = int.Parse(match.Groups[2].Value); dailyItem.Third3 = int.Parse(match.Groups[3].Value); dailyItem.Third4 = int.Parse(match.Groups[4].Value); dailyItem.Third5 = int.Parse(match.Groups[5].Value); dailyItem.Third6 = int.Parse(match.Groups[6].Value); } else _logger.Error("Do not found third data on day:" + dailyItem.Date.ToShortDateString()); match = Fourth.Match(result); if (match.Success) { dailyItem.Fourth1 = int.Parse(match.Groups[1].Value); dailyItem.Fourth2 = int.Parse(match.Groups[2].Value); dailyItem.Fourth3 = int.Parse(match.Groups[3].Value); dailyItem.Fourth4 = int.Parse(match.Groups[4].Value); } else _logger.Error("Do not found fourth data on day:" + dailyItem.Date.ToShortDateString()); match = Fifth.Match(result); if (match.Success) { dailyItem.Fifth1 = int.Parse(match.Groups[1].Value); dailyItem.Fifth2 = int.Parse(match.Groups[2].Value); dailyItem.Fifth3 = int.Parse(match.Groups[3].Value); dailyItem.Fifth4 = int.Parse(match.Groups[4].Value); dailyItem.Fifth5 = int.Parse(match.Groups[5].Value); dailyItem.Fifth6 = int.Parse(match.Groups[6].Value); } else _logger.Error("Do not found fifth data on day:" + dailyItem.Date.ToShortDateString()); match = Sixth.Match(result); if (match.Success) { dailyItem.Sixth1 = int.Parse(match.Groups[1].Value); dailyItem.Sixth2 = int.Parse(match.Groups[2].Value); dailyItem.Sixth3 = int.Parse(match.Groups[3].Value); } else _logger.Error("Do not found sixth data on day:" + dailyItem.Date.ToShortDateString()); match = Seventh.Match(result); if (match.Success) { dailyItem.Seventh1 = int.Parse(match.Groups[1].Value); dailyItem.Seventh2 = int.Parse(match.Groups[2].Value); dailyItem.Seventh3 = int.Parse(match.Groups[3].Value); dailyItem.Seventh4 = int.Parse(match.Groups[4].Value); } else _logger.Error("Do not found seventh data on day:" + dailyItem.Date.ToShortDateString()); return found; }