Extract PitchFX from MLB.com


/ Published in: PHP
Save to your folder(s)

Open PHP file and enter User Credentials for localhost. The default dates are set to negotiate the 2011 season, but change the date variables to match your desired season data. The start date should be the day before opening day, and the end date the last day of the regular season.

Append ignored attributes to conditional statement on line 81 to match your pitchfx table structure.


Copy this code and paste it in your HTML
  1. <?php
  2. $uname = '';
  3. $pswrd = '';
  4. $con = mysql_connect("localhost", $uname, $pswrd);
  5. if (!$con)
  6. {
  7. die('Could not connect: ' . mysql_error());
  8. }
  9. mysql_select_db("pitchfx",$con) or die('Error while selecting db');
  10.  
  11.  
  12. /*Debugging purposes
  13. $ob_file = fopen('test.txt' , 'w');
  14. function ob_file_callback($buffer)
  15. {
  16.   global $ob_file;
  17.   fwrite($ob_file,$buffer);
  18. }
  19. ob_start('ob_file_callback');*/
  20.  
  21. $start_date = '2011-03-30'; //Opening day -1 day
  22. $end_date = '2011-10-03'; //Last regular season game
  23. $cur_d = $start_date;
  24.  
  25. //Open dedicated XML stream
  26. $opts = array(
  27. 'http' => array(
  28. 'user_agent' => 'PHP libxml agent',
  29. )
  30. );
  31.  
  32. $context = stream_context_create($opts);
  33.  
  34. $name_p = array();
  35. $value_p = array();
  36. $p_seq = array();
  37. while ($cur_d != $end_date) {
  38. $cur_d = date ("Y-m-d", strtotime ($cur_d . "+1 day"));
  39. $year = date("Y",strtotime($cur_d));
  40. $month = date("m",strtotime($cur_d));
  41. $day = date("d",strtotime($cur_d));
  42. $xmldoc = new DOMDocument();
  43. $url = 'http://gd2.mlb.com/components/game/mlb/year_' . $year . '/month_' . $month . '/day_' . $day . '/scoreboard.xml';
  44. $xmldoc->load($url);
  45. $doc = $xmldoc->documentElement;
  46. $game = $doc->getElementsbyTagName('game');
  47. foreach($game as $g){
  48. $gid = $g->getattribute('id');
  49. $t_url = 'http://gd2.mlb.com/components/game/mlb/year_' . $year . '/month_' . $month . '/day_' . $day . '/gid_' . $gid . '/inning/inning_all.xml';
  50. $xmldoc = new DOMDocument();
  51. $xmldoc->load($t_url);
  52. if(!$xmldoc->load($t_url)){
  53. continue;
  54. }
  55. $doc = $xmldoc->documentElement;
  56. $event = $doc->getElementsbyTagName('inning');
  57. $away = $event->item(0)->getattribute('away_team');
  58. $home = $event->item(0)->getattribute('home_team');
  59.  
  60. foreach($event as $ev){
  61. $inning = $ev->getattribute('num');
  62. $atbat = $ev->getElementsbyTagName('atbat');
  63. foreach($atbat as $ab){
  64. if ($ab->parentNode->nodeName == 'top'){
  65. $top = 1;
  66. }
  67. else {
  68. $top = 0;
  69. }
  70. $ab_id = $ab->getattribute('batter');
  71. $p_id = $ab->getattribute('pitcher');
  72. $ab_hand = $ab->getattribute('stand');
  73. $p_hand = $ab->getattribute('p_throws');
  74. $pitch = $ab->getElementsbyTagName('pitch');
  75. foreach ($pitch as $p){
  76. unset($name_p);
  77. unset($value_p);
  78. unset($p_seq);
  79. foreach($p->attributes as $a) {
  80. if ($a->nodeName == 'tfs' || $a->nodeName == 'tfs_zulu' || $a->nodeName == 'mt' || $a->nodeName=='cc' ) {
  81. continue;
  82. }
  83. $value_p[] = $a->nodeValue;
  84. $name_p[] = $a->nodeName;
  85. $p_seq[] = $p->getattribute('type');
  86. }
  87. $sql = "INSERT INTO pitchfx.pitches (inning,date_id,top_bot,away,home,pitch_id,pitch_hand,ab_id,ab_hand," . implode($name_p,',') . ",pitch_seq) VALUES ('$inning','$gid','$top','$away','$home','$p_id','$p_hand','$ab_id','$ab_hand','" . implode($value_p,"','") . "','" . implode($p_seq) . "')";
  88. mysql_query($sql,$con) or die ('Error executing query. ' . mysql_error() . "\n");
  89. }
  90. }
  91. }
  92. }
  93. }
  94. //ob_end_flush();
  95. //fclose($ob_file);
  96. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.