/weatherpitch.php

https://github.com/chopsuei3/oscc · PHP · 113 lines · 94 code · 19 blank · 0 comment · 4 complexity · 3c8875af3e4e09e05c27895dfa3c949f MD5 · raw file

  1. <html>
  2. <title>WeatherPitch</title>
  3. <table border="1">
  4. <tr>
  5. <th>Teams</th>
  6. <th>Probables</th>
  7. <th>Gametime</th>
  8. <th>Venue, Lat/Long</th>
  9. <th>Forecast</th>
  10. <th>Expected Temperature (F)</th>
  11. <th>Expected Rainfall (in/hr)</th>
  12. </tr>
  13. <?php
  14. $today = getdate();
  15. $day = $today['mday'];
  16. $day = $day + 1;
  17. if($day<10)
  18. {
  19. $day = "0" . $day;
  20. }
  21. else
  22. {
  23. $day = $today['mday'];
  24. }
  25. if($month = $today['mon']<10)
  26. {
  27. $month = "0" . $today['mon'];
  28. }
  29. else
  30. {
  31. $month = $today['mon'];
  32. }
  33. $year = $today['year'];
  34. $url = "http://gd2.mlb.com/components/game/mlb/year_$year/month_$month/day_$day/master_scoreboard.json";
  35. $cURL = curl_init();
  36. curl_setopt($cURL, CURLOPT_URL, $url);
  37. curl_setopt($cURL, CURLOPT_HTTPGET, true);
  38. curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
  39. curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
  40. 'Content-Type: application/json',
  41. 'Accept: application/json'
  42. ));
  43. curl_setopt($cURL, CURLOPT_FAILONERROR, false);
  44. $result = curl_exec($cURL);
  45. curl_close($cURL);
  46. $data = json_decode($result, true);
  47. foreach($data['data']['games']['game'] as $games)
  48. {
  49. $gametime = $games['time_date'] . " " . $games['ampm'];
  50. $gametimeepoch = strtotime($gametime);
  51. $address = $games['venue'];
  52. $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.urlencode($address).'&sensor=false');
  53. $output= json_decode($geocode);
  54. $lat = $output->results[0]->geometry->location->lat;
  55. $long = $output->results[0]->geometry->location->lng;
  56. $dsurl = "https://api.forecast.io/forecast/05a58263279dde9059158665f6ac6c71/$lat,$long,$gametimeepoch";
  57. $cURL = curl_init();
  58. curl_setopt($cURL, CURLOPT_URL, $dsurl);
  59. curl_setopt($cURL, CURLOPT_HTTPGET, true);
  60. curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
  61. curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
  62. 'Content-Type: application/json',
  63. 'Accept: application/json'
  64. ));
  65. curl_setopt($cURL, CURLOPT_FAILONERROR, false);
  66. $dsresult = curl_exec($cURL);
  67. curl_close($cURL);
  68. $dsResponse = json_decode($dsresult, true);
  69. $rainRate = $dsResponse['currently']['precipIntensity'];
  70. $rainProb = $dsResponse['currently']['precipProbability'] * 100;
  71. $temperature = $dsResponse['currently']['temperature'];
  72. $weathersummary = $dsResponse['currently']['summary'];
  73. $home_team = $games['home_team_name'];
  74. $away_team = $games['away_team_name'];
  75. if ($rainProb > 50)
  76. {
  77. echo '<tr bgcolor="red">';
  78. }
  79. else
  80. {
  81. echo '<tr>';
  82. }
  83. echo '<td>' . $home_team . ' vs ' . $away_team . '</td>';
  84. echo '<td>' . $games['away_probable_pitcher']['first_name'] . ' ' . $games['away_probable_pitcher']['last_name'] . ' vs ' . $games['home_probable_pitcher']['first_name'] . ' ' . $games['home_probable_pitcher']['last_name'] . '</td>';
  85. echo '<td>' . $gametime . ' ' . $games['home_time_zone'] . '</td>';
  86. echo '<td>' . $address . ', ' . $lat . '/' . $long . '</td>';
  87. echo '<td align="center" valign="middle">' . $weathersummary . ' with a ' . $rainProb . '% chance of rain</td>';
  88. echo '<td align="center" valign="middle">' . $temperature . '</td>';
  89. echo '<td align="center" valign="middle">' . $rainRate . '</td>';
  90. echo '</tr>';
  91. }
  92. echo $url;
  93. ?>
  94. </html>