2015-06-03 20:07:06 +00:00
|
|
|
"""Transform name location to GPS coordinates"""
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-11-30 23:49:09 +00:00
|
|
|
# PYTHON STUFFS #######################################################
|
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import quote
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
2015-06-03 20:07:06 +00:00
|
|
|
from nemubot.hooks import hook
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.tools import web
|
2014-08-15 21:32:19 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2015-11-30 23:49:09 +00:00
|
|
|
# GLOBALS #############################################################
|
|
|
|
|
2015-06-03 20:07:06 +00:00
|
|
|
URL_API = "http://open.mapquestapi.com/geocoding/v1/address?key=%s&location=%%s"
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2015-11-30 23:49:09 +00:00
|
|
|
|
|
|
|
# LOADING #############################################################
|
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
def load(context):
|
2015-10-27 17:03:28 +00:00
|
|
|
if not context.config or "apikey" not in context.config:
|
2015-06-03 20:07:06 +00:00
|
|
|
raise ImportError("You need a MapQuest API key in order to use this "
|
|
|
|
"module. Add it to the module configuration file:\n"
|
|
|
|
"<module name=\"mapquest\" key=\"XXXXXXXXXXXXXXXX\" "
|
|
|
|
"/>\nRegister at http://developer.mapquest.com/")
|
|
|
|
global URL_API
|
|
|
|
URL_API = URL_API % context.config["apikey"].replace("%", "%%")
|
2014-08-15 21:32:19 +00:00
|
|
|
|
|
|
|
|
2015-11-30 23:49:09 +00:00
|
|
|
# MODULE CORE #########################################################
|
2014-08-15 21:32:19 +00:00
|
|
|
|
|
|
|
def geocode(location):
|
2015-06-03 20:07:06 +00:00
|
|
|
obj = web.getJSON(URL_API % quote(location))
|
2014-08-15 21:32:19 +00:00
|
|
|
|
|
|
|
if "results" in obj and "locations" in obj["results"][0]:
|
|
|
|
for loc in obj["results"][0]["locations"]:
|
|
|
|
yield loc
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
def where(loc):
|
2014-12-17 06:32:34 +00:00
|
|
|
return re.sub(" +", " ",
|
|
|
|
"{street} {adminArea5} {adminArea4} {adminArea3} "
|
|
|
|
"{adminArea1}".format(**loc)).strip()
|
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
|
2015-11-30 23:49:09 +00:00
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
|
|
|
@hook.command("geocode",
|
|
|
|
help="Get GPS coordinates of a place",
|
|
|
|
help_usage={
|
|
|
|
"PLACE": "Get GPS coordinates of PLACE"
|
|
|
|
})
|
2014-08-15 21:32:19 +00:00
|
|
|
def cmd_geocode(msg):
|
2015-06-03 20:07:06 +00:00
|
|
|
if not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("indicate a name")
|
2014-08-15 21:32:19 +00:00
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
res = Response(channel=msg.channel, nick=msg.nick,
|
|
|
|
nomore="No more geocode", count=" (%s more geocode)")
|
|
|
|
|
2015-06-03 20:07:06 +00:00
|
|
|
for loc in geocode(' '.join(msg.args)):
|
2014-12-17 06:32:34 +00:00
|
|
|
res.append_message("%s is at %s,%s (%s precision)" %
|
|
|
|
(where(loc),
|
|
|
|
loc["latLng"]["lat"],
|
|
|
|
loc["latLng"]["lng"],
|
|
|
|
loc["geocodeQuality"].lower()))
|
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
return res
|