New mapquest module: can geocode (will help #45)
This commit is contained in:
parent
d0b1336d07
commit
46c8048b53
1 changed files with 49 additions and 0 deletions
49
modules/mapquest.py
Normal file
49
modules/mapquest.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
# coding=utf-8
|
||||||
|
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
from urllib.parse import quote
|
||||||
|
from urllib.request import urlopen
|
||||||
|
|
||||||
|
nemubotversion = 3.4
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
from hooks import Hook
|
||||||
|
add_hook("cmd_hook", Hook(cmd_geocode, "geocode"))
|
||||||
|
|
||||||
|
|
||||||
|
def help_tiny ():
|
||||||
|
"""Line inserted in the response to the command !help"""
|
||||||
|
return "The mapquest module"
|
||||||
|
|
||||||
|
def help_full ():
|
||||||
|
return "!geocode /place/: get coordinate of /place/."
|
||||||
|
|
||||||
|
|
||||||
|
def geocode(location):
|
||||||
|
raw = urlopen("http://open.mapquestapi.com/geocoding/v1/address?key=%s&location=%s" % (CONF.getNode("mapquestapi")["key"], quote(location)))
|
||||||
|
obj = json.loads(raw.read().decode())
|
||||||
|
|
||||||
|
if "results" in obj and "locations" in obj["results"][0]:
|
||||||
|
for loc in obj["results"][0]["locations"]:
|
||||||
|
yield loc
|
||||||
|
|
||||||
|
def where(loc):
|
||||||
|
return re.sub(" +", " ", "%s %s %s %s %s" % (loc["street"], loc["adminArea5"], loc["adminArea4"], loc["adminArea3"], loc["adminArea1"])).strip()
|
||||||
|
|
||||||
|
def cmd_geocode(msg):
|
||||||
|
if len(msg.cmds) < 2:
|
||||||
|
raise IRCException("indicate a name")
|
||||||
|
|
||||||
|
locname = ' '.join(msg.cmds[1:])
|
||||||
|
res = Response(msg.sender, channel=msg.channel, nick=msg.nick, nomore="No more geocode", count=" (%s more geocode)")
|
||||||
|
for loc in geocode(locname):
|
||||||
|
res.append_message("%s is at %s,%s (%s precision)" % (where(loc), loc["latLng"]["lat"], loc["latLng"]["lng"], loc["geocodeQuality"].lower()))
|
||||||
|
return res
|
||||||
Loading…
Add table
Add a link
Reference in a new issue