Use new web tool into various modules

This commit is contained in:
Némunaire 2012-11-04 16:26:20 +01:00
commit 9b38b21898
7 changed files with 96 additions and 137 deletions

View file

@ -1,19 +1,13 @@
# coding=utf-8
import http.client
import re
from urllib.parse import quote
import xmlparser
from tools import web
class DDGSearch:
def __init__(self, terms):
self.terms = terms
(res, page) = getPage(terms)
if res == http.client.OK or res == http.client.SEE_OTHER:
self.ddgres = xmlparser.parse_string(page)
else:
self.ddgres = None
self.ddgres = web.getXML("http://api.duckduckgo.com/?q=%s&format=xml" % quote(terms))
@property
def type(self):
@ -55,7 +49,7 @@ class DDGSearch:
@property
def answer(self):
try:
return striphtml(self.ddgres.getFirstNode("Answer").getContent())
return web.striphtml(self.ddgres.getFirstNode("Answer").getContent())
except:
return None
@ -68,22 +62,3 @@ class DDGSearch:
return None
except:
return None
def striphtml(data):
p = re.compile(r'<.*?>')
return p.sub('', data).replace("&#x28;", "/(").replace("&#x29;", ")/").replace("&#x22;", "\"")
def getPage(terms):
conn = http.client.HTTPConnection("api.duckduckgo.com", timeout=5)
try:
conn.request("GET", "/?q=%s&format=xml" % quote(terms))
except socket.gaierror:
print ("impossible de récupérer la page %s."%(p))
return (http.client.INTERNAL_SERVER_ERROR, None)
res = conn.getresponse()
data = res.read()
conn.close()
return (res.status, data)

View file

@ -1,19 +1,22 @@
# coding=utf-8
import http.client
import re
import socket
from urllib.parse import quote
import xmlparser
from tools import web
class WFASearch:
def __init__(self, terms):
self.terms = terms
(res, page) = getPage(terms)
if res == http.client.OK:
self.wfares = xmlparser.parse_string(page)
else:
try:
self.wfares = web.getXML("http://api.wolframalpha.com/v2/query?"
"input=%s&appid=%s"
% (quote(terms),
CONF.getNode("wfaapi")["key"]))
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"
" key=\"XXXXXX-XXXXXXXXXX\" />\nRegister at "
"http://products.wolframalpha.com/api/")
self.wfares = None
@property
@ -25,7 +28,9 @@ class WFASearch:
@property
def error(self):
if self.wfares["error"] == "true":
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()
elif self.wfares.hasNode("didyoumeans"):
start = "Did you mean: "
@ -62,21 +67,3 @@ class WFASearch:
yield node["title"] + " " + subnode["title"] + ": " + subnode.getFirstNode("plaintext").getContent()
except IndexError:
pass
def getPage(terms):
conn = http.client.HTTPConnection("api.wolframalpha.com", timeout=15)
try:
conn.request("GET", "/v2/query?input=%s&appid=%s" % (quote(terms), CONF.getNode("wfaapi")["key"]))
except socket.gaierror:
print ("impossible de récupérer la page Wolfram|Alpha.")
return (http.client.INTERNAL_SERVER_ERROR, None)
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 key=\"XXXXXX-XXXXXXXXXX\" />\nRegister at http://products.wolframalpha.com/api/")
return (http.client.INTERNAL_SERVER_ERROR, None)
res = conn.getresponse()
data = res.read()
conn.close()
return (res.status, data)