24h-Vorhersage für forecast
This commit is contained in:
parent
2e5d9b97bf
commit
4fa5abe1e4
@ -46,11 +46,17 @@ local function get_condition_symbol(weather, n)
|
||||
end
|
||||
end
|
||||
|
||||
local function get_temp(weather, n)
|
||||
local function get_temp(weather, n, hourly)
|
||||
if hourly then
|
||||
local temperature = round(weather.data[n].temperature, 1)
|
||||
local condition = weather.data[n].summary
|
||||
return temperature..'°C | '..get_condition_symbol(weather, n)..' '..condition
|
||||
else
|
||||
local day = round(weather.data[n].temperatureMax, 1)
|
||||
local night = round(weather.data[n].temperatureMin, 1)
|
||||
local condition = weather.data[n].summary
|
||||
return '☀️ '..day..'°C | 🌙 '..night..'°C | '..get_condition_symbol(weather, n)..' '..condition
|
||||
end
|
||||
end
|
||||
|
||||
local function get_forecast(lat, lng)
|
||||
@ -82,21 +88,65 @@ local function get_forecast(lat, lng)
|
||||
|
||||
for day in pairs(weather.data) do
|
||||
if day > 2 then
|
||||
text = text..'\n'..convert_timestamp(weather.data[day].time, '%d.%m')..': '..get_temp(weather, day)
|
||||
text = text..'\n'..convert_timestamp(weather.data[day].time, '%a, %d.%m')..': '..get_temp(weather, day)
|
||||
end
|
||||
end
|
||||
|
||||
local text = string.gsub(text, "Mon", "Mo")
|
||||
local text = string.gsub(text, "Tue", "Di")
|
||||
local text = string.gsub(text, "Wed", "Mi")
|
||||
local text = string.gsub(text, "Thu", "Do")
|
||||
local text = string.gsub(text, "Fri", "Fr")
|
||||
local text = string.gsub(text, "Sat", "Sa")
|
||||
local text = string.gsub(text, "Sun", "So")
|
||||
|
||||
cache_data('forecast', lat..','..lng, header..text, tonumber(ttl), 'key')
|
||||
|
||||
return header..text
|
||||
end
|
||||
|
||||
local function get_forecast_hourly(lat, lng)
|
||||
print('Finde stündliches Wetter in '..lat..', '..lng)
|
||||
local text = redis:get('telegram:cache:forecast:'..lat..','..lng..':hourly')
|
||||
if text then print('...aus dem Cache..') return text end
|
||||
|
||||
local url = BASE_URL..'/'..apikey..'/'..lat..','..lng..'?lang=de&units=si&exclude=currently,minutely,daily,alerts,flags'
|
||||
|
||||
local response_body = {}
|
||||
local request_constructor = {
|
||||
url = url,
|
||||
method = "GET",
|
||||
sink = ltn12.sink.table(response_body)
|
||||
}
|
||||
local ok, response_code, response_headers, response_status_line = https.request(request_constructor)
|
||||
if not ok then return nil end
|
||||
local data = json:decode(table.concat(response_body))
|
||||
local ttl = string.sub(response_headers["cache-control"], 9)
|
||||
|
||||
|
||||
local weather = data.hourly
|
||||
local city = get_city_name(lat, lng)
|
||||
|
||||
local header = '24-Stunden-Vorhersage für '..city..':\n'..weather.summary
|
||||
local text = ""
|
||||
|
||||
for hour in pairs(weather.data) do
|
||||
if hour < 26 then
|
||||
text = text..'\n'..convert_timestamp(weather.data[hour].time, '%H:%M Uhr')..' | '..get_temp(weather, hour, true)
|
||||
end
|
||||
end
|
||||
|
||||
cache_data('forecast', lat..','..lng..':hourly', header..text, tonumber(ttl), 'key')
|
||||
|
||||
return header..text
|
||||
end
|
||||
|
||||
local function run(msg, matches)
|
||||
local user_id = msg.from.id
|
||||
local city = get_location(user_id)
|
||||
|
||||
if matches[1] ~= '/forecast' and matches[1] ~= '/f' then
|
||||
city = matches[1]
|
||||
if matches[2] then
|
||||
city = matches[2]
|
||||
else
|
||||
local set_location = get_location(user_id)
|
||||
if not set_location then
|
||||
@ -120,7 +170,11 @@ local function run(msg, matches)
|
||||
redis:hset('telegram:cache:weather:'..string.lower(city), 'lat', lat)
|
||||
redis:hset('telegram:cache:weather:'..string.lower(city), 'lng', lng)
|
||||
|
||||
local text = get_forecast(lat, lng)
|
||||
if matches[1] == '/forecasth' or matches[1] == '/fh' then
|
||||
text = get_forecast_hourly(lat, lng)
|
||||
else
|
||||
text = get_forecast(lat, lng)
|
||||
end
|
||||
if not text then
|
||||
text = 'Konnte die Wettervorhersage für diese Stadt nicht bekommen.'
|
||||
end
|
||||
@ -130,14 +184,20 @@ end
|
||||
return {
|
||||
description = "Wettervorhersage für deinen oder einen gewählten Ort",
|
||||
usage = {
|
||||
"/forecast: Wettervorhersage für deine Stadt (/location set [Ort])",
|
||||
"/forecast (Stadt): Wettervorhersage für diese Stadt"
|
||||
"/f: Wettervorhersage für deine Stadt (/location set [Ort])",
|
||||
"/f (Stadt): Wettervorhersage für diese Stadt",
|
||||
"/fh: 24-Stunden-Wettervorhersage für deine Stadt (/location set [Ort])",
|
||||
"/fh (Stadt): 24-Stunden-Wettervorhersage für diese Stadt"
|
||||
},
|
||||
patterns = {
|
||||
"^/f$",
|
||||
"^/f (.*)$",
|
||||
"^/forecast$",
|
||||
"^/forecast (.*)$"
|
||||
"^(/f)$",
|
||||
"^(/f) (.*)$",
|
||||
"^(/fh)$",
|
||||
"^(/fh) (.*)$",
|
||||
"^(/forecast)$",
|
||||
"^(/forecast) (.*)$",
|
||||
"^(/forecasth)$",
|
||||
"^(/forecasth) (.*)$"
|
||||
},
|
||||
run = run
|
||||
}
|
||||
|
Reference in New Issue
Block a user