Example #1
0
        /// <summary>
        /// Fetch a page from the internet
        /// </summary>
        /// <param name="page">page URL</param>
        /// <param name="ewriter">Log writer</param>
        public WebFetch(string page, ErrorWriter ewriter)
        {
            errorwriter = ewriter;
            errorwriter.Write("Getting page: " + page);

            try
            {
                pageContent = string.Empty;

                // used to build entire input
                StringBuilder sb = new StringBuilder();

                // used on each read operation
                byte[] buf = new byte[8192];

                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)
                    WebRequest.Create(page);

                // execute the request
                HttpWebResponse response = (HttpWebResponse)
                    request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                    }
                }
                while (count > 0); // any more data to read?

                pageContent = sb.ToString();
                Cleanup();
            }
            catch (System.Net.WebException e)
            {
                errorwriter.Write("Error fetching liquipedia page ( " + e.Message + ")");
            }
        }
Example #2
0
        public Stats(ErrorWriter ewriter)
        {
            errorwriter = ewriter;

            int index = 0;
            int tempindex = 0;
            int tableEnd = 0;

            // Initialize AlternateNames
            altSearcher = new AlternateNames(errorwriter);

            // Read Do Not Draft list
            try
            {
                StreamReader reader = new StreamReader(".//DoNotDraft.txt");
                do
                {
                    string temp = reader.ReadLine();
                    doNotDraftList.Add(temp.ToUpper());
                } while (!reader.EndOfStream);
            }
            catch (FileNotFoundException e)
            {
                errorwriter.Write("Could not find DoNotDraft.txt ( " + e.Message + ")");
            }
            catch (Exception e)
            {
                errorwriter.Write("Error making the do not draft list ( " + e.Message + ")");
            }

            // Read source for player and team lists
            WebFetch fetch = new WebFetch(FPLSTATS, errorwriter);
            string source = fetch.pageContent;

            // Locate start of team table
            index = source.IndexOf("<table class='highlight'>");

            // Locate end of team table
            tableEnd = source.IndexOf("</table>", index);

            // Get info for all teams
            index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
            do
            {
                try
                {
                    // Get team name
                    tempindex = source.IndexOf(">", index) + 1;
                    index = source.IndexOf("<", tempindex);
                    string name = source.Substring(tempindex, index - tempindex);

                    // Skip 15 columns
                    for (int i = 0; i < 15; i++)
                    {
                        index = source.IndexOf("<td", index) + 3;
                    }

                    // Read team cost
                    tempindex = source.IndexOf("<td", index) + 3;
                    tempindex = source.IndexOf(">", tempindex) + 1;
                    index = source.IndexOf("<", tempindex);
                    int cost = Convert.ToInt32(source.Substring(tempindex, index - tempindex));

                    teamList.Add(name.ToUpper(), new Team(name, cost, ARRAYSIZE, TOTALGAMES));
                }
                catch (Exception e)
                {
                    errorwriter.Write("Error reading team list (" + e.Message + ")");
                }
                finally
                {
                    index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
                }
            } while (index < tableEnd && index != -1);

            // Output log entry
            errorwriter.Write("Info for " + teamList.Count + " teams read");

            // Locate start of player table
            index = source.IndexOf("<table class='highlight'>", tableEnd);

            // Locate end of player table
            tableEnd = source.IndexOf("</table>", index);

            // Get info for all players
            index = source.IndexOf("<img src='/tlpd/images/", index);
            do
            {
                try
                {
                    // Get race
                    index += "<img src='/tlpd/images/".Length;
                    string race = source.Substring(index, 1).ToUpper();

                    // Get name
                    index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
                    tempindex = source.IndexOf(">", index) + 1;
                    index = source.IndexOf("<", tempindex);
                    string name = source.Substring(tempindex, index - tempindex);

                    // Get team
                    index = source.IndexOf("http://www.teamliquid.net/tlpd/sc2-korean", index);
                    tempindex = source.IndexOf(">", index) + 1;
                    index = source.IndexOf("<", tempindex);
                    string team = FullTeamName(source.Substring(tempindex, index - tempindex));

                    // Skip 12 columns
                    for (int i = 0; i < 12; i++)
                    {
                        index = source.IndexOf("<td", index) + 3;
                    }

                    // Read player cost
                    tempindex = source.IndexOf("<td", index) + 4;
                    tempindex = source.IndexOf(">", tempindex) + 1;
                    index = source.IndexOf("<", tempindex);
                    int cost = Convert.ToInt32(source.Substring(tempindex, index - tempindex));

                    playerList.Add(name.ToUpper(), new Player(name, cost, race, team, ARRAYSIZE, TOTALGAMES));
                }
                catch (Exception e)
                {
                    errorwriter.Write("Error reading player list (" + e.Message + ")");
                }
                finally
                {
                    index = source.IndexOf("<img src='/tlpd/images/", index);
                }
            } while (index < tableEnd && index != -1);

            // Output log entry
            errorwriter.Write("Info for " + playerList.Count + " players read");
        }