Advertisement
Guest User

Untitled

a guest
May 17th, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. <?php
  2. set_time_limit(2400);
  3. function url_exists($url) {
  4. // Version 4.x supported
  5. $handle = curl_init($url);
  6. if (false === $handle)
  7. {
  8. return false;
  9. }
  10. curl_setopt($handle, CURLOPT_HEADER, false);
  11. curl_setopt($handle, CURLOPT_FAILONERROR, true); // this works
  12. curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox
  13. curl_setopt($handle, CURLOPT_NOBODY, true);
  14. curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
  15. $connectable = curl_exec($handle);
  16. curl_close($handle);
  17. return $connectable;
  18. }
  19.  
  20. function insertPitchFX($inf, $inning, $state, $gameid, $connection) {
  21. $ab = $inf->attributes();
  22. $ab_num = $ab->num;
  23. $b = $ab->b;
  24. $s = $ab->s;
  25. $o = $ab->o;
  26. $batter = $ab->batter;
  27. $stand = $ab->stand;
  28. $b_height = $ab->b_height;
  29. $pitcher = $ab->pitcher;
  30. $p_throws = $ab->p_throws;
  31. $des = $ab->des;
  32. $event = $ab->event;
  33. $numPitches = $inf->pitch->count();
  34. if ($numPitches > 0) {
  35. if (isset($inf->pitch[0]->attributes()->on_1b)) { $on_1B = $inf->pitch[0]->attributes()->on_1b; } else { $on_1B = ""; }
  36. if (isset($inf->pitch[0]->attributes()->on_2b)) { $on_2B = $inf->pitch[0]->attributes()->on_2b; } else { $on_2B = ""; }
  37. if (isset($inf->pitch[0]->attributes()->on_3b)) { $on_3B = $inf->pitch[0]->attributes()->on_3b; } else { $on_3B = ""; }
  38. } else {
  39. $on_1B = "";
  40. $on_2B = "";
  41. $on_3B = "";
  42. }
  43. $SQL = "INSERT INTO `PitchFX`.`AtBats` (`gameid`, `atbat_num`, `inning`, `state`, `b`, `s`, `o`, `batter`, `stand`, `b_height`, `pitcher`, `p_throws`, `des`, `event`, `numPitches`, `on_1B`, `on_2B`, `on_3B`) VALUES ('$gameid', '$ab_num', '$inning', '$state', '$b', '$s', '$o', '$batter', '$stand', '$b_height', '$pitcher', '$p_throws', '$des', '$event', '$numPitches', '$on_1B', '$on_2B', '$on_3B');";
  44. mysql_query($SQL, $connection);
  45. foreach ($inf->pitch as $pitch) {
  46. $p = $pitch->attributes();
  47. if (isset($p->on_1b)) { $on_1B = $p->on_1b; } else { $on_1B = ""; }
  48. if (isset($p->on_2b)) { $on_1B = $p->on_2b; } else { $on_2B = ""; }
  49. if (isset($p->on_3b)) { $on_1B = $p->on_3b; } else { $on_3B = ""; }
  50. if (isset($p->des)) { $des = $p->des; } else { $des = ""; }
  51. if (isset($p->id)) { $pitch_id = $p->id; } else { $pitch_id = ""; }
  52. if (isset($p->type)) { $type = $p->type; } else { $type = ""; }
  53. if (isset($p->x)) { $x = $p->x; } else { $x = ""; }
  54. if (isset($p->y)) { $y = $p->y; } else { $y = ""; }
  55. if (isset($p->start_speed)) { $start_speed = $p->start_speed; } else { $start_speed = ""; }
  56. if (isset($p->end_speed)) { $end_speed = $p->end_speed; } else { $end_speed = ""; }
  57. if (isset($p->sz_top)) { $sz_top = $p->sz_top; } else { $sz_top = ""; }
  58. if (isset($p->sz_bot)) { $sz_bot = $p->sz_bot; } else { $sz_bot = ""; }
  59. if (isset($p->pfx_x)) { $pfx_x = $p->pfx_x; } else { $pfx_x = ""; }
  60. if (isset($p->pfx_z)) { $pfx_z = $p->pfx_z; } else { $pfx_z = ""; }
  61. if (isset($p->px)) { $px = $p->px; } else { $px = ""; }
  62. if (isset($p->pz)) { $pz = $p->pz; } else { $pz = ""; }
  63. if (isset($p->x0)) { $x0 = $p->x0; } else { $x0 = ""; }
  64. if (isset($p->y0)) { $y0 = $p->y0; } else { $y0 = ""; }
  65. if (isset($p->z0)) { $z0 = $p->z0; } else { $z0 = ""; }
  66. if (isset($p->vx0)) { $vx0 = $p->vx0; } else { $vx0 = ""; }
  67. if (isset($p->vy0)) { $vy0 = $p->vy0; } else { $vy0 = ""; }
  68. if (isset($p->vz0)) { $vz0 = $p->vz0; } else { $vz0 = ""; }
  69. if (isset($p->ax)) { $ax = $p->ax; } else { $ax = ""; }
  70. if (isset($p->ay)) { $ay = $p->ay; } else { $ay = ""; }
  71. if (isset($p->az)) { $az = $p->az; } else { $az = ""; }
  72. if (isset($p->break_y)) { $break_y = $p->break_y; } else { $break_y = ""; }
  73. if (isset($p->break_angle)) { $break_angle = $p->break_angle; } else { $break_angle = ""; }
  74. if (isset($p->break_length)) { $break_length = $p->break_length; } else { $break_length = ""; }
  75. if (isset($p->pitch_type)) { $pitch_type = $p->pitch_type; } else { $pitch_type = ""; }
  76. if (isset($p->type_confidence)) { $type_confidence = $p->type_confidence; } else { $type_confidence = ""; }
  77. if (isset($p->zone)) { $zone = $p->zone; } else { $zone = ""; }
  78. if (isset($p->nasty)) { $nasty = $p->nasty; } else { $nasty = ""; }
  79. if (isset($p->spin_dir)) { $spin_dir = $p->spin_dir; } else { $spin_dir = ""; }
  80. if (isset($p->spin_rate)) { $spin_rate = $p->spin_rate; } else { $spin_rate = ""; }
  81. if (isset($p->cc)) { $cc = $p->cc; } else { $cc = ""; }
  82. if (isset($p->mt)) { $mt = $p->mt; } else { $mt = ""; }
  83. $SQL = "INSERT INTO `PitchFX`.`Pitches` (`gameid`, `pitch_id`, `pitcher`, `batter`, `atbat_num`, `inning`, `state`, `on_1B`, `on_2B`, `on_3B`, `des`, `type`, `x`, `y`, `start_speed`, `end_speed`, `sz_top`, `sz_bot`, `pfx_x`, `pfx_z`, `px`, `pz`, `x0`, `y0`, `z0`, `vx0`, `vy0`, `vz0`, `ax`, `ay`, `az`, `break_y`, `break_angle`, `break_length`, `pitch_type`, `type_confidence`, `zone`, `nasty`, `spin_dir`, `spin_rate`, `cc`, `mt`) VALUES ('$gameid', '$pitch_id', '$pitcher', '$batter', '$ab_num', '$inning', '$state', '$on_1B', '$on_2B', '$on_3B', '$des', '$type', '$x', '$y', '$start_speed', '$end_speed', '$sz_top', '$sz_bot', '$pfx_x', '$pfx_z', '$px', '$pz', '$x0', '$y0', '$z0', '$vx0', '$vy0', '$vz0', '$ax', '$ay', '$az', '$break_y', '$break_angle', '$break_length', '$pitch_type', '$type_confidence', '$zone', '$nasty', '$spin_dir', '$spin_rate', '$cc', '$mt');";
  84. mysql_query($SQL, $connection);
  85. }
  86. }
  87.  
  88. function PitchCount($gameid, $connection) {
  89. $query = mysql_query("SELECT * FROM PitchFX.AtBats where gameid = '$gameid' group by pitcher", $connection);
  90. while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
  91. $numPitches = 0;
  92. $numBalls = 0;
  93. $numGroundouts = 0;
  94. $numLineouts = 0;
  95. $numFlyouts = 0;
  96. $numPopouts = 0;
  97. $numDP = 0;
  98. $numSO = 0;
  99. $numWalks = 0;
  100. $query2 = mysql_query("SELECT * FROM PitchFX.AtBats where gameid = '$gameid' and pitcher = '" . $row['pitcher'] . "'", $connection);
  101. while ($row2 = mysql_fetch_array($query2, MYSQL_ASSOC)) {
  102. $numPitches = $numPitches + (int)$row2['numPitches'];
  103. $numBalls = $numBalls + (int)$row2['b'];
  104. If ($row2['event'] == "Pop Out") { $numPopouts++; }
  105. If ($row2['event'] == "Flyout") { $numFlyouts++; }
  106. If ($row2['event'] == "Lineout") { $numLineouts++; }
  107. If ($row2['event'] == "Groundout") { $numGroundouts++; }
  108. If ($row2['event'] == "Grounded Into DP") { $numGroundouts++; $numDP++; }
  109. If ($row2['event'] == "Walk") { $numWalks++; }
  110. If ($row2['event'] == "Strikeout") { $numSO++; }
  111. }
  112. $numStrikes = $numPitches - $numBalls;
  113. mysql_query("INSERT INTO `PitchFX`.`PitchCount` (`gameid`, `pitcher`, `pitches`, `strikes`, `balls`, `groundouts`, `popouts`, `flyouts`, `lineouts`, `doubleplays`, `walks`, `strikeouts`, `innings`) VALUES ('$gameid', '" . $row['pitcher'] . "', '$numPitches', '$numStrikes', '$numBalls', '$numGroundouts', '$numPopouts', '$numFlyouts', '$numLineouts', '$numDP', '$numWalks', '$numSO', '0')", $connection);
  114. }
  115. }
  116.  
  117. $connection = mysql_connect("","","");
  118. for ($x = 2; $x < 3; $x++) {
  119. $year = date("Y", strtotime("-" . $x . " days"));
  120. $month = date("m", strtotime("-" . $x . " days"));
  121. $day = date("d", strtotime("-" . $x . " days"));
  122. echo $x . "!!<BR>";
  123. $url = "http://gd2.mlb.com/components/game/mlb/year_" . $year . "/month_" . $month . "/day_" . $day . "/master_scoreboard.xml";
  124. If (url_exists($url) == TRUE) {
  125. $xml = simplexml_load_file($url);
  126. foreach ($xml->game as $xml) {
  127. $gameID = $xml->attributes()->game_pk;
  128. $gamedayID = $xml->attributes()->gameday;
  129. $lnk = "http://gd2.mlb.com/components/game/mlb/year_" . $year . "/month_" . $month . "/day_" . $day . "/gid_" . $gamedayID . "/inning/inning_all.xml";
  130. echo $lnk . "<BR>";
  131. If (url_exists($lnk) == TRUE) {
  132. $xfile = simplexml_load_file($lnk);
  133. foreach ($xfile->inning as $i) {
  134. $inning = $i->attributes()->num;
  135. if (isset($i->top)) { foreach ($i->top->atbat as $t) { insertPitchFX($t, $inning, 'T', $gameID, $connection); } }
  136. if (isset($i->bottom)) { foreach ($i->bottom->atbat as $b) { insertPitchFX($b, $inning, 'B', $gameID, $connection); } }
  137. }
  138. PitchCount($gameID, $connection);
  139. }
  140. }
  141. }
  142. }
  143. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement