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,17 +1,20 @@
# coding=utf-8
from urllib.parse import quote
from urllib.request import urlopen
from tools import web
from tools.xmlparser import parse_string
class DDGSearch:
def __init__(self, terms):
self.terms = terms
raw = urlopen("https://api.duckduckgo.com/?q=%s&format=xml&no_redirect=1" % quote(terms), timeout=10)
self.ddgres = parse_string(raw.read())
self.ddgres = web.getXML(
"https://api.duckduckgo.com/?q=%s&format=xml&no_redirect=1" %
quote(terms),
timeout=10)
@property
def type(self):

View file

@ -1,15 +1,18 @@
# coding=utf-8
import json
from urllib.parse import quote
from urllib.request import urlopen
from tools import web
class UrbanDictionnary:
def __init__(self, terms):
self.terms = terms
raw = urlopen("http://api.urbandictionary.com/v0/define?term=%s" % quote(terms), timeout=10)
self.udres = json.loads(raw.read().decode())
self.udres = web.getJSON(
"http://api.urbandictionary.com/v0/define?term=%s" % quote(terms),
timeout=10)
@property
def result_type(self):

View file

@ -1,19 +1,17 @@
# coding=utf-8
from urllib.parse import quote
from urllib.request import urlopen
from tools.xmlparser import parse_string
from tools import web
class WFASearch:
def __init__(self, terms):
self.terms = terms
try:
raw = urlopen("http://api.wolframalpha.com/v2/query?"
"input=%s&appid=%s"
% (quote(terms),
CONF.getNode("wfaapi")["key"]), timeout=15)
self.wfares = parse_string(raw.read())
url = ("http://api.wolframalpha.com/v2/query?input=%s&appid=%s" %
(quote(terms), CONF.getNode("wfaapi")["key"]))
self.wfares = web.getXML(url)
except (TypeError, KeyError):
print ("You need a Wolfram|Alpha API key in order to use this "
"module. Add it to the module configuration file:\n<wfaapi"
@ -33,7 +31,8 @@ class WFASearch:
if self.wfares is None:
return "An error occurs during computation."
elif self.wfares["error"] == "true":
return "An error occurs during computation: " + self.wfares.getNode("error").getNode("msg").getContent()
return ("An error occurs during computation: " +
self.wfares.getNode("error").getNode("msg").getContent())
elif self.wfares.hasNode("didyoumeans"):
start = "Did you mean: "
tag = "didyoumean"
@ -66,6 +65,7 @@ class WFASearch:
for node in self.wfares.getNodes("pod"):
for subnode in node.getNodes("subpod"):
if subnode.getFirstNode("plaintext").getContent() != "":
yield node["title"] + " " + subnode["title"] + ": " + subnode.getFirstNode("plaintext").getContent()
yield (node["title"] + " " + subnode["title"] + ": " +
subnode.getFirstNode("plaintext").getContent())
except IndexError:
pass