This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/weather.lua

42 lines
1.2 KiB
Lua
Raw Normal View History

2015-07-03 00:15:52 +02:00
local PLUGIN = {}
PLUGIN.doc = [[
2015-07-08 09:38:04 +02:00
/weather <location>
2015-07-03 00:15:52 +02:00
Returns the current temperature and weather conditions for a specified location.
Non-city locations are accepted; "/weather Buckingham Palace" will return the weather for Westminster.
Results and weather data are powered by Yahoo.
2015-07-03 00:15:52 +02:00
]]
PLUGIN.triggers = {
2015-07-08 09:38:04 +02:00
'^/weather'
2015-07-03 00:15:52 +02:00
}
function PLUGIN.action(msg)
local input = get_input(msg.text)
if not input then
return send_msg(msg, PLUGIN.doc)
end
local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20item.condition%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%281%29%20where%20text%3D%22' .. URL.escape(input) .. '%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys'
2015-07-03 00:15:52 +02:00
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
2015-07-03 00:15:52 +02:00
end
local jdat = JSON.decode(jstr)
if not jdat.query.results then
return send_msg(msg, config.locale.errors.results)
end
local data = jdat.query.results.channel.item.condition
2015-07-03 00:15:52 +02:00
local fahrenheit = data.temp
local celsius = string.format('%.0f', (fahrenheit - 32) * 5/9)
local message = celsius .. '°C | ' .. fahrenheit .. '°F, ' .. data.text .. '.'
2015-07-03 00:15:52 +02:00
send_msg(msg, message)
end
return PLUGIN