static void Main(string[] args)
        {
            XmlConfigurator.Configure();

            //string LogName = GetType().Assembly.GetName().Name + ".log";
            //log4net.GlobalContext.Properties["LogName"] = LogName;

            UserTeamRepository              userTeamRepository              = new UserTeamRepository();
            UserTeamSeasonRepository        userTeamSeasonRepository        = new UserTeamSeasonRepository();
            UserTeamClassicLeagueRepository userTeamClassicLeagueRepository = new UserTeamClassicLeagueRepository();

            UserTeams                 userTeamInsert                  = new UserTeams();
            UserTeamSeasons           userTeamSeasonsInsert           = new UserTeamSeasons();
            UserTeamGameweekHistories userTeamGameweekHistoriesInsert = new UserTeamGameweekHistories();
            UserTeamChips             userTeamChipsInsert             = new UserTeamChips();
            UserTeamClassicLeagues    userTeamClassicLeaguesInsert    = new UserTeamClassicLeagues();
            UserTeamH2hLeagues        userTeamH2hLeaguesInsert        = new UserTeamH2hLeagues();

            SqlConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["FantasyPremierLeagueUserTeam202021"].ConnectionString);

            try
            {
                Logger.Out("Starting...");
                Logger.Out("");

                int userTeamRetries = 0;

                int  startingUserTeamId = 1;
                bool test = true;

                if (args.Length == 0)
                {
                    System.Console.WriteLine("No arguments passed into program");
                }
                else
                {
                    test = int.TryParse(args[0], out startingUserTeamId);
                    if (test == false)
                    {
                        System.Console.WriteLine("Arguments passed into program were not integer");
                        return;
                    }
                }

                Logger.Out("Starting UserTeams data load");
                Logger.Out("");

                FantasyPremierLeagueAPI_Bootstrap.GetPlayerBootstrapDataJson();

                string userTeamUrl = ConfigSettings.ReadSetting("userTeamURL");
                //string userTeamLeaguesUrl = ConfigSettings.ReadSetting("userTeamLeaguesURL");

                using (db)
                {
                    db.Open();

                    SetLatestGameweek("UserTeamGameweekHistory");
                    SetActualGameweek("UserTeamGameweekPick");

                    //List<string> userTeamSeasonNames;

                    Logger.Out("Starting GetAllUserTeamIds call");
                    List <int> existingUserTeamIds = userTeamRepository.GetAllUserTeamIds(startingUserTeamId, db);
                    Logger.Out("Starting GetAllUserTeamIdsWithLeagues call");
                    List <int> userTeamIdsWithLeagues = userTeamClassicLeagueRepository.GetAllUserTeamIdsWithLeagues(startingUserTeamId, db);
                    Logger.Out("Starting GetAllUserTeamIdsWithSeasons call");
                    List <int> userTeamIdsWithSeasons = userTeamSeasonRepository.GetAllUserTeamIdsWithSeasons(startingUserTeamId, db);

                    int userTeamsToProcess = Globals.bootstrapUserTeamCount - startingUserTeamId;

                    if (userTeamsToProcess < 0)
                    {
                        userTeamsToProcess = 0;
                    }

                    List <int> userTeamIds = Enumerable.Range(startingUserTeamId, userTeamsToProcess).ToList();

                    //List<int> toDoUserTeamIds = userTeamIds.Except(userTeamIdsWithLeagues).ToList();
                    List <int> toDoUserTeamIds = userTeamIds.Except(existingUserTeamIds).ToList();

                    Logger.Out("");
                    Logger.Out("Starting UserTeamId" + startingUserTeamId.ToString());
                    Logger.Out("");

                    Globals.apiCalls                = 0;
                    Globals.apiUserTeamCalls        = 0;
                    Globals.apiUserTeamHistoryCalls = 0;
                    Globals.userTeamInsertCount     = 0;

                    FantasyPremierLeagueAPIClient.GetUserTeamDataJson(startingUserTeamId, toDoUserTeamIds, existingUserTeamIds, userTeamIdsWithSeasons, userTeamUrl, userTeamInsert, userTeamGameweekHistoriesInsert, userTeamChipsInsert, userTeamSeasonsInsert, userTeamClassicLeaguesInsert, userTeamH2hLeaguesInsert, userTeamRetries, db);
                    db.Close();
                }

                Logger.Out("UserTeams data load complete");
                Logger.Out("");

                Logger.Out("Finished!!!");

                //// Wait for user input - keep the program running
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Logger.Error("Program error: " + ex.Message);
                throw ex;
            }
        }
Esempio n. 2
0
        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);
            }
        }