diff options
author | Luke Bratch <luke@bratch.co.uk> | 2022-01-19 09:16:45 +0000 |
---|---|---|
committer | Luke Bratch <luke@bratch.co.uk> | 2022-01-19 09:16:45 +0000 |
commit | cdef5b03f0c0239b329dc0b0fb0760e9211eccf8 (patch) | |
tree | c7bad49dedda3f56427fe11498ba8f62da779cc8 | |
parent | 04eecafe7d7e208a5b93cb4fac48fb61e7ea1bb8 (diff) |
Initial changes to support Jersey Met's new JSON/JavaScript powered website
-rw-r--r-- | index.php | 139 |
1 files changed, 46 insertions, 93 deletions
@@ -11,110 +11,63 @@ libxml_use_internal_errors(true); date_default_timezone_set("UTC"); -file_put_contents("time-index.txt", date("U")); - -$dom = new DOMDocument; -$dom->loadHTML(file_get_contents("http://www.gov.je/weather/Pages/Jersey-Forecast.aspx")); - -$divs = $dom->getElementsByTagName("div"); - -$day = 0; -$maxcount = 0; // How many max temperatures we have encountered (minus one for each min temp) -$nightmode = 0; - -$valid = array(); -$temp = array(); -$misc = array(); -$wind = array(); - -foreach ($divs as $div) { - if ($day > 2) { - break; - } - - foreach ($div->attributes as $attr) { - if ($attr->nodeValue == "forecastValid") { - array_push($valid, $div->nodeValue); - $day++; - } - - if ($attr->nodeValue == "forecastText" || $attr->nodeValue == "forecastText last") { - array_push($misc, $div->nodeValue); - } - - if ($attr->nodeValue == "main") { - $spans = $div->getElementsByTagName("span"); - foreach ($spans as $span) { - // If we get a min without a max, we're probably in night mode (only min and current listed) - if ($span->nodeValue == "Max") { - $maxcount++; - } else if ($span->nodeValue == "Min" && $maxcount < 1) { - $nightmode = 1; - } else if ($span->nodeValue == "Min") { - $maxcount--; - } - foreach ($span->attributes as $sattr) { - if ($sattr->nodeValue == "temp" || $sattr->nodeValue == "currentc") { - array_push($temp, $span->nodeValue); - } - } - } - } - - if ($attr->nodeValue == "wind") { - $spans = $div->getElementsByTagName("span"); - foreach ($spans as $span) { - array_push($wind, $span->nodeValue); - } - } - - } - -} - -// Replace newlines with spaces, and strip multiple spaces -for ($i = 0; $i < count($misc); $i++) { - $misc[$i] = preg_replace("/[\r\n]+/", " ", $misc[$i]); - $misc[$i] = preg_replace("/\s+/", " ", $misc[$i]); -} - -for ($i = 0; $i < count($temp); $i++) { -// $temp[$i] = str_replace("°", "°", $temp[$i]); -} - -if (!$nightmode) { - echo "<title>Max $temp[0], Min $temp[1], Cur $temp[2], Wind $wind[0], $misc[0]</title>\n\n"; -} else { - echo "<title>Min $temp[0], Cur $temp[1], $misc[0]</title>\n\n"; -} +$json = file_get_contents("https://prodgojweatherstorage.blob.core.windows.net/data/jerseyForecast.json"); +$arr_json = json_decode($json, true); + +$curtemp = $arr_json['currentTemprature']; + +// Today + +$maxtemp[0] = $arr_json['forecastDay']['0']['maxTemp']; +$mintemp[0] = $arr_json['forecastDay']['0']['minTemp']; +$wind[0] = $arr_json['forecastDay']['0']['windSpeed']; +$uv[0] = $arr_json['forecastDay']['0']['uvIndex']; +$morning[0] = $arr_json['forecastDay']['0']['morningDescripiton']; // Try to... +$afternoon[0] = $arr_json['forecastDay']['0']['afternoonDescripiton']; // ...find a... +$night[0] = $arr_json['forecastDay']['0']['nightDescripiton']; // ...day summary +$frost[0] = $arr_json['forecastDay']['0']['frost']; +$winddesc[0] = $arr_json['forecastDay']['0']['windDescription']; + +// Tomorrow + +$maxtemp[1] = $arr_json['forecastDay']['1']['maxTemp']; +$mintemp[1] = $arr_json['forecastDay']['1']['minTemp']; +$wind[1] = $arr_json['forecastDay']['1']['windSpeed']; +$uv[1] = $arr_json['forecastDay']['1']['uvIndex']; +$morning[1] = $arr_json['forecastDay']['1']['morningDescripiton']; +$afternoon[1] = $arr_json['forecastDay']['1']['afternoonDescripiton']; +$night[1] = $arr_json['forecastDay']['1']['nightDescripiton']; +$frost[1] = $arr_json['forecastDay']['1']['frost']; +$winddesc[1] = $arr_json['forecastDay']['1']['windDescription']; + + +echo "<title>Max $maxtemp[0], Min $mintemp[0], Cur $curtemp, UV $uv[0], Wind $wind[0], $morning[0] $afternoon[0] $night[0] $frost[0]</title>\n\n"; + echo "</head>\n\n"; echo "<body>\n\n"; echo "<p>Forecasts | <a href=\"./graphs.php\">Graphs</a></p>\n\n"; echo "<h2>Today</h2>\n\n"; -echo "<p><em>$valid[0]</em><br><br>\n"; -if (!$nightmode) { - echo "<strong>Max:</strong> $temp[0], <strong>Min:</strong> $temp[1], <strong>Current</strong>: $temp[2], <strong>Wind</strong>: $wind[0]<br><br>\n"; -} else { - echo "<strong>Min:</strong> $temp[0], <strong>Cur:</strong> $temp[1]<br><br>\n"; -} -echo "<strong>Summary:</strong> $misc[0]<br><br>\n"; -echo "<strong>Wind:</strong> $misc[1]<br><br>\n"; +//echo "<p><em>$valid[0]</em><br><br>\n"; // If a day summary is found, use a sensible time range here +echo "<strong>Max:</strong> $maxtemp[0], <strong>Min:</strong> $mintemp[0], <strong>Current</strong>: $curtemp, <strong>UV Index</strong>: $uv[0], <strong>Wind</strong>: $wind[0]<br><br>\n"; +echo "<strong>Morning:</strong> $morning[0]<br><br>\n"; +echo "<strong>Afternoon:</strong> $afternoon[0]<br><br>\n"; +echo "<strong>Night:</strong> $night[0]<br><br>\n"; +echo "<strong>Frost:</strong> $frost[0]<br><br>\n"; +echo "<strong>Wind:</strong> $winddesc[0]<br><br>\n"; echo "<h2>Tomorrow</h2>\n\n"; -echo "<p><em>$valid[1]</em><br><br>\n"; -if (!$nightmode) { - echo "<strong>Max</strong>: $temp[3], <strong>Min</strong>: $temp[4], <strong>Wind</strong>: $wind[2]<br><br>\n"; -} else { - echo "<strong>Max</strong>: $temp[2], <strong>Min</strong>: $temp[3]<br><br>\n"; -} -echo "<strong>Summary:</strong> $misc[2]<br><br>\n"; -echo "<strong>Wind:</strong> $misc[3]</p>"; +//echo "<p><em>$valid[1]</em><br><br>\n"; +echo "<strong>Max</strong>: $maxtemp[1], <strong>Min</strong>: $mintemp[1], <strong>UV Index</strong>: $uv[1], <strong>Wind</strong>: $wind[1]<br><br>\n"; +echo "<strong>Morning:</strong> $morning[1]<br><br>\n"; +echo "<strong>Afternoon:</strong> $afternoon[1]<br><br>\n"; +echo "<strong>Night:</strong> $night[1]<br><br>\n"; +echo "<strong>Frost:</strong> $frost[1]<br><br>\n"; +echo "<strong>Wind:</strong> $winddesc[1]</p>"; ?> - <p><small><a href="http://www.blatech.co.uk/l_bratch/w.of.je/tree/master">Source?</a></small></p> </body> |