Replace Dark Sky with OpenWeatherMap

master
Skylar Ittner 1 year ago
parent ea0d7068de
commit 9f8b1fa943

@ -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); $weather = json_decode($json, TRUE);
$minutely = []; $minutely = [];
foreach ($weather["minutely"] as $m) { foreach ($weather["minutely"]["data"] as $m) {
$minutely[] = [ $minutely[] = [
"time" => $m["dt"], "time" => $m["time"],
"precip" => round($m["precipitation"] / 25.4, 3) "precip" => round($m["precipIntensity"], 3)
]; ];
} }
$hourly = []; $hourly = [];
@ -43,42 +43,42 @@ $oldicon = "clear";
$nowicon = "clear"; $nowicon = "clear";
$lasttimestamp = time(); $lasttimestamp = time();
foreach ($weather["hourly"] as $w) { foreach ($weather["hourly"]["data"] as $w) {
if ($w["dt"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) { if ($w["time"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) {
continue; continue;
} }
$hourly[] = [ $hourly[] = [
"time" => $w["dt"], "time" => $w["time"],
"temp" => $w["temp"], "temp" => $w["temperature"],
"feelslike" => $w["feels_like"], "feelslike" => $w["apparentTemperature"],
"windspeed" => $w["wind_speed"], "windspeed" => $w["windSpeed"],
"winddirection" => $w["wind_deg"], "winddirection" => $w["windBearing"],
"precipchance" => $w["pop"] "precipchance" => $w["precipProbability"]
]; ];
if ($mintemp === false) { if ($mintemp === false) {
$mintemp = $w["temp"]; $mintemp = $w["temperature"];
} }
if ($maxtemp === false) { if ($maxtemp === false) {
$maxtemp = $w["temp"]; $maxtemp = $w["temperature"];
} }
if ($maxwind === false) { if ($maxwind === false) {
$maxwind = $w["wind_speed"]; $maxwind = $w["windSpeed"];
} }
if ($precipchance === false) { if ($precipchance === false) {
$precipchance = $w["pop"]; $precipchance = $w["precipProbability"];
} }
if ($maxcloudcover === false) { if ($maxcloudcover === false) {
$maxcloudcover = $w["clouds"]; $maxcloudcover = $w["cloudCover"] * 100.0;
} }
$mintemp = min($w["temp"], $mintemp); $mintemp = min($w["temperature"], $mintemp);
$maxtemp = max($w["temp"], $maxtemp); $maxtemp = max($w["temperature"], $maxtemp);
$maxwind = max($w["wind_speed"], $maxwind); $maxwind = max($w["windSpeed"], $maxwind);
$precipchance = max($w["pop"], $precipchance); $precipchance = max($w["precipProbability"], $precipchance);
$maxcloudcover = max($w["clouds"], $maxcloudcover); $maxcloudcover = max($w["cloudCover"] * 100.0, $maxcloudcover);
$cloudcovers[] = $w["clouds"]; $cloudcovers[] = $w["cloudCover"];
$lasttimestamp = max($w["dt"], $lasttimestamp); $lasttimestamp = max($w["time"], $lasttimestamp);
} }
$avgcloudcover = array_sum($cloudcovers) / count($cloudcovers); $avgcloudcover = array_sum($cloudcovers) / count($cloudcovers);
@ -106,45 +106,35 @@ if ($precipchance > 0.2) {
$oldicon = "partly-cloudy"; $oldicon = "partly-cloudy";
} }
if (((string) $weather["current"]["weather"][0]["id"])[0] == "2") { if ($weather["currently"]["icon"] == "rain") {
$nowicon = "rain"; $nowicon = "rain";
} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "3") { } else if ($weather["currently"]["icon"] == "snow") {
$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"; $nowicon = "snow";
} else if (((string) $weather["current"]["weather"][0]["id"])[0] == "7") { } else if ($weather["currently"]["icon"] == "fog") {
$nowicon = "fog"; $nowicon = "fog";
if ($weather["current"]["weather"][0]["id"] == 781) { } else if ($weather["currently"]["icon"] == "partly-cloudy-day" || $weather["currently"]["icon"] == "partly-cloudy-night") {
// 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"; $nowicon = "partly-cloudy";
} else if ($weather["current"]["weather"][0]["id"] >= 803) { } else if ($weather["currently"]["icon"] == "cloudy") {
$nowicon = "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"; $nowicon = "heat";
} }
$dailyforecast = []; $dailyforecast = [];
foreach ($weather["daily"] as $w) { foreach ($weather["daily"]["data"] as $w) {
$dailyforecast[] = [ $dailyforecast[] = [
"date" => $w["dt"], "date" => $w["time"],
"temp" => [ "temp" => [
"min" => $w["temp"]["min"], "min" => $w["temperatureLow"],
"max" => $w["temp"]["max"] "max" => $w["temperatureHigh"]
], ],
"precipitation" => [ "precipitation" => [
"chance" => empty($w["pop"]) ? 0 : $w["pop"] "chance" => empty($w["precipProbability"]) ? 0 : $w["precipProbability"]
], ],
"windspeed" => $w["wind_speed"], "windspeed" => $w["windSpeed"],
"uv_index" => round($w["uvi"], 1) "uv_index" => round($w["uvIndex"], 1)
]; ];
} }
@ -205,11 +195,11 @@ $output = [
], ],
"now" => [ "now" => [
"icon" => $nowicon, "icon" => $nowicon,
"temp" => $weather["current"]["temp"], "temp" => $weather["currently"]["temperature"],
"feelslike" => $weather["current"]["feels_like"], "feelslike" => $weather["currently"]["apparentTemperature"],
"uv_index" => (is_null($epa_uv_index) ? min(round($weather["current"]["uvi"], 1), 11) : $epa_uv_index), "uv_index" => (is_null($epa_uv_index) ? min(round($weather["currently"]["uvIndex"], 1), 11) : $epa_uv_index),
"windspeed" => $weather["current"]["wind_speed"], "windspeed" => $weather["currently"]["windSpeed"],
"winddirection" => $weather["current"]["wind_deg"] "winddirection" => $weather["currently"]["windBearing"]
], ],
"today" => [ "today" => [
"minutely" => $minutely, "minutely" => $minutely,
@ -236,8 +226,8 @@ $output = [
"latitude" => $lat, "latitude" => $lat,
"longitude" => $lon, "longitude" => $lon,
"source" => [ "source" => [
"text" => "Data: OpenWeatherMap.org" . (is_null($epa_uv_index) ? "" : ", EPA"), "text" => "Powered by Dark Sky" . (is_null($epa_uv_index) ? "" : ", EPA"),
"url" => "https://openweathermap.org" "url" => "https://darksky.net/poweredby/"
] ]
]; ];

@ -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); $weather = json_decode($json, TRUE);
$minutely = []; $minutely = [];
foreach ($weather["minutely"]["data"] as $m) { foreach ($weather["minutely"] as $m) {
$minutely[] = [ $minutely[] = [
"time" => $m["time"], "time" => $m["dt"],
"precip" => round($m["precipIntensity"], 3) "precip" => round($m["precipitation"] / 25.4, 3)
]; ];
} }
$hourly = []; $hourly = [];
@ -43,42 +43,42 @@ $oldicon = "clear";
$nowicon = "clear"; $nowicon = "clear";
$lasttimestamp = time(); $lasttimestamp = time();
foreach ($weather["hourly"]["data"] as $w) { foreach ($weather["hourly"] as $w) {
if ($w["time"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) { if ($w["dt"] > strtotime("now + " . env("weather_summary_hours", 8) . " hours")) {
continue; continue;
} }
$hourly[] = [ $hourly[] = [
"time" => $w["time"], "time" => $w["dt"],
"temp" => $w["temperature"], "temp" => $w["temp"],
"feelslike" => $w["apparentTemperature"], "feelslike" => $w["feels_like"],
"windspeed" => $w["windSpeed"], "windspeed" => $w["wind_speed"],
"winddirection" => $w["windBearing"], "winddirection" => $w["wind_deg"],
"precipchance" => $w["precipProbability"] "precipchance" => $w["pop"]
]; ];
if ($mintemp === false) { if ($mintemp === false) {
$mintemp = $w["temperature"]; $mintemp = $w["temp"];
} }
if ($maxtemp === false) { if ($maxtemp === false) {
$maxtemp = $w["temperature"]; $maxtemp = $w["temp"];
} }
if ($maxwind === false) { if ($maxwind === false) {
$maxwind = $w["windSpeed"]; $maxwind = $w["wind_speed"];
} }
if ($precipchance === false) { if ($precipchance === false) {
$precipchance = $w["precipProbability"]; $precipchance = $w["pop"];
} }
if ($maxcloudcover === false) { if ($maxcloudcover === false) {
$maxcloudcover = $w["cloudCover"] * 100.0; $maxcloudcover = $w["clouds"];
} }
$mintemp = min($w["temperature"], $mintemp); $mintemp = min($w["temp"], $mintemp);
$maxtemp = max($w["temperature"], $maxtemp); $maxtemp = max($w["temp"], $maxtemp);
$maxwind = max($w["windSpeed"], $maxwind); $maxwind = max($w["wind_speed"], $maxwind);
$precipchance = max($w["precipProbability"], $precipchance); $precipchance = max($w["pop"], $precipchance);
$maxcloudcover = max($w["cloudCover"] * 100.0, $maxcloudcover); $maxcloudcover = max($w["clouds"], $maxcloudcover);
$cloudcovers[] = $w["cloudCover"]; $cloudcovers[] = $w["clouds"];
$lasttimestamp = max($w["time"], $lasttimestamp); $lasttimestamp = max($w["dt"], $lasttimestamp);
} }
$avgcloudcover = array_sum($cloudcovers) / count($cloudcovers); $avgcloudcover = array_sum($cloudcovers) / count($cloudcovers);
@ -106,35 +106,45 @@ if ($precipchance > 0.2) {
$oldicon = "partly-cloudy"; $oldicon = "partly-cloudy";
} }
if ($weather["currently"]["icon"] == "rain") { if (((string) $weather["current"]["weather"][0]["id"])[0] == "2") {
$nowicon = "rain"; $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"; $nowicon = "snow";
} else if ($weather["currently"]["icon"] == "fog") { } else if (((string) $weather["current"]["weather"][0]["id"])[0] == "7") {
$nowicon = "fog"; $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"; $nowicon = "partly-cloudy";
} else if ($weather["currently"]["icon"] == "cloudy") { } else if ($weather["current"]["weather"][0]["id"] >= 803) {
$nowicon = "cloudy"; $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"; $nowicon = "heat";
} }
$dailyforecast = []; $dailyforecast = [];
foreach ($weather["daily"]["data"] as $w) { foreach ($weather["daily"] as $w) {
$dailyforecast[] = [ $dailyforecast[] = [
"date" => $w["time"], "date" => $w["dt"],
"temp" => [ "temp" => [
"min" => $w["temperatureLow"], "min" => $w["temp"]["min"],
"max" => $w["temperatureHigh"] "max" => $w["temp"]["max"]
], ],
"precipitation" => [ "precipitation" => [
"chance" => empty($w["precipProbability"]) ? 0 : $w["precipProbability"] "chance" => empty($w["pop"]) ? 0 : $w["pop"]
], ],
"windspeed" => $w["windSpeed"], "windspeed" => $w["wind_speed"],
"uv_index" => round($w["uvIndex"], 1) "uv_index" => round($w["uvi"], 1)
]; ];
} }
@ -195,11 +205,11 @@ $output = [
], ],
"now" => [ "now" => [
"icon" => $nowicon, "icon" => $nowicon,
"temp" => $weather["currently"]["temperature"], "temp" => $weather["current"]["temp"],
"feelslike" => $weather["currently"]["apparentTemperature"], "feelslike" => $weather["current"]["feels_like"],
"uv_index" => (is_null($epa_uv_index) ? min(round($weather["currently"]["uvIndex"], 1), 11) : $epa_uv_index), "uv_index" => (is_null($epa_uv_index) ? min(round($weather["current"]["uvi"], 1), 11) : $epa_uv_index),
"windspeed" => $weather["currently"]["windSpeed"], "windspeed" => $weather["current"]["wind_speed"],
"winddirection" => $weather["currently"]["windBearing"] "winddirection" => $weather["current"]["wind_deg"]
], ],
"today" => [ "today" => [
"minutely" => $minutely, "minutely" => $minutely,
@ -226,8 +236,8 @@ $output = [
"latitude" => $lat, "latitude" => $lat,
"longitude" => $lon, "longitude" => $lon,
"source" => [ "source" => [
"text" => "Powered by Dark Sky" . (is_null($epa_uv_index) ? "" : ", EPA"), "text" => "Data: OpenWeatherMap.org" . (is_null($epa_uv_index) ? "" : ", EPA"),
"url" => "https://darksky.net/poweredby/" "url" => "https://openweathermap.org"
] ]
]; ];

Loading…
Cancel
Save