DDG module: implement last features from DDG API not implemented
This commit is contained in:
parent
84378d7e70
commit
b6c4d2c391
@ -11,14 +11,14 @@ class DDGSearch:
|
||||
self.terms = terms
|
||||
self.curRT = -1
|
||||
(res, page) = getPage(terms)
|
||||
if res == http.client.OK:
|
||||
if res == http.client.OK or res == http.client.SEE_OTHER:
|
||||
self.ddgres = xmlparser.parse_string(page)
|
||||
else:
|
||||
self.ddgres = None
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
if self.ddgres.hasNode("Type"):
|
||||
if self.ddgres and self.ddgres.hasNode("Type"):
|
||||
return self.ddgres.getFirstNode("Type").getContent()
|
||||
else:
|
||||
return ""
|
||||
@ -32,13 +32,17 @@ class DDGSearch:
|
||||
|
||||
@property
|
||||
def nextRes(self):
|
||||
if self.type == "D":
|
||||
if len(self.ddgres.getFirstNode("RelatedTopics").getNodes("RelatedTopic")) > self.curRT + 1:
|
||||
if (self.type == "D" or self.type == "C") and len(self.ddgres.getFirstNode("RelatedTopics").getNodes("RelatedTopic")) > self.curRT + 1:
|
||||
self.curRT += 1
|
||||
node = self.ddgres.getFirstNode("RelatedTopics").getNodes("RelatedTopic")[self.curRT]
|
||||
return node.getFirstNode("Text").getContent()
|
||||
elif self.ddgres.hasNode("Answer"):
|
||||
if self.curRT < 0:
|
||||
elif self.ddgres.hasNode("Redirect") and self.ddgres.getFirstNode("Redirect").getContent() != "":
|
||||
return self.ddgres.getFirstNode("Redirect").getContent()
|
||||
elif self.ddgres.hasNode("Results") and self.ddgres.getFirstNode("Results").hasNode("Result") and self.curRT < 0:
|
||||
self.curRT = 0
|
||||
node = self.ddgres.getFirstNode("Results").getFirstNode("Result")
|
||||
return node.getFirstNode("Text").getContent() + ": " + node.getFirstNode("FirstURL").getContent()
|
||||
elif self.ddgres.hasNode("Answer") and self.curRT < 0:
|
||||
self.curRT = 0
|
||||
return striphtml(self.ddgres.getFirstNode("Answer").getContent())
|
||||
elif self.ddgres.hasNode("Abstract") and len (self.ddgres.getNode("Abstract").getContent()) > 0:
|
||||
@ -54,7 +58,7 @@ class DDGSearch:
|
||||
|
||||
def striphtml(data):
|
||||
p = re.compile(r'<.*?>')
|
||||
return p.sub('', data)
|
||||
return p.sub('', data).replace("(", "/(").replace(")", ")/").replace(""", "\"")
|
||||
|
||||
def getPage(terms):
|
||||
conn = http.client.HTTPConnection("api.duckduckgo.com")
|
||||
|
Loading…
Reference in New Issue
Block a user