/// <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 + ")"); } }
public AlternateNames(ErrorWriter ewriter) { errorwriter = ewriter; }
public Stats(ErrorWriter ewriter, double inputPointModifier) { errorwriter = ewriter; int index = 0; int tempindex = 0; int tableEnd = 0; pointModifier = inputPointModifier; // 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 int tableStart = source.IndexOf("<table class='highlight'>"); index = tableStart; // Locate end of team table tableEnd = source.IndexOf("</table>", index); // Get # of columns between team and cost index = source.IndexOf("Team", index) + 1; int costRows = 0; do { index = source.IndexOf("<th", index); index = source.IndexOf("<a", index); tempindex = source.IndexOf(">", index) + 1; costRows++; } while (source.Substring(tempindex, 1) != "$"); // 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 to cost column for (int i = 0; i < costRows - 1; 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, pointModifier)); } 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); // Locate # of columns from team to cost stat index = source.IndexOf("Team", index); costRows = 0; do { index = source.IndexOf("<th", index) + 3; costRows++; } while (index < source.IndexOf("Cost", tableStart) && index != -1 + 3); costRows--; // 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 columns to reach cost column for (int i = 0; i < costRows - 1; 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, pointModifier)); } 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"); }