public void Mine() { bool notFound = false; int dayId = StartMatchDayId; int oldDayId = 0; int matchId = StartMatchId; while (dayId <= EndMatchDayId) { if (oldDayId != dayId) { Console.Out.WriteLine(String.Format("Mining game events from matchday {0}", dayId)); oldDayId = dayId; } try { string data = GetUrlData(detailsUrl, dayId, matchId); Game game = GetGameFromDetailsData(data); bool valid = true; // check for parse errors if (game == null) { Console.Out.WriteLine(String.Format("Unable to match game id {0}", matchId)); valid = false; } else { // check to see if game exists using (var repository = new GameRepository()) { var existing = from g in repository.GetAll() where g.MatchDay == dayId && g.HomeTeamId == game.HomeTeamId && g.AwayTeamId == game.AwayTeamId && g.EventsRetrieved == RetrievalStatus.COMPLETED select g; if (existing.Count() > 0) valid = false; } } if (valid) { // add more identity information to the game game.LiveMatchId = matchId; game.MatchDay = dayId; game.EventsRetrieved = RetrievalStatus.IN_PROGRESS; using (var repository = new GameRepository()) { repository.Save(game); } Team home = null; Team away = null; using (var repository = new TeamRepository()) { home = repository.GetAll().Where(t => t.Id == game.HomeTeamId).First(); away = repository.GetAll().Where(t => t.Id == game.AwayTeamId).First(); } Console.Out.WriteLine(String.Format("Matched data from {0} vs {1}", home.Name, away.Name)); // get the events and save them to the db List<GameEvent> gameEvents = new List<GameEvent>(); if (BSTDateTime.FromBSTDateTime(game.Start.AddHours(2)) <= DateTime.UtcNow) { gameEvents = GetGameEvents(game); } if (gameEvents.Count > 0) { using (var repository = new GameEventRepository()) { repository.AddRange(gameEvents); } // update the game event retrieval status game.EventsRetrieved = RetrievalStatus.COMPLETED; } else { // no events found game.EventsRetrieved = RetrievalStatus.NONE; } using (var repository = new GameRepository()) { repository.Save(game); } Console.Out.WriteLine(String.Format("Mined {0} events from game", gameEvents.Count)); } else { Console.Out.WriteLine(String.Format("Unable to match game id {0}", matchId)); } // update loop variants notFound = false; matchId++; } catch { if (notFound) break; notFound = true; dayId++; } } }