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.terms = terms
|
||||||
self.curRT = -1
|
self.curRT = -1
|
||||||
(res, page) = getPage(terms)
|
(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)
|
self.ddgres = xmlparser.parse_string(page)
|
||||||
else:
|
else:
|
||||||
self.ddgres = None
|
self.ddgres = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self):
|
def type(self):
|
||||||
if self.ddgres.hasNode("Type"):
|
if self.ddgres and self.ddgres.hasNode("Type"):
|
||||||
return self.ddgres.getFirstNode("Type").getContent()
|
return self.ddgres.getFirstNode("Type").getContent()
|
||||||
else:
|
else:
|
||||||
return ""
|
return ""
|
||||||
@ -32,13 +32,17 @@ class DDGSearch:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def nextRes(self):
|
def nextRes(self):
|
||||||
if self.type == "D":
|
if (self.type == "D" or self.type == "C") and len(self.ddgres.getFirstNode("RelatedTopics").getNodes("RelatedTopic")) > self.curRT + 1:
|
||||||
if len(self.ddgres.getFirstNode("RelatedTopics").getNodes("RelatedTopic")) > self.curRT + 1:
|
|
||||||
self.curRT += 1
|
self.curRT += 1
|
||||||
node = self.ddgres.getFirstNode("RelatedTopics").getNodes("RelatedTopic")[self.curRT]
|
node = self.ddgres.getFirstNode("RelatedTopics").getNodes("RelatedTopic")[self.curRT]
|
||||||
return node.getFirstNode("Text").getContent()
|
return node.getFirstNode("Text").getContent()
|
||||||
elif self.ddgres.hasNode("Answer"):
|
elif self.ddgres.hasNode("Redirect") and self.ddgres.getFirstNode("Redirect").getContent() != "":
|
||||||
if self.curRT < 0:
|
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
|
self.curRT = 0
|
||||||
return striphtml(self.ddgres.getFirstNode("Answer").getContent())
|
return striphtml(self.ddgres.getFirstNode("Answer").getContent())
|
||||||
elif self.ddgres.hasNode("Abstract") and len (self.ddgres.getNode("Abstract").getContent()) > 0:
|
elif self.ddgres.hasNode("Abstract") and len (self.ddgres.getNode("Abstract").getContent()) > 0:
|
||||||
@ -54,7 +58,7 @@ class DDGSearch:
|
|||||||
|
|
||||||
def striphtml(data):
|
def striphtml(data):
|
||||||
p = re.compile(r'<.*?>')
|
p = re.compile(r'<.*?>')
|
||||||
return p.sub('', data)
|
return p.sub('', data).replace("(", "/(").replace(")", ")/").replace(""", "\"")
|
||||||
|
|
||||||
def getPage(terms):
|
def getPage(terms):
|
||||||
conn = http.client.HTTPConnection("api.duckduckgo.com")
|
conn = http.client.HTTPConnection("api.duckduckgo.com")
|
||||||
|
Loading…
Reference in New Issue
Block a user