Wetter und Forecast Plugin auf forecast.io (by @iCON)
This commit is contained in:
119
plugins/pluginsold/forecast.lua
Normal file
119
plugins/pluginsold/forecast.lua
Normal file
@@ -0,0 +1,119 @@
|
||||
do
|
||||
|
||||
local BASE_URL = "http://api.openweathermap.org/data/2.5/forecast/daily"
|
||||
|
||||
local function get_condition_symbol(weather, n)
|
||||
if weather.list[n].weather[1].main == 'Clear' then
|
||||
return ' ☀'
|
||||
elseif weather.list[n].weather[1].main == 'Clouds' then
|
||||
return ' ☁☁'
|
||||
elseif weather.list[n].weather[1].main == 'Rain' then
|
||||
return ' ☔'
|
||||
elseif weather.list[n].weather[1].main == 'Thunderstorm' then
|
||||
return ' ☔☔☔☔'
|
||||
elseif weather.list[n].weather[1].main == 'Snow' then
|
||||
return ' ❄️'
|
||||
elseif weather.weather[1].main == 'Fog' then
|
||||
return ' 🌫'
|
||||
else
|
||||
return ''
|
||||
end
|
||||
end
|
||||
|
||||
local function get_temp(weather, n)
|
||||
local day = (round(weather.list[n].temp.day, 1))
|
||||
local night = (round(weather.list[n].temp.night, 1))
|
||||
local condition = weather.list[n].weather[1].description
|
||||
return '☀️ '..day..'°C | 🌙 '..night..'°C | '..condition
|
||||
end
|
||||
|
||||
local function get_forecast(location, days)
|
||||
print("Bekomme Wettervorhersage für ", location)
|
||||
local location = string.gsub(location," ","+")
|
||||
local url = BASE_URL
|
||||
local apikey = cred_data.owm_apikey
|
||||
local url = url..'?q='..location
|
||||
local url = url..'&lang=de&units=metric&cnt='..days..'&APPID='..apikey
|
||||
|
||||
local b, c, h = http.request(url)
|
||||
if c ~= 200 then return nil end
|
||||
|
||||
local weather = json:decode(b)
|
||||
local city = weather.city.name
|
||||
if weather.city.country == "" then
|
||||
country = ''
|
||||
else
|
||||
country = ' ('..weather.city.country..')'
|
||||
end
|
||||
local header = 'Vorhersage für '..city..country..':\n'
|
||||
|
||||
local text = 'Heute: '..get_temp(weather, 1)..get_condition_symbol(weather, 1)
|
||||
|
||||
if days > 1 then
|
||||
text = text..'\nMorgen: '..get_temp(weather, 2)..get_condition_symbol(weather, 2)
|
||||
end
|
||||
if days > 2 then
|
||||
text = text..'\nÜbermorgen: '..get_temp(weather, 3)..get_condition_symbol(weather, 3)
|
||||
end
|
||||
|
||||
if days > 3 then
|
||||
for day in pairs(weather.list) do
|
||||
if day > 3 then
|
||||
local actual_day = day-1
|
||||
text = text..'\n'..actual_day..' Tage: '..get_temp(weather, day)..get_condition_symbol(weather, day)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return header..text
|
||||
end
|
||||
|
||||
local function run(msg, matches)
|
||||
local user_id = msg.from.id
|
||||
local city = get_location(user_id)
|
||||
if not city then city = 'Berlin' end
|
||||
|
||||
if tonumber(matches[1]) then
|
||||
days = matches[1]+1
|
||||
else
|
||||
days = 4
|
||||
end
|
||||
|
||||
if matches[2] then
|
||||
days = matches[1]+1
|
||||
city = matches[2]
|
||||
end
|
||||
|
||||
if not tonumber(matches[1]) and matches[1] ~= '/forecast' then
|
||||
city = matches[1]
|
||||
end
|
||||
|
||||
if days > 17 then
|
||||
return 'Wettervorhersagen gehen nur von 1-16 Tagen!'
|
||||
end
|
||||
|
||||
local text = get_forecast(city, days)
|
||||
if not text then
|
||||
text = 'Konnte die Wettervorhersage für diese Stadt nicht bekommen.'
|
||||
end
|
||||
return text
|
||||
end
|
||||
|
||||
return {
|
||||
description = "Wettervorhersage für deinen oder einen gewählten Ort",
|
||||
usage = {
|
||||
"/forecast: Wettervorhersage für deine Stadt (!location set [Ort])",
|
||||
"/forecast [0-16]: Wettervorhersage für X Tage für deine Stadt (!location set [Ort])",
|
||||
"/forecast (Stadt): Wettervorhersage für diese Stadt",
|
||||
"/forecast [0-16] (Stadt): Wettervorhersage für X Tage für diese Stadt"
|
||||
},
|
||||
patterns = {
|
||||
"^/forecast$",
|
||||
"^/forecast (%d+) (.*)$",
|
||||
"^/forecast (%d+)",
|
||||
"^/forecast (.*)$"
|
||||
},
|
||||
run = run
|
||||
}
|
||||
|
||||
end
|
77
plugins/pluginsold/weather.lua
Normal file
77
plugins/pluginsold/weather.lua
Normal file
@@ -0,0 +1,77 @@
|
||||
do
|
||||
|
||||
local BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
|
||||
|
||||
local function get_weather(location)
|
||||
print("Finde Wetter in ", location)
|
||||
local location = string.gsub(location," ","+")
|
||||
local url = BASE_URL
|
||||
local apikey = cred_data.owm_apikey
|
||||
local url = url..'?q='..location
|
||||
local url = url..'&lang=de&units=metric&APPID='..apikey
|
||||
|
||||
local b, c, h = http.request(url)
|
||||
if c ~= 200 then return nil end
|
||||
|
||||
local weather = json:decode(b)
|
||||
local city = weather.name
|
||||
if weather.sys.country == 'none' then
|
||||
country = ''
|
||||
else
|
||||
country = ' ('..weather.sys.country..')'
|
||||
end
|
||||
local temperature = round(weather.main.temp, 1)
|
||||
local temp = 'Wetter in '..city..country..':\n'..temperature..'°C'
|
||||
local conditions = ' | '..weather.weather[1].description
|
||||
if weather.weather[1].main == 'Clear' then
|
||||
conditions = conditions..' ☀'
|
||||
elseif weather.weather[1].main == 'Clouds' then
|
||||
conditions = conditions..' ☁☁'
|
||||
elseif weather.weather[1].main == 'Rain' then
|
||||
conditions = conditions..' ☔'
|
||||
elseif weather.weather[1].main == 'Thunderstorm' then
|
||||
conditions = conditions..' ☔☔☔☔'
|
||||
elseif weather.weather[1].main == 'Snow' then
|
||||
conditions = conditions..' ❄️'
|
||||
elseif weather.weather[1].main == 'Fog' then
|
||||
conditions = conditions..' 🌫'
|
||||
else
|
||||
conditions = conditions..''
|
||||
end
|
||||
return temp..conditions
|
||||
end
|
||||
|
||||
local function run(msg, matches)
|
||||
local user_id = msg.from.id
|
||||
|
||||
if matches[1] ~= '/wetter' then
|
||||
city = matches[1]
|
||||
else
|
||||
local set_location = get_location(user_id)
|
||||
if not set_location then
|
||||
city = 'Berlin'
|
||||
else
|
||||
city = set_location
|
||||
end
|
||||
end
|
||||
local text = get_weather(city)
|
||||
if not text then
|
||||
text = 'Konnte das Wetter von dieser Stadt nicht bekommen.'
|
||||
end
|
||||
return text
|
||||
end
|
||||
|
||||
return {
|
||||
description = "Wetter für deinen oder einen gewählten Ort",
|
||||
usage = {
|
||||
"/wetter: Wetter für deinen Wohnort (!location set [Ort])",
|
||||
"/wetter (Stadt): Wetter für diese Stadt"
|
||||
},
|
||||
patterns = {
|
||||
"^/wetter$",
|
||||
"^/wetter (.*)$"
|
||||
},
|
||||
run = run
|
||||
}
|
||||
|
||||
end
|
Reference in New Issue
Block a user