/nfl_scrape.php

https://github.com/mikeygee/miscellaneous · PHP · 243 lines · 163 code · 24 blank · 56 comment · 51 complexity · 6f6a38f418fd2f8e4fc499927ff947ef MD5 · raw file

  1. <?php
  2. // NFL score and odds scraper
  3. include 'simple_html_dom.php';
  4. // returns array of score maps from NFL.com
  5. // if no arguments passed, current scores will be returned
  6. // only final scores are returned due to in-progress games using Javascript
  7. // pass year and week arguments to retrieve past scores
  8. function getNFLScores() {
  9. if(func_num_args() == 2) {
  10. $year = func_get_arg(0);
  11. $week = func_get_arg(1);
  12. }
  13. if(isset($year) && isset($week))
  14. $html = file_get_html("http://www.nfl.com/scores/$year/REG$week");
  15. else
  16. $html = file_get_html("http://www.nfl.com/scores");
  17. $time = $html->find('.time-left');
  18. $awayTeam = $html->find('.away-team .team-name a');
  19. $homeTeam = $html->find('.home-team .team-name a');
  20. $awayScore = $html->find('.away-team .total-score');
  21. $homeScore = $html->find('.home-team .total-score');
  22. $games = array();
  23. for($i = 0, $len = sizeof($awayTeam); $i < $len; $i++) {
  24. $tm = $time[$i]->innertext;
  25. $at = $awayTeam[$i]->innertext;
  26. $ht = $homeTeam[$i]->innertext;
  27. $as = (int) $awayScore[$i]->innertext;
  28. $hs = (int) $homeScore[$i]->innertext;
  29. $winner = ($tm == 'FINAL') ? (($hs > $as) ? $ht : $at) : NULL;
  30. array_push($games, array(
  31. 'time' => $tm,
  32. 'awayTeam' => $at,
  33. 'homeTeam' => $ht,
  34. 'awayScore' => $as,
  35. 'homeScore' => $hs,
  36. 'winner' => $winner
  37. ));
  38. }
  39. return $games;
  40. }
  41. // returns array of current odds maps from covers.com
  42. // teams id'd by city name, for NY - trim down to team name, also trimmed period out of St. Louis to match db
  43. function getNFLOdds() {
  44. $html = file_get_html("http://www.covers.com/odds/football/nfl-spreads.aspx");
  45. $awayTeam = $html->find('.team_away strong');
  46. $homeTeam = $html->find('.team_home strong');
  47. $total = $html->find('.vegas_top .line_top a');
  48. $spread = $html->find('.vegas_top .vegas_bottom a');
  49. $odds = array();
  50. for($i = 0, $len = sizeof($awayTeam); $i < $len; $i++) {
  51. $at = preg_replace('/(N\.Y\.\s)|\./','', $awayTeam[$i]->innertext);
  52. $ht = preg_replace('/(N\.Y\.\s)|\./','', trim($homeTeam[$i]->innertext, '@'));
  53. $t = (float) $total[$i]->innertext;
  54. $s = (float) $spread[$i]->innertext;
  55. array_push($odds, array(
  56. 'awayTeam' => $at,
  57. 'homeTeam' => $ht,
  58. 'total' => $t,
  59. 'spread' => $s,
  60. 'favorite' => ($s < 0) ? $ht : (($s > 0) ? $at : 'pick')
  61. ));
  62. }
  63. return $odds;
  64. }
  65. // scrape scores and write them to nflive database, return true on success
  66. function updateScores() {
  67. if(func_num_args() == 2) {
  68. $year = func_get_arg(0);
  69. $week = func_get_arg(1);
  70. }
  71. // connect to db
  72. $db = new mysqli('localhost', 'root', 'root','nflive_db');
  73. if(mysqli_connect_errno())
  74. die($db->error);
  75. // scrape the scores
  76. if(isset($year) && isset($week))
  77. $scores = getNFLScores($year, $week);
  78. else
  79. $scores = getNFLScores();
  80. foreach($scores as $val) {
  81. // store values
  82. $ht = $val['homeTeam'];
  83. $at = $val['awayTeam'];
  84. $hs = $val['homeScore'];
  85. $as = $val['awayScore'];
  86. $time = $val['time'];
  87. // get home team id
  88. $query = "select nfl_team_id from nfl_team where mascot = '$ht'";
  89. if(!($result = $db->query($query)))
  90. die($db->error);
  91. $row = $result->fetch_assoc();
  92. $hid = $row['nfl_team_id'];
  93. // get away team id
  94. $query = "select nfl_team_id from nfl_team where mascot = '$at'";
  95. if(!($result = $db->query($query)))
  96. die($db->error);
  97. $row = $result->fetch_assoc();
  98. $aid = $row['nfl_team_id'];
  99. // set winner if game is over
  100. if($time == 'FINAL')
  101. $winner = ($hs > $as) ? $hid : $aid;
  102. else
  103. $winner = 0;
  104. // update game table
  105. $query = "update game set home_score = $hs, away_score = $as, winner_id = $winner where home_team_id = $hid and away_team_id = $aid";
  106. if($result = $db->query($query)) {
  107. $db->commit();
  108. }
  109. else
  110. die($db->error);
  111. }
  112. return true;
  113. }
  114. // scrape odds and write them to nflive database, return true on success
  115. function updateOdds() {
  116. // connect to db
  117. $db = new mysqli('localhost', 'root', 'root','nflive_db1112');
  118. if(mysqli_connect_errno())
  119. die($db->error);
  120. // scrape the odds
  121. $odds = getNFLOdds();
  122. foreach($odds as $val) {
  123. // store values
  124. $ht = $val['homeTeam'];
  125. $at = $val['awayTeam'];
  126. $total = $val['total'];
  127. $spread = $val['spread'];
  128. // get home team id, workaround for the two NY teams
  129. $query = ($ht == 'Giants' || $ht == 'Jets') ? "select nfl_team_id from nfl_team where mascot = '$ht'" : "select nfl_team_id from nfl_team where city = '$ht'";
  130. if(!($result = $db->query($query)))
  131. die($db->error);
  132. $row = $result->fetch_assoc();
  133. $hid = $row['nfl_team_id'];
  134. // get away team id
  135. $query = ($at == 'Giants' || $at == 'Jets') ? "select nfl_team_id from nfl_team where mascot = '$at'" : "select nfl_team_id from nfl_team where city = '$at'";
  136. if(!($result = $db->query($query)))
  137. die($db->error);
  138. $row = $result->fetch_assoc();
  139. $aid = $row['nfl_team_id'];
  140. // set favorite, if spread is 0, set favorite id to 0, update spread to abs value
  141. $fav = ($spread < 0) ? $hid : (($spread > 0) ? $aid : 0);
  142. $spread = abs($spread);
  143. // update game table
  144. $query = "update game set point_spread = $spread, over_under = $total, favorite = $fav where home_team_id = $hid and away_team_id = $aid";
  145. if($result = $db->query($query)) {
  146. $db->commit();
  147. }
  148. else
  149. die($db->error);
  150. }
  151. return true;
  152. }
  153. /*------------
  154. usage: nfl_scrape.php [options]
  155. -scores : print current scores to console
  156. -scores [year] [week] : print scores from a specific week
  157. -odds : print current odds to console
  158. -updateOdds : write current odds to nflive database
  159. -updateScores : write current scores to nflive database
  160. -updateScores [year] [week] : write scores for a specific week to nflive database
  161. -updateAll : write current scores and odds to nflive database
  162. ------------*/
  163. if(isset($argv)) {
  164. if($argv[1] == '-scores') {
  165. if(sizeof($argv) == 2)
  166. $scores = getNFLScores();
  167. else if(sizeof($argv) == 4)
  168. $scores = getNFLScores($argv[2], $argv[3]);
  169. print_r($scores);
  170. }
  171. else if($argv[1] == '-odds') {
  172. $odds = getNFLOdds();
  173. print_r($odds);
  174. }
  175. else if($argv[1] == '-updateScores') {
  176. if(sizeof($argv) == 4) {
  177. updateScores($argv[2], $argv[3]);
  178. echo "Scores updated\n";
  179. }
  180. else {
  181. updateScores();
  182. echo "Scores updated\n";
  183. }
  184. }
  185. else if($argv[1] == '-updateOdds') {
  186. updateOdds();
  187. echo "Odds updated\n";
  188. }
  189. else if($argv[1] == '-updateAll') {
  190. updateScores();
  191. echo "Scores updated\n";
  192. updateOdds();
  193. echo "Odds updated\n";
  194. }
  195. }
  196. /*-------Test Queries--------
  197. SELECT g.week_no, t1.full_name as away_team, t2.full_name as home_team, g.point_spread, g.over_under, t3.full_name as favorite
  198. FROM game g
  199. left outer join nfl_team t1 on g.away_team_id = t1.nfl_team_id
  200. left outer join nfl_team t2 on g.home_team_id = t2.nfl_team_id
  201. left outer join nfl_team t3 on g.favorite = t3.nfl_team_id
  202. where g.week_no = 1
  203. SELECT g.week_no, t1.full_name as away_team, g.away_score, t2.full_name as home_team, g.home_score, t3.full_name as winner
  204. FROM game g
  205. left outer join nfl_team t1 on g.away_team_id = t1.nfl_team_id
  206. left outer join nfl_team t2 on g.home_team_id = t2.nfl_team_id
  207. left outer join nfl_team t3 on g.winner_id = t3.nfl_team_id
  208. where g.week_no = 1
  209. update game
  210. set home_score=NULL, away_score=NULL, winner_id=NULL
  211. where week_no = 1
  212. update game
  213. set point_spread=NULL, over_under=NULL, favorite=NULL
  214. where week_no = 1
  215. -----------------------------*/
  216. ?>