gMaps.lua -> location.lua

Also moved some stuff from where it shouldn't be.
This commit is contained in:
topkecleon 2016-10-04 13:30:22 -04:00
parent 9f760114bd
commit 4e0d717adc
5 changed files with 33 additions and 28 deletions

View File

@ -162,7 +162,7 @@ Send /help to get started.
'dice', 'dice',
'echo', 'echo',
'eightball', 'eightball',
'gMaps', 'location',
'hackernews', 'hackernews',
'imdb', 'imdb',
'nick', 'nick',

View File

@ -23,15 +23,19 @@ function gMaps:action(msg, config)
return return
end end
local coords = utilities.get_coords(input, config) local lat, lon = utilities.get_coords(input)
if type(coords) == 'string' then if lat == nil then
utilities.send_reply(msg, coords) utilities.send_reply(msg, config.errors.connection)
return
elseif lat == false then
utilities.send_reply(msg, config.errors.results)
return
end end
bindings.sendLocation{ bindings.sendLocation{
chat_id = msg.chat.id, chat_id = msg.chat.id,
latitude = coords.lat, latitude = lat,
longitude = coords.lon, longitude = lon,
reply_to_message_id = msg.message_id reply_to_message_id = msg.message_id
} }
end end

View File

@ -20,15 +20,18 @@ function time:action(msg, config)
return return
end end
local coords = utilities.get_coords(input, config) local lat, lon = utilities.get_coords(input)
if type(coords) == 'string' then if lat == nil then
utilities.send_reply(msg, coords) utilities.send_reply(msg, config.errors.connection)
return
elseif lat == false then
utilities.send_reply(msg, config.errors.results)
return return
end end
local now = os.time() local now = os.time()
local utc = os.time(os.date('!*t', now)) local utc = os.time(os.date('!*t', now))
local url = time.base_url:format(coords.lat, coords.lon, utc) local url = time.base_url:format(lat, lon, utc)
local jstr, code = HTTPS.request(url) local jstr, code = HTTPS.request(url)
if code ~= 200 then if code ~= 200 then
utilities.send_reply(msg, config.errors.connection) utilities.send_reply(msg, config.errors.connection)

View File

@ -25,13 +25,16 @@ function weather:action(msg, config)
return return
end end
local coords = utilities.get_coords(input, config) local lat, lon = utilities.get_coords(input)
if type(coords) == 'string' then if lat == nil then
utilities.send_reply(msg, coords) utilities.send_reply(msg, config.errors.connection)
return
elseif lat == false then
utilities.send_reply(msg, config.errors.results)
return return
end end
local url = 'http://api.openweathermap.org/data/2.5/weather?APPID=' .. config.owm_api_key .. '&lat=' .. coords.lat .. '&lon=' .. coords.lon local url = 'http://api.openweathermap.org/data/2.5/weather?APPID=' .. config.owm_api_key .. '&lat=' .. lat .. '&lon=' .. lon
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then

View File

@ -123,25 +123,18 @@ function utilities.save_data(filename, data)
end end
-- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua. -- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua.
function utilities.get_coords(input, config) -- Returns nil for a connection error and false for zero results.
function utilities.get_coords(input)
local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input) local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
local jstr, res = HTTP.request(url) local jstr, res = HTTP.request(url)
if res ~= 200 then if res ~= 200 then
return config.errors.connection return
end end
local jdat = JSON.decode(jstr) local jdat = JSON.decode(jstr)
if jdat.status == 'ZERO_RESULTS' then if jdat.status == 'ZERO_RESULTS' then
return config.errors.results return false
end end
return jdat.results[1].geometry.location.lat, jdat.results[1].geometry.location.lng
return {
lat = jdat.results[1].geometry.location.lat,
lon = jdat.results[1].geometry.location.lng
}
end end
-- Get the number of values in a key/value table. -- Get the number of values in a key/value table.
@ -219,8 +212,10 @@ end
function utilities.md_escape(text) function utilities.md_escape(text)
return text:gsub('_', '\\_') return text:gsub('_', '\\_')
:gsub('%[', '\\['):gsub('%]', '\\]') :gsub('%[', '\\[')
:gsub('%*', '\\*'):gsub('`', '\\`') :gsub('%]', '\\]')
:gsub('%*', '\\*')
:gsub('`', '\\`')
end end
function utilities.html_escape(text) function utilities.html_escape(text)