summaryrefslogtreecommitdiff
path: root/index.php
blob: 27f88a698011da580f1d897c73db7252287a6852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<?php

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();

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" && $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);
          }
        }
      }
    }
  }

}

// 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("°", "&deg;", $temp[$i]);
}

if (!$nightmode) {
  echo "<title>Max $temp[0], Min $temp[1], Cur $temp[2], $misc[0]</title>\n\n";
} else {
  echo "<title>Min $temp[0], Cur $temp[1], $misc[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]<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>Visibility:</strong> $misc[1]<br><br>\n";
echo "<strong>Wind:</strong> $misc[2]<br><br>\n";
echo "<strong>Sea state:</strong> $misc[3]</p>\n\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]<br><br>\n";
} else {
  echo "<strong>Max</strong>: $temp[2], <strong>Min</strong>: $temp[3]<br><br>\n";
}
echo "<strong>Summary:</strong> $misc[4]<br><br>\n";
echo "<strong>Wind:</strong> $misc[5]</p>";

?>

</body>

</html>