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',
'echo',
'eightball',
'gMaps',
'location',
'hackernews',
'imdb',
'nick',

View File

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

View File

@ -20,15 +20,18 @@ function time:action(msg, config)
return
end
local coords = utilities.get_coords(input, config)
if type(coords) == 'string' then
utilities.send_reply(msg, coords)
local lat, lon = utilities.get_coords(input)
if lat == nil then
utilities.send_reply(msg, config.errors.connection)
return
elseif lat == false then
utilities.send_reply(msg, config.errors.results)
return
end
local now = os.time()
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)
if code ~= 200 then
utilities.send_reply(msg, config.errors.connection)

View File

@ -25,13 +25,16 @@ function weather:action(msg, config)
return
end
local coords = utilities.get_coords(input, config)
if type(coords) == 'string' then
utilities.send_reply(msg, coords)
local lat, lon = utilities.get_coords(input)
if lat == nil then
utilities.send_reply(msg, config.errors.connection)
return
elseif lat == false then
utilities.send_reply(msg, config.errors.results)
return
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)
if res ~= 200 then

View File

@ -123,25 +123,18 @@ function utilities.save_data(filename, data)
end
-- 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 jstr, res = HTTP.request(url)
if res ~= 200 then
return config.errors.connection
return
end
local jdat = JSON.decode(jstr)
if jdat.status == 'ZERO_RESULTS' then
return config.errors.results
return false
end
return {
lat = jdat.results[1].geometry.location.lat,
lon = jdat.results[1].geometry.location.lng
}
return jdat.results[1].geometry.location.lat, jdat.results[1].geometry.location.lng
end
-- Get the number of values in a key/value table.
@ -219,8 +212,10 @@ end
function utilities.md_escape(text)
return text:gsub('_', '\\_')
:gsub('%[', '\\['):gsub('%]', '\\]')
:gsub('%*', '\\*'):gsub('`', '\\`')
:gsub('%[', '\\[')
:gsub('%]', '\\]')
:gsub('%*', '\\*')
:gsub('`', '\\`')
end
function utilities.html_escape(text)