public static void WriteUserTeamPicksToDB(int pageId, UserTeamPicks userTeamPicksInsert, SqlConnection db) { try { UserTeamPickRepository userTeamPickRepository = new UserTeamPickRepository(); //Write UserTeamPick to the db Globals.UserTeamPickRowsInserted = userTeamPickRepository.InsertUserTeamPick(userTeamPicksInsert, db); Logger.Out("UserTeamPick bulk insert complete (PageId: " + Convert.ToString(pageId) + ")"); userTeamPicksInsert.Clear(); } catch (Exception ex) { Logger.Error("Program WriteUserTeamPickToDB error: " + ex.Message); //WriteToDB(userTeamRowsInserted, userTeamSeasonRowsInserted, userTeamInsert, userTeamSeasonsInsert, db); } }
public static bool GetLeagueDataJson(int leagueId, List <int> userTeamIds, string userTeamLeaguesUrl, string userTeamUrl, UserTeams userTeamsUpdateInsert, UserTeamCupMatches userTeamCupInsert, UserTeamClassicLeagues userTeamClassicLeaguesInsert, UserTeamH2hLeagues userTeamH2hLeaguesInsert, UserTeamGameweekHistories userTeamGameweekHistoriesInsert, UserTeamPicks userTeamPicksInsert, UserTeamPickAutomaticSubs userTeamPickAutomaticSubsInsert, UserTeamChips userTeamChipsInsert, UserTeamTransferHistoryData userTeamTransferHistoriesInsert, UserTeamSeasons userTeamSeasonsInsert, SqlConnection db) { UserTeamRepository userTeamRepository = new UserTeamRepository(); UserTeamPickRepository userTeamPickRepository = new UserTeamPickRepository(); UserTeamGameweekHistoryRepository userTeamGameweekHistoryRepository = new UserTeamGameweekHistoryRepository(); UserTeamTransferHistoryRepository userTeamTransferHistoryRepository = new UserTeamTransferHistoryRepository(); UserTeamClassicLeagueRepository userTeamClassicLeagueRepository = new UserTeamClassicLeagueRepository(); UserTeamSeasonRepository userTeamSeasonRepository = new UserTeamSeasonRepository(); try { int userTeamId = 0; bool has_next = false; List <int> leagueUserTeamIds = new List <int>(); string url = string.Format(userTeamLeaguesUrl, leagueId, Globals.PageId); JsonSerializer serializer = new JsonSerializer() { Formatting = Formatting.None }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient client = new HttpClient()) using (Stream s = client.GetStreamAsync(url).Result) using (StreamReader sr = new StreamReader(s)) using (JsonReader reader = new JsonTextReader(sr)) { Globals.ApiCalls += 1; Globals.ApiPageCalls += 1; // read the json from a stream // json size doesn't matter because only a small piece is read at a time from the HTTP request var leagueStandingsData = serializer.Deserialize <LeagueStandingsData>(reader); if (leagueStandingsData != null) { League league = leagueStandingsData.league; Standings standings = leagueStandingsData.standings; string leagueName = league.name; has_next = standings.has_next; Logger.Out(leagueName + " (" + Convert.ToString(leagueId) + "): Page " + Convert.ToString(Globals.PageId)); Logger.Out(""); if (db.State == ConnectionState.Closed) { db.ConnectionString = ConfigurationManager.ConnectionStrings["FantasyPremierLeagueUserTeam"].ConnectionString; db.Open(); Logger.Error("GetLeagueDataJson Info (LeagueId: " + leagueId.ToString() + ", PageId:" + Globals.PageId.ToString() + "): Reopening closed db connection"); } List <int> pageUserTeamIds = new List <int>(); foreach (TeamLeaguePosition teamLeaguePosition in standings.results) { userTeamId = teamLeaguePosition.entry; //Add each UserTeamId to a list for checking if GameweekHistory, TransferHistory, and Picks have already been processed for that GW by UserTeamId pageUserTeamIds.Add(userTeamId); } using (DataTable dtUserMaxGWForUserTeamForGameweekHistory = userTeamGameweekHistoryRepository.GetMaxGameweekIdFromUserTeamGameweekHistoryForUserTeamIds(pageUserTeamIds, db)) using (DataTable dtUserMaxGWForUserTeamForPicks = userTeamPickRepository.GetMaxGameweekIdFromUserTeamPickForUserTeamIds(pageUserTeamIds, db)) using (DataTable dtUserMaxGWForUserTeamForTransferHistory = userTeamTransferHistoryRepository.GetMaxGameweekIdFromUserTeamTransferHistoryForUserTeamIds(pageUserTeamIds, db)) using (DataTable dtClassicLeagueCountForUserTeam = userTeamClassicLeagueRepository.GetClassicLeagueCountFromUserTeamClassicLeagueForUserTeamIds(pageUserTeamIds, db)) using (DataTable dtSeasonCountForUserTeam = userTeamSeasonRepository.GetSeasonCountFromUserTeamSeasonForUserTeamIds(pageUserTeamIds, db)) using (DataTable dtUserTeam = userTeamRepository.GetUserTeamForUserTeamIds(pageUserTeamIds, db)) { foreach (TeamLeaguePosition teamLeaguePosition in standings.results) { userTeamId = teamLeaguePosition.entry; Globals.MaxGWFromGameweekHistoryForUserTeamId = GetColumnNameValue(userTeamId, dtUserMaxGWForUserTeamForGameweekHistory, "gameweekid"); Globals.MaxGWFromPicksForUserTeamId = GetColumnNameValue(userTeamId, dtUserMaxGWForUserTeamForPicks, "gameweekid"); Globals.MaxGWFromTransferHistoryForUserTeamId = GetColumnNameValue(userTeamId, dtUserMaxGWForUserTeamForTransferHistory, "gameweekid"); Globals.LeagueCountFromUserTeamClassicLeagueForUserTeamId = GetColumnNameValue(userTeamId, dtClassicLeagueCountForUserTeam, "leagueCount"); Globals.SeasonCountFromUserTeamSeasonForUserTeamId = GetColumnNameValue(userTeamId, dtSeasonCountForUserTeam, "seasonCount"); Globals.ExistingUserTeamId = GetColumnNameValue(userTeamId, dtUserTeam, "userteamid"); FantasyPremierLeagueAPIClient.GetUserTeamDataJson(userTeamId, userTeamIds, userTeamUrl, userTeamsUpdateInsert, userTeamCupInsert, userTeamClassicLeaguesInsert, userTeamH2hLeaguesInsert, userTeamGameweekHistoriesInsert, userTeamPicksInsert, userTeamPickAutomaticSubsInsert, userTeamChipsInsert, userTeamTransferHistoriesInsert, userTeamSeasonsInsert, db); } } pageUserTeamIds.Clear(); } return(has_next); } } catch (Exception ex) { Logger.Error("GetLeagueDataJson data exception (LeagueId: " + leagueId.ToString() + ", PageId:" + Globals.PageId.ToString() + "): " + ex.Message); bool has_next = true; if (Globals.LeagueRetries < 10) { has_next = GetLeagueDataJson(leagueId, userTeamIds, userTeamLeaguesUrl, userTeamUrl, userTeamsUpdateInsert, userTeamCupInsert, userTeamClassicLeaguesInsert, userTeamH2hLeaguesInsert, userTeamGameweekHistoriesInsert, userTeamPicksInsert, userTeamPickAutomaticSubsInsert, userTeamChipsInsert, userTeamTransferHistoriesInsert, userTeamSeasonsInsert, db); Globals.LeagueRetries += 1; } else { Logger.Error("GetLeagueDataJson data exception (LeagueId: " + leagueId.ToString() + "): League/Page doesn't exist skipping to next!!!"); //Program.WriteToDB(pageId, userTeamGameweekHistoriesInsert, userTeamPicksInsert, userTeamPickAutomaticSubsInsert, userTeamChipsInsert, userTeamTransferHistoriesInsert, userTeamSeasonsInsert, userTeamClassicLeaguesInsert, userTeamH2hLeaguesInsert, db); Globals.LeagueRetries = 0; //throw new Exception("GetLeagueDataJson data exception (LeagueId: " + leagueId.ToString() + ", PageId:" + Globals.PageId.ToString() + "): " + ex.Message); } return(has_next); } }
//public static void GetUserTeamPickDataJson(int userTeamId, int gameweekId, UserTeamPicks userTeamPicksInsert, UserTeamPickAutomaticSubs userTeamPickAutomaticSubsInsert, string userTeamPicksUrl, SqlConnection db) //{ // //Logger.Out("GetUserTeamPickDataJson: Gameweek " + Convert.ToString(gameweekId) + " - starting"); // var urlUserTeamPicks = ""; // try // { // UserTeamPickRepository userTeamPickRepository = new UserTeamPickRepository(); // urlUserTeamPicks = string.Format(userTeamPicksUrl, userTeamId, gameweekId); // HttpClient clientUserTeamPicks = new HttpClient(); // JsonSerializer serializerUserTeamPicks = new JsonSerializer(); // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // //Logger.Out("Before using statements - url: " + urlUserTeamPicks); // using (Stream sUserTeamPicks = clientUserTeamPicks.GetStreamAsync(urlUserTeamPicks).Result) // using (StreamReader srUserTeamPicks = new StreamReader(sUserTeamPicks)) // using (JsonReader readerUserTeamPicks = new JsonTextReader(srUserTeamPicks)) // { // Globals.ApiCalls += 1; // Globals.ApiUserTeamPickCalls += 1; // // read the json from a stream // // json size doesn't matter because only a small piece is read at a time from the HTTP request // var userTeamPickData = serializerUserTeamPicks.Deserialize<UserTeamPickData>(readerUserTeamPicks); // if (userTeamPickData != null) // { // GetUserTeamPickJson(userTeamId, gameweekId, userTeamPicksInsert, userTeamPickData, db); // GetUserTeamPickAutomaticSubJson(userTeamId, gameweekId, userTeamPickAutomaticSubsInsert, userTeamPickData, db); // } // } // //Logger.Out("GetUserTeamPickDataJson: Gameweek " + Convert.ToString(gameweekId) + " - completed"); // } // catch (Exception ex) // { // Logger.Error("GetUserTeamPickDataJson data exception (UserTeamId: " + userTeamId.ToString() + " GameweekId: " + gameweekId.ToString() + "): " + ex.Message); // //throw new Exception("GetUserTeamPickDataJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message); // GetUserTeamPickDataJson(userTeamId, gameweekId, userTeamPicksInsert, userTeamPickAutomaticSubsInsert, userTeamPicksUrl, db); // } //} public static void GetUserTeamPickJson(int userTeamId, int gameweekId, string urlUserTeamPicks, UserTeamPicks userTeamPicksInsert, UserTeamPickAutomaticSubs userTeamPickAutomaticSubsInsert, SqlConnection db) { try { //Process UserTeamPick and UserTeamPickAutomaticSub if (gameweekId >= Globals.MaxGWFromPicksForUserTeamId) { UserTeamPickRepository userTeamPickRepository = new UserTeamPickRepository(); urlUserTeamPicks = string.Format(urlUserTeamPicks, userTeamId, gameweekId); JsonSerializer serializerUserTeamPicks = new JsonSerializer() { Formatting = Formatting.None }; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; using (HttpClient clientUserTeamPicks = new HttpClient()) using (Stream sUserTeamPicks = clientUserTeamPicks.GetStreamAsync(urlUserTeamPicks).Result) using (StreamReader srUserTeamPicks = new StreamReader(sUserTeamPicks)) using (JsonReader readerUserTeamPicks = new JsonTextReader(srUserTeamPicks)) { Globals.ApiCalls += 1; Globals.ApiUserTeamPickCalls += 1; // read the json from a stream // json size doesn't matter because only a small piece is read at a time from the HTTP request var userTeamPickData = serializerUserTeamPicks.Deserialize <UserTeamPickData>(readerUserTeamPicks); if (userTeamPickData != null) { //Load UserTeamPick data //int userTeamPickIds_userteamid = 0; //int userTeamPickIds_gameweekid = 0; //UserTeamPickId userTeamPickId = new UserTeamPickId(); //userTeamPickId.userteamid = userTeamId; //userTeamPickId.gameweekid = gameweekId; ///List<UserTeamPickId> userTeamPickIds = userTeamPickRepository.GetAllUserTeamPickIdsForUserTeamIdAndGameweekId(userTeamId, gameweekId, db); //if (userTeamPickIds.Count > 0) //{ // userTeamPickIds_userteamid = userTeamPickIds[0].userteamid; // userTeamPickIds_gameweekid = userTeamPickIds[0].gameweekid; //} //UserTeamPicks userTeamPicksUpdate = new UserTeamPicks(); foreach (UserTeamPick userTeamPick in userTeamPickData.picks) { UserTeamPickId userTeamPickId = new UserTeamPickId ( userTeamId, gameweekId, userTeamPick.position ); //needed if want to assign value from parent to add into db table userTeamPick.userteamid = userTeamId; userTeamPick.gameweekid = gameweekId; //if (!userTeamPickIds.Contains(userTeamPickId) && !userTeamPicksInsert.Contains(userTeamPick)) if (!userTeamPicksInsert.Contains(userTeamPick)) { userTeamPicksInsert.Add(userTeamPick); } //else //{ // userTeamPickRepository.UpdateUserTeamPick(userTeamPick); //} } //Logger.Out("GetUserTeamPickJson: Gameweek " + Convert.ToString(gameweekId) + " - completed"); } GetUserTeamPickAutomaticSubJson(userTeamId, gameweekId, userTeamPickAutomaticSubsInsert, userTeamPickData, db); } } //Logger.Out("GetUserTeamPickDataJson: Gameweek " + Convert.ToString(gameweekId) + " - completed"); } catch (Exception ex) { Logger.Error("GetUserTeamPickJson data exception (UserTeamId:" + userTeamId.ToString() + "/GameweekId:" + gameweekId.ToString() + "): " + ex.Message); Logger.Error("GetUserTeamPickJson data exception (UserTeamId:" + userTeamId.ToString() + "/GameweekId:" + gameweekId.ToString() + "): skipping userteam/gameweek"); //throw new Exception("GetUserTeamPickJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message); //GetUserTeamPickJson(userTeamId, gameweekId, urlUserTeamPicks, maxGWFromPicksForUserTeamId, userTeamPicksInsert, userTeamPickAutomaticSubsInsert, db); //if (gameweekId + 1 < Globals.ActualGameweek) //{ // gameweekId++; // GetUserTeamPickJson(userTeamId, gameweekId, urlUserTeamPicks, maxGWFromPicksForUserTeamId, userTeamPicksInsert, userTeamPickAutomaticSubsInsert, db); //} } }