-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetTides.php
116 lines (99 loc) · 4.4 KB
/
getTides.php
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
113
114
115
116
<?php
// Get the tides from the BOM website for the entire year, and put them into JSON format, so that they're available for the Spearers website
// Just execute with location in "loc" attribute (i.e. loc=westend, breakfast, noosa, coomera). It will refresh the tides_loc.js file.
//what location for tides
$loc = $_GET["loc"];
//Get Brisbane Tides for this year
$lastWeek = date('Y-m-d', strtotime('-14 days'));
if ($loc == 'westend' || $loc == 'breakfast') {
$urlroot = 'http://www.bom.gov.au/australia/tides/print.php?aac=QLD_TP138&type=tide&date=';
} else if ($loc == 'noosa') {
$urlroot = 'http://www.bom.gov.au/australia/tides/print.php?aac=QLD_TP021&type=tide&date=';
} else if ($loc == 'coomera') {
$urlroot = 'http://www.bom.gov.au/australia/tides/print.php?aac=QLD_TP146&type=tide&date=';
} else {
echo "include a location in the attributes. I.e. getTides.php?loc=westend or breakfast, noosa, coomera";
die();
}
$url = $urlroot.$lastWeek.'®ion=QLD&tz=Australia/Brisbane&tz_js=AEST&days=367';
$options = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" . // check function.stream-context-create on php.net
"User-Agent: Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.102011-10-16 20:23:10\r\n"
));
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === false) {
http_response_code(500);
echo json_encode(["error" => "Failed to fetch webpage content"]);
exit;
}
$doc = new DOMDocument();
@$doc->loadHTML($response);
$tide_info_list = array();
// Find all td elements
$elements = $doc->getElementsByTagName('td');
$i = 0;
while ($i < count($elements)) {
// Get the inner HTML content of the td element
$height = substr($elements[$i+1]->nodeValue, 0, -2);
if (strlen($height) > 2) {
$time_local = $elements[$i]->getAttribute('data-time-local');
if (str_contains($elements[$i]->getAttribute('class'),'high-tide')) {
$tide_type = 'HIGH';
} else {
$tide_type = 'LOW';
}
// Store the content and attributes in the array
$tide_info_list[] = array(
'tide' => $tide_type,
'time_local' => $time_local,
'height' => $height,
);
}
$i += 2;
}
//Now get the tides for next year
$today = new DateTime();
$firstDayNextYear = new DateTime((int)$today->format('Y') + 1 . '-01-01');
$nextYear = $firstDayNextYear->format('Y-m-d');
$url = $urlroot.$nextYear.'®ion=QLD&tz=Australia/Brisbane&tz_js=AEST&days=367';
$response = file_get_contents($url, false, $context);
if ($response === false) {
http_response_code(500);
echo json_encode(["error" => "Failed to fetch webpage content"]);
exit;
}
$doc = new DOMDocument();
@$doc->loadHTML($response);
// Find all td elements
$elements = $doc->getElementsByTagName('td');
$i = 0;
while ($i < count($elements)) {
// Get the inner HTML content of the td element
$height = substr($elements[$i+1]->nodeValue, 0, -2);
if (strlen($height) > 2) {
$time_local = $elements[$i]->getAttribute('data-time-local');
if (str_contains($elements[$i]->getAttribute('class'),'high-tide')) {
$tide_type = 'HIGH';
} else {
$tide_type = 'LOW';
}
// Store the content and attributes in the array
$tide_info_list[] = array(
'tide' => $tide_type,
'time_local' => $time_local,
'height' => $height,
);
}
$i += 2;
}
$txt = "let tide_list = " . json_encode($tide_info_list) . ';';
// Write the array of td contents and attributes to the file
$myfile = fopen("tides_".$loc.".js", "w") or die("Unable to open file!");
fwrite($myfile, $txt);
fclose($myfile);
echo "tides_".$loc.".js updated";
?>