コード例 #1
0
        public FanGraphsHitter CreateHitterForTesting(string playerName)
        {
            FanGraphsHitter newFanGraphsHitter = new FanGraphsHitter
            {
                FanGraphsName = playerName,
                FanGraphsTeam = "Chicago Cubs",
                GamesPlayed   = 123,
                // PlateAppearances            = "123",
                // HomeRuns            = "123",
                // Runs             = "123",
                // RunsBattedIn           = "123",
                // StolenBases            = "123",
                // WalkPercentage    = "23%",
                // StrikeoutPercentage     = "23%",
                // Iso           = ".321",
                // Babip         = ".321",
                // BattingAverage           = ".321",
                // OnBasePercentage           = ".321",
                // SluggingPercentage           = ".321",
                // wOba          = ".321",
                // wRcPlus      = "789",
                // BaseRunningRunsAboveReplacement           = "789",
                // Offense           = "789",
                // Defense           = "789",
                // WinsAboveReplacement           = "6",
            };

            return(newFanGraphsHitter);
        }
コード例 #2
0
        // STATUS: in progress
        // TO-DO: the class/model type (FanGraphsHitter) is defined within the method. This needs to be passed as an argument
        /// <summary> Add a new row / record to a sheet in an existing xlsx </summary>
        /// <param name="fileName"> The name of the file you are targeting </param>
        /// <param name="sheetName"> The name of the tab / sheet you are targeting </param>
        /// <example> _eM.AddRecordToSheet("BaseballScraper", "FgHitters"); </example>
        public void AddRecordToSheet(string fileName, string sheetName)
        {
            RegisterProviderToStart();
            SetThreadCurrentCulture();

            FanGraphsHitter newFanGraphsHitter = CreateHitterForTesting("Kenny Lofton");

            Mapper mapper = new Mapper();

            string targetWorkbook = ManageWorkbookNames(fileName);

            // ARGUMENTS --> (1) string path  (2) IEnumerable<T> Objects  (3) string sheetName (4) bool overwrite
            // * (4A) true = create new workbook
            // * (4B) false = workbook already exists so update the existing workbook instead of creating a new one
            mapper.Save(targetWorkbook, new[] { newFanGraphsHitter }, sheetName, overwrite: false);
        }
コード例 #3
0
        // THIS WORKS
        public void HitterCrawler()
        {
            _helpers.StartMethod();

            List <string> listOfUrls = GetUrlsOfPagesToScrape().ToList();
            int           numOfUrls  = listOfUrls.Count;

            _helpers.Intro(numOfUrls, "url count");

            string tableBodyXpath = GetXPathOfTableBodyToScrape();

            HtmlWeb htmlWeb = new HtmlWeb();

            int loopCount = 1;

            foreach (string urlForThisPageInLoop in listOfUrls)
            {
                _helpers.Intro(urlForThisPageInLoop, "single page url");
                _helpers.Intro(loopCount, "this is loop number");
                loopCount++;

                HtmlDocument htmlWeb1 = htmlWeb.Load(urlForThisPageInLoop);

                // COUNT = 1
                HtmlNodeCollection tableBody = htmlWeb1.DocumentNode.SelectNodes(tableBodyXpath);

                foreach (HtmlNode tableBodyNode in tableBody)
                {
                    // * This can be gotten from Chrome
                    // * Right-click 'Inspect', view the html for the table
                    // * Right-click on any item(in this case a row) and select Copy > tableBodyXpath
                    const string preForRows  = "//*[@id='LeaderBoard1_dg1_ctl00__";
                    const string postForRows = "']";

                    // 52 for first page; 13 for last page
                    int tbNodeChildCount = tableBodyNode.ChildNodes.Count;
                    int adjustedCount    = tbNodeChildCount - 2;

                    for (int i = 0; i <= adjustedCount - 1; i++)
                    {
                        // TR FOR EACH PLAYER ---> //*[@id='LeaderBoard1_dg1_ctl00__11']
                        string trForEachPlayer = $"{preForRows}{i}{postForRows}";

                        // Count = 1
                        HtmlNodeCollection nodeRowEachPlayer = tableBodyNode.SelectNodes(trForEachPlayer);

                        foreach (HtmlNode playerItem in nodeRowEachPlayer)
                        {
                            // Count = 24
                            int playerItemChildrenCount = playerItem.ChildNodes.Count;

                            //  e.g. --->   12Manny Machado- - -101440245066710.9 %12.5 %.252.310.311.384.563.393152-0.526.3-3.23.9
                            string       preForData  = $"{trForEachPlayer}/td[";
                            const string postForData = "]";

                            List <string> playerItems = new List <string> ();

                            const int numberOfColumns = 22;
                            int       keyCount        = 1;

                            for (int j = 1; j <= numberOfColumns; j++)
                            {
                                // TD FOR EACH PLAYER ---> //*[@id='LeaderBoard1_dg1_ctl00__11']/td[1]
                                string tdForEachPlayer = $"{preForData}{j}{postForData}";

                                // Count = 1
                                HtmlNodeCollection playersNodeCollection = playerItem.SelectNodes(tdForEachPlayer);

                                // go this way if looking for player name or player team
                                if (j == 2 || j == 3)
                                {
                                    try
                                    {
                                        const string postPost = "/a";

                                        // NAME AND TEAM X-PATHS ---> //*[@id='LeaderBoard1_dg1_ctl00__11']/td[2]/a
                                        string nameAndTeamTableBodyXpaths = $"{tdForEachPlayer}{postPost}";

                                        // Count = 1
                                        HtmlNodeCollection nameAndTeam = playerItem.SelectNodes(nameAndTeamTableBodyXpaths);

                                        foreach (HtmlNode actualNumber in nameAndTeam)
                                        {
                                            // e.g. '3.9', '26.3' etc. The players actual numbers for each stats
                                            string numToAddToList = actualNumber.InnerText;
                                            playerItems.Add(numToAddToList);
                                        }
                                    }

                                    catch (Exception ex)
                                    {
                                        Debug.WriteLine(ex);
                                        _helpers.Spotlight("NAME or TEAM is broken");
                                        const string cellIsBlank = "";
                                        playerItems.Add(cellIsBlank);
                                    }
                                }

                                else
                                {
                                    foreach (HtmlNode actualNumber in playersNodeCollection)
                                    {
                                        // e.g. '3.9', '26.3' etc. The players actual numbers for each stats
                                        string numToAddToList = actualNumber.InnerText;
                                        playerItems.Add(numToAddToList);
                                    }
                                }
                                keyCount++;
                            }

                            List <FanGraphsHitter> hitters = new List <FanGraphsHitter> ();

                            FanGraphsHitter newFGHitter = new FanGraphsHitter
                            {
                                FanGraphsName       = playerItems[1],
                                FanGraphsTeam       = playerItems[2],
                                GamesPlayed         = Convert.ToInt32(playerItems[3], CultureInfo.CurrentCulture),
                                PlateAppearances    = Convert.ToInt32(playerItems[4], CultureInfo.CurrentCulture),
                                HomeRuns            = Convert.ToInt32(playerItems[5], CultureInfo.CurrentCulture),
                                Runs                = Convert.ToInt32(playerItems[6], CultureInfo.CurrentCulture),
                                RunsBattedIn        = Convert.ToInt32(playerItems[7], CultureInfo.CurrentCulture),
                                StolenBases         = Convert.ToInt32(playerItems[8], CultureInfo.CurrentCulture),
                                WalkPercentage      = Convert.ToInt32(playerItems[9], CultureInfo.CurrentCulture),
                                StrikeoutPercentage = Convert.ToInt32(playerItems[10], CultureInfo.CurrentCulture),
                                Iso                = Convert.ToInt32(playerItems[11], CultureInfo.CurrentCulture),
                                Babip              = Convert.ToInt32(playerItems[12], CultureInfo.CurrentCulture),
                                BattingAverage     = Convert.ToInt32(playerItems[13], CultureInfo.CurrentCulture),
                                OnBasePercentage   = Convert.ToInt32(playerItems[14], CultureInfo.CurrentCulture),
                                SluggingPercentage = Convert.ToInt32(playerItems[15], CultureInfo.CurrentCulture),
                                wOba               = Convert.ToInt32(playerItems[16], CultureInfo.CurrentCulture),
                                wRcPlus            = Convert.ToInt32(playerItems[17], CultureInfo.CurrentCulture),
                                BaseRunningRunsAboveReplacement = Convert.ToInt32(playerItems[18], CultureInfo.CurrentCulture),
                                Offense = Convert.ToInt32(playerItems[19], CultureInfo.CurrentCulture),
                                Defense = Convert.ToInt32(playerItems[20], CultureInfo.CurrentCulture),
                                WinsAboveReplacement = Convert.ToInt32(playerItems[21], CultureInfo.CurrentCulture),
                            };

                            hitters.Add(newFGHitter);

                            foreach (FanGraphsHitter hitter in hitters)
                            {
                                Console.WriteLine(hitter.FanGraphsName);
                                Console.WriteLine(hitter.WinsAboveReplacement);
                            }
                        }
                    }
                }
            }
        }