summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlastair Poole <netstar@gmail.com>2021-02-18 12:12:00 +0000
committerAlastair Poole <netstar@gmail.com>2021-02-18 12:12:00 +0000
commit16b2bdc289455796898ad5c1bbcf7c0e8d5901c4 (patch)
treef10fe62514f0d8c8ae7ab515d4e9e7c8c9f89c8b
parent4aec8d62fd6f16330ccc9a8aa56e35752223e046 (diff)
weathers: rain, sleep and shines.
-rw-r--r--public_html/weather.php91
-rw-r--r--public_html/weather/.gitignore0
2 files changed, 91 insertions, 0 deletions
diff --git a/public_html/weather.php b/public_html/weather.php
new file mode 100644
index 00000000..f3e340e6
--- /dev/null
+++ b/public_html/weather.php
@@ -0,0 +1,91 @@
1<?php
2
3const CACHE_PATH = '/weather';
4
5function response_code($code, $why)
6{
7 header("HTTP/1.1 $code $why");
8 exit(0);
9}
10
11function weather_cache_path($lat, $lon)
12{
13 $t = time();
14 $d = date('Y-m-d h', $t);
15 $m = date('i', $t);
16
17 // One per coord per 15 mins
18 $file = md5(sprintf("%s Q%d:%.4f:%.4f", $d, $m/15, $lat, $lon));
19
20 $path = $_SERVER["DOCUMENT_ROOT"] .'/'. CACHE_PATH . '/'. $file;
21 return $path;
22}
23
24function weather_cache_get($lat, $lon)
25{
26 $path = weather_cache_path($lat, $lon);
27 if (file_exists($path)) {
28 $json = file_get_contents($path);
29 if ($json === false) {
30 response_code(500, "Internal Server Error");
31 }
32 return file_get_contents($path);
33 }
34 return null;
35}
36
37function weather_cache_save($lat, $lon, $json)
38{
39 $path = weather_cache_path($lat, $lon);
40 if (file_put_contents($path, $json) === false) {
41 response_code(500, "Internal Server Error");
42 }
43}
44
45function weather_get($lat, $lon)
46{
47 $json = weather_cache_get($lat, $lon);
48 if (isset($json)) return $json;
49
50 $url = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=$lat&lon=$lon";
51 $c = curl_init();
52 curl_setopt($c, CURLOPT_USERAGENT, "eWeatherProxy/1.0");
53 curl_setopt($c, CURLOPT_URL, $url);
54 curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
55
56 $json = curl_exec($c);
57 $s = curl_getinfo($c, CURLINFO_RESPONSE_CODE);
58 curl_close($c);
59 if ($s != 200) {
60 response_code($s, "Meh Meh Meh");
61 }
62
63 weather_cache_save($lat, $lon, $json);
64
65 return $json;
66}
67
68function main()
69{
70 if ($_SERVER['REQUEST_METHOD'] !== "GET") {
71 response_code(405, "Method Not Allowed");
72 }
73
74 if ((count($_GET) != 2) || (!isset($_GET['lon'])) || (!isset($_GET['lat']))) {
75 response_code(400, "Bad Request");
76 }
77
78 $lat = floatval($_GET['lat']);
79 $lon = floatval($_GET['lon']);
80 if ((($lat < -90) || ($lat > 90)) || (($lon < -180) || ($lon > 180))) {
81 response_code(400, "Bad Request");
82 }
83
84 $resp = weather_get($lat, $lon);
85 header('Content-Length: ' . strlen($resp));
86 header('Content-Type: application/json; charset=utf-8');
87 print $resp;
88}
89
90MAIN();
91?>
diff --git a/public_html/weather/.gitignore b/public_html/weather/.gitignore
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/public_html/weather/.gitignore