You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
2.5 KiB

<?php
define('INIT', true);
require_once 'config.php';
function e($s) {
return htmlspecialchars($s);
}
function wc() {
global $CFG;
return $CFG['web_context'];
}
if (!isset($_GET['location'])) {
require_once 'search.html.php';
exit;
}
$location = $_GET['location'];
$apicall = 'https://api.openweathermap.org/data/2.5/forecast?'
. 'q=' . urlencode($location)
. '&appid=' . urlencode($CFG['api_key']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apicall);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$body = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode !== 200) {
$CFG['failure'] = $httpcode;
require_once 'search.html.php';
exit;
}
$data = json_decode($body);
$DATA = [
'city' => $data->city->name . ', ' . $data->city->country,
'population' => $data->city->population,
'sunrise' => date('H:i:s', $data->city->sunrise),
'sunset' => date('H:i:s', $data->city->sunset)
];
$icons = [
//'01d' => "&#D83C;&#DF1E;",
'01d' => '🌞',
'01n' => '🌚',
'02d' => "🌤",
'02n' => "🌤",
'03d' => "🌥",
'03n' => "🌥",
'04d' => "",
'04n' => "",
'09d' => "🌦",
'09n' => "🌦",
'10d' => "🌧",
'10n' => "🌧",
'11d' => "🌩",
'11n' => "🌩",
'13d' => "",
'13n' => "",
'50d' => "🌫",
'50n' => "🌫"
];
$list = [];
foreach ($data->list as $line) {
$temp = $line->main->temp - 273.15;
$date = date('D. d.m. H:i', $line->dt);
$icon = $icons[$line->weather[0]->icon];
$rainArr = (array) ($line->rain ?? []);
$snowArr = (array) ($line->snow ?? []);
$rain = ($rainArr['3h'] ?? 0) + ($snowArr['3h'] ?? 0);
$wind = $line->wind->speed;
$flying = $rain === 0 && $wind < 2.5 ? 'Y'
: $rain <= 0.3 && $wind < 3.1 ? 'M'
: 'N';
$flyingIcon = $flying === 'Y' ? "🛩"
: $flying === 'M' ? "🛩"
: '⏚';
$riding = $rain === 0 && $temp > 15 ? 'Y'
: $rain <= 0.3 && $temp > 10 ? 'M'
: 'N';
$ridingIcon = $riding === 'Y' ? "🏍"
: $riding === 'M' ? "🏍"
: "🚳";
$list[] = [
'temp' => $temp,
'date' => $date,
'icon' => $icon,
'rain' => $rain,
'wind' => $wind,
'flying' => $flying,
'flyingIcon' => $flyingIcon,
'riding' => $riding,
'ridingIcon' => $ridingIcon
];
}
$lastDay = substr($list[0]['date'], 0, 11);
$dayGroup = [];
$grouped = [];
foreach($list as $line) {
$dayStr = substr($line['date'], 0, 11);
if ($dayStr === $lastDay) {
$dayGroup[] = $line;
} else {
$grouped[] = $dayGroup;
$dayGroup = [];
$lastDay = $dayStr;
}
}
$grouped[] = $dayGroup;
require_once 'display.html.php';