public ArrayList GetGamePerformances( string GameId ) { string endpoint = "boxscoretraditionalv2"; Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("GameId", GameId ); parameters.Add("Season", "2016-17"); parameters.Add("SeasonType", "Regular%20Season"); parameters.Add("RangeType", "0"); parameters.Add("StartPeriod", "0"); parameters.Add("EndPeriod", "0"); parameters.Add("StartRange", "0"); parameters.Add("EndRange", "0"); string page = ScrapeNBAApi(endpoint, parameters); ArrayList perfs = new ArrayList(); JObject sb = JObject.Parse(page); JArray performances = (JArray)sb["resultSets"][0]["rowSet"]; Regex ExtractPlayerName = new Regex(@"^([\w\.\'-]+)\s+?([\w\.\'-]+(?:\s[\w.]+)?)(?:.*?)"); foreach (var perf in performances) { if(((string)perf[7]).Length > 0 ) { // DNP continue; } PlayerPerformance p = new PlayerPerformance(); p.NBAId = Int32.Parse( (string)perf[4] ); p.Assists = Int32.Parse((string)perf[21]); p.Blocks = Int32.Parse((string)perf[23]); p.DefensiveRebounds = Int32.Parse((string)perf[19]); // these come in a single string try { if (((string)perf[5]).Equals("Nene")) { p.FirstName = "Nene"; p.LastName = "Hilario"; } else { MatchCollection PlayerName = ExtractPlayerName.Matches((string)perf[5]); p.FirstName = PlayerName[0].Groups[1].Value; p.LastName = PlayerName[0].Groups[2].Value; if (p.FirstName.Length == 2) { p.FirstName = p.FirstName.Substring(0, 1); } } } catch (Exception e) { StatGrabberException ex = new StatGrabberException( "Having trouble with: " + (string)perf[5] ); throw ex; } // this comes as MM:SS string[] minuteSplit = ((string)perf[8]).Split(':'); if (int.Parse(minuteSplit[1]) > 29) { p.Minutes = int.Parse(minuteSplit[0]) + 1; } else { p.Minutes = int.Parse(minuteSplit[0]); } p.Fouls = Int32.Parse((string)perf[25]); p.FTAttempts = Int32.Parse((string)perf[16]); p.FTsMade = Int32.Parse((string)perf[15]); p.OffensiveRebounds = Int32.Parse((string)perf[18]); p.PlusMinus = Int32.Parse((string)perf[27]); p.ShotAttempts = Int32.Parse((string)perf[10]); p.ShotsMade = Int32.Parse((string)perf[9]); p.Steals = Int32.Parse((string)perf[22]); p.TeamName = (string)perf[2]; p.ThreeAttempts = Int32.Parse((string)perf[13]); p.ThreesMade = Int32.Parse((string)perf[12]); p.Turnovers = Int32.Parse((string)perf[24]); perfs.Add(p); } return perfs; }
public ArrayList GetGamePerformances( string url, ArrayList problems ) { Regex GetTeams = new Regex( @"6px;""></div>(.*)</th></tr><tr" ); Regex GetPlayerStatRows = new Regex( @"<td style=.text-align:left. nowrap>(.*)?</td></tr>" ); Regex SplitStatRows = new Regex( @"</td><td.*?>" ); // Regex ExtractPlayerName = new Regex( @"^(?:.+?>)?([\w\.\'-]+)\s+?([\w\.\'-]+(?:\s[\w.]+)?)(?:.*?)?$" ); //Regex ExtractPlayerName = new Regex( @"^(?:.+?>)?([\w\.\'-\(\)]+)\s+?([\w\.\'-\(\)]+(?:\s[\w.\(\)]+)?)(?:.*?)?$" ); // -atag- --first name-- ---last name------------------- Regex ExtractPlayerName = new Regex( @"^(?:.+?>)?(.+?)(\s)(.+?)<.*?$" ); // julie put in this simplified version 11/26/11; seems to work fine, not sure // why we had a more complex version before. definitely wasnt working for players // with a dash in first name // <td style="text-align:left;" nowrap>A Thomas II</td> // <td style="text-align:left;" nowrap><a href="http://espn.go.com/mens-college-basketball/player/_/id/51354/okaro-white">Okaro White</a>, F</td> // <td style="text-align:left" nowrap><a href="http://espn.go.com/mens-college-basketball/player/_/id/58169/javonte-green">Javonte Green</a>, F</td><td>19</td><td>6-11</td><td>0-2</td><td>3-3</td><td align="right">2</td><td align="right">4</td><td>6</td><td>0</td><td>2</td><td>0</td><td>1</td><td>3</td><td>15</td></tr><tr align="right" class=odd> Regex ExtractPlayerNameNoLink = new Regex( @"^([A-Z])\s(.+)" ); Regex ExtractShots = new Regex( @"([0-9]*)\-([0-9]*)" ); string Page = WebPageToString( url ); MatchCollection TeamMatches = GetTeams.Matches( Page ); if( TeamMatches.Count != 2 ) { StatGrabberException ex = new StatGrabberException( "Couldn't find the teams in game " + url + " ... maybe ESPN hasn't finalized them?" ); throw ex; } int HomeAfterThis = TeamMatches[1].Groups[1].Index; ArrayList perfs = new ArrayList(); MatchCollection StatLines = GetPlayerStatRows.Matches( Page ); foreach( Match i in StatLines ) { if( i.Groups.Count < 2 ) { StatGrabberException ex = new StatGrabberException( "Seems to be a problem in the statline structure in game " + url + " ... maybe ESPN has changed format?" ); throw ex; } string[] Cells = SplitStatRows.Split( i.Groups[1].Value ); PlayerPerformance p = new PlayerPerformance(); MatchCollection PlayerName = ExtractPlayerName.Matches( Cells[0] ); bool happy = true; if( PlayerName.Count >= 1 ) { p.FirstName = PlayerName[0].Groups[1].Value; p.LastName = PlayerName[0].Groups[3].Value; } else { // try the no-link version PlayerName = ExtractPlayerNameNoLink.Matches( Cells[0] ); if( PlayerName.Count >= 1 ) { p.FirstName = PlayerName[0].Groups[1].Value; p.LastName = PlayerName[0].Groups[2].Value; } else { p.FirstName = "Player's name doesn't match the pattern: " + Cells[0]; p.LastName = " in " + Cells[0]; problems.Add( p ); happy = false; } } if( happy ) { if( i.Index >= HomeAfterThis ) { p.TeamName = TeamMatches[1].Groups[1].Value; p.Opponent = TeamMatches[0].Groups[1].Value; } else { p.TeamName = TeamMatches[0].Groups[1].Value; p.Opponent = TeamMatches[1].Groups[1].Value; } try { // cell 1 has minutes p.Minutes = Convert.ToInt32( Cells[1] ); // cell 2 has made-attempted FG Match fgs = ExtractShots.Match( Cells[2] ); p.ShotsMade = Convert.ToInt32( fgs.Groups[1].Value ); p.ShotAttempts = Convert.ToInt32( fgs.Groups[2].Value ); // cell 3 has made-attempted on 3 pointers Match threes = ExtractShots.Match( Cells[3] ); p.ThreesMade = Convert.ToInt32( threes.Groups[1].Value ); p.ThreeAttempts = Convert.ToInt32( threes.Groups[2].Value ); // cell 4 has made-attempted FT Match fts = ExtractShots.Match( Cells[4] ); p.FTsMade = Convert.ToInt32( fts.Groups[1].Value ); p.FTAttempts = Convert.ToInt32( fts.Groups[2].Value ); // cell 5 has off rebs p.OffensiveRebounds = Convert.ToInt32( Cells[5] ); // cell 6 has rebs p.DefensiveRebounds = Convert.ToInt32( Cells[6] ) - p.OffensiveRebounds; // cell 7 has ast p.Assists = Convert.ToInt32( Cells[7] ); // cell 8 has stl p.Steals = Convert.ToInt32( Cells[8] ); // cell 9 has blocks p.Blocks += Convert.ToInt32( Cells[9] ); // cell 10 has to p.Turnovers = Convert.ToInt32( Cells[10] ); // cell 11 has pf p.Fouls = Convert.ToInt32( Cells[11] ); perfs.Add( p ); } catch( System.FormatException e ) { // yes, this is a horrible thing to do, but it works and i'm // too lazy to switch it // this exception happens when the player gets a DNP of some // sort } } } return perfs; }