2014-08-15 21:32:19 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
"""The mapquest module"""
|
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
import re
|
|
|
|
from urllib.parse import quote
|
2014-12-17 06:32:34 +00:00
|
|
|
|
|
|
|
from tools import web
|
2014-08-15 21:32:19 +00:00
|
|
|
|
|
|
|
nemubotversion = 3.4
|
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
def load(context):
|
|
|
|
if not CONF or not CONF.hasNode("mapquestapi") or not CONF.getNode("mapquestapi").hasAttribute("key"):
|
|
|
|
print ("You need a MapQuest API key in order to use this "
|
|
|
|
"module. Add it to the module configuration file:\n<mapquestapi"
|
|
|
|
" key=\"XXXXXXXXXXXXXXXX\" />\nRegister at "
|
|
|
|
"http://developer.mapquest.com/")
|
|
|
|
return None
|
|
|
|
|
2014-09-11 19:20:56 +00:00
|
|
|
from hooks.messagehook import MessageHook
|
|
|
|
add_hook("cmd_hook", MessageHook(cmd_geocode, "geocode"))
|
2014-08-15 21:32:19 +00:00
|
|
|
|
|
|
|
|
2014-12-17 06:32:34 +00:00
|
|
|
def help_full():
|
2014-08-15 21:32:19 +00:00
|
|
|
return "!geocode /place/: get coordinate of /place/."
|
|
|
|
|
|
|
|
|
|
|
|
def geocode(location):
|
2014-12-17 06:32:34 +00:00
|
|
|
obj = web.getJSON("http://open.mapquestapi.com/geocoding/v1/address?key=%s&location=%s" %
|
|
|
|
(CONF.getNode("mapquestapi")["key"], 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
|
|
|
|
|
|
|
def cmd_geocode(msg):
|
|
|
|
if len(msg.cmds) < 2:
|
|
|
|
raise IRCException("indicate a name")
|
|
|
|
|
|
|
|
locname = ' '.join(msg.cmds[1:])
|
2014-12-17 06:32:34 +00:00
|
|
|
res = Response(channel=msg.channel, nick=msg.nick,
|
|
|
|
nomore="No more geocode", count=" (%s more geocode)")
|
|
|
|
|
2014-08-15 21:32:19 +00:00
|
|
|
for loc in geocode(locname):
|
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
|