From 9f8b1fa943c10ad2db1b682dc10a2d67a75669b5 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 28 Feb 2023 19:44:52 -0700 Subject: [PATCH] Replace Dark Sky with OpenWeatherMap --- ...eather.owm.php => gis.weather.darksky.php} | 98 +++++++++---------- endpoints/gis.weather.php | 98 ++++++++++--------- 2 files changed, 98 insertions(+), 98 deletions(-) rename endpoints/{gis.weather.owm.php => gis.weather.darksky.php} (66%) diff --git a/endpoints/gis.weather.owm.php b/endpoints/gis.weather.darksky.php similarity index 66% rename from endpoints/gis.weather.owm.php rename to endpoints/gis.weather.darksky.php index 1f1fc12..42a487b 100644 --- a/endpoints/gis.weather.owm.php +++ b/endpoints/gis.weather.darksky.php @@ -19,15 +19,15 @@ if (empty($VARS["nocache"])) { } } -$json = file_get_contents("https://api.openweathermap.org/data/2.5/onecall?lat=$lat&lon=$lon&units=imperial&appid=" . env("openweathermap_appid", "")); +$json = file_get_contents("https://api.darksky.net/forecast/" . env("darksky_apikey", "") . "/$lat,$lon?units=us&exclude=alerts,flags"); $weather = json_decode($json, TRUE); $minutely = []; -foreach ($weather["minutely"] as $m) { +foreach ($weather["minutely"]["data"] as $m) { $minutely[] = [ - "time" => $m["dt"], - "precip" => round($m["precipitation"] / 25.4, 3) + "time" => $m["time"], + "precip" => round($m["precipIntensity"], 3) ]; } $hourly = []; @@ -43,42 +43,42 @@ $oldicon = "clear"; $nowicon = "clear"; $lasttimestamp = time(); -foreach ($weather["hourly"] as $w) { - if ($w["dt"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) { +foreach ($weather["hourly"]["data"] as $w) { + if ($w["time"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) { continue; } $hourly[] = [ - "time" => $w["dt"], - "temp" => $w["temp"], - "feelslike" => $w["feels_like"], - "windspeed" => $w["wind_speed"], - "winddirection" => $w["wind_deg"], - "precipchance" => $w["pop"] + "time" => $w["time"], + "temp" => $w["temperature"], + "feelslike" => $w["apparentTemperature"], + "windspeed" => $w["windSpeed"], + "winddirection" => $w["windBearing"], + "precipchance" => $w["precipProbability"] ]; if ($mintemp === false) { - $mintemp = $w["temp"]; + $mintemp = $w["temperature"]; } if ($maxtemp === false) { - $maxtemp = $w["temp"]; + $maxtemp = $w["temperature"]; } if ($maxwind === false) { - $maxwind = $w["wind_speed"]; + $maxwind = $w["windSpeed"]; } if ($precipchance === false) { - $precipchance = $w["pop"]; + $precipchance = $w["precipProbability"]; } if ($maxcloudcover === false) { - $maxcloudcover = $w["clouds"]; + $maxcloudcover = $w["cloudCover"] * 100.0; } - $mintemp = min($w["temp"], $mintemp); - $maxtemp = max($w["temp"], $maxtemp); - $maxwind = max($w["wind_speed"], $maxwind); - $precipchance = max($w["pop"], $precipchance); - $maxcloudcover = max($w["clouds"], $maxcloudcover); - $cloudcovers[] = $w["clouds"]; - $lasttimestamp = max($w["dt"], $lasttimestamp); + $mintemp = min($w["temperature"], $mintemp); + $maxtemp = max($w["temperature"], $maxtemp); + $maxwind = max($w["windSpeed"], $maxwind); + $precipchance = max($w["precipProbability"], $precipchance); + $maxcloudcover = max($w["cloudCover"] * 100.0, $maxcloudcover); + $cloudcovers[] = $w["cloudCover"]; + $lasttimestamp = max($w["time"], $lasttimestamp); } $avgcloudcover = array_sum($cloudcovers) / count($cloudcovers); @@ -106,45 +106,35 @@ if ($precipchance > 0.2) { $oldicon = "partly-cloudy"; } -if (((string) $weather["current"]["weather"][0]["id"])[0] == "2") { +if ($weather["currently"]["icon"] == "rain") { $nowicon = "rain"; -} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "3") { - $nowicon = "rain"; -} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "5") { - $nowicon = "rain"; -} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "6") { +} else if ($weather["currently"]["icon"] == "snow") { $nowicon = "snow"; -} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "7") { +} else if ($weather["currently"]["icon"] == "fog") { $nowicon = "fog"; - if ($weather["current"]["weather"][0]["id"] == 781) { - // Tornado!!! - $nowicon = "hazard"; - } -} else if ($weather["current"]["weather"][0]["id"] == 801) { - $nowicon = "mostly-sunny"; -} else if ($weather["current"]["weather"][0]["id"] == 802) { +} else if ($weather["currently"]["icon"] == "partly-cloudy-day" || $weather["currently"]["icon"] == "partly-cloudy-night") { $nowicon = "partly-cloudy"; -} else if ($weather["current"]["weather"][0]["id"] >= 803) { +} else if ($weather["currently"]["icon"] == "cloudy") { $nowicon = "cloudy"; } -if (($nowicon == "mostly-sunny" || $nowicon == "clear" || $nowicon == "partly-cloudy") && $weather["current"]["feels_like"] > 80) { +if (($nowicon == "mostly-sunny" || $nowicon == "clear" || $nowicon == "partly-cloudy") && $weather["currently"]["apparentTemperature"] > 80) { $nowicon = "heat"; } $dailyforecast = []; -foreach ($weather["daily"] as $w) { +foreach ($weather["daily"]["data"] as $w) { $dailyforecast[] = [ - "date" => $w["dt"], + "date" => $w["time"], "temp" => [ - "min" => $w["temp"]["min"], - "max" => $w["temp"]["max"] + "min" => $w["temperatureLow"], + "max" => $w["temperatureHigh"] ], "precipitation" => [ - "chance" => empty($w["pop"]) ? 0 : $w["pop"] + "chance" => empty($w["precipProbability"]) ? 0 : $w["precipProbability"] ], - "windspeed" => $w["wind_speed"], - "uv_index" => round($w["uvi"], 1) + "windspeed" => $w["windSpeed"], + "uv_index" => round($w["uvIndex"], 1) ]; } @@ -205,11 +195,11 @@ $output = [ ], "now" => [ "icon" => $nowicon, - "temp" => $weather["current"]["temp"], - "feelslike" => $weather["current"]["feels_like"], - "uv_index" => (is_null($epa_uv_index) ? min(round($weather["current"]["uvi"], 1), 11) : $epa_uv_index), - "windspeed" => $weather["current"]["wind_speed"], - "winddirection" => $weather["current"]["wind_deg"] + "temp" => $weather["currently"]["temperature"], + "feelslike" => $weather["currently"]["apparentTemperature"], + "uv_index" => (is_null($epa_uv_index) ? min(round($weather["currently"]["uvIndex"], 1), 11) : $epa_uv_index), + "windspeed" => $weather["currently"]["windSpeed"], + "winddirection" => $weather["currently"]["windBearing"] ], "today" => [ "minutely" => $minutely, @@ -236,8 +226,8 @@ $output = [ "latitude" => $lat, "longitude" => $lon, "source" => [ - "text" => "Data: OpenWeatherMap.org" . (is_null($epa_uv_index) ? "" : ", EPA"), - "url" => "https://openweathermap.org" + "text" => "Powered by Dark Sky" . (is_null($epa_uv_index) ? "" : ", EPA"), + "url" => "https://darksky.net/poweredby/" ] ]; diff --git a/endpoints/gis.weather.php b/endpoints/gis.weather.php index 42a487b..1f1fc12 100644 --- a/endpoints/gis.weather.php +++ b/endpoints/gis.weather.php @@ -19,15 +19,15 @@ if (empty($VARS["nocache"])) { } } -$json = file_get_contents("https://api.darksky.net/forecast/" . env("darksky_apikey", "") . "/$lat,$lon?units=us&exclude=alerts,flags"); +$json = file_get_contents("https://api.openweathermap.org/data/2.5/onecall?lat=$lat&lon=$lon&units=imperial&appid=" . env("openweathermap_appid", "")); $weather = json_decode($json, TRUE); $minutely = []; -foreach ($weather["minutely"]["data"] as $m) { +foreach ($weather["minutely"] as $m) { $minutely[] = [ - "time" => $m["time"], - "precip" => round($m["precipIntensity"], 3) + "time" => $m["dt"], + "precip" => round($m["precipitation"] / 25.4, 3) ]; } $hourly = []; @@ -43,42 +43,42 @@ $oldicon = "clear"; $nowicon = "clear"; $lasttimestamp = time(); -foreach ($weather["hourly"]["data"] as $w) { - if ($w["time"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) { +foreach ($weather["hourly"] as $w) { + if ($w["dt"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) { continue; } $hourly[] = [ - "time" => $w["time"], - "temp" => $w["temperature"], - "feelslike" => $w["apparentTemperature"], - "windspeed" => $w["windSpeed"], - "winddirection" => $w["windBearing"], - "precipchance" => $w["precipProbability"] + "time" => $w["dt"], + "temp" => $w["temp"], + "feelslike" => $w["feels_like"], + "windspeed" => $w["wind_speed"], + "winddirection" => $w["wind_deg"], + "precipchance" => $w["pop"] ]; if ($mintemp === false) { - $mintemp = $w["temperature"]; + $mintemp = $w["temp"]; } if ($maxtemp === false) { - $maxtemp = $w["temperature"]; + $maxtemp = $w["temp"]; } if ($maxwind === false) { - $maxwind = $w["windSpeed"]; + $maxwind = $w["wind_speed"]; } if ($precipchance === false) { - $precipchance = $w["precipProbability"]; + $precipchance = $w["pop"]; } if ($maxcloudcover === false) { - $maxcloudcover = $w["cloudCover"] * 100.0; + $maxcloudcover = $w["clouds"]; } - $mintemp = min($w["temperature"], $mintemp); - $maxtemp = max($w["temperature"], $maxtemp); - $maxwind = max($w["windSpeed"], $maxwind); - $precipchance = max($w["precipProbability"], $precipchance); - $maxcloudcover = max($w["cloudCover"] * 100.0, $maxcloudcover); - $cloudcovers[] = $w["cloudCover"]; - $lasttimestamp = max($w["time"], $lasttimestamp); + $mintemp = min($w["temp"], $mintemp); + $maxtemp = max($w["temp"], $maxtemp); + $maxwind = max($w["wind_speed"], $maxwind); + $precipchance = max($w["pop"], $precipchance); + $maxcloudcover = max($w["clouds"], $maxcloudcover); + $cloudcovers[] = $w["clouds"]; + $lasttimestamp = max($w["dt"], $lasttimestamp); } $avgcloudcover = array_sum($cloudcovers) / count($cloudcovers); @@ -106,35 +106,45 @@ if ($precipchance > 0.2) { $oldicon = "partly-cloudy"; } -if ($weather["currently"]["icon"] == "rain") { +if (((string) $weather["current"]["weather"][0]["id"])[0] == "2") { $nowicon = "rain"; -} else if ($weather["currently"]["icon"] == "snow") { +} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "3") { + $nowicon = "rain"; +} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "5") { + $nowicon = "rain"; +} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "6") { $nowicon = "snow"; -} else if ($weather["currently"]["icon"] == "fog") { +} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "7") { $nowicon = "fog"; -} else if ($weather["currently"]["icon"] == "partly-cloudy-day" || $weather["currently"]["icon"] == "partly-cloudy-night") { + if ($weather["current"]["weather"][0]["id"] == 781) { + // Tornado!!! + $nowicon = "hazard"; + } +} else if ($weather["current"]["weather"][0]["id"] == 801) { + $nowicon = "mostly-sunny"; +} else if ($weather["current"]["weather"][0]["id"] == 802) { $nowicon = "partly-cloudy"; -} else if ($weather["currently"]["icon"] == "cloudy") { +} else if ($weather["current"]["weather"][0]["id"] >= 803) { $nowicon = "cloudy"; } -if (($nowicon == "mostly-sunny" || $nowicon == "clear" || $nowicon == "partly-cloudy") && $weather["currently"]["apparentTemperature"] > 80) { +if (($nowicon == "mostly-sunny" || $nowicon == "clear" || $nowicon == "partly-cloudy") && $weather["current"]["feels_like"] > 80) { $nowicon = "heat"; } $dailyforecast = []; -foreach ($weather["daily"]["data"] as $w) { +foreach ($weather["daily"] as $w) { $dailyforecast[] = [ - "date" => $w["time"], + "date" => $w["dt"], "temp" => [ - "min" => $w["temperatureLow"], - "max" => $w["temperatureHigh"] + "min" => $w["temp"]["min"], + "max" => $w["temp"]["max"] ], "precipitation" => [ - "chance" => empty($w["precipProbability"]) ? 0 : $w["precipProbability"] + "chance" => empty($w["pop"]) ? 0 : $w["pop"] ], - "windspeed" => $w["windSpeed"], - "uv_index" => round($w["uvIndex"], 1) + "windspeed" => $w["wind_speed"], + "uv_index" => round($w["uvi"], 1) ]; } @@ -195,11 +205,11 @@ $output = [ ], "now" => [ "icon" => $nowicon, - "temp" => $weather["currently"]["temperature"], - "feelslike" => $weather["currently"]["apparentTemperature"], - "uv_index" => (is_null($epa_uv_index) ? min(round($weather["currently"]["uvIndex"], 1), 11) : $epa_uv_index), - "windspeed" => $weather["currently"]["windSpeed"], - "winddirection" => $weather["currently"]["windBearing"] + "temp" => $weather["current"]["temp"], + "feelslike" => $weather["current"]["feels_like"], + "uv_index" => (is_null($epa_uv_index) ? min(round($weather["current"]["uvi"], 1), 11) : $epa_uv_index), + "windspeed" => $weather["current"]["wind_speed"], + "winddirection" => $weather["current"]["wind_deg"] ], "today" => [ "minutely" => $minutely, @@ -226,8 +236,8 @@ $output = [ "latitude" => $lat, "longitude" => $lon, "source" => [ - "text" => "Powered by Dark Sky" . (is_null($epa_uv_index) ? "" : ", EPA"), - "url" => "https://darksky.net/poweredby/" + "text" => "Data: OpenWeatherMap.org" . (is_null($epa_uv_index) ? "" : ", EPA"), + "url" => "https://openweathermap.org" ] ];