Modules: global dusting: call getJSON instead of making raw calls to urllib

This commit is contained in:
nemunaire 2014-12-17 07:32:34 +01:00
commit d14fec4cec
14 changed files with 118 additions and 116 deletions

View file

@ -1,14 +1,17 @@
# coding=utf-8
import json
"""The mapquest module"""
import re
from urllib.parse import quote
from urllib.request import urlopen
from tools import web
nemubotversion = 3.4
from more import Response
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 "
@ -21,31 +24,38 @@ def load(context):
add_hook("cmd_hook", MessageHook(cmd_geocode, "geocode"))
def help_tiny ():
"""Line inserted in the response to the command !help"""
return "The mapquest module"
def help_full ():
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())
obj = web.getJSON("http://open.mapquestapi.com/geocoding/v1/address?key=%s&location=%s" %
(CONF.getNode("mapquestapi")["key"], quote(location)))
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()
return re.sub(" +", " ",
"{street} {adminArea5} {adminArea4} {adminArea3} "
"{adminArea1}".format(**loc)).strip()
def cmd_geocode(msg):
if len(msg.cmds) < 2:
raise IRCException("indicate a name")
locname = ' '.join(msg.cmds[1:])
res = Response(channel=msg.channel, nick=msg.nick, nomore="No more geocode", count=" (%s more geocode)")
res = Response(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()))
res.append_message("%s is at %s,%s (%s precision)" %
(where(loc),
loc["latLng"]["lat"],
loc["latLng"]["lng"],
loc["geocodeQuality"].lower()))
return res