From 1cd7ce1a9ff8f305f7d54e9b8ca674500d0ff77b Mon Sep 17 00:00:00 2001 From: Marcel van der Boom Date: Fri, 19 Dec 2014 16:31:25 +0100 Subject: [PATCH] Improvements for the location plugin - use an api_key if one is configured - send the location by using the api - provide same pin color as standard map - send overview map based on the type of the location scale - remove the temporary file after download --- plugins/location.lua | 64 +++++++++++++++++++++++++++++++++++--------- plugins/time.lua | 3 ++- 2 files changed, 53 insertions(+), 14 deletions(-) diff --git a/plugins/location.lua b/plugins/location.lua index cf3e6b3..7990a15 100644 --- a/plugins/location.lua +++ b/plugins/location.lua @@ -1,3 +1,14 @@ +-- Implement a command !loc [area] which uses +-- the static map API to get a location image + +-- Not sure if this is the proper way +-- Intent: get_latlong is in time.lua, we need it here +-- loadfile "time.lua" + +-- Globals +-- If you have a google api key for the geocoding/timezone api +api_key = config.google_api_key or nil +base_api = "https://maps.googleapis.com/maps/api" function delay_s(delay) delay = delay or 1 @@ -5,25 +16,52 @@ function delay_s(delay) while os.time() < time_to do end end +function get_staticmap(area) + local api = base_api .. "/staticmap?" + + -- Get a sense of scale + lat,lng,acc,types = get_latlong(area) + + local scale=types[1] + if scale=="locality" then zoom=8 + elseif scale=="country" then zoom=4 + else zoom=13 end + + local parameters = + "size=600x300" .. + "&zoom=" .. zoom .. + "¢er=" .. URL.escape(area) .. + "&markers=color:red"..URL.escape("|"..area) + + if api_key ~=nil and api_key ~= "" then + parameters = parameters .. "&key="..api_key + end + return lat, lng, api..parameters +end + + function run(msg, matches) - local loc = string.gsub(matches[1], "%s+", "+") - local receiver = get_receiver(msg) + local receiver = get_receiver(msg) + local lat,lng,url = get_staticmap(matches[1]) + + local file_path = download_to_file(url) - local zooms = {16, 18} + -- Send the actual location, is a google maps link + send_location(receiver, lat, lng, ok_cb, false) + + -- Send a picture of the map, which takes scale into account + send_photo(receiver, file_path, ok_cb, false) + delay_s(2) + + -- Clean up after some time + postpone(rmtmp_cb, file_path, 20.0) - for i = 1, #zooms do - local zoom = zooms[i] - local url = "http://maps.googleapis.com/maps/api/staticmap?zoom=" .. zoom .. "&size=600x300&maptype=roadmap¢er=" .. loc .. "&markers=color:blue%7Clabel:A%7C" .. loc - local file = download_to_file(url) - send_photo(receiver, file, ok_cb, false) - delay_s(2) - end - - return "www.google.es/maps/place/" .. loc + -- Return a link to the google maps stuff is now not needed anymore + return nil end return { - description = "generates a map showing the given location", + description = "Gets information about a location, maplink and overview", usage = "!loc (location)", patterns = {"^!loc (.*)$"}, run = run diff --git a/plugins/time.lua b/plugins/time.lua index b222df5..462a2e2 100644 --- a/plugins/time.lua +++ b/plugins/time.lua @@ -36,7 +36,8 @@ function get_latlong(area) 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 + types= data.results[1].types + return lat,lng,acc,types end end