From 8ba00e764ad7902e5f8ac88b61c2c6c21868139d Mon Sep 17 00:00:00 2001 From: Frankie B Date: Sun, 11 Dec 2022 13:25:48 +0000 Subject: [PATCH] Add weather data to homepage --- inc/weatherdata.inc.php | 68 +++++++++++++++++++++++++++++++++++++++++ index.php | 5 ++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 inc/weatherdata.inc.php diff --git a/inc/weatherdata.inc.php b/inc/weatherdata.inc.php new file mode 100644 index 0000000..a87a117 --- /dev/null +++ b/inc/weatherdata.inc.php @@ -0,0 +1,68 @@ + "http://weather.diskfloppy.me/weatherData.json", + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 300, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", +]); + +$current_response = curl_exec($curl_current); +$err = curl_error($curl_current); + +curl_close($curl_current); + +$curl_current = curl_init(); + +curl_setopt_array($curl_current, [ + CURLOPT_URL => "http://weather.diskfloppy.me/got.txt", + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 300, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "GET", +]); + +$last_update = curl_exec($curl_current); +$err = curl_error($curl_current); + +curl_close($curl_current); + +if ($err) { + die("cURL Error: " . $err); +} +$current_response = json_decode($current_response, true); +$weather_data = $current_response['data']['conditions'][0]; +echo '

Local Weather (Last Update: '.rtrim($last_update).')

'; +$windDirDeg = $weather_data['wind_dir_last']; +$windDir = ""; +// W I N D +if (349 <= $windDirDeg || $windDirDeg <= 11) { $windDir = "N"; } +else if (12 <= $windDirDeg && $windDirDeg <= 33) { $windDir = "NNE"; } +else if (34 <= $windDirDeg && $windDirDeg <= 56) { $windDir = "NE"; } +else if (57 <= $windDirDeg && $windDirDeg <= 78) { $windDir = "ENE"; } +else if (79 <= $windDirDeg && $windDirDeg <= 101) { $windDir = "E"; } +else if (102 <= $windDirDeg && $windDirDeg <= 123) { $windDir = "ESE"; } +else if (124 <= $windDirDeg && $windDirDeg <= 146) { $windDir = "SE"; } +else if (147 <= $windDirDeg && $windDirDeg <= 168) { $windDir = "SSE"; } +else if (169 <= $windDirDeg && $windDirDeg <= 191) { $windDir = "S"; } +else if (192 <= $windDirDeg && $windDirDeg <= 213) { $windDir = "SSW"; } +else if (214 <= $windDirDeg && $windDirDeg <= 236) { $windDir = "SW"; } +else if (237 <= $windDirDeg && $windDirDeg <= 258) { $windDir = "WSW"; } +else if (259 <= $windDirDeg && $windDirDeg <= 281) { $windDir = "W"; } +else if (282 <= $windDirDeg && $windDirDeg <= 303) { $windDir = "WNE"; } +else if (304 <= $windDirDeg && $windDirDeg <= 326) { $windDir = "NW"; } +else if (327 <= $windDirDeg && $windDirDeg <= 348) { $windDir = "WNW"; } + + +echo 'Wind Speed: '.$weather_data['wind_speed_last'].' mph
'; +echo 'Wind Direction: '.$weather_data['wind_dir_last'].'°, '.$windDir.'
'; +echo 'Temperature: '.round(($weather_data['temp']-32)*(5/9), 1).'°C
'; +echo 'Rain Rate: '.round($weather_data['rain_rate_last']*0.2, 2).' mm/hr
'; +echo 'Humidity: '.$weather_data['hum'].'%
'; diff --git a/index.php b/index.php index b1160c2..08a2761 100755 --- a/index.php +++ b/index.php @@ -25,7 +25,10 @@

This is the personal homepage of floppydisk.

- +

Contact

Mail: contact@diskfloppy.me