public JsonResult SaveTeamStandingGameUrl(int teamId, int clubId, string url, string teamName) { try { var service = new ScrapperService(); var isTeamExist = teamRepo.GetById(teamId) != null; var result = service.StandingScraper(url); if (!isTeamExist && result.Any(t => t.Team != teamName)) { ProcessHelper.ClosePhantomJSProcess(); return(Json(new { Success = false, Error = "Such team not exist." })); } int standingGameId = teamRepo.SaveTeamStandingUrl(teamId, clubId, url, teamName); bool isSuccess = standingGameId > 0; if (isSuccess) { gamesRepo.UpdateTeamStandingsFromModel(result, standingGameId, url); service.Quit(); } ProcessHelper.ClosePhantomJSProcess(); return(Json(new { Success = isSuccess, Data = result })); } catch (Exception e) { return(Json(new { Sucess = false, Error = e.Message })); } }
public void UpdateTeamStandingsFromScrapper(IList <string> gamesUrl) { var standings = new List <StandingDTO>(); var service = new ScrapperService(); foreach (var standingUrl in gamesUrl) { var items = service.StandingScraper(standingUrl); standings.AddRange(items); } foreach (var standing in standings) { //var strToIntRank = Convert.ToInt32(standing.Rank.Replace(".", string.Empty)); int outVal; int.TryParse(standing.Rank.Replace(".", string.Empty), out outVal); var dbStanding = db.TeamStandings.FirstOrDefault( x => x.Team == standing.Team); //if we find the team update it if (dbStanding != null) { dbStanding.Rank = outVal; dbStanding.Games = standing.Games.ToByte(); dbStanding.Wins = standing.Win.ToByte(); dbStanding.Lost = standing.Lost.ToByte(); dbStanding.Pts = standing.Pts.ToByte(); dbStanding.Papf = standing.PaPf; dbStanding.Home = standing.Home; dbStanding.Road = standing.Road; dbStanding.ScoreHome = standing.ScoreHome; dbStanding.ScoreRoad = standing.ScoreRoad; dbStanding.Last5 = standing.Last5; dbStanding.PlusMinusField = standing.PlusMinusField; } //else create new teamStanding else { var standingGameId = db.TeamStandingGames.FirstOrDefault(x => x.GamesUrl == standing.Url)?.Id; var newTeamStnading = new TeamStanding { Team = standing.Team, Rank = outVal, Games = standing.Games.ToByte(), Wins = standing.Win.ToByte(), Lost = standing.Lost.ToByte(), Pts = standing.Pts.ToByte(), Papf = standing.PaPf, Home = standing.Home, Road = standing.Road, ScoreHome = standing.ScoreHome, ScoreRoad = standing.ScoreRoad, Last5 = standing.Last5, TeamStandingGamesId = standingGameId, PlusMinusField = standing.PlusMinusField, }; db.TeamStandings.Add(newTeamStnading); } } try { Save(); service.Quit(); } catch (DbEntityValidationException e) { } catch (Exception) { throw; } }