From cdef5b03f0c0239b329dc0b0fb0760e9211eccf8 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Wed, 19 Jan 2022 09:16:45 +0000 Subject: Initial changes to support Jersey Met's new JSON/JavaScript powered website --- index.php | 139 +++++++++++++++++++++----------------------------------------- 1 file changed, 46 insertions(+), 93 deletions(-) diff --git a/index.php b/index.php index a6de44c..fc92d21 100644 --- a/index.php +++ b/index.php @@ -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 "Max $temp[0], Min $temp[1], Cur $temp[2], Wind $wind[0], $misc[0]\n\n"; -} else { - echo "Min $temp[0], Cur $temp[1], $misc[0]\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 "Max $maxtemp[0], Min $mintemp[0], Cur $curtemp, UV $uv[0], Wind $wind[0], $morning[0] $afternoon[0] $night[0] $frost[0]\n\n"; + echo "\n\n"; echo "\n\n"; echo "

Forecasts | Graphs

\n\n"; echo "

Today

\n\n"; -echo "

$valid[0]

\n"; -if (!$nightmode) { - echo "Max: $temp[0], Min: $temp[1], Current: $temp[2], Wind: $wind[0]

\n"; -} else { - echo "Min: $temp[0], Cur: $temp[1]

\n"; -} -echo "Summary: $misc[0]

\n"; -echo "Wind: $misc[1]

\n"; +//echo "

$valid[0]

\n"; // If a day summary is found, use a sensible time range here +echo "Max: $maxtemp[0], Min: $mintemp[0], Current: $curtemp, UV Index: $uv[0], Wind: $wind[0]

\n"; +echo "Morning: $morning[0]

\n"; +echo "Afternoon: $afternoon[0]

\n"; +echo "Night: $night[0]

\n"; +echo "Frost: $frost[0]

\n"; +echo "Wind: $winddesc[0]

\n"; echo "

Tomorrow

\n\n"; -echo "

$valid[1]

\n"; -if (!$nightmode) { - echo "Max: $temp[3], Min: $temp[4], Wind: $wind[2]

\n"; -} else { - echo "Max: $temp[2], Min: $temp[3]

\n"; -} -echo "Summary: $misc[2]

\n"; -echo "Wind: $misc[3]

"; +//echo "

$valid[1]

\n"; +echo "Max: $maxtemp[1], Min: $mintemp[1], UV Index: $uv[1], Wind: $wind[1]

\n"; +echo "Morning: $morning[1]

\n"; +echo "Afternoon: $afternoon[1]

\n"; +echo "Night: $night[1]

\n"; +echo "Frost: $frost[1]

\n"; +echo "Wind: $winddesc[1]

"; ?> -

Source?

-- cgit v1.2.3