private void DoUpdateNFLMatchupsESPN() { try { System.Net.WebClient client = new System.Net.WebClient(); string json = client.DownloadString("http://site.api.espn.com/apis/site/v2/sports/football/nfl/scoreboard"); API.ESPN.Feed model = Newtonsoft.Json.JsonConvert.DeserializeObject <API.ESPN.Feed>(json); var data = DBModel(); int[] nflYears = data.NFL_years.Where(x => x.sport_id == 100).Select(x => x.id).ToArray(); int[] nflWeeks = data.NFL_weeks.Where(x => nflYears.Contains(x.year_id.Value)).Select(x => x.week).ToArray(); var matchupsWithoutWinner = data.NFL_Matchups.Where(x => x.winner == null && nflWeeks.Contains(x.week) && model.week.number == x.year_week).ToList(); foreach (var matchup in matchupsWithoutWinner) { int week = matchup.year_week ?? matchup.week; int yearId = data.NFL_weeks.First(x => x.week == matchup.week).year_id ?? 1; int year = data.NFL_years.First(x => x.id == yearId).year; API.ESPN.Event apiMatchup = model.events.FirstOrDefault(m => CheckForWeirdName(m.competitions.First().competitors.First(c => c.homeAway == "home").team.shortDisplayName) == matchup.home && CheckForWeirdName(m.competitions.First().competitors.First(c => c.homeAway == "away").team.shortDisplayName) == matchup.away); if (apiMatchup != null) { UpdateMatchup(matchup, apiMatchup); } } if (_changesMade) { data.SubmitChanges(); } checkIfWeeksNeedToBeClosed(); } catch (Exception ex) { try { //if (IsAppInTestMode()) LibCommon.SendEmail("*****@*****.**", "Exception in DoUpdateNFLMatchupsESPN()", ex.ToString(), "EricWrightSite"); } catch { } } }
private void UpdateMatchup(db.NFL_Matchup matchup, API.ESPN.Event node) { matchup.status = node.status.type.state; matchup.eid = Convert.ToInt64(node.id); matchup.scheduled = node.date.ToLocalTime().ToShortDateString(); List <string> channels = new List <string>(); node.competitions.ToList().ForEach(c => c.broadcasts.ToList().ForEach(b => b.names.ToList().ForEach(n => channels.Add(n)))); matchup.channel = string.Join(", ", channels.Distinct().ToArray()); List <string> venues = new List <string>(); node.competitions.ToList().ForEach(c => venues.Add(c.venue.fullName)); matchup.stadium = string.Join(", ", venues.Distinct().ToArray()); //int statusInt; bool live = node.status.type.state != "pre" && node.status.type.state != "post"; matchup.live_update = live ? matchup.live_update = $"Clock: { node.status.displayClock }, Quarter: { node.status.period }" : null; if (live) { _changesMade = true; } if (matchup.status != null) { int?oldHome = matchup.home_score; int?oldAway = matchup.away_score; int homeScore; string homeScoreString = node.competitions.First().competitors.First(c => c.homeAway == "home").score; if (int.TryParse(homeScoreString, out homeScore)) { matchup.home_score = homeScore; } int awayScore; string awayScoreString = node.competitions.First().competitors.First(c => c.homeAway == "away").score; if (int.TryParse(awayScoreString, out awayScore)) { matchup.away_score = awayScore; } if (matchup.home_score.HasValue && matchup.away_score.HasValue && (oldAway != matchup.away_score.Value || oldHome != matchup.home_score.Value)) { _changesMade = true; } } if (matchup.status != null && node.status.type.shortDetail.ToLower().StartsWith("final")) { string oldWinner = matchup.winner; matchup.status = node.status.type.shortDetail; if (matchup.home_score > matchup.away_score) { matchup.winner = matchup.home; } else if (matchup.away_score > matchup.home_score) { matchup.winner = matchup.away; } else { matchup.winner = "TIE"; } if (!string.IsNullOrEmpty(matchup.winner) && oldWinner != matchup.winner) { _changesMade = true; } } }