2014-11-22 10:29:05 +01:00
|
|
|
-- Implement a command !time [area] which uses
|
|
|
|
-- 2 Google APIs to get the desired result:
|
|
|
|
-- 1. Geocoding to get from area to a lat/long pair
|
|
|
|
-- 2. Timezone to get the local time in that lat/long location
|
|
|
|
|
|
|
|
-- Globals
|
2014-11-22 11:59:34 +01:00
|
|
|
-- If you have a google api key for the geocoding/timezone api
|
2014-11-22 14:37:54 +01:00
|
|
|
api_key = config.google_api_key or nil
|
2014-11-22 10:29:05 +01:00
|
|
|
base_api = "https://maps.googleapis.com/maps/api"
|
|
|
|
dateFormat = "%A %d %B - %H:%M:%S"
|
|
|
|
|
|
|
|
-- Need the utc time for the google api
|
|
|
|
function utctime()
|
|
|
|
return os.time(os.date("!*t"))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Use the geocoding api to get the lattitude and longitude with accuracy specifier
|
|
|
|
-- CHECKME: this seems to work without a key??
|
|
|
|
function get_latlong(area)
|
|
|
|
local api = base_api .. "/geocode/json?"
|
|
|
|
local parameters = "address=".. (URL.escape(area) or "")
|
2014-11-22 14:37:54 +01:00
|
|
|
if api_key ~= nil then
|
2014-11-22 10:29:05 +01:00
|
|
|
parameters = parameters .. "&key="..api_key
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Do the request
|
|
|
|
local res, code = https.request(api..parameters)
|
|
|
|
if code ~=200 then return nil end
|
|
|
|
local data = json:decode(res)
|
|
|
|
|
|
|
|
if (data.status == "ZERO_RESULTS") then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if (data.status == "OK") then
|
|
|
|
-- Get the data
|
|
|
|
lat = data.results[1].geometry.location.lat
|
|
|
|
lng = data.results[1].geometry.location.lng
|
|
|
|
acc = data.results[1].geometry.location_type
|
|
|
|
return lat,lng,acc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Use timezone api to get the time in the lat,
|
|
|
|
-- Note: this needs an API key
|
|
|
|
function get_time(lat,lng)
|
|
|
|
local api = base_api .. "/timezone/json?"
|
|
|
|
|
|
|
|
-- Get a timestamp (server time is relevant here)
|
|
|
|
local timestamp = utctime()
|
|
|
|
local parameters = "location=" ..
|
|
|
|
URL.escape(lat) .. "," ..
|
|
|
|
URL.escape(lng) ..
|
|
|
|
"×tamp="..URL.escape(timestamp)
|
|
|
|
if api_key ~=nil then
|
|
|
|
parameters = parameters .. "&key="..api_key
|
|
|
|
end
|
|
|
|
|
|
|
|
local res,code = https.request(api..parameters)
|
|
|
|
if code ~= 200 then return nil end
|
|
|
|
local data = json:decode(res)
|
|
|
|
|
|
|
|
if (data.status == "ZERO_RESULTS") then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if (data.status == "OK") then
|
|
|
|
-- Construct what we want
|
|
|
|
-- The local time in the location is:
|
|
|
|
-- timestamp + rawOffset + dstOffset
|
|
|
|
local localTime = timestamp + data.rawOffset + data.dstOffset
|
2014-11-22 15:59:49 +01:00
|
|
|
return localTime, data.timeZoneId
|
2014-11-22 10:29:05 +01:00
|
|
|
end
|
|
|
|
return localTime
|
|
|
|
end
|
|
|
|
|
|
|
|
function getformattedLocalTime(area)
|
|
|
|
if area == nil then
|
|
|
|
return "The time in nowhere is never"
|
|
|
|
end
|
|
|
|
|
|
|
|
lat,lng,acc = get_latlong(area)
|
2014-11-22 15:59:49 +01:00
|
|
|
if lat == nil and lng == nil then
|
|
|
|
return 'It seems that in "'..area..'" they do not have a concept of time.'
|
2014-11-22 10:29:05 +01:00
|
|
|
end
|
2014-11-22 15:59:49 +01:00
|
|
|
local localTime, timeZoneId = get_time(lat,lng)
|
2014-11-22 10:29:05 +01:00
|
|
|
|
2014-11-22 15:59:49 +01:00
|
|
|
return "The local time in "..timeZoneId.." is: ".. os.date(dateFormat,localTime)
|
2014-11-22 10:29:05 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function run(msg, matches)
|
|
|
|
return getformattedLocalTime(matches[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
description = "Displays the local time in an area",
|
|
|
|
usage = "!time [area]",
|
|
|
|
patterns = {"^!time (.*)$"},
|
|
|
|
run = run
|
|
|
|
}
|