From 53cf517409b23428b8d64eb578ac1faa59bf83be Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Tue, 20 Jun 2017 22:31:22 +0100 Subject: Support Jersey Met's night mode, where only the minimum and current temperature is listed --- index.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'index.php') diff --git a/index.php b/index.php index a9f4db2..27f88a6 100644 --- a/index.php +++ b/index.php @@ -19,6 +19,8 @@ $dom->loadHTML(file_get_contents("http://www.gov.je/weather/Pages/Jersey-Forecas $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(); @@ -42,6 +44,14 @@ foreach ($divs as $div) { 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" && $max < 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); @@ -63,7 +73,11 @@ for ($i = 0; $i < count($temp); $i++) { // $temp[$i] = str_replace("°", "°", $temp[$i]); } -echo "Max $temp[0], Min $temp[1], Cur $temp[2], $misc[0]\n\n"; +if (!$nightmode) { + echo "Max $temp[0], Min $temp[1], Cur $temp[2], $misc[0]\n\n"; +} else { + echo "Min $temp[0], Cur $temp[1], $misc[0]\n\n"; +} echo "\n\n"; echo "\n\n"; @@ -71,7 +85,11 @@ echo "

Forecasts | Graphs

\n\n"; echo "

Today

\n\n"; echo "

$valid[0]

\n"; -echo "Max: $temp[0], Min: $temp[1], Current: $temp[2]

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

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

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

\n"; echo "Visibility: $misc[1]

\n"; echo "Wind: $misc[2]

\n"; @@ -79,7 +97,11 @@ echo "Sea state: $misc[3]

\n\n"; echo "

Tomorrow

\n\n"; echo "

$valid[1]

\n"; -echo "Max: $temp[3], Min: $temp[4]

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

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

\n"; +} echo "Summary: $misc[4]

\n"; echo "Wind: $misc[5]

"; -- cgit v1.2.3