From 4e0d717adcfca84533747df9646d0d702a5b68b2 Mon Sep 17 00:00:00 2001 From: topkecleon Date: Tue, 4 Oct 2016 13:30:22 -0400 Subject: [PATCH] gMaps.lua -> location.lua Also moved some stuff from where it shouldn't be. --- config.lua | 2 +- otouto/plugins/{gMaps.lua => location.lua} | 14 ++++++++----- otouto/plugins/time.lua | 11 +++++++---- otouto/plugins/weather.lua | 11 +++++++---- otouto/utilities.lua | 23 +++++++++------------- 5 files changed, 33 insertions(+), 28 deletions(-) rename otouto/plugins/{gMaps.lua => location.lua} (72%) diff --git a/config.lua b/config.lua index ccbc4ce..f2910af 100644 --- a/config.lua +++ b/config.lua @@ -162,7 +162,7 @@ Send /help to get started. 'dice', 'echo', 'eightball', - 'gMaps', + 'location', 'hackernews', 'imdb', 'nick', diff --git a/otouto/plugins/gMaps.lua b/otouto/plugins/location.lua similarity index 72% rename from otouto/plugins/gMaps.lua rename to otouto/plugins/location.lua index 38a1967..299577f 100644 --- a/otouto/plugins/gMaps.lua +++ b/otouto/plugins/location.lua @@ -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 diff --git a/otouto/plugins/time.lua b/otouto/plugins/time.lua index d42116c..3b5a729 100644 --- a/otouto/plugins/time.lua +++ b/otouto/plugins/time.lua @@ -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) diff --git a/otouto/plugins/weather.lua b/otouto/plugins/weather.lua index ec0b81a..75048f4 100644 --- a/otouto/plugins/weather.lua +++ b/otouto/plugins/weather.lua @@ -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 diff --git a/otouto/utilities.lua b/otouto/utilities.lua index e02fc31..bde8c17 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -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)